#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 /*<<< orphan*/ errno;
int /*<<< orphan*/ execvp(char *const, char *const *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fatal(char *, char *const, ...);
scalar_t__ fork();
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
scalar_t__ wait(int *);
int execute(char *const args[]) {
  pid_t pid;
  int status;
  if ((pid = fork()) == 0) {
    execvp(args[0], args);
    exit(127);
  } else if (pid < 0) {
    fatal(("Could not execute %s: %s\n"), args[0], strerror(errno));
  }
  pid = wait(&status);
  if (pid < 0)
    fatal(("Error waiting for child process to terminate: %s\n"),
          strerror(errno));
  return WEXITSTATUS(status);
}


x86 assembly (clang, -O0):
<execute>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11ab <execute+0x3b>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <execvp@plt>
mov    $0x7f,%edi
call   1050 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jge    11e3 <execute+0x73>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x20(%rbp),%rsi
mov    %eax,%edx
lea    0xe24(%rip),%rdi
mov    $0x0,%al
call   1070 <fatal@plt>
jmp    11e8 <execute+0x78>
lea    -0x14(%rbp),%rdi
call   1080 <wait@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    121f <execute+0xaf>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
movslq %eax,%rsi
lea    0xe02(%rip),%rdi
mov    $0x0,%al
call   1070 <fatal@plt>
mov    -0x14(%rbp),%edi
call   1030 <WEXITSTATUS@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<execute>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
je     11e7 <execute+0x77>
jns    11aa <execute+0x3a>
mov    (%rbx),%rbx
mov    0x2e50(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe62(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <fatal@plt>
lea    0xc(%rsp),%rdi
call   1080 <wait@plt>
test   %rax,%rax
jns    11d8 <execute+0x68>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
movslq %eax,%rsi
lea    0xe49(%rip),%rdi
xor    %eax,%eax
call   1070 <fatal@plt>
mov    0xc(%rsp),%edi
call   1030 <WEXITSTATUS@plt>
add    $0x10,%rsp
pop    %rbx
ret
mov    (%rbx),%rdi
mov    %rbx,%rsi
call   1040 <execvp@plt>
mov    $0x7f,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [26 x i8] c"Could not execute %s: %s\0A\00"
@errno = global i32 0
@.str.1 = [50 x i8] c"Error waiting for child process to terminate: %s\0A\00"
define i32 @execute(i8** %0) {
%2 = alloca i8**
%3 = alloca i64
%4 = alloca i32
store i8** %0, i8*** %2
%5 = call i64 (...) @fork()
store i64 %5, i64* %3
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %14
7: ; preds = %1
%8 = load i8**, i8*** %2
%9 = getelementptr inbounds i8*, i8** %8, i64 0
%10 = load i8*, i8** %9
%11 = load i8**, i8*** %2
%12 = call i32 @execvp(i8* %10, i8** %11)
%13 = call i32 @exit(i32 127)
unreachable
14: ; preds = %1
%15 = load i64, i64* %3
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %24
17: ; preds = %14
%18 = load i8**, i8*** %2
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = load i32, i32* @errno
%22 = call i32 @strerror(i32 %21)
%23 = call i32 (i8*, i8*, ...) @fatal(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %20, i32 %22)
br label %24
24: ; preds = %17, %14
br label %25
25: ; preds = %24
%26 = call i64 @wait(i32* %4)
store i64 %26, i64* %3
%27 = load i64, i64* %3
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %35
29: ; preds = %25
%30 = load i32, i32* @errno
%31 = call i32 @strerror(i32 %30)
%32 = sext i32 %31 to i64
%33 = inttoptr i64 %32 to i8*
%34 = call i32 (i8*, i8*, ...) @fatal(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0), i8* %33)
br label %35
35: ; preds = %29, %25
%36 = load i32, i32* %4
%37 = call i32 @WEXITSTATUS(i32 %36)
ret i32 %37
}

LLVM IR (-O2):
@.str = [26 x i8] c"Could not execute %s: %s\0A\00"
@errno = global i32 0
@.str.1 = [50 x i8] c"Error waiting for child process to terminate: %s\0A\00"
define i32 @execute(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 %10
6: ; preds = %1
%7 = load i8*, i8** %0
%8 = tail call i32 @execvp(i8* %7, i8** %0)
%9 = tail call i32 @exit(i32 127)
unreachable
10: ; preds = %1
%11 = icmp slt i64 %4, 0
br i1 %11, label %12, label %17
12: ; preds = %10
%13 = load i8*, i8** %0
%14 = load i32, i32* @errno
%15 = tail call i32 @strerror(i32 %14)
%16 = tail call i32 (i8*, i8*, ...) @fatal(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %13, i32 %15)
br label %17
17: ; preds = %10, %12
%18 = call i64 @wait(i32* %2)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %17
%21 = load i32, i32* @errno
%22 = call i32 @strerror(i32 %21)
%23 = sext i32 %22 to i64
%24 = inttoptr i64 %23 to i8*
%25 = call i32 (i8*, i8*, ...) @fatal(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0), i8* %24)
br label %26
26: ; preds = %20, %17
%27 = load i32, i32* %2
%28 = call i32 @WEXITSTATUS(i32 %27)
ret i32 %28
}