> For the complete documentation index, see [llms.txt](https://alham-rizvi.gitbook.io/alhamrizvi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://alham-rizvi.gitbook.io/alhamrizvi/picoctf-writeups/picoctf/reverse-engineering/forky.md).

# forky

We are given a binary and asked to reverse it using Ghidra. The goal is to find the final integer passed to the function `doNothing()`.

#### Step 1: Understand the Code

Main function:

```c
piVar1 = mmap(...);
*piVar1 = 1000000000;

fork();
fork();
fork();
fork();

*piVar1 = *piVar1 + 0x499602d2;

doNothing(*piVar1);
```

What’s happening:

* A number is stored: `1000000000`
* The program calls `fork()` 4 times
* Then it adds `0x499602d2`
* Then sends the result to `doNothing()`

Each `fork()` doubles the number of processes.

So:

* 1st fork → 2 processes
* 2nd fork → 4 processes
* 3rd fork → 8 processes
* 4th fork → 16 processes

Final: **16 processes running**

Each process executes:

```c
*piVar1 = *piVar1 + 0x499602d2;
```

So total addition:

```id="calc1"
1000000000 + (16 × 0x499602d2)
```

C uses **32-bit integers**, so the value may overflow.

We simulate this in Python:

```python
import ctypes
ctypes.c_int32(1000000000 + 16*0x499602D2)
```

Output:

```id="calc3"
-721750240
```

```id="calc4"
picoCTF{-721750240}
```

#### Key Idea

* `fork()` multiplies processes
* Each process runs the same code
* Total effect = number of processes × operation
* Always consider **integer overflow in C**


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://alham-rizvi.gitbook.io/alhamrizvi/picoctf-writeups/picoctf/reverse-engineering/forky.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
