#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 /*<<< orphan*/ ps();
int /*<<< orphan*/ setpsname(char *);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
int /*<<< orphan*/ yield();
void forktest() {
  int pid1, pid2;
  pid1 = fork();
  if (pid1 != 0) {
    pid2 = fork();
    if (pid2 != 0) {
      yield();
      printf("Parent calling ps():\n");
      ps();
    };
  };
  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   10a0 <fork@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     11d1 <forktest+0x51>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     11cc <forktest+0x4c>
mov    $0x0,%al
call   1050 <yield@plt>
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1080 <ps@plt>
jmp    11d1 <forktest+0x51>
mov    0xe3e(%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     1204 <forktest+0x84>
cmpl   $0x0,-0x8(%rbp)
jne    1248 <forktest+0xc8>
lea    0xe0d(%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    0xdf1(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%edi
add    $0x64,%edi
call   1090 <exit@plt>
mov    -0x4(%rbp),%edi
lea    -0x14(%rbp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x14(%rbp),%edx
lea    0xddf(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x14(%rbp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x14(%rbp),%edx
lea    0xdb7(%rip),%rdi
mov    $0x0,%al
call   1060 <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   10b0 <fork@plt>
test   %eax,%eax
je     1232 <forktest+0xa2>
mov    %eax,%ebp
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     1232 <forktest+0xa2>
mov    %eax,%ebx
xor    %eax,%eax
call   1060 <yield@plt>
lea    0xe88(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1090 <ps@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xc(%rsp),%r14
mov    %ebp,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    0xc(%rsp),%edx
lea    0xe36(%rip),%rbp
mov    %rbp,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %ebx,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    0xc(%rsp),%edx
mov    %rbp,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xdc0(%rip),%rdi
call   1050 <setpsname@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0x64(%rax),%edx
lea    0xdaa(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0x64(%rax),%edi
call   10a0 <exit@plt>

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

LLVM IR (-O2):
@.str.1 = [9 x i8] c"forktest\00"
@.str.2 = [39 x i8] c"[%d]u I am a child. Calling exit(%d).\0A\00"
@.str.3 = [28 x i8] c"[%d]u waitpid() returns %d\0A\00"
@str = [21 x i8] c"Parent calling ps():\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.1, 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*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, 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 (...) @yield()
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%21 = tail call i32 (...) @ps()
%22 = tail call i32 (...) @getpid()
%23 = bitcast i32* %1 to i8*
%24 = call i32 @waitpid(i32 %2, i32* %1, i32 0)
%25 = call i32 (...) @getpid()
%26 = load i32, i32* %1
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %25, i32 %26)
%28 = call i32 @waitpid(i32 %5, i32* %1, i32 0)
%29 = call i32 (...) @getpid()
%30 = load i32, i32* %1
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %29, i32 %30)
ret void
}