#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 /*<<< orphan*/ printf(char *, ...);
int wait();
void forktest(void) {
  int n, pid;
  printf("fork test\n");
  for (n = 0; n < 1000; n++) {
    pid = fork();
    if (pid < 0)
      break;
    if (pid == 0)
      exit(1);
  }
  if (n == 1000) {
    printf("fork claimed to work N times!\n", 1000);
    exit(1);
  }
  for (; n > 0; n--) {
    if (wait() < 0) {
      printf("wait stopped early\n");
      exit(1);
    }
  }
  if (wait() != -1) {
    printf("wait got too many\n");
    exit(1);
  }
  printf("fork test OK\n");
}


x86 assembly (clang, -O0):
<forktest>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xeb1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
cmpl   $0x3e8,-0x4(%rbp)
jge    11aa <forktest+0x6a>
mov    $0x0,%al
call   1060 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1183 <forktest+0x43>
jmp    11aa <forktest+0x6a>
cmpl   $0x0,-0x8(%rbp)
jne    1197 <forktest+0x57>
mov    $0x1,%edi
call   1040 <exit@plt>
jmp    119c <forktest+0x5c>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    115d <forktest+0x1d>
cmpl   $0x3e8,-0x4(%rbp)
jne    11d4 <forktest+0x94>
lea    0xe4d(%rip),%rdi
mov    $0x3e8,%esi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1040 <exit@plt>
jmp    11d9 <forktest+0x99>
cmpl   $0x0,-0x4(%rbp)
jle    121e <forktest+0xde>
mov    $0x0,%al
call   1050 <wait@plt>
cmp    $0x0,%eax
jge    120b <forktest+0xcb>
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1040 <exit@plt>
jmp    1210 <forktest+0xd0>
mov    -0x4(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x4(%rbp)
jmp    11d9 <forktest+0x99>
mov    $0x0,%al
call   1050 <wait@plt>
cmp    $0xffffffff,%eax
je     1246 <forktest+0x106>
lea    0xe09(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1040 <exit@plt>
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<forktest>:
push   %rbx
lea    0xeb8(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
call   1060 <fork@plt>
test   %eax,%eax
js     1181 <forktest+0x41>
je     11bc <forktest+0x7c>
add    $0x1,%ebx
cmp    $0x3e9,%ebx
jne    1160 <forktest+0x20>
lea    0xebd(%rip),%rdi
jmp    11cd <forktest+0x8d>
mov    %ebx,%eax
add    $0xffffffff,%eax
je     11a3 <forktest+0x63>
cmp    $0x3e8,%eax
je     1178 <forktest+0x38>
nop
xor    %eax,%eax
call   1050 <wait@plt>
test   %eax,%eax
js     11c6 <forktest+0x86>
add    $0xffffffff,%ebx
cmp    $0x2,%ebx
jge    1190 <forktest+0x50>
xor    %eax,%eax
call   1050 <wait@plt>
cmp    $0xffffffff,%eax
jne    11dc <forktest+0x9c>
lea    0xe54(%rip),%rdi
pop    %rbx
jmp    1030 <puts@plt>
mov    $0x1,%edi
call   1040 <exit@plt>
lea    0xe5c(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1040 <exit@plt>
lea    0xe34(%rip),%rdi
jmp    11cd <forktest+0x8d>

LLVM IR (-O0):
@.str = [11 x i8] c"fork test\0A\00"
@.str.1 = [31 x i8] c"fork claimed to work N times!\0A\00"
@.str.2 = [20 x i8] c"wait stopped early\0A\00"
@.str.3 = [19 x i8] c"wait got too many\0A\00"
@.str.4 = [14 x i8] c"fork test OK\0A\00"
define void @forktest() {
%1 = alloca i32
%2 = alloca i32
%3 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %1
br label %4
4: ; preds = %18, %0
%5 = load i32, i32* %1
%6 = icmp slt i32 %5, 1000
br i1 %6, label %7, label %21
7: ; preds = %4
%8 = call i32 (...) @fork()
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %7
br label %21
12: ; preds = %7
%13 = load i32, i32* %2
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %12
br label %18
18: ; preds = %17
%19 = load i32, i32* %1
%20 = add nsw i32 %19, 1
store i32 %20, i32* %1
br label %4
21: ; preds = %11, %4
%22 = load i32, i32* %1
%23 = icmp eq i32 %22, 1000
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 1000)
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %21
br label %28
28: ; preds = %38, %27
%29 = load i32, i32* %1
%30 = icmp sgt i32 %29, 0
br i1 %30, label %31, label %41
31: ; preds = %28
%32 = call i32 (...) @wait()
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %31
br label %38
38: ; preds = %37
%39 = load i32, i32* %1
%40 = add nsw i32 %39, -1
store i32 %40, i32* %1
br label %28
41: ; preds = %28
%42 = call i32 (...) @wait()
%43 = icmp ne i32 %42, -1
br i1 %43, label %44, label %47
44: ; preds = %41
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %41
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@str = [10 x i8] c"fork test\00"
@str.5 = [13 x i8] c"fork test OK\00"
@str.6 = [18 x i8] c"wait got too many\00"
@str.7 = [19 x i8] c"wait stopped early\00"
@str.8 = [30 x i8] c"fork claimed to work N times!\00"
define void @forktest() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
br label %2
2: ; preds = %0, %10
%3 = phi i32 [ 0, %0 ], [ %11, %10 ]
%4 = tail call i32 (...) @fork()
%5 = icmp slt i32 %4, 0
br i1 %5, label %13, label %6
6: ; preds = %2
%7 = icmp eq i32 %4, 0
br i1 %7, label %8, label %10
8: ; preds = %6
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %6
%11 = add nuw nsw i32 %3, 1
%12 = icmp eq i32 %11, 1000
br i1 %12, label %14, label %2
13: ; preds = %2
switch i32 %3, label %20 [
i32 1000, label %14
i32 0, label %27
]
14: ; preds = %10, %13
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.8, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %20
%18 = add nsw i32 %21, -1
%19 = icmp sgt i32 %21, 1
br i1 %19, label %20, label %27
20: ; preds = %13, %17
%21 = phi i32 [ %18, %17 ], [ %3, %13 ]
%22 = tail call i32 (...) @wait()
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %17
24: ; preds = %20
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.7, i64 0, i64 0))
%26 = tail call i32 @exit(i32 1)
unreachable
27: ; preds = %17, %13
%28 = tail call i32 (...) @wait()
%29 = icmp eq i32 %28, -1
br i1 %29, label %33, label %30
30: ; preds = %27
%31 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.6, i64 0, i64 0))
%32 = tail call i32 @exit(i32 1)
unreachable
33: ; preds = %27
%34 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.5, i64 0, i64 0))
ret void
}