#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 WEXITSTATUS(int);
int execve(char const *, char **, int /*<<< orphan*/ *);
scalar_t__ fork();
int waitpid(scalar_t__, int *, int /*<<< orphan*/);
int exec_prog(const char **argv) {
  pid_t pid;
  int status;
  pid = fork();
  if (pid == 0) {
    if (execve(argv[0], (char **)argv, NULL) == -1)
      return -1;
    if (waitpid(pid, &status, 0) == -1)
      return -1;
    return WEXITSTATUS(status);
  }
  return -1;
}


x86 assembly (clang, -O0):
<exec_prog>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1060 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11bf <exec_prog+0x7f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <execve@plt>
cmp    $0xffffffff,%eax
jne    118b <exec_prog+0x4b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c6 <exec_prog+0x86>
mov    -0x18(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    11af <exec_prog+0x6f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c6 <exec_prog+0x86>
mov    -0x1c(%rbp),%edi
call   1040 <WEXITSTATUS@plt>
mov    %eax,-0x4(%rbp)
jmp    11c6 <exec_prog+0x86>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<exec_prog>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %eax,%eax
call   1060 <fork@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    1187 <exec_prog+0x47>
mov    (%rbx),%rdi
mov    %rbx,%rsi
xor    %edx,%edx
call   1030 <execve@plt>
cmp    $0xffffffff,%eax
je     1187 <exec_prog+0x47>
lea    0x4(%rsp),%rsi
xor    %edi,%edi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmp    $0xffffffff,%eax
je     1187 <exec_prog+0x47>
mov    0x4(%rsp),%edi
call   1040 <WEXITSTATUS@plt>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @exec_prog(i8** %0) {
%2 = alloca i32
%3 = alloca i8**
%4 = alloca i64
%5 = alloca i32
store i8** %0, i8*** %3
%6 = call i64 (...) @fork()
store i64 %6, i64* %4
%7 = load i64, i64* %4
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %25
9: ; preds = %1
%10 = load i8**, i8*** %3
%11 = getelementptr inbounds i8*, i8** %10, i64 0
%12 = load i8*, i8** %11
%13 = load i8**, i8*** %3
%14 = call i32 @execve(i8* %12, i8** %13, i32* null)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %17
16: ; preds = %9
store i32 -1, i32* %2
br label %26
17: ; preds = %9
%18 = load i64, i64* %4
%19 = call i32 @waitpid(i64 %18, i32* %5, i32 0)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %22
21: ; preds = %17
store i32 -1, i32* %2
br label %26
22: ; preds = %17
%23 = load i32, i32* %5
%24 = call i32 @WEXITSTATUS(i32 %23)
store i32 %24, i32* %2
br label %26
25: ; preds = %1
store i32 -1, i32* %2
br label %26
26: ; preds = %25, %22, %21, %16
%27 = load i32, i32* %2
ret i32 %27
}

LLVM IR (-O2):
define i32 @exec_prog(i8** %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = tail call i64 (...) @fork()
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %16
6: ; preds = %1
%7 = load i8*, i8** %0
%8 = tail call i32 @execve(i8* %7, i8** %0, i32* null)
%9 = icmp eq i32 %8, -1
br i1 %9, label %16, label %10
10: ; preds = %6
%11 = call i32 @waitpid(i64 0, i32* %2, i32 0)
%12 = icmp eq i32 %11, -1
br i1 %12, label %16, label %13
13: ; preds = %10
%14 = load i32, i32* %2
%15 = call i32 @WEXITSTATUS(i32 %14)
br label %16
16: ; preds = %1, %10, %6, %13
%17 = phi i32 [ %15, %13 ], [ -1, %6 ], [ -1, %10 ], [ -1, %1 ]
ret i32 %17
}