#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 */
int /*<<< orphan*/ assert(int);
int fork();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ remove_dir();
int setup_dir();
int /*<<< orphan*/ test_memdev();
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int ret = 0;
  int count = 0;
  pid_t child;
  int status = 0;
  ret = setup_dir();
  if (ret < 0) {
    return ret;
  }
  while (count < 100) {
    printf("Loop %d (%d)\n", count, status);
    child = fork();
    if (child == -1) {
      printf("Failed to fork\n");
      return -1;
    } else if (child > 0) {
      status = 0;
      waitpid(child, &status, 0);
      if (status == 0) {
        goto done;
      }
    } else {
      assert(child == 0);
      test_memdev();
      return 0;
    }
    count++;
  }
done:
  remove_dir();
  if (count == 100) {
    printf("Err\n");
    return -1;
  } else {
    printf("Ok\n");
    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)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
mov    $0x0,%al
call   1070 <setup_dir@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11ba <main+0x4a>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12ba <main+0x14a>
jmp    11bf <main+0x4f>
cmpl   $0x64,-0x18(%rbp)
jge    1275 <main+0x105>
mov    -0x18(%rbp),%esi
mov    -0x20(%rbp),%edx
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    120b <main+0x9b>
lea    0xe16(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ba <main+0x14a>
cmpl   $0x0,-0x1c(%rbp)
jle    123e <main+0xce>
movl   $0x0,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
cmpl   $0x0,-0x20(%rbp)
jne    1239 <main+0xc9>
jmp    127a <main+0x10a>
jmp    1262 <main+0xf2>
cmpl   $0x0,-0x1c(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    $0x0,%al
call   1040 <test_memdev@plt>
movl   $0x0,-0x4(%rbp)
jmp    12ba <main+0x14a>
jmp    1267 <main+0xf7>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11bf <main+0x4f>
jmp    127a <main+0x10a>
mov    $0x0,%al
call   1060 <remove_dir@plt>
cmpl   $0x64,-0x18(%rbp)
jne    12a5 <main+0x135>
lea    0xd8c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ba <main+0x14a>
lea    0xd77(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
movl   $0x0,0xc(%rsp)
xor    %eax,%eax
call   1080 <setup_dir@plt>
test   %eax,%eax
js     1200 <main+0x80>
lea    0xe5e(%rip),%rbx
lea    0xc(%rsp),%r14
xor    %edx,%edx
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
cmp    $0xffffffff,%eax
je     1204 <main+0x84>
test   %eax,%eax
jle    1217 <main+0x97>
movl   $0x0,0xc(%rsp)
mov    %eax,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1090 <waitpid@plt>
mov    0xc(%rsp),%edx
test   %edx,%edx
je     122d <main+0xad>
add    $0x1,%ebp
cmp    $0x64,%ebp
jne    11b0 <main+0x30>
xor    %eax,%eax
call   1070 <remove_dir@plt>
lea    0xe13(%rip),%rdi
jmp    120b <main+0x8b>
mov    %eax,%ebx
jmp    1242 <main+0xc2>
lea    0xe0a(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
jmp    1242 <main+0xc2>
sete   %al
movzbl %al,%edi
call   1060 <assert@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <test_memdev@plt>
jmp    1242 <main+0xc2>
xor    %ebx,%ebx
xor    %eax,%eax
call   1070 <remove_dir@plt>
lea    0xdd1(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"Loop %d (%d)\0A\00"
@.str.1 = [16 x i8] c"Failed to fork\0A\00"
@.str.2 = [5 x i8] c"Err\0A\00"
@.str.3 = [4 x i8] c"Ok\0A\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
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32 0, i32* %7
store i32 0, i32* %9
%10 = call i32 (...) @setup_dir()
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = load i32, i32* %6
store i32 %14, i32* %3
br label %57
15: ; preds = %2
br label %16
16: ; preds = %45, %15
%17 = load i32, i32* %7
%18 = icmp slt i32 %17, 100
br i1 %18, label %19, label %48
19: ; preds = %16
%20 = load i32, i32* %7
%21 = load i32, i32* %9
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %20, i32 %21)
%23 = call i32 (...) @fork()
store i32 %23, i32* %8
%24 = load i32, i32* %8
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %19
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %57
28: ; preds = %19
%29 = load i32, i32* %8
%30 = icmp sgt i32 %29, 0
br i1 %30, label %31, label %38
31: ; preds = %28
store i32 0, i32* %9
%32 = load i32, i32* %8
%33 = call i32 @waitpid(i32 %32, i32* %9, i32 0)
%34 = load i32, i32* %9
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %31
br label %49
37: ; preds = %31
br label %44
38: ; preds = %28
%39 = load i32, i32* %8
%40 = icmp eq i32 %39, 0
%41 = zext i1 %40 to i32
%42 = call i32 @assert(i32 %41)
%43 = call i32 (...) @test_memdev()
store i32 0, i32* %3
br label %57
44: ; preds = %37
br label %45
45: ; preds = %44
%46 = load i32, i32* %7
%47 = add nsw i32 %46, 1
store i32 %47, i32* %7
br label %16
48: ; preds = %16
br label %49
49: ; preds = %48, %36
%50 = call i32 (...) @remove_dir()
%51 = load i32, i32* %7
%52 = icmp eq i32 %51, 100
br i1 %52, label %53, label %55
53: ; preds = %49
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %57
55: ; preds = %49
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %3
br label %57
57: ; preds = %55, %53, %38, %26, %13
%58 = load i32, i32* %3
ret i32 %58
}

LLVM IR (-O2):
@.str = [14 x i8] c"Loop %d (%d)\0A\00"
@str = [3 x i8] c"Ok\00"
@str.4 = [4 x i8] c"Err\00"
@str.5 = [15 x i8] c"Failed to fork\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%5 = tail call i32 (...) @setup_dir()
%6 = icmp slt i32 %5, 0
br i1 %6, label %35, label %7
7: ; preds = %2, %26
%8 = phi i32 [ %19, %26 ], [ 0, %2 ]
%9 = phi i32 [ %27, %26 ], [ 0, %2 ]
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %9, i32 %8)
%11 = call i32 (...) @fork()
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %7
%14 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.5, i64 0, i64 0))
br label %35
15: ; preds = %7
%16 = icmp sgt i32 %11, 0
br i1 %16, label %17, label %21
17: ; preds = %15
store i32 0, i32* %3
%18 = call i32 @waitpid(i32 %11, i32* %3, i32 0)
%19 = load i32, i32* %3
%20 = icmp eq i32 %19, 0
br i1 %20, label %32, label %26
21: ; preds = %15
%22 = icmp eq i32 %11, 0
%23 = zext i1 %22 to i32
%24 = call i32 @assert(i32 %23)
%25 = call i32 (...) @test_memdev()
br label %35
26: ; preds = %17
%27 = add nuw nsw i32 %9, 1
%28 = icmp eq i32 %27, 100
br i1 %28, label %29, label %7
29: ; preds = %26
%30 = call i32 (...) @remove_dir()
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @str.4, i64 0, i64 0))
br label %35
32: ; preds = %17
%33 = call i32 (...) @remove_dir()
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @str, i64 0, i64 0))
br label %35
35: ; preds = %2, %32, %29, %21, %13
%36 = phi i32 [ -1, %13 ], [ -1, %29 ], [ 0, %32 ], [ 0, %21 ], [ %5, %2 ]
ret i32 %36
}