#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);
scalar_t__ fork();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ ps();
int wait(int *);
int main() {
  pid_t pid, wp;
  int status;
  printf("\nWe are in userland\n");
  printf("Let's fork(), baby!\n");
  pid = fork();
  if (pid == 0) {
    printf("I am the child\n");
    return 2;
  } else {
    wp = wait(&status);
    if (pid != wp) {
      printf("fork returned different pid\n");
      exit(1);
    }
    if (status != 2) {
      printf("fork returned different exit status\n");
      exit(2);
    }
    printf("OK child %d exited with status: %d\n", wp, status);
  }
  status = -1;
  wp = 0;
  wp = wait(&status);
  if (wp != -1) {
    printf("wait too many\n");
    exit(4);
  }
  ps();
  printf("Test OK\n\n");
  printf("fork() stress test\n");
  int i;
  for (i = 0; i < 1000; i++) {
    if (!(i % 10))
      printf(".");
    pid = fork();
    if (pid < 0) {
      printf("fork returned -1\n");
      break;
    } else {
      exit(0);
    }
  }
  for (; i > 0; i--) {
    if (!(i % 10))
      printf("-");
    if ((pid = wait(NULL)) < 0) {
      printf("Wait exit early\n");
      exit(0);
    }
  }
  if (wait(NULL) != -1) {
    printf("Wait got too many\n");
    exit(0);
  }
  printf("Fork test OK\n");
  ps();
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe9a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xea1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ab <main+0x5b>
lea    0xe92(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    138f <main+0x23f>
lea    -0x1c(%rbp),%rdi
call   1060 <wait@plt>
cltq
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     11e0 <main+0x90>
lea    0xe6b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
cmpl   $0x2,-0x1c(%rbp)
je     1202 <main+0xb2>
lea    0xe66(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x2,%edi
call   1050 <exit@plt>
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
lea    0xe6c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x1c(%rbp)
movq   $0x0,-0x18(%rbp)
lea    -0x1c(%rbp),%rdi
call   1060 <wait@plt>
cltq
mov    %rax,-0x18(%rbp)
cmpq   $0xffffffffffffffff,-0x18(%rbp)
je     1258 <main+0x108>
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x4,%edi
call   1050 <exit@plt>
mov    $0x0,%al
call   1040 <ps@plt>
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe45(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x20(%rbp)
cmpl   $0x3e8,-0x20(%rbp)
jge    12e1 <main+0x191>
mov    -0x20(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    12b1 <main+0x161>
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    12da <main+0x18a>
lea    0xe01(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12e1 <main+0x191>
xor    %edi,%edi
call   1050 <exit@plt>
jmp    12e6 <main+0x196>
cmpl   $0x0,-0x20(%rbp)
jle    1353 <main+0x203>
mov    -0x20(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    1312 <main+0x1c2>
lea    0xdd6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <wait@plt>
cltq
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jge    1340 <main+0x1f0>
lea    0xdb1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   1050 <exit@plt>
jmp    1345 <main+0x1f5>
mov    -0x20(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x20(%rbp)
jmp    12e6 <main+0x196>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <wait@plt>
cmp    $0xffffffff,%eax
je     137a <main+0x22a>
lea    0xd88(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   1050 <exit@plt>
lea    0xd86(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1040 <ps@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
lea    0xea8(%rip),%rdi
call   1040 <puts@plt>
lea    0xeb0(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
je     1259 <main+0xe9>
mov    %rax,%rbx
lea    0xc(%rsp),%rdi
call   1080 <wait@plt>
cltq
cmp    %rax,%rbx
jne    1272 <main+0x102>
cmpl   $0x2,0xc(%rsp)
jne    1288 <main+0x118>
lea    0xe39(%rip),%rdi
mov    %rbx,%rsi
mov    $0x2,%edx
xor    %eax,%eax
call   1050 <printf@plt>
movl   $0xffffffff,0xc(%rsp)
ff
lea    0xc(%rsp),%rdi
call   1080 <wait@plt>
cmp    $0xffffffff,%eax
jne    129e <main+0x12e>
xor    %eax,%eax
call   1060 <ps@plt>
lea    0xe4d(%rip),%rdi
call   1040 <puts@plt>
lea    0xe4a(%rip),%rdi
call   1040 <puts@plt>
mov    $0x2e,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jns    12b4 <main+0x144>
lea    0xe37(%rip),%rdi
call   1040 <puts@plt>
xor    %ebx,%ebx
xor    %edi,%edi
call   1080 <wait@plt>
cmp    $0xffffffff,%eax
jne    12bb <main+0x14b>
lea    0xe2e(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1060 <ps@plt>
jmp    126a <main+0xfa>
lea    0xe86(%rip),%rdi
call   1040 <puts@plt>
mov    $0x2,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe51(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xe17(%rip),%rdi
call   1040 <puts@plt>
mov    $0x2,%edi
call   1070 <exit@plt>
lea    0xdf3(%rip),%rdi
call   1040 <puts@plt>
mov    $0x4,%edi
call   1070 <exit@plt>
xor    %edi,%edi
call   1070 <exit@plt>
lea    0xdc4(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [21 x i8] c"\0AWe are in userland\0A\00"
@.str.1 = [21 x i8] c"Let's fork(), baby!\0A\00"
@.str.2 = [16 x i8] c"I am the child\0A\00"
@.str.3 = [29 x i8] c"fork returned different pid\0A\00"
@.str.4 = [37 x i8] c"fork returned different exit status\0A\00"
@.str.5 = [36 x i8] c"OK child %d exited with status: %d\0A\00"
@.str.6 = [15 x i8] c"wait too many\0A\00"
@.str.7 = [10 x i8] c"Test OK\0A\0A\00"
@.str.8 = [20 x i8] c"fork() stress test\0A\00"
@.str.9 = [2 x i8] c".\00"
@.str.10 = [18 x i8] c"fork returned -1\0A\00"
@.str.11 = [2 x i8] c"-\00"
@.str.12 = [17 x i8] c"Wait exit early\0A\00"
@.str.13 = [19 x i8] c"Wait got too many\0A\00"
@.str.14 = [14 x i8] c"Fork test OK\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%8 = call i64 (...) @fork()
store i64 %8, i64* %2
%9 = load i64, i64* %2
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %1
br label %94
13: ; preds = %0
%14 = call i32 @wait(i32* %4)
%15 = sext i32 %14 to i64
store i64 %15, i64* %3
%16 = load i64, i64* %2
%17 = load i64, i64* %3
%18 = icmp ne i64 %16, %17
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %13
%23 = load i32, i32* %4
%24 = icmp ne i32 %23, 2
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
%27 = call i32 @exit(i32 2)
unreachable
28: ; preds = %22
%29 = load i64, i64* %3
%30 = load i32, i32* %4
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i64 %29, i32 %30)
br label %32
32: ; preds = %28
store i32 -1, i32* %4
store i64 0, i64* %3
%33 = call i32 @wait(i32* %4)
%34 = sext i32 %33 to i64
store i64 %34, i64* %3
%35 = load i64, i64* %3
%36 = icmp ne i64 %35, -1
br i1 %36, label %37, label %40
37: ; preds = %32
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%39 = call i32 @exit(i32 4)
unreachable
40: ; preds = %32
%41 = call i32 (...) @ps()
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0))
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.8, i64 0, i64 0))
store i32 0, i32* %5
br label %44
44: ; preds = %61, %40
%45 = load i32, i32* %5
%46 = icmp slt i32 %45, 1000
br i1 %46, label %47, label %64
47: ; preds = %44
%48 = load i32, i32* %5
%49 = srem i32 %48, 10
%50 = icmp ne i32 %49, 0
br i1 %50, label %53, label %51
51: ; preds = %47
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0))
br label %53
53: ; preds = %51, %47
%54 = call i64 (...) @fork()
store i64 %54, i64* %2
%55 = load i64, i64* %2
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %53
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.10, i64 0, i64 0))
br label %64
59: ; preds = %53
%60 = call i32 @exit(i32 0)
unreachable
61: ; No predecessors!
%62 = load i32, i32* %5
%63 = add nsw i32 %62, 1
store i32 %63, i32* %5
br label %44
64: ; preds = %57, %44
br label %65
65: ; preds = %82, %64
%66 = load i32, i32* %5
%67 = icmp sgt i32 %66, 0
br i1 %67, label %68, label %85
68: ; preds = %65
%69 = load i32, i32* %5
%70 = srem i32 %69, 10
%71 = icmp ne i32 %70, 0
br i1 %71, label %74, label %72
72: ; preds = %68
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.11, i64 0, i64 0))
br label %74
74: ; preds = %72, %68
%75 = call i32 @wait(i32* null)
%76 = sext i32 %75 to i64
store i64 %76, i64* %2
%77 = icmp slt i64 %76, 0
br i1 %77, label %78, label %81
78: ; preds = %74
%79 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.12, i64 0, i64 0))
%80 = call i32 @exit(i32 0)
unreachable
81: ; preds = %74
br label %82
82: ; preds = %81
%83 = load i32, i32* %5
%84 = add nsw i32 %83, -1
store i32 %84, i32* %5
br label %65
85: ; preds = %65
%86 = call i32 @wait(i32* null)
%87 = icmp ne i32 %86, -1
br i1 %87, label %88, label %91
88: ; preds = %85
%89 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.13, i64 0, i64 0))
%90 = call i32 @exit(i32 0)
unreachable
91: ; preds = %85
%92 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.14, i64 0, i64 0))
%93 = call i32 (...) @ps()
br label %94
94: ; preds = %91, %11
%95 = load i32, i32* %1
ret i32 %95
}

LLVM IR (-O2):
@.str.5 = [36 x i8] c"OK child %d exited with status: %d\0A\00"
@str = [20 x i8] c"\0AWe are in userland\00"
@str.15 = [20 x i8] c"Let's fork(), baby!\00"
@str.16 = [9 x i8] c"Test OK\0A\00"
@str.17 = [19 x i8] c"fork() stress test\00"
@str.18 = [17 x i8] c"fork returned -1\00"
@str.19 = [13 x i8] c"Fork test OK\00"
@str.20 = [18 x i8] c"Wait got too many\00"
@str.21 = [14 x i8] c"wait too many\00"
@str.22 = [36 x i8] c"fork returned different exit status\00"
@str.23 = [28 x i8] c"fork returned different pid\00"
@str.24 = [15 x i8] c"I am the child\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.15, i64 0, i64 0))
%5 = tail call i64 (...) @fork()
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.24, i64 0, i64 0))
br label %48
9: ; preds = %0
%10 = call i32 @wait(i32* %1)
%11 = sext i32 %10 to i64
%12 = icmp eq i64 %5, %11
br i1 %12, label %16, label %13
13: ; preds = %9
%14 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.23, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %9
%17 = load i32, i32* %1
%18 = icmp eq i32 %17, 2
br i1 %18, label %22, label %19
19: ; preds = %16
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.22, i64 0, i64 0))
%21 = call i32 @exit(i32 2)
unreachable
22: ; preds = %16
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i64 %5, i32 2)
store i32 -1, i32* %1
%24 = call i32 @wait(i32* %1)
%25 = icmp eq i32 %24, -1
br i1 %25, label %29, label %26
26: ; preds = %22
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.21, i64 0, i64 0))
%28 = call i32 @exit(i32 4)
unreachable
29: ; preds = %22
%30 = call i32 (...) @ps()
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.16, i64 0, i64 0))
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.17, i64 0, i64 0))
%33 = call i32 @putchar(i32 46)
%34 = call i64 (...) @fork()
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %29
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.18, i64 0, i64 0))
%38 = call i32 @wait(i32* null)
%39 = icmp eq i32 %38, -1
br i1 %39, label %45, label %42
40: ; preds = %29
%41 = call i32 @exit(i32 0)
unreachable
42: ; preds = %36
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.20, i64 0, i64 0))
%44 = call i32 @exit(i32 0)
unreachable
45: ; preds = %36
%46 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.19, i64 0, i64 0))
%47 = call i32 (...) @ps()
br label %48
48: ; preds = %45, %7
%49 = phi i32 [ 2, %7 ], [ 0, %45 ]
ret i32 %49
}