#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*/ execvp(char *const, char *const *);
scalar_t__ fork();
int /*<<< orphan*/ sys_die(char *, ...);
extern int sys_exec(char *const argv[]) {
  pid_t pid;
  if ((pid = fork()) == 0) {
    execvp(argv[0], argv);
    sys_die("sys: execv of %s failed\n", argv[0]);
  } else if (pid < 0)
    sys_die("sys: cannot create a child process\n");
  return pid;
}


x86 assembly (clang, -O0):
<sys_exec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1050 <fork@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    117b <sys_exec+0x4b>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <execvp@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
lea    0xe91(%rip),%rdi
mov    $0x0,%al
call   1030 <sys_die@plt>
jmp    1199 <sys_exec+0x69>
cmpq   $0x0,-0x10(%rbp)
jge    1194 <sys_exec+0x64>
lea    0xe8c(%rip),%rdi
mov    $0x0,%al
call   1030 <sys_die@plt>
jmp    1199 <sys_exec+0x69>
mov    -0x10(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sys_exec>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %eax,%eax
call   1050 <fork@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1158 <sys_exec+0x28>
jns    1174 <sys_exec+0x44>
lea    0xeca(%rip),%rdi
xor    %eax,%eax
call   1030 <sys_die@plt>
jmp    1174 <sys_exec+0x44>
mov    (%r14),%rdi
mov    %r14,%rsi
call   1040 <execvp@plt>
mov    (%r14),%rsi
lea    0xe93(%rip),%rdi
xor    %eax,%eax
call   1030 <sys_die@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [25 x i8] c"sys: execv of %s failed\0A\00"
@.str.1 = [36 x i8] c"sys: cannot create a child process\0A\00"
define i32 @sys_exec(i8** %0) {
%2 = alloca i8**
%3 = alloca i64
store i8** %0, i8*** %2
%4 = call i64 (...) @fork()
store i64 %4, i64* %3
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %16
6: ; preds = %1
%7 = load i8**, i8*** %2
%8 = getelementptr inbounds i8*, i8** %7, i64 0
%9 = load i8*, i8** %8
%10 = load i8**, i8*** %2
%11 = call i32 @execvp(i8* %9, i8** %10)
%12 = load i8**, i8*** %2
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i32 (i8*, ...) @sys_die(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %14)
br label %22
16: ; preds = %1
%17 = load i64, i64* %3
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = call i32 (i8*, ...) @sys_die(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %19, %16
br label %22
22: ; preds = %21, %6
%23 = load i64, i64* %3
%24 = trunc i64 %23 to i32
ret i32 %24
}

LLVM IR (-O2):
@.str = [25 x i8] c"sys: execv of %s failed\0A\00"
@.str.1 = [36 x i8] c"sys: cannot create a child process\0A\00"
define i32 @sys_exec(i8** %0) {
%2 = tail call i64 (...) @fork()
%3 = icmp eq i64 %2, 0
br i1 %3, label %4, label %9
4: ; preds = %1
%5 = load i8*, i8** %0
%6 = tail call i32 @execvp(i8* %5, i8** %0)
%7 = load i8*, i8** %0
%8 = tail call i32 (i8*, ...) @sys_die(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %7)
br label %13
9: ; preds = %1
%10 = icmp slt i64 %2, 0
br i1 %10, label %11, label %13
11: ; preds = %9
%12 = tail call i32 (i8*, ...) @sys_die(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %9, %11, %4
%14 = trunc i64 %2 to i32
ret i32 %14
}