#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 /*<<< orphan*/);
int fork();
int /*<<< orphan*/ printf(char *, ...);
void forktest(void) {
  int n, pid;
  printf("fork test\n");
  for (n = 0; n < 32; n++) {
    pid = fork();
    if (pid < 0) {
      break;
    }
    if (pid == 0) {
      exit(0);
    }
  }
  if (n == 32) {
    printf("fork claimed to work %d times!\n", 32);
  }
}


x86 assembly (clang, -O0):
<forktest>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xec1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
cmpl   $0x20,-0x4(%rbp)
jge    1194 <forktest+0x64>
mov    $0x0,%al
call   1050 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1170 <forktest+0x40>
jmp    1194 <forktest+0x64>
cmpl   $0x0,-0x8(%rbp)
jne    1181 <forktest+0x51>
xor    %edi,%edi
call   1040 <exit@plt>
jmp    1186 <forktest+0x56>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    114d <forktest+0x1d>
cmpl   $0x20,-0x4(%rbp)
jne    11b1 <forktest+0x81>
lea    0xe66(%rip),%rdi
mov    $0x20,%esi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<forktest>:
push   %rbx
lea    0xed8(%rip),%rdi
call   1030 <puts@plt>
mov    $0x20,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
call   1060 <fork@plt>
test   %eax,%eax
js     1186 <forktest+0x46>
je     1188 <forktest+0x48>
add    $0xffffffff,%ebx
jne    1160 <forktest+0x20>
lea    0xe87(%rip),%rdi
mov    $0x20,%esi
xor    %eax,%eax
pop    %rbx
jmp    1040 <printf@plt>
pop    %rbx
ret
xor    %edi,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"fork test\0A\00"
@.str.1 = [32 x i8] c"fork claimed to work %d times!\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, 32
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 0)
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, 32
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 32)
br label %26
26: ; preds = %24, %21
ret void
}

LLVM IR (-O2):
@.str.1 = [32 x i8] c"fork claimed to work %d times!\0A\00"
@str = [10 x i8] c"fork test\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 %15, 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 0)
unreachable
10: ; preds = %6
%11 = add nuw nsw i32 %3, 1
%12 = icmp eq i32 %11, 32
br i1 %12, label %13, label %2
13: ; preds = %10
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 32)
br label %15
15: ; preds = %2, %13
ret void
}