#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*/ EXIT_FAILURE;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ do_foo(int);
int /*<<< orphan*/ execvp(char *, char **);
int fork();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
void main() {
  printf("sleep!\n");
  sleep(5);
  printf("fork!\n");
  pid_t pid = fork();
  char *args[64] = {NULL};
  printf("%lu\n", pid);
  if (pid == -1) {
    printf("error, failed to fork()");
  } else if (pid > 0) {
    do_foo(1000);
    int status;
    waitpid(pid, &status, 0);
  } else {
    do_foo(1000);
    char *name[] = {"bash", "-c", "echo 'Hello World'", NULL};
    execvp(name[0], name);
    _exit(EXIT_FAILURE);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x5,%edi
call   1090 <sleep@plt>
lea    0xe5e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x4(%rbp)
lea    -0x210(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1060 <memset@plt>
mov    -0x4(%rbp),%esi
lea    0xe37(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0xffffffff,-0x4(%rbp)
jne    11fc <main+0x7c>
lea    0xe24(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    128e <main+0x10e>
cmpl   $0x0,-0x4(%rbp)
jle    1226 <main+0xa6>
mov    $0x3e8,%edi
call   1040 <do_foo@plt>
mov    -0x4(%rbp),%edi
lea    -0x214(%rbp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
jmp    1289 <main+0x109>
mov    $0x3e8,%edi
call   1040 <do_foo@plt>
mov    0x2bb9(%rip),%rax
mov    %rax,-0x240(%rbp)
mov    0x2bb3(%rip),%rax
mov    %rax,-0x238(%rbp)
mov    0x2bad(%rip),%rax
mov    %rax,-0x230(%rbp)
mov    0x2ba7(%rip),%rax
mov    %rax,-0x228(%rbp)
mov    -0x240(%rbp),%rdi
lea    -0x240(%rbp),%rsi
call   1080 <execvp@plt>
mov    0x2d56(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
jmp    128e <main+0x10e>
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
lea    0xeac(%rip),%rdi
call   1040 <puts@plt>
mov    $0x5,%edi
call   1090 <sleep@plt>
lea    0xe9d(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,%ebx
lea    0xe49(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
cmp    $0xffffffff,%ebx
je     11e5 <main+0x65>
mov    $0x3e8,%edi
call   1050 <do_foo@plt>
test   %ebx,%ebx
jle    11f8 <main+0x78>
mov    %rsp,%rsi
mov    %ebx,%edi
xor    %edx,%edx
call   1070 <waitpid@plt>
add    $0x20,%rsp
pop    %rbx
ret
lea    0xe19(%rip),%rdi
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
jmp    1060 <printf@plt>
movaps 0x2c01(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0x2be5(%rip),%xmm0
movaps %xmm0,(%rsp)
lea    0xe07(%rip),%rdi
mov    %rsp,%rsi
call   1080 <execvp@plt>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"sleep!\0A\00"
@.str.1 = [7 x i8] c"fork!\0A\00"
@.str.2 = [5 x i8] c"%lu\0A\00"
@.str.3 = [24 x i8] c"error, failed to fork()\00"
@.str.4 = [5 x i8] c"bash\00"
@.str.5 = [3 x i8] c"-c\00"
@.str.6 = [19 x i8] c"echo 'Hello World'\00"
@__const.main.name = [4 x i8*] [i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i32 0, i32 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i32 0, i32 0), i8* null]
@EXIT_FAILURE = global i32 0
define void @main() {
%1 = alloca i32
%2 = alloca [64 x i8*]
%3 = alloca i32
%4 = alloca [4 x i8*]
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%6 = call i32 @sleep(i32 5)
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%8 = call i32 (...) @fork()
store i32 %8, i32* %1
%9 = bitcast [64 x i8*]* %2 to i8*
%10 = load i32, i32* %1
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %10)
%12 = load i32, i32* %1
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
br label %33
16: ; preds = %0
%17 = load i32, i32* %1
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = call i32 @do_foo(i32 1000)
%21 = load i32, i32* %1
%22 = call i32 @waitpid(i32 %21, i32* %3, i32 0)
br label %32
23: ; preds = %16
%24 = call i32 @do_foo(i32 1000)
%25 = bitcast [4 x i8*]* %4 to i8*
%26 = getelementptr inbounds [4 x i8*], [4 x i8*]* %4, i64 0, i64 0
%27 = load i8*, i8** %26
%28 = getelementptr inbounds [4 x i8*], [4 x i8*]* %4, i64 0, i64 0
%29 = call i32 @execvp(i8* %27, i8** %28)
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @_exit(i32 %30)
unreachable
32: ; preds = %19
br label %33
33: ; preds = %32, %14
ret void
}

LLVM IR (-O2):
@.str.2 = [5 x i8] c"%lu\0A\00"
@.str.3 = [24 x i8] c"error, failed to fork()\00"
@.str.4 = [5 x i8] c"bash\00"
@.str.5 = [3 x i8] c"-c\00"
@.str.6 = [19 x i8] c"echo 'Hello World'\00"
@__const.main.name = [4 x i8*] [i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i32 0, i32 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i32 0, i32 0), i8* null]
@EXIT_FAILURE = global i32 0
@str = [7 x i8] c"sleep!\00"
@str.7 = [6 x i8] c"fork!\00"
define void @main() {
%1 = alloca i32
%2 = alloca [4 x i8*]
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str, i64 0, i64 0))
%4 = tail call i32 @sleep(i32 5)
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str.7, i64 0, i64 0))
%6 = tail call i32 (...) @fork()
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %6)
%8 = icmp eq i32 %6, -1
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
br label %23
11: ; preds = %0
%12 = icmp sgt i32 %6, 0
%13 = tail call i32 @do_foo(i32 1000)
br i1 %12, label %14, label %17
14: ; preds = %11
%15 = bitcast i32* %1 to i8*
%16 = call i32 @waitpid(i32 %6, i32* %1, i32 0)
br label %23
17: ; preds = %11
%18 = bitcast [4 x i8*]* %2 to i8*
%19 = getelementptr inbounds [4 x i8*], [4 x i8*]* %2, i64 0, i64 0
%20 = call i32 @execvp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8** %19)
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @_exit(i32 %21)
unreachable
23: ; preds = %14, %9
ret void
}