#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 */

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int fork();
int getpid();
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ resign();
int /*<<< orphan*/ setpsname(char *);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
void forktest() {
  int pid1, pid2;
  pid1 = fork();
  if (pid1 != 0) {
    pid2 = fork();
    if (pid2 != 0) {
      resign();
    };
  };
  char string[] = "X";
  string[0] = 'a' + getpid();
  int i = 0;
  int status;
  if (pid1 == 0 || pid2 == 0) {
    setpsname("forktest");
    printf("[%d]u I am a child. Calling exit(%d).\n", getpid(), 100 + getpid());
    exit(100 + getpid());
    printf("[%d]u ERROR: Code nach exit.\n", getpid());
  } else {
    waitpid(pid1, &status, 0);
    printf("[%d]u waitpid() returns %d\n", getpid(), status);
    waitpid(pid2, &status, 0);
    printf("[%d]u waitpid() returns %d\n", getpid(), status);
    return;
  }
}


x86 assembly (clang, -O0):
<forktest>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     11ac <forktest+0x3c>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     11a7 <forktest+0x37>
mov    $0x0,%al
call   1080 <resign@plt>
jmp    11ac <forktest+0x3c>
mov    0xe4d(%rip),%ax
mov    %ax,-0xa(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
add    $0x61,%eax
mov    %al,-0xa(%rbp)
movl   $0x0,-0x10(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     11df <forktest+0x6f>
cmpl   $0x0,-0x8(%rbp)
jne    1223 <forktest+0xb3>
lea    0xe1c(%rip),%rdi
call   1040 <setpsname@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x18(%rbp),%esi
mov    %eax,%edx
add    $0x64,%edx
lea    0xe00(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%edi
add    $0x64,%edi
call   1070 <exit@plt>
mov    -0x4(%rbp),%edi
lea    -0x14(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x14(%rbp),%edx
lea    0xdee(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x14(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x14(%rbp),%edx
lea    0xdc6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<forktest>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
je     11fb <forktest+0x8b>
mov    %eax,%ebp
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
je     11fb <forktest+0x8b>
mov    %eax,%ebx
xor    %eax,%eax
call   1080 <resign@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xc(%rsp),%r14
mov    %ebp,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    0xc(%rsp),%edx
lea    0xe6d(%rip),%rbp
mov    %rbp,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    0xc(%rsp),%edx
mov    %rbp,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xdf7(%rip),%rdi
call   1040 <setpsname@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0x64(%rax),%edx
lea    0xde1(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0x64(%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@__const.forktest.string = [2 x i8] c"X\00"
@.str = [9 x i8] c"forktest\00"
@.str.1 = [39 x i8] c"[%d]u I am a child. Calling exit(%d).\0A\00"
@.str.2 = [28 x i8] c"[%d]u waitpid() returns %d\0A\00"
define void @forktest() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [2 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = call i32 (...) @fork()
store i32 %6, i32* %1
%7 = load i32, i32* %1
%8 = icmp ne i32 %7, 0
br i1 %8, label %9, label %16
9: ; preds = %0
%10 = call i32 (...) @fork()
store i32 %10, i32* %2
%11 = load i32, i32* %2
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = call i32 (...) @resign()
br label %15
15: ; preds = %13, %9
br label %16
16: ; preds = %15, %0
%17 = bitcast [2 x i8]* %3 to i8*
%18 = call i32 (...) @getpid()
%19 = add nsw i32 97, %18
%20 = trunc i32 %19 to i8
%21 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
store i8 %20, i8* %21
store i32 0, i32* %4
%22 = load i32, i32* %1
%23 = icmp eq i32 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %16
%25 = load i32, i32* %2
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %36
27: ; preds = %24, %16
%28 = call i32 @setpsname(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%29 = call i32 (...) @getpid()
%30 = call i32 (...) @getpid()
%31 = add nsw i32 100, %30
%32 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %29, i32 %31)
%33 = call i32 (...) @getpid()
%34 = add nsw i32 100, %33
%35 = call i32 @exit(i32 %34)
unreachable
36: ; preds = %24
%37 = load i32, i32* %1
%38 = call i32 @waitpid(i32 %37, i32* %5, i32 0)
%39 = call i32 (...) @getpid()
%40 = load i32, i32* %5
%41 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 %39, i32 %40)
%42 = load i32, i32* %2
%43 = call i32 @waitpid(i32 %42, i32* %5, i32 0)
%44 = call i32 (...) @getpid()
%45 = load i32, i32* %5
%46 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 %44, i32 %45)
ret void
}

LLVM IR (-O2):
@.str = [9 x i8] c"forktest\00"
@.str.1 = [39 x i8] c"[%d]u I am a child. Calling exit(%d).\0A\00"
@.str.2 = [28 x i8] c"[%d]u waitpid() returns %d\0A\00"
define void @forktest() {
%1 = alloca i32
%2 = tail call i32 (...) @fork()
%3 = icmp eq i32 %2, 0
br i1 %3, label %7, label %4
4: ; preds = %0
%5 = tail call i32 (...) @fork()
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %18
7: ; preds = %0, %4
%8 = tail call i32 (...) @getpid()
%9 = bitcast i32* %1 to i8*
%10 = tail call i32 @setpsname(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 (...) @getpid()
%12 = tail call i32 (...) @getpid()
%13 = add nsw i32 %12, 100
%14 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %11, i32 %13)
%15 = tail call i32 (...) @getpid()
%16 = add nsw i32 %15, 100
%17 = tail call i32 @exit(i32 %16)
unreachable
18: ; preds = %4
%19 = tail call i32 (...) @resign()
%20 = tail call i32 (...) @getpid()
%21 = bitcast i32* %1 to i8*
%22 = call i32 @waitpid(i32 %2, i32* %1, i32 0)
%23 = call i32 (...) @getpid()
%24 = load i32, i32* %1
%25 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 %23, i32 %24)
%26 = call i32 @waitpid(i32 %5, i32* %1, i32 0)
%27 = call i32 (...) @getpid()
%28 = load i32, i32* %1
%29 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 %27, i32 %28)
ret void
}