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

/* Variables and functions */
scalar_t__ EXIT_SUCCESS;
scalar_t__ WEXITSTATUS(int);
int /*<<< orphan*/ WIFEXITED(int);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int waitpid(int, int *, int /*<<< orphan*/);
int run_exec(char *argv[]) {
  pid_t pid, p2;
  int st;
  pid = fork();
  if (pid == 0) {
    execvp(argv[0], argv);
    perror("run_exec: execvp failed");
    exit(-1);
  } else if (pid > 0) {
    do {
      p2 = waitpid(pid, &st, 0);
      if (p2 == -1) {
        perror("run_exec: waitpid failed");
        return -1;
      } else if (p2 != pid) {
        fprintf(stderr,
                "run_exec: waitpid unexpected pid: %d "
                "expected %d\n",
                p2, pid);
        return -1;
      }
    } while (p2 != pid);
    if (!WIFEXITED(st) || WEXITSTATUS(st) != EXIT_SUCCESS) {
      return -1;
    }
  } else {
    perror("run_exec: fork failed");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<run_exec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    11c6 <run_exec+0x46>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <execvp@plt>
lea    0xe49(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
jle    128b <run_exec+0x10b>
jmp    11d5 <run_exec+0x55>
mov    -0x14(%rbp),%edi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1208 <run_exec+0x88>
lea    0xe21(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12af <run_exec+0x12f>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     123b <run_exec+0xbb>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
mov    -0x14(%rbp),%ecx
lea    0xe07(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12af <run_exec+0x12f>
jmp    1240 <run_exec+0xc0>
jmp    1245 <run_exec+0xc5>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    11d5 <run_exec+0x55>
mov    -0x1c(%rbp),%edi
call   1080 <WIFEXITED@plt>
cmp    $0x0,%eax
je     127a <run_exec+0xfa>
mov    -0x1c(%rbp),%edi
call   1040 <WEXITSTATUS@plt>
mov    0x2d67(%rip),%rcx
cmp    (%rcx),%rax
je     1286 <run_exec+0x106>
movl   $0xffffffff,-0x4(%rbp)
jmp    12af <run_exec+0x12f>
jmp    12a3 <run_exec+0x123>
lea    0xdd1(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12af <run_exec+0x12f>
jmp    12a8 <run_exec+0x128>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_exec>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     121a <run_exec+0x9a>
jle    11f8 <run_exec+0x78>
mov    %eax,%ebx
lea    0x4(%rsp),%rsi
mov    %eax,%edi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmp    $0xffffffff,%eax
je     1201 <run_exec+0x81>
cmp    %ebx,%eax
jne    11dd <run_exec+0x5d>
mov    0x4(%rsp),%edi
call   1080 <WIFEXITED@plt>
test   %eax,%eax
je     120d <run_exec+0x8d>
mov    0x4(%rsp),%edi
call   1040 <WEXITSTATUS@plt>
mov    %rax,%rcx
mov    0x2e07(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%rcx
setne  %al
neg    %eax
jmp    1212 <run_exec+0x92>
mov    0x2dfc(%rip),%rcx
mov    (%rcx),%edi
lea    0xe44(%rip),%rsi
mov    %eax,%edx
mov    %ebx,%ecx
call   1030 <fprintf@plt>
jmp    120d <run_exec+0x8d>
lea    0xe64(%rip),%rdi
jmp    1208 <run_exec+0x88>
lea    0xe10(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    (%r14),%rdi
mov    %r14,%rsi
call   1070 <execvp@plt>
lea    0xdd4(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [24 x i8] c"run_exec: execvp failed\00"
@.str.1 = [25 x i8] c"run_exec: waitpid failed\00"
@stderr = global i32 0
@.str.2 = [50 x i8] c"run_exec: waitpid unexpected pid: %d expected %d\0A\00"
@EXIT_SUCCESS = global i64 0
@.str.3 = [22 x i8] c"run_exec: fork failed\00"
define i32 @run_exec(i8** %0) {
%2 = alloca i32
%3 = alloca i8**
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8** %0, i8*** %3
%7 = call i32 (...) @fork()
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %18
10: ; preds = %1
%11 = load i8**, i8*** %3
%12 = getelementptr inbounds i8*, i8** %11, i64 0
%13 = load i8*, i8** %12
%14 = load i8**, i8*** %3
%15 = call i32 @execvp(i8* %13, i8** %14)
%16 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 -1)
unreachable
18: ; preds = %1
%19 = load i32, i32* %4
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %55
21: ; preds = %18
br label %22
22: ; preds = %40, %21
%23 = load i32, i32* %4
%24 = call i32 @waitpid(i32 %23, i32* %6, i32 0)
store i32 %24, i32* %5
%25 = load i32, i32* %5
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %59
29: ; preds = %22
%30 = load i32, i32* %5
%31 = load i32, i32* %4
%32 = icmp ne i32 %30, %31
br i1 %32, label %33, label %38
33: ; preds = %29
%34 = load i32, i32* @stderr
%35 = load i32, i32* %5
%36 = load i32, i32* %4
%37 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i32 %35, i32 %36)
store i32 -1, i32* %2
br label %59
38: ; preds = %29
br label %39
39: ; preds = %38
br label %40
40: ; preds = %39
%41 = load i32, i32* %5
%42 = load i32, i32* %4
%43 = icmp ne i32 %41, %42
br i1 %43, label %22, label %44
44: ; preds = %40
%45 = load i32, i32* %6
%46 = call i32 @WIFEXITED(i32 %45)
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %53
48: ; preds = %44
%49 = load i32, i32* %6
%50 = call i64 @WEXITSTATUS(i32 %49)
%51 = load i64, i64* @EXIT_SUCCESS
%52 = icmp ne i64 %50, %51
br i1 %52, label %53, label %54
53: ; preds = %48, %44
store i32 -1, i32* %2
br label %59
54: ; preds = %48
br label %57
55: ; preds = %18
%56 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %2
br label %59
57: ; preds = %54
br label %58
58: ; preds = %57
store i32 0, i32* %2
br label %59
59: ; preds = %58, %55, %53, %33, %27
%60 = load i32, i32* %2
ret i32 %60
}

LLVM IR (-O2):
@.str = [24 x i8] c"run_exec: execvp failed\00"
@.str.1 = [25 x i8] c"run_exec: waitpid failed\00"
@stderr = global i32 0
@.str.2 = [50 x i8] c"run_exec: waitpid unexpected pid: %d expected %d\0A\00"
@EXIT_SUCCESS = global i64 0
@.str.3 = [22 x i8] c"run_exec: fork failed\00"
define i32 @run_exec(i8** %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = tail call i32 (...) @fork()
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %11
6: ; preds = %1
%7 = load i8*, i8** %0
%8 = tail call i32 @execvp(i8* %7, i8** %0)
%9 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 -1)
unreachable
11: ; preds = %1
%12 = icmp sgt i32 %4, 0
br i1 %12, label %13, label %33
13: ; preds = %11
%14 = call i32 @waitpid(i32 %4, i32* %2, i32 0)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %35
18: ; preds = %13
%19 = icmp eq i32 %14, %4
br i1 %19, label %23, label %20
20: ; preds = %18
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i32 %14, i32 %4)
br label %35
23: ; preds = %18
%24 = load i32, i32* %2
%25 = call i32 @WIFEXITED(i32 %24)
%26 = icmp eq i32 %25, 0
br i1 %26, label %35, label %27
27: ; preds = %23
%28 = load i32, i32* %2
%29 = call i64 @WEXITSTATUS(i32 %28)
%30 = load i64, i64* @EXIT_SUCCESS
%31 = icmp ne i64 %29, %30
%32 = sext i1 %31 to i32
br label %35
33: ; preds = %11
%34 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %35
35: ; preds = %27, %23, %33, %20, %16
%36 = phi i32 [ -1, %16 ], [ -1, %20 ], [ -1, %33 ], [ -1, %23 ], [ %32, %27 ]
ret i32 %36
}