#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*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int WEXITSTATUS(int);
int execv(char const *, char **);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ perror(char const *);
int /*<<< orphan*/ stderr;
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int execute(const char *filename, char **args) {
  pid_t pid = fork();
  if (pid >= 0) {
    if (pid == 0) {
      int exec_res = execv(filename, args);
      if (exec_res == -1) {
        perror(filename);
        exit(EXIT_FAILURE);
      } else {
        exit(EXIT_SUCCESS);
      }
    } else {
      int child_status;
      pid_t child_pid = waitpid(pid, &child_status, 0);
      if (child_pid == -1) {
        fprintf(stderr, "Could not retrieve child's status");
        return -1;
      }
      return WEXITSTATUS(child_status);
    }
  } else {
    fprintf(stderr, "Could not fork in order to execute %s", filename);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<execute>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jl     1231 <execute+0xc1>
cmpq   $0x0,-0x20(%rbp)
jne    11e0 <execute+0x70>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <execv@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11d2 <execute+0x62>
mov    -0x10(%rbp),%rdi
call   1060 <perror@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0xffffffffffffffff,-0x30(%rbp)
jne    1221 <execute+0xb1>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
lea    0xdf2(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1253 <execute+0xe3>
mov    -0x28(%rbp),%edi
call   1040 <WEXITSTATUS@plt>
mov    %eax,-0x4(%rbp)
jmp    1253 <execute+0xe3>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xddd(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<execute>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     11a8 <execute+0x38>
je     11e8 <execute+0x78>
lea    0x4(%rsp),%rsi
mov    %rax,%rdi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmp    $0xffffffffffffffff,%rax
je     11c4 <execute+0x54>
mov    0x4(%rsp),%edi
call   1040 <WEXITSTATUS@plt>
jmp    11e0 <execute+0x70>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
lea    0xe6a(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1030 <fprintf@plt>
jmp    11db <execute+0x6b>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
lea    0xe2c(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    %r14,%rdi
mov    %rbx,%rsi
call   1080 <execv@plt>
cmp    $0xffffffff,%eax
jne    120e <execute+0x9e>
mov    %r14,%rdi
call   1060 <perror@plt>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@stderr = global i32 0
@.str = [34 x i8] c"Could not retrieve child's status\00"
@.str.1 = [38 x i8] c"Could not fork in order to execute %s\00"
define i32 @execute(i8* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
store i8* %0, i8** %4
store i8** %1, i8*** %5
%10 = call i64 (...) @fork()
store i64 %10, i64* %6
%11 = load i64, i64* %6
%12 = icmp sge i64 %11, 0
br i1 %12, label %13, label %41
13: ; preds = %2
%14 = load i64, i64* %6
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %30
16: ; preds = %13
%17 = load i8*, i8** %4
%18 = load i8**, i8*** %5
%19 = call i32 @execv(i8* %17, i8** %18)
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %27
22: ; preds = %16
%23 = load i8*, i8** %4
%24 = call i32 @perror(i8* %23)
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %16
%28 = load i32, i32* @EXIT_SUCCESS
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %13
%31 = load i64, i64* %6
%32 = call i64 @waitpid(i64 %31, i32* %8, i32 0)
store i64 %32, i64* %9
%33 = load i64, i64* %9
%34 = icmp eq i64 %33, -1
br i1 %34, label %35, label %38
35: ; preds = %30
%36 = load i32, i32* @stderr
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %45
38: ; preds = %30
%39 = load i32, i32* %8
%40 = call i32 @WEXITSTATUS(i32 %39)
store i32 %40, i32* %3
br label %45
41: ; preds = %2
%42 = load i32, i32* @stderr
%43 = load i8*, i8** %4
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %43)
store i32 -1, i32* %3
br label %45
45: ; preds = %41, %38, %35
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@stderr = global i32 0
@.str = [34 x i8] c"Could not retrieve child's status\00"
@.str.1 = [38 x i8] c"Could not fork in order to execute %s\00"
define i32 @execute(i8* %0, i8** %1) {
%3 = alloca i32
%4 = tail call i64 (...) @fork()
%5 = icmp sgt i64 %4, -1
br i1 %5, label %6, label %30
6: ; preds = %2
%7 = icmp eq i64 %4, 0
br i1 %7, label %8, label %18
8: ; preds = %6
%9 = tail call i32 @execv(i8* %0, i8** %1)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %15
11: ; preds = %8
%12 = tail call i32 @perror(i8* %0)
%13 = load i32, i32* @EXIT_FAILURE
%14 = tail call i32 @exit(i32 %13)
unreachable
15: ; preds = %8
%16 = load i32, i32* @EXIT_SUCCESS
%17 = tail call i32 @exit(i32 %16)
unreachable
18: ; preds = %6
%19 = bitcast i32* %3 to i8*
%20 = call i64 @waitpid(i64 %4, i32* %3, i32 0)
%21 = icmp eq i64 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = load i32, i32* @stderr
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
br label %28
25: ; preds = %18
%26 = load i32, i32* %3
%27 = call i32 @WEXITSTATUS(i32 %26)
br label %28
28: ; preds = %25, %22
%29 = phi i32 [ -1, %22 ], [ %27, %25 ]
br label %33
30: ; preds = %2
%31 = load i32, i32* @stderr
%32 = tail call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %33
33: ; preds = %30, %28
%34 = phi i32 [ %29, %28 ], [ -1, %30 ]
ret i32 %34
}