#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ printf(char *, int, int);
int waitpid(int, int *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  int status = 0, ret = 0;
  pid_t pid = fork();
  if (pid == 0) {
    pid = fork();
    exit(getpid());
  }
  ret = waitpid(-1, &status, 0);
  printf("Process %d exited with %d\n", ret, status);
  ret = waitpid(-1, &status, 0);
  printf("Process %d exited with %d\n", ret, status);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    119c <main+0x4c>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%edi
call   1060 <exit@plt>
mov    $0xffffffff,%edi
lea    -0x14(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
mov    -0x14(%rbp),%edx
lea    0xe44(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
lea    -0x14(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
mov    -0x14(%rbp),%edx
lea    0xe1f(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
movl   $0x0,0x4(%rsp)
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
je     11b8 <main+0x68>
lea    0x4(%rsp),%r14
mov    $0xffffffff,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    0x4(%rsp),%edx
lea    0xe79(%rip),%rbx
mov    %rbx,%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    0x4(%rsp),%edx
mov    %rbx,%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
xor    %eax,%eax
call   1070 <fork@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [27 x i8] c"Process %d exited with %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
store i32 %0, i32* %3
store i8** %1, i8*** %4
store i32 0, i32* %5
store i32 0, i32* %6
%8 = call i64 (...) @fork()
store i64 %8, i64* %7
%9 = load i64, i64* %7
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = call i64 (...) @fork()
store i64 %12, i64* %7
%13 = call i32 (...) @getpid()
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %2
%16 = call i32 @waitpid(i32 -1, i32* %5, i32 0)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = load i32, i32* %5
%19 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %17, i32 %18)
%20 = call i32 @waitpid(i32 -1, i32* %5, i32 0)
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = load i32, i32* %5
%23 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %21, i32 %22)
ret i32 0
}

LLVM IR (-O2):
@.str = [27 x i8] c"Process %d exited with %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%5 = tail call i64 (...) @fork()
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %11
7: ; preds = %2
%8 = tail call i64 (...) @fork()
%9 = tail call i32 (...) @getpid()
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %2
%12 = call i32 @waitpid(i32 -1, i32* %3, i32 0)
%13 = load i32, i32* %3
%14 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %12, i32 %13)
%15 = call i32 @waitpid(i32 -1, i32* %3, i32 0)
%16 = load i32, i32* %3
%17 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %15, i32 %16)
ret i32 0
}