#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 int pid_t;

/* Variables and functions */
scalar_t__ chdir(char *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ execl(char *, char *, char *, char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fopen(char *, char *);
int fork();
char *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ sscanf(char *, char *, int *);
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ waitpid(int, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char **argv) {
  int num;
  if (argc < 2) {
    num = 0;
  } else {
    sscanf(argv[1], "%d", &num);
  }
  if (num == 7) {
    if (chdir("tmp"))
      exit(1);
    argv[0] = "../tests/forks.test";
  }
  if (num == 37) {
    if (chdir("subdir2"))
      exit(1);
    argv[0] = "../../tests/forks.test";
  }
  if (num == 73) {
    if (chdir(".."))
      exit(1);
    argv[0] = "../tests/forks.test";
  }
  if (num == 137) {
    if (chdir("subdir1"))
      exit(1);
    argv[0] = "../../tests/forks.test";
  }
  if (num > 1000) {
    printf("We are finally all done!\n");
    if (!fopen("deepdir/forks", "w"))
      exit(1);
    exit(0);
  }
  pid_t child = fork();
  if (child) {
    printf("Child %d was %d\n", num, child);
    waitpid(child, 0, 0);
    exit(0);
  } else {
    char *numname = malloc(1000);
    sprintf(numname, "%d", num + 1);
    execl(argv[0], argv[0], numname, (char *)0);
    printf("execl failed: %s\n", strerror(errno));
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11dc <main+0x2c>
movl   $0x0,-0x14(%rbp)
jmp    11f4 <main+0x44>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe15(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1060 <sscanf@plt>
cmpl   $0x7,-0x14(%rbp)
jne    122c <main+0x7c>
lea    0xdfe(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
je     121e <main+0x6e>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
lea    0xde9(%rip),%rcx
mov    %rcx,(%rax)
cmpl   $0x25,-0x14(%rbp)
jne    1264 <main+0xb4>
lea    0xdca(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
je     1256 <main+0xa6>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
lea    0xdae(%rip),%rcx
mov    %rcx,(%rax)
cmpl   $0x49,-0x14(%rbp)
jne    129c <main+0xec>
lea    0xdb1(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
je     128e <main+0xde>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
lea    0xd79(%rip),%rcx
mov    %rcx,(%rax)
cmpl   $0x89,-0x14(%rbp)
jne    12d7 <main+0x127>
lea    0xd79(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
je     12c9 <main+0x119>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
lea    0xd3b(%rip),%rcx
mov    %rcx,(%rax)
cmpl   $0x3e8,-0x14(%rbp)
jle    131f <main+0x16f>
lea    0xd46(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xd52(%rip),%rdi
lea    0xd59(%rip),%rsi
call   1080 <fopen@plt>
cmp    $0x0,%eax
jne    1318 <main+0x168>
mov    $0x1,%edi
call   10a0 <exit@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     135a <main+0x1aa>
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    0xd1b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1070 <waitpid@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    $0x3e8,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%edx
add    $0x1,%edx
lea    0xc87(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x20(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   10c0 <execl@plt>
mov    0x2c40(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xcbb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x1,%edi
jg     11d8 <main+0x18>
movl   $0x0,0x4(%rsp)
jmp    129e <main+0xde>
mov    0x8(%rbx),%rdi
lea    0xe1d(%rip),%rsi
lea    0x4(%rsp),%rdx
call   1070 <sscanf@plt>
mov    0x4(%rsp),%eax
cmp    $0x7,%eax
jne    1219 <main+0x59>
lea    0xe06(%rip),%rdi
call   1040 <chdir@plt>
test   %rax,%rax
jne    134f <main+0x18f>
lea    0xe00(%rip),%rax
mov    %rax,(%rbx)
mov    0x4(%rsp),%eax
cmp    $0x25,%eax
jne    1241 <main+0x81>
lea    0xde2(%rip),%rdi
call   1040 <chdir@plt>
test   %rax,%rax
jne    134f <main+0x18f>
lea    0xdd5(%rip),%rax
mov    %rax,(%rbx)
mov    0x4(%rsp),%eax
cmp    $0x49,%eax
jne    1269 <main+0xa9>
lea    0xdd9(%rip),%rdi
call   1040 <chdir@plt>
test   %rax,%rax
jne    134f <main+0x18f>
lea    0xdb0(%rip),%rax
mov    %rax,(%rbx)
mov    0x4(%rsp),%eax
cmp    $0x89,%eax
jne    1293 <main+0xd3>
lea    0xdb2(%rip),%rdi
call   1040 <chdir@plt>
test   %rax,%rax
jne    134f <main+0x18f>
lea    0xd83(%rip),%rax
mov    %rax,(%rbx)
mov    0x4(%rsp),%eax
cmp    $0x3e9,%eax
jge    132c <main+0x16c>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
jne    1304 <main+0x144>
mov    $0x3e8,%edi
call   1060 <malloc@plt>
mov    %rax,%rbp
mov    0x4(%rsp),%edx
add    $0x1,%edx
lea    0xd3c(%rip),%rsi
mov    %rax,%rdi
call   10a0 <sprintf@plt>
mov    (%rbx),%rdi
mov    %rdi,%rsi
mov    %rbp,%rdx
xor    %ecx,%ecx
call   10d0 <execl@plt>
mov    0x2cfd(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd61(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    %eax,%ebp
mov    0x4(%rsp),%esi
lea    0xd30(%rip),%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <waitpid@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
lea    0xd31(%rip),%rdi
call   1030 <puts@plt>
lea    0xcf2(%rip),%rdi
lea    0xcf9(%rip),%rsi
call   1090 <fopen@plt>
test   %eax,%eax
jne    1359 <main+0x199>
mov    $0x1,%edi
call   10b0 <exit@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [4 x i8] c"tmp\00"
@.str.2 = [20 x i8] c"../tests/forks.test\00"
@.str.3 = [8 x i8] c"subdir2\00"
@.str.4 = [23 x i8] c"../../tests/forks.test\00"
@.str.5 = [3 x i8] c"..\00"
@.str.6 = [8 x i8] c"subdir1\00"
@.str.7 = [26 x i8] c"We are finally all done!\0A\00"
@.str.8 = [14 x i8] c"deepdir/forks\00"
@.str.9 = [2 x i8] c"w\00"
@.str.10 = [17 x i8] c"Child %d was %d\0A\00"
@.str.11 = [18 x i8] c"execl failed: %s\0A\00"
@errno = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 2
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 0, i32* %6
br label %17
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = call i32 @sscanf(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %6)
br label %17
17: ; preds = %12, %11
%18 = load i32, i32* %6
%19 = icmp eq i32 %18, 7
br i1 %19, label %20, label %28
20: ; preds = %17
%21 = call i64 @chdir(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %20
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 0
store i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8** %27
br label %28
28: ; preds = %25, %17
%29 = load i32, i32* %6
%30 = icmp eq i32 %29, 37
br i1 %30, label %31, label %39
31: ; preds = %28
%32 = call i64 @chdir(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %31
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 0
store i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i8** %38
br label %39
39: ; preds = %36, %28
%40 = load i32, i32* %6
%41 = icmp eq i32 %40, 73
br i1 %41, label %42, label %50
42: ; preds = %39
%43 = call i64 @chdir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %42
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %42
%48 = load i8**, i8*** %5
%49 = getelementptr inbounds i8*, i8** %48, i64 0
store i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8** %49
br label %50
50: ; preds = %47, %39
%51 = load i32, i32* %6
%52 = icmp eq i32 %51, 137
br i1 %52, label %53, label %61
53: ; preds = %50
%54 = call i64 @chdir(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
%55 = icmp ne i64 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %53
%57 = call i32 @exit(i32 1)
unreachable
58: ; preds = %53
%59 = load i8**, i8*** %5
%60 = getelementptr inbounds i8*, i8** %59, i64 0
store i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i8** %60
br label %61
61: ; preds = %58, %50
%62 = load i32, i32* %6
%63 = icmp sgt i32 %62, 1000
br i1 %63, label %64, label %72
64: ; preds = %61
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0))
%66 = call i32 @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0))
%67 = icmp ne i32 %66, 0
br i1 %67, label %70, label %68
68: ; preds = %64
%69 = call i32 @exit(i32 1)
unreachable
70: ; preds = %64
%71 = call i32 @exit(i32 0)
unreachable
72: ; preds = %61
%73 = call i32 (...) @fork()
store i32 %73, i32* %7
%74 = load i32, i32* %7
%75 = icmp ne i32 %74, 0
br i1 %75, label %76, label %83
76: ; preds = %72
%77 = load i32, i32* %6
%78 = load i32, i32* %7
%79 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.10, i64 0, i64 0), i32 %77, i32 %78)
%80 = load i32, i32* %7
%81 = call i32 @waitpid(i32 %80, i32 0, i32 0)
%82 = call i32 @exit(i32 0)
unreachable
83: ; preds = %72
%84 = call i8* @malloc(i32 1000)
store i8* %84, i8** %8
%85 = load i8*, i8** %8
%86 = load i32, i32* %6
%87 = add nsw i32 %86, 1
%88 = call i32 @sprintf(i8* %85, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %87)
%89 = load i8**, i8*** %5
%90 = getelementptr inbounds i8*, i8** %89, i64 0
%91 = load i8*, i8** %90
%92 = load i8**, i8*** %5
%93 = getelementptr inbounds i8*, i8** %92, i64 0
%94 = load i8*, i8** %93
%95 = load i8*, i8** %8
%96 = call i32 @execl(i8* %91, i8* %94, i8* %95, i8* null)
%97 = load i32, i32* @errno
%98 = call i8* @strerror(i32 %97)
%99 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.11, i64 0, i64 0), i8* %98)
br label %100
100: ; preds = %83
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [4 x i8] c"tmp\00"
@.str.2 = [20 x i8] c"../tests/forks.test\00"
@.str.3 = [8 x i8] c"subdir2\00"
@.str.4 = [23 x i8] c"../../tests/forks.test\00"
@.str.5 = [3 x i8] c"..\00"
@.str.6 = [8 x i8] c"subdir1\00"
@.str.8 = [14 x i8] c"deepdir/forks\00"
@.str.9 = [2 x i8] c"w\00"
@.str.10 = [17 x i8] c"Child %d was %d\0A\00"
@.str.11 = [18 x i8] c"execl failed: %s\0A\00"
@errno = global i32 0
@str = [25 x i8] c"We are finally all done!\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %7
6: ; preds = %2
store i32 0, i32* %3
br label %61
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = call i32 @sscanf(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %3)
%11 = load i32, i32* %3
%12 = icmp eq i32 %11, 7
br i1 %12, label %13, label %20
13: ; preds = %7
%14 = call i64 @chdir(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i64 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %13
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %13
store i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8** %1
%19 = load i32, i32* %3
br label %20
20: ; preds = %18, %7
%21 = phi i32 [ %19, %18 ], [ %11, %7 ]
%22 = icmp eq i32 %21, 37
br i1 %22, label %23, label %30
23: ; preds = %20
%24 = call i64 @chdir(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%25 = icmp eq i64 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %23
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %23
store i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i8** %1
%29 = load i32, i32* %3
br label %30
30: ; preds = %28, %20
%31 = phi i32 [ %29, %28 ], [ %21, %20 ]
%32 = icmp eq i32 %31, 73
br i1 %32, label %33, label %40
33: ; preds = %30
%34 = call i64 @chdir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%35 = icmp eq i64 %34, 0
br i1 %35, label %38, label %36
36: ; preds = %33
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %33
store i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8** %1
%39 = load i32, i32* %3
br label %40
40: ; preds = %38, %30
%41 = phi i32 [ %39, %38 ], [ %31, %30 ]
%42 = icmp eq i32 %41, 137
br i1 %42, label %43, label %50
43: ; preds = %40
%44 = call i64 @chdir(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
%45 = icmp eq i64 %44, 0
br i1 %45, label %48, label %46
46: ; preds = %43
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %43
store i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i8** %1
%49 = load i32, i32* %3
br label %50
50: ; preds = %48, %40
%51 = phi i32 [ %49, %48 ], [ %41, %40 ]
%52 = icmp sgt i32 %51, 1000
br i1 %52, label %53, label %61
53: ; preds = %50
%54 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%55 = call i32 @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0))
%56 = icmp eq i32 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %53
%58 = call i32 @exit(i32 1)
unreachable
59: ; preds = %53
%60 = call i32 @exit(i32 0)
unreachable
61: ; preds = %6, %50
%62 = call i32 (...) @fork()
%63 = icmp eq i32 %62, 0
br i1 %63, label %69, label %64
64: ; preds = %61
%65 = load i32, i32* %3
%66 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.10, i64 0, i64 0), i32 %65, i32 %62)
%67 = call i32 @waitpid(i32 %62, i32 0, i32 0)
%68 = call i32 @exit(i32 0)
unreachable
69: ; preds = %61
%70 = call dereferenceable_or_null(1000) i8* @malloc(i32 1000)
%71 = load i32, i32* %3
%72 = add nsw i32 %71, 1
%73 = call i32 @sprintf(i8* dereferenceable(1) %70, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %72)
%74 = load i8*, i8** %1
%75 = call i32 @execl(i8* %74, i8* %74, i8* %70, i8* null)
%76 = load i32, i32* @errno
%77 = call i8* @strerror(i32 %76)
%78 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.11, i64 0, i64 0), i8* %77)
ret i32 0
}