#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 *, char *const *, int /*<<< orphan*/ *);
int fork();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ waitpid(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  printf("Forking...\n");
  int x;
  for (x = 0; x < 100; x++) {
    int f = fork();
    if (!f) {
      printf("Execing child...\n");
      char *arg1 = "/bin/stall";
      char *const argv[] = {arg1, NULL};
      execve(arg1, argv, NULL);
    }
    int old = f;
    f = fork();
    if (!f) {
      printf("Execing child...\n");
      char *arg1 = "/bin/stall";
      char *const argv[] = {arg1, NULL};
      execve(arg1, argv, NULL);
    } else {
      printf("Parent wait.\n");
      waitpid(old, NULL, 0);
      waitpid(f, NULL, 0);
      printf("Parent is back.\n");
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xea3(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0x64,-0x14(%rbp)
jge    125d <main+0x11d>
mov    $0x0,%al
call   1060 <fork@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    11bf <main+0x7f>
lea    0xe7e(%rip),%rdi
call   1030 <printf@plt>
lea    0xe84(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x30(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <execve@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    $0x0,%al
call   1060 <fork@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    1216 <main+0xd6>
lea    0xe2c(%rip),%rdi
call   1030 <printf@plt>
lea    0xe32(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x40(%rbp),%rdi
lea    -0x50(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <execve@plt>
jmp    124a <main+0x10a>
lea    0xe0c(%rip),%rdi
call   1030 <printf@plt>
mov    -0x34(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    -0x18(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
lea    0xdf2(%rip),%rdi
call   1030 <printf@plt>
jmp    124f <main+0x10f>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1169 <main+0x29>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xeb6(%rip),%rdi
call   1030 <puts@plt>
mov    $0x64,%r13d
lea    0xeaf(%rip),%r14
lea    0xe92(%rip),%rbx
mov    %rsp,%r15
jmp    11b8 <main+0x78>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %eax,%r12d
lea    0xe9d(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    %r12d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
lea    0xe87(%rip),%rdi
call   1030 <puts@plt>
add    $0xffffffff,%r13d
je     121a <main+0xda>
xor    %eax,%eax
call   1060 <fork@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    11e7 <main+0xa7>
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %rbx,(%rsp)
movq   $0x0,0x8(%rsp)
mov    %rbx,%rdi
mov    %r15,%rsi
xor    %edx,%edx
call   1040 <execve@plt>
xor    %eax,%eax
call   1060 <fork@plt>
test   %eax,%eax
jne    1180 <main+0x40>
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %rbx,(%rsp)
movq   $0x0,0x8(%rsp)
mov    %rbx,%rdi
mov    %r15,%rsi
xor    %edx,%edx
call   1040 <execve@plt>
add    $0xffffffff,%r13d
jne    11b8 <main+0x78>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"Forking...\0A\00"
@.str.1 = [18 x i8] c"Execing child...\0A\00"
@.str.2 = [11 x i8] c"/bin/stall\00"
@.str.3 = [14 x i8] c"Parent wait.\0A\00"
@.str.4 = [17 x i8] c"Parent is back.\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 i8*
%9 = alloca [2 x i8*]
%10 = alloca i32
%11 = alloca i8*
%12 = alloca [2 x i8*]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %6
br label %14
14: ; preds = %50, %2
%15 = load i32, i32* %6
%16 = icmp slt i32 %15, 100
br i1 %16, label %17, label %53
17: ; preds = %14
%18 = call i32 (...) @fork()
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = icmp ne i32 %19, 0
br i1 %20, label %29, label %21
21: ; preds = %17
%22 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8** %8
%23 = getelementptr inbounds [2 x i8*], [2 x i8*]* %9, i64 0, i64 0
%24 = load i8*, i8** %8
store i8* %24, i8** %23
%25 = getelementptr inbounds i8*, i8** %23, i64 1
store i8* null, i8** %25
%26 = load i8*, i8** %8
%27 = getelementptr inbounds [2 x i8*], [2 x i8*]* %9, i64 0, i64 0
%28 = call i32 @execve(i8* %26, i8** %27, i32* null)
br label %29
29: ; preds = %21, %17
%30 = load i32, i32* %7
store i32 %30, i32* %10
%31 = call i32 (...) @fork()
store i32 %31, i32* %7
%32 = load i32, i32* %7
%33 = icmp ne i32 %32, 0
br i1 %33, label %42, label %34
34: ; preds = %29
%35 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8** %11
%36 = getelementptr inbounds [2 x i8*], [2 x i8*]* %12, i64 0, i64 0
%37 = load i8*, i8** %11
store i8* %37, i8** %36
%38 = getelementptr inbounds i8*, i8** %36, i64 1
store i8* null, i8** %38
%39 = load i8*, i8** %11
%40 = getelementptr inbounds [2 x i8*], [2 x i8*]* %12, i64 0, i64 0
%41 = call i32 @execve(i8* %39, i8** %40, i32* null)
br label %49
42: ; preds = %29
%43 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%44 = load i32, i32* %10
%45 = call i32 @waitpid(i32 %44, i32* null, i32 0)
%46 = load i32, i32* %7
%47 = call i32 @waitpid(i32 %46, i32* null, i32 0)
%48 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
br label %49
49: ; preds = %42, %34
br label %50
50: ; preds = %49
%51 = load i32, i32* %6
%52 = add nsw i32 %51, 1
store i32 %52, i32* %6
br label %14
53: ; preds = %14
ret i32 0
}

LLVM IR (-O2):
@.str.2 = [11 x i8] c"/bin/stall\00"
@str = [11 x i8] c"Forking...\00"
@str.6 = [17 x i8] c"Execing child...\00"
@str.7 = [13 x i8] c"Parent wait.\00"
@str.8 = [16 x i8] c"Parent is back.\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [2 x i8*]
%4 = alloca [2 x i8*]
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
%6 = bitcast [2 x i8*]* %3 to i8*
%7 = getelementptr inbounds [2 x i8*], [2 x i8*]* %3, i64 0, i64 0
%8 = getelementptr inbounds [2 x i8*], [2 x i8*]* %3, i64 0, i64 1
%9 = bitcast [2 x i8*]* %4 to i8*
%10 = getelementptr inbounds [2 x i8*], [2 x i8*]* %4, i64 0, i64 0
%11 = getelementptr inbounds [2 x i8*], [2 x i8*]* %4, i64 0, i64 1
br label %12
12: ; preds = %2, %30
%13 = phi i32 [ 0, %2 ], [ %31, %30 ]
%14 = call i32 (...) @fork()
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.6, i64 0, i64 0))
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8** %7
store i8* null, i8** %8
%18 = call i32 @execve(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8** %7, i32* null)
br label %19
19: ; preds = %16, %12
%20 = call i32 (...) @fork()
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.6, i64 0, i64 0))
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8** %10
store i8* null, i8** %11
%24 = call i32 @execve(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8** %10, i32* null)
br label %30
25: ; preds = %19
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.7, i64 0, i64 0))
%27 = call i32 @waitpid(i32 %14, i32* null, i32 0)
%28 = call i32 @waitpid(i32 %20, i32* null, i32 0)
%29 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.8, i64 0, i64 0))
br label %30
30: ; preds = %25, %22
%31 = add nuw nsw i32 %13, 1
%32 = icmp eq i32 %31, 100
br i1 %32, label %33, label %12
33: ; preds = %30
ret i32 0
}