#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__ WEXITSTATUS(int);
int /*<<< orphan*/ WIFEXITED(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ execl(char *, char *, char *, char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fopen(char *, char *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ sscanf(char *, char *, int *);
char *strerror(int /*<<< orphan*/);
int vfork();
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  int num;
  if (argc < 2) {
    num = 0;
  } else {
    sscanf(argv[1], "%d", &num);
  }
  if (num > 1000) {
    printf("We are finally all done!\n");
    if (!fopen("tmp.vforks", "w"))
      exit(1);
    exit(0);
  }
  pid_t child = vfork();
  if (child) {
    printf("Child %d was %d\n", num, child);
    int status;
    waitpid(child, &status, 0);
    if (!WIFEXITED(status) || WEXITSTATUS(status)) {
      printf("Child did not exit right.\n");
      exit(1);
    }
    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    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11ec <main+0x2c>
movl   $0x0,-0x14(%rbp)
jmp    1204 <main+0x44>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe05(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1050 <sscanf@plt>
cmpl   $0x3e8,-0x14(%rbp)
jle    124c <main+0x8c>
lea    0xdeb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xdf7(%rip),%rdi
lea    0xdfb(%rip),%rsi
call   1080 <fopen@plt>
cmp    $0x0,%eax
jne    1245 <main+0x85>
mov    $0x1,%edi
call   10c0 <exit@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
call   1090 <vfork@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     12c2 <main+0x102>
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    0xdbf(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%edi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
mov    -0x1c(%rbp),%edi
call   10a0 <WIFEXITED@plt>
cmp    $0x0,%eax
je     12a3 <main+0xe3>
mov    -0x1c(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
cmp    $0x0,%rax
je     12bb <main+0xfb>
lea    0xd91(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    $0x3e8,%edi
call   1040 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%edx
add    $0x1,%edx
lea    0xd1f(%rip),%rsi
call   10b0 <sprintf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x28(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   10e0 <execl@plt>
mov    0x2cd8(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
lea    0xd3d(%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
mov    %rsi,%rbx
cmp    $0x1,%edi
jg     11e4 <main+0x14>
movl   $0x0,(%rsp)
jmp    1204 <main+0x34>
mov    0x8(%rbx),%rdi
lea    0xe11(%rip),%rsi
mov    %rsp,%rdx
call   1060 <sscanf@plt>
cmpl   $0x3e9,(%rsp)
jge    12bf <main+0xef>
call   10a0 <vfork@plt>
test   %eax,%eax
jne    1269 <main+0x99>
mov    $0x3e8,%edi
call   1050 <malloc@plt>
mov    %rax,%rbp
mov    (%rsp),%edx
add    $0x1,%edx
lea    0xdd9(%rip),%rsi
mov    %rax,%rdi
call   10c0 <sprintf@plt>
mov    (%rbx),%rdi
xor    %ebx,%ebx
mov    %ebx,%ecx
mov    %rdi,%rsi
mov    %rbp,%rdx
call   10f0 <execl@plt>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xdcb(%rip),%rdi
mov    %rax,%rsi
mov    %ebx,%eax
call   1040 <printf@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    %eax,%ebx
mov    (%rsp),%esi
lea    0xd9b(%rip),%rdi
xor    %ebp,%ebp
mov    %ebp,%eax
mov    %ebx,%edx
call   1040 <printf@plt>
lea    0x4(%rsp),%rsi
mov    %ebx,%edi
mov    %ebp,%edx
call   1080 <waitpid@plt>
mov    0x4(%rsp),%edi
call   10b0 <WIFEXITED@plt>
test   %eax,%eax
je     12a9 <main+0xd9>
mov    0x4(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
test   %rax,%rax
je     12ec <main+0x11c>
lea    0xd83(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xd87(%rip),%rdi
call   1030 <puts@plt>
lea    0xd31(%rip),%rdi
lea    0xd35(%rip),%rsi
call   1090 <fopen@plt>
test   %eax,%eax
jne    12ec <main+0x11c>
mov    $0x1,%edi
call   10d0 <exit@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [26 x i8] c"We are finally all done!\0A\00"
@.str.2 = [11 x i8] c"tmp.vforks\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [17 x i8] c"Child %d was %d\0A\00"
@.str.5 = [27 x i8] c"Child did not exit right.\0A\00"
@.str.6 = [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 i32
%9 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 2
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 0, i32* %6
br label %18
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call i32 @sscanf(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %6)
br label %18
18: ; preds = %13, %12
%19 = load i32, i32* %6
%20 = icmp sgt i32 %19, 1000
br i1 %20, label %21, label %29
21: ; preds = %18
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%24 = icmp ne i32 %23, 0
br i1 %24, label %27, label %25
25: ; preds = %21
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %21
%28 = call i32 @exit(i32 0)
unreachable
29: ; preds = %18
%30 = call i32 @vfork()
store i32 %30, i32* %7
%31 = load i32, i32* %7
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %51
33: ; preds = %29
%34 = load i32, i32* %6
%35 = load i32, i32* %7
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %34, i32 %35)
%37 = load i32, i32* %7
%38 = call i32 @waitpid(i32 %37, i32* %8, i32 0)
%39 = load i32, i32* %8
%40 = call i32 @WIFEXITED(i32 %39)
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %33
%43 = load i32, i32* %8
%44 = call i64 @WEXITSTATUS(i32 %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %42, %33
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %42
%50 = call i32 @exit(i32 0)
unreachable
51: ; preds = %29
%52 = call i8* @malloc(i32 1000)
store i8* %52, i8** %9
%53 = load i8*, i8** %9
%54 = load i32, i32* %6
%55 = add nsw i32 %54, 1
%56 = call i32 @sprintf(i8* %53, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %55)
%57 = load i8**, i8*** %5
%58 = getelementptr inbounds i8*, i8** %57, i64 0
%59 = load i8*, i8** %58
%60 = load i8**, i8*** %5
%61 = getelementptr inbounds i8*, i8** %60, i64 0
%62 = load i8*, i8** %61
%63 = load i8*, i8** %9
%64 = call i32 @execl(i8* %59, i8* %62, i8* %63, i8* null)
%65 = load i32, i32* @errno
%66 = call i8* @strerror(i32 %65)
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i8* %66)
br label %68
68: ; preds = %51
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.2 = [11 x i8] c"tmp.vforks\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [17 x i8] c"Child %d was %d\0A\00"
@.str.6 = [18 x i8] c"execl failed: %s\0A\00"
@errno = global i32 0
@str = [26 x i8] c"Child did not exit right.\00"
@str.7 = [25 x i8] c"We are finally all done!\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
%6 = icmp slt i32 %0, 2
br i1 %6, label %7, label %8
7: ; preds = %2
store i32 0, i32* %3
br label %22
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = call i32 @sscanf(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %3)
%12 = load i32, i32* %3
%13 = icmp sgt i32 %12, 1000
br i1 %13, label %14, label %22
14: ; preds = %8
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.7, i64 0, i64 0))
%16 = call i32 @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %14
%21 = call i32 @exit(i32 0)
unreachable
22: ; preds = %7, %8
%23 = call i32 @vfork()
%24 = icmp eq i32 %23, 0
br i1 %24, label %42, label %25
25: ; preds = %22
%26 = load i32, i32* %3
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %26, i32 %23)
%28 = bitcast i32* %4 to i8*
%29 = call i32 @waitpid(i32 %23, i32* %4, i32 0)
%30 = load i32, i32* %4
%31 = call i32 @WIFEXITED(i32 %30)
%32 = icmp eq i32 %31, 0
br i1 %32, label %37, label %33
33: ; preds = %25
%34 = load i32, i32* %4
%35 = call i64 @WEXITSTATUS(i32 %34)
%36 = icmp eq i64 %35, 0
br i1 %36, label %40, label %37
37: ; preds = %33, %25
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %33
%41 = call i32 @exit(i32 0)
unreachable
42: ; preds = %22
%43 = call dereferenceable_or_null(1000) i8* @malloc(i32 1000)
%44 = load i32, i32* %3
%45 = add nsw i32 %44, 1
%46 = call i32 @sprintf(i8* dereferenceable(1) %43, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %45)
%47 = load i8*, i8** %1
%48 = call i32 @execl(i8* %47, i8* %47, i8* %43, i8* null)
%49 = load i32, i32* @errno
%50 = call i8* @strerror(i32 %49)
%51 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i8* %50)
ret i32 0
}