#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*/ execve(char *, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int fork();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int status;
  printf("In forktest file!");
  int n = fork();
  int i = 0;
  for (i = 0; i < 5; i++) {
    int k = fork();
    printf("fork no %d,pid %d", i, k);
    if (k) {
      printf("parent  ");
      waitpid(2, &status, 0);
    } else {
      printf("  child");
    }
  }
  printf("n = %d ---- ", n);
  int b = 3;
  printf("%d", b);
  if (n) {
    printf("parent  ");
    waitpid(2, &status, 0);
  } else {
    printf("  child");
    execve("bin/ps", NULL, NULL);
  }
  printf("back");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xea3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1060 <fork@plt>
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x5,-0x1c(%rbp)
jge    11f2 <main+0xb2>
mov    $0x0,%al
call   1060 <fork@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%esi
mov    -0x20(%rbp),%edx
lea    0xe75(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x0,-0x20(%rbp)
je     11d1 <main+0x91>
lea    0xe6f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x2,%edi
lea    -0x14(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
jmp    11df <main+0x9f>
lea    0xe55(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11e4 <main+0xa4>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    117c <main+0x3c>
mov    -0x18(%rbp),%esi
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x3,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x0,-0x18(%rbp)
je     1248 <main+0x108>
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x2,%edi
lea    -0x14(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
jmp    1269 <main+0x129>
lea    0xdde(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xde5(%rip),%rdi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   1040 <execve@plt>
lea    0xdd9(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
lea    0xeb6(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
xor    %eax,%eax
call   1060 <fork@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1060 <fork@plt>
mov    %eax,%ebp
lea    0xea8(%rip),%rdi
xor    %esi,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <printf@plt>
test   %ebp,%ebp
je     119a <main+0x5a>
lea    0xea4(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0x4(%rsp),%rsi
mov    $0x2,%edi
xor    %edx,%edx
call   1050 <waitpid@plt>
jmp    11a8 <main+0x68>
lea    0xe8c(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
xor    %eax,%eax
call   1060 <fork@plt>
mov    %eax,%ebp
lea    0xe5a(%rip),%rdi
mov    $0x1,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <printf@plt>
test   %ebp,%ebp
je     11eb <main+0xab>
lea    0xe53(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0x4(%rsp),%rsi
mov    $0x2,%edi
xor    %edx,%edx
call   1050 <waitpid@plt>
jmp    11f9 <main+0xb9>
lea    0xe3b(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
xor    %eax,%eax
call   1060 <fork@plt>
mov    %eax,%ebp
lea    0xe09(%rip),%rdi
mov    $0x2,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <printf@plt>
test   %ebp,%ebp
je     123c <main+0xfc>
lea    0xe02(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0x4(%rsp),%rsi
mov    $0x2,%edi
xor    %edx,%edx
call   1050 <waitpid@plt>
jmp    124a <main+0x10a>
lea    0xdea(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
xor    %eax,%eax
call   1060 <fork@plt>
mov    %eax,%ebp
lea    0xdb8(%rip),%rdi
mov    $0x3,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <printf@plt>
test   %ebp,%ebp
je     128d <main+0x14d>
lea    0xdb1(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0x4(%rsp),%rsi
mov    $0x2,%edi
xor    %edx,%edx
call   1050 <waitpid@plt>
jmp    129b <main+0x15b>
lea    0xd99(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
xor    %eax,%eax
call   1060 <fork@plt>
mov    %eax,%ebp
lea    0xd67(%rip),%rdi
mov    $0x4,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <printf@plt>
test   %ebp,%ebp
je     12de <main+0x19e>
lea    0xd60(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0x4(%rsp),%rsi
mov    $0x2,%edi
xor    %edx,%edx
call   1050 <waitpid@plt>
jmp    12ec <main+0x1ac>
lea    0xd48(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xd42(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xd1e(%rip),%rdi
mov    $0x3,%esi
xor    %eax,%eax
call   1030 <printf@plt>
test   %ebx,%ebx
je     1334 <main+0x1f4>
lea    0xd0a(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0x4(%rsp),%rsi
mov    $0x2,%edi
xor    %edx,%edx
call   1050 <waitpid@plt>
jmp    1352 <main+0x212>
lea    0xcf2(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xcf9(%rip),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1040 <execve@plt>
lea    0xcf0(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [18 x i8] c"In forktest file!\00"
@.str.1 = [18 x i8] c"fork no %d,pid %d\00"
@.str.2 = [9 x i8] c"parent \00"
@.str.3 = [8 x i8] c" child\00"
@.str.4 = [13 x i8] c"n = %d ---- \00"
@.str.5 = [3 x i8] c"%d\00"
@.str.6 = [7 x i8] c"bin/ps\00"
@.str.7 = [5 x i8] c"back\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%12 = call i32 (...) @fork()
store i32 %12, i32* %7
store i32 0, i32* %8
store i32 0, i32* %8
br label %13
13: ; preds = %29, %2
%14 = load i32, i32* %8
%15 = icmp slt i32 %14, 5
br i1 %15, label %16, label %32
16: ; preds = %13
%17 = call i32 (...) @fork()
store i32 %17, i32* %9
%18 = load i32, i32* %8
%19 = load i32, i32* %9
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %18, i32 %19)
%21 = load i32, i32* %9
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %16
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%25 = call i32 @waitpid(i32 2, i32* %6, i32 0)
br label %28
26: ; preds = %16
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %28
28: ; preds = %26, %23
br label %29
29: ; preds = %28
%30 = load i32, i32* %8
%31 = add nsw i32 %30, 1
store i32 %31, i32* %8
br label %13
32: ; preds = %13
%33 = load i32, i32* %7
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 %33)
store i32 3, i32* %10
%35 = load i32, i32* %10
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 %35)
%37 = load i32, i32* %7
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %32
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%41 = call i32 @waitpid(i32 2, i32* %6, i32 0)
br label %45
42: ; preds = %32
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%44 = call i32 @execve(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i32* null, i32* null)
br label %45
45: ; preds = %42, %39
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [18 x i8] c"In forktest file!\00"
@.str.1 = [18 x i8] c"fork no %d,pid %d\00"
@.str.2 = [9 x i8] c"parent \00"
@.str.3 = [8 x i8] c" child\00"
@.str.4 = [13 x i8] c"n = %d ---- \00"
@.str.5 = [3 x i8] c"%d\00"
@.str.6 = [7 x i8] c"bin/ps\00"
@.str.7 = [5 x i8] c"back\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 (...) @fork()
%7 = call i32 (...) @fork()
%8 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 0, i32 %7)
%9 = icmp eq i32 %7, 0
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%12 = call i32 @waitpid(i32 2, i32* %3, i32 0)
br label %15
13: ; preds = %2
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %15
15: ; preds = %13, %10
%16 = call i32 (...) @fork()
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 1, i32 %16)
%18 = icmp eq i32 %16, 0
br i1 %18, label %22, label %19
19: ; preds = %15
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%21 = call i32 @waitpid(i32 2, i32* %3, i32 0)
br label %24
22: ; preds = %15
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %24
24: ; preds = %22, %19
%25 = call i32 (...) @fork()
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 2, i32 %25)
%27 = icmp eq i32 %25, 0
br i1 %27, label %31, label %28
28: ; preds = %24
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%30 = call i32 @waitpid(i32 2, i32* %3, i32 0)
br label %33
31: ; preds = %24
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %33
33: ; preds = %31, %28
%34 = call i32 (...) @fork()
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 3, i32 %34)
%36 = icmp eq i32 %34, 0
br i1 %36, label %40, label %37
37: ; preds = %33
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%39 = call i32 @waitpid(i32 2, i32* %3, i32 0)
br label %42
40: ; preds = %33
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %42
42: ; preds = %40, %37
%43 = call i32 (...) @fork()
%44 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 4, i32 %43)
%45 = icmp eq i32 %43, 0
br i1 %45, label %49, label %46
46: ; preds = %42
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%48 = call i32 @waitpid(i32 2, i32* %3, i32 0)
br label %51
49: ; preds = %42
%50 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %51
51: ; preds = %49, %46
%52 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 %6)
%53 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 3)
%54 = icmp eq i32 %6, 0
br i1 %54, label %58, label %55
55: ; preds = %51
%56 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%57 = call i32 @waitpid(i32 2, i32* %3, i32 0)
br label %61
58: ; preds = %51
%59 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%60 = call i32 @execve(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i32* null, i32* null)
br label %61
61: ; preds = %58, %55
%62 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
ret i32 0
}