#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 */
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int WTERMSIG(int);
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int, int /*<<< orphan*/, char *, char const *const,
                         ...);
int /*<<< orphan*/ execve(char const *const, char *const *, char *const *);
int fork();
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
void run_argv(char *const argv[], char *const env[]) {
  const char *const exe = argv[0];
  pid_t pid = fork();
  if (pid == -1)
    error(1, errno, "can't fork %s", exe);
  if (pid == 0) {
    chdir("/tmp");
    execve(exe, argv, env);
    error(1, errno, "can't exec %s", exe);
  } else {
    int r = 0;
    waitpid(pid, &r, 0);
    if (WIFEXITED(r)) {
      int x = WEXITSTATUS(r);
      if (x != 0)
        error(1, 0, "%s exited with non-zero code %d", exe, x);
    } else if (WIFSIGNALED(r)) {
      int sig = WTERMSIG(r);
      error(1, 0, "%s caught signal %d and aborted", exe, sig);
    }
  }
}


x86 assembly (clang, -O0):
<run_argv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11df <run_argv+0x4f>
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rcx
mov    $0x1,%edi
lea    0xe28(%rip),%rdx
mov    $0x0,%al
call   1080 <error@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    122b <run_argv+0x9b>
lea    0xe1e(%rip),%rdi
call   1030 <chdir@plt>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1040 <execve@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rcx
mov    $0x1,%edi
lea    0xdf4(%rip),%rdx
mov    $0x0,%al
call   1080 <error@plt>
jmp    12cd <run_argv+0x13d>
movl   $0x0,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1090 <waitpid@plt>
mov    -0x20(%rbp),%edi
call   10a0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1289 <run_argv+0xf9>
mov    -0x20(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1284 <run_argv+0xf4>
mov    -0x18(%rbp),%rcx
mov    -0x24(%rbp),%r8d
mov    $0x1,%edi
xor    %esi,%esi
lea    0xda4(%rip),%rdx
mov    $0x0,%al
call   1080 <error@plt>
jmp    12c8 <run_argv+0x138>
mov    -0x20(%rbp),%edi
call   1050 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     12c3 <run_argv+0x133>
mov    -0x20(%rbp),%edi
call   1060 <WTERMSIG@plt>
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rcx
mov    -0x28(%rbp),%r8d
mov    $0x1,%edi
xor    %esi,%esi
lea    0xd85(%rip),%rdx
mov    $0x0,%al
call   1080 <error@plt>
jmp    12c8 <run_argv+0x138>
jmp    12cd <run_argv+0x13d>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_argv>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%rbx
mov    (%rdi),%r14
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     120b <run_argv+0x7b>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11d1 <run_argv+0x41>
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
lea    0xe3e(%rip),%rdx
mov    $0x1,%edi
mov    %r14,%rcx
xor    %eax,%eax
call   1080 <error@plt>
movl   $0x0,0x4(%rsp)
lea    0x4(%rsp),%rsi
mov    %ebp,%edi
xor    %edx,%edx
call   1090 <waitpid@plt>
mov    0x4(%rsp),%edi
call   10a0 <WIFEXITED@plt>
mov    0x4(%rsp),%edi
test   %rax,%rax
je     124e <run_argv+0xbe>
call   1070 <WEXITSTATUS@plt>
test   %eax,%eax
je     127c <run_argv+0xec>
lea    0xe18(%rip),%rdx
jmp    1268 <run_argv+0xd8>
lea    0xdfc(%rip),%rdi
call   1030 <chdir@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1040 <execve@plt>
mov    0x2db4(%rip),%rax
mov    (%rax),%esi
lea    0xdde(%rip),%rdx
mov    $0x1,%edi
mov    %r14,%rcx
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <error@plt>
call   1050 <WIFSIGNALED@plt>
test   %rax,%rax
je     127c <run_argv+0xec>
mov    0x4(%rsp),%edi
call   1060 <WTERMSIG@plt>
lea    0xdd9(%rip),%rdx
mov    $0x1,%edi
xor    %esi,%esi
mov    %r14,%rcx
mov    %eax,%r8d
xor    %eax,%eax
call   1080 <error@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@.str = [14 x i8] c"can't fork %s\00"
@.str.1 = [5 x i8] c"/tmp\00"
@.str.2 = [14 x i8] c"can't exec %s\00"
@.str.3 = [32 x i8] c"%s exited with non-zero code %d\00"
@.str.4 = [32 x i8] c"%s caught signal %d and aborted\00"
define void @run_argv(i8** %0, i8** %1) {
%3 = alloca i8**
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8** %0, i8*** %3
store i8** %1, i8*** %4
%10 = load i8**, i8*** %3
%11 = getelementptr inbounds i8*, i8** %10, i64 0
%12 = load i8*, i8** %11
store i8* %12, i8** %5
%13 = call i32 (...) @fork()
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = load i32, i32* @errno
%18 = load i8*, i8** %5
%19 = call i32 (i32, i32, i8*, i8*, ...) @error(i32 1, i32 %17, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %18)
br label %20
20: ; preds = %16, %2
%21 = load i32, i32* %6
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %32
23: ; preds = %20
%24 = call i32 @chdir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%25 = load i8*, i8** %5
%26 = load i8**, i8*** %3
%27 = load i8**, i8*** %4
%28 = call i32 @execve(i8* %25, i8** %26, i8** %27)
%29 = load i32, i32* @errno
%30 = load i8*, i8** %5
%31 = call i32 (i32, i32, i8*, i8*, ...) @error(i32 1, i32 %29, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %30)
br label %60
32: ; preds = %20
store i32 0, i32* %7
%33 = load i32, i32* %6
%34 = call i32 @waitpid(i32 %33, i32* %7, i32 0)
%35 = load i32, i32* %7
%36 = call i64 @WIFEXITED(i32 %35)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %48
38: ; preds = %32
%39 = load i32, i32* %7
%40 = call i32 @WEXITSTATUS(i32 %39)
store i32 %40, i32* %8
%41 = load i32, i32* %8
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %38
%44 = load i8*, i8** %5
%45 = load i32, i32* %8
%46 = call i32 (i32, i32, i8*, i8*, ...) @error(i32 1, i32 0, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %44, i32 %45)
br label %47
47: ; preds = %43, %38
br label %59
48: ; preds = %32
%49 = load i32, i32* %7
%50 = call i64 @WIFSIGNALED(i32 %49)
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %58
52: ; preds = %48
%53 = load i32, i32* %7
%54 = call i32 @WTERMSIG(i32 %53)
store i32 %54, i32* %9
%55 = load i8*, i8** %5
%56 = load i32, i32* %9
%57 = call i32 (i32, i32, i8*, i8*, ...) @error(i32 1, i32 0, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i8* %55, i32 %56)
br label %58
58: ; preds = %52, %48
br label %59
59: ; preds = %58, %47
br label %60
60: ; preds = %59, %23
ret void
}

LLVM IR (-O2):
@errno = global i32 0
@.str = [14 x i8] c"can't fork %s\00"
@.str.1 = [5 x i8] c"/tmp\00"
@.str.2 = [14 x i8] c"can't exec %s\00"
@.str.3 = [32 x i8] c"%s exited with non-zero code %d\00"
@.str.4 = [32 x i8] c"%s caught signal %d and aborted\00"
define void @run_argv(i8** %0, i8** %1) {
%3 = alloca i32
%4 = load i8*, i8** %0
%5 = tail call i32 (...) @fork()
switch i32 %5, label %14 [
i32 -1, label %6
i32 0, label %9
]
6: ; preds = %2
%7 = load i32, i32* @errno
%8 = tail call i32 (i32, i32, i8*, i8*, ...) @error(i32 1, i32 %7, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %4)
br label %14
9: ; preds = %2
%10 = tail call i32 @chdir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%11 = tail call i32 @execve(i8* %4, i8** %0, i8** %1)
%12 = load i32, i32* @errno
%13 = tail call i32 (i32, i32, i8*, i8*, ...) @error(i32 1, i32 %12, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %4)
br label %34
14: ; preds = %6, %2
%15 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%16 = call i32 @waitpid(i32 %5, i32* %3, i32 0)
%17 = load i32, i32* %3
%18 = call i64 @WIFEXITED(i32 %17)
%19 = icmp eq i64 %18, 0
%20 = load i32, i32* %3
br i1 %19, label %26, label %21
21: ; preds = %14
%22 = call i32 @WEXITSTATUS(i32 %20)
%23 = icmp eq i32 %22, 0
br i1 %23, label %33, label %24
24: ; preds = %21
%25 = call i32 (i32, i32, i8*, i8*, ...) @error(i32 1, i32 0, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %4, i32 %22)
br label %33
26: ; preds = %14
%27 = call i64 @WIFSIGNALED(i32 %20)
%28 = icmp eq i64 %27, 0
br i1 %28, label %33, label %29
29: ; preds = %26
%30 = load i32, i32* %3
%31 = call i32 @WTERMSIG(i32 %30)
%32 = call i32 (i32, i32, i8*, i8*, ...) @error(i32 1, i32 0, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i8* %4, i32 %31)
br label %33
33: ; preds = %21, %24, %26, %29
br label %34
34: ; preds = %33, %9
ret void
}