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

/* Variables and functions */
int /*<<< orphan*/ execv(char *, char **);
scalar_t__ fork();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
int /*<<< orphan*/ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int forkAndExec(int argc, char *argv[]) {
  pid_t pid;
  int status = 0;
  pid = fork();
  if (pid == 0) {
    char path[30];
    sprintf(path, "%s%s", "./", argv[0]);
    execv(path, argv);
  } else if (pid < 0) {
    printf("Lol couldn't fork, that's a big problem\n");
    return 1;
  } else if (pid > 0) {
    waitpid(pid, &status, 0);
  }
  return status;
}


x86 assembly (clang, -O0):
<forkAndExec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ac <forkAndExec+0x5c>
lea    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
lea    0xe72(%rip),%rsi
lea    0xe70(%rip),%rdx
call   1050 <sprintf@plt>
lea    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <execv@plt>
jmp    11f3 <forkAndExec+0xa3>
cmpq   $0x0,-0x18(%rbp)
jge    11cf <forkAndExec+0x7f>
lea    0xe4a(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11f9 <forkAndExec+0xa9>
cmpq   $0x0,-0x18(%rbp)
jle    11e9 <forkAndExec+0x99>
mov    -0x18(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
jmp    11ee <forkAndExec+0x9e>
jmp    11f3 <forkAndExec+0xa3>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<forkAndExec>:
push   %r15
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %rsi,%r14
movl   $0x0,0xc(%rsp)
xor    %ebx,%ebx
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
je     1189 <forkAndExec+0x39>
js     11b4 <forkAndExec+0x64>
lea    0xc(%rsp),%rsi
mov    %rax,%rdi
xor    %edx,%edx
call   1040 <waitpid@plt>
mov    0xc(%rsp),%ebx
jmp    11c5 <forkAndExec+0x75>
mov    (%r14),%rcx
lea    0xe6d(%rip),%rsi
lea    0xe6b(%rip),%rdx
lea    0x10(%rsp),%r15
mov    %r15,%rdi
call   1050 <sprintf@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1060 <execv@plt>
jmp    11c5 <forkAndExec+0x75>
lea    0xe4d(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%s%s\00"
@.str.1 = [3 x i8] c"./\00"
@.str.2 = [41 x i8] c"Lol couldn't fork, that's a big problem\0A\00"
define i32 @forkAndExec(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
%8 = alloca [30 x i8]
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %7
%9 = call i64 (...) @fork()
store i64 %9, i64* %6
%10 = load i64, i64* %6
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %21
12: ; preds = %2
%13 = getelementptr inbounds [30 x i8], [30 x i8]* %8, i64 0, i64 0
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 @sprintf(i8* %13, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %16)
%18 = getelementptr inbounds [30 x i8], [30 x i8]* %8, i64 0, i64 0
%19 = load i8**, i8*** %5
%20 = call i32 @execv(i8* %18, i8** %19)
br label %34
21: ; preds = %2
%22 = load i64, i64* %6
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %36
26: ; preds = %21
%27 = load i64, i64* %6
%28 = icmp sgt i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = load i64, i64* %6
%31 = call i32 @waitpid(i64 %30, i32* %7, i32 0)
br label %32
32: ; preds = %29, %26
br label %33
33: ; preds = %32
br label %34
34: ; preds = %33, %12
%35 = load i32, i32* %7
store i32 %35, i32* %3
br label %36
36: ; preds = %34, %24
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
@.str = [5 x i8] c"%s%s\00"
@.str.1 = [3 x i8] c"./\00"
@str = [40 x i8] c"Lol couldn't fork, that's a big problem\00"
define i32 @forkAndExec(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca [30 x i8]
%5 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%6 = tail call i64 (...) @fork()
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %13
8: ; preds = %2
%9 = getelementptr inbounds [30 x i8], [30 x i8]* %4, i64 0, i64 0
%10 = load i8*, i8** %1
%11 = call i32 @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %10)
%12 = call i32 @execv(i8* %9, i8** %1)
br label %20
13: ; preds = %2
%14 = icmp slt i64 %6, 0
br i1 %14, label %15, label %17
15: ; preds = %13
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str, i64 0, i64 0))
br label %20
17: ; preds = %13
%18 = call i32 @waitpid(i64 %6, i32* %3, i32 0)
%19 = load i32, i32* %3
br label %20
20: ; preds = %8, %17, %15
%21 = phi i32 [ 1, %15 ], [ %19, %17 ], [ 0, %8 ]
ret i32 %21
}