#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
char *basename(char const *);
int dup(int /*<<< orphan*/);
int dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(char *, ...);
int execv(char const *, char **);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char const *, char *);
char *strerror(int /*<<< orphan*/);
void run_program(const char *path, int output_fd, char *argv[]) {
  int ret;
  int err_fd;
  if ((err_fd = dup(STDERR_FILENO)) == -1) {
    error("Failed to save stderr.");
    goto exit;
  }
  if ((dup2(output_fd, STDOUT_FILENO) == -1) ||
      (dup2(output_fd, STDERR_FILENO) == -1)) {
    error("Failed to redirect %s program output.", path);
    goto exit;
  }
  argv[0] = basename(path);
  ret = execv(path, argv);
  if (ret != -1)
    goto exit;
  FILE *file = fdopen(err_fd, "w");
  if (file == NULL)
    goto exit;
  fprintf(file, "execv failed on %s, %s\n", path, strerror(errno));
  fclose(file);
exit:
  _exit(EXIT_FAILURE);
}


x86 assembly (clang, -O0):
<run_program>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
call   1060 <dup@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
jne    11e0 <run_program+0x40>
lea    0xe2c(%rip),%rdi
mov    $0x0,%al
call   10a0 <error@plt>
jmp    12be <run_program+0x11e>
mov    -0xc(%rbp),%edi
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
cmp    $0xffffffff,%eax
je     1214 <run_program+0x74>
mov    -0xc(%rbp),%edi
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
cmp    $0xffffffff,%eax
jne    122b <run_program+0x8b>
mov    -0x8(%rbp),%rsi
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   10a0 <error@plt>
jmp    12be <run_program+0x11e>
mov    -0x8(%rbp),%rdi
call   1080 <basename@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10c0 <execv@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
je     125d <run_program+0xbd>
jmp    12be <run_program+0x11e>
mov    -0x20(%rbp),%edi
lea    0xdd6(%rip),%rsi
call   1090 <fdopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1280 <run_program+0xe0>
jmp    12be <run_program+0x11e>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xd8f(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    0x2d03(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>

x86 assembly (clang, -O2):
<run_program>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r15
mov    %esi,%ebx
mov    %rdi,%r14
mov    0x2e19(%rip),%r12
mov    (%r12),%edi
call   1060 <dup@plt>
cmp    $0xffffffff,%eax
jne    11d5 <run_program+0x35>
lea    0xe34(%rip),%rdi
xor    %eax,%eax
call   10a0 <error@plt>
jmp    120d <run_program+0x6d>
mov    %eax,%ebp
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1050 <dup2@plt>
cmp    $0xffffffff,%eax
je     11fc <run_program+0x5c>
mov    (%r12),%esi
mov    %ebx,%edi
call   1050 <dup2@plt>
cmp    $0xffffffff,%eax
jne    121b <run_program+0x7b>
lea    0xe14(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   10a0 <error@plt>
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
mov    %r14,%rdi
call   1080 <basename@plt>
mov    %rax,(%r15)
mov    %r14,%rdi
mov    %r15,%rsi
call   10c0 <execv@plt>
cmp    $0xffffffff,%eax
jne    120d <run_program+0x6d>
lea    0xe00(%rip),%rsi
mov    %ebp,%edi
call   1090 <fdopen@plt>
test   %rax,%rax
je     120d <run_program+0x6d>
mov    %rax,%rbx
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdde(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
mov    %rax,%rcx
call   1070 <fprintf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    120d <run_program+0x6d>

LLVM IR (-O0):
@STDERR_FILENO = global i32 0
@.str = [23 x i8] c"Failed to save stderr.\00"
@STDOUT_FILENO = global i32 0
@.str.1 = [38 x i8] c"Failed to redirect %s program output.\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [24 x i8] c"execv failed on %s, %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
define void @run_program(i8* %0, i32 %1, i8** %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8** %2, i8*** %6
%10 = load i32, i32* @STDERR_FILENO
%11 = call i32 @dup(i32 %10)
store i32 %11, i32* %8
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %53
15: ; preds = %3
%16 = load i32, i32* %5
%17 = load i32, i32* @STDOUT_FILENO
%18 = call i32 @dup2(i32 %16, i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %25, label %20
20: ; preds = %15
%21 = load i32, i32* %5
%22 = load i32, i32* @STDERR_FILENO
%23 = call i32 @dup2(i32 %21, i32 %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %20, %15
%26 = load i8*, i8** %4
%27 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %26)
br label %53
28: ; preds = %20
%29 = load i8*, i8** %4
%30 = call i8* @basename(i8* %29)
%31 = load i8**, i8*** %6
%32 = getelementptr inbounds i8*, i8** %31, i64 0
store i8* %30, i8** %32
%33 = load i8*, i8** %4
%34 = load i8**, i8*** %6
%35 = call i32 @execv(i8* %33, i8** %34)
store i32 %35, i32* %7
%36 = load i32, i32* %7
%37 = icmp ne i32 %36, -1
br i1 %37, label %38, label %39
38: ; preds = %28
br label %53
39: ; preds = %28
%40 = load i32, i32* %8
%41 = call i32* @fdopen(i32 %40, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %41, i32** %9
%42 = load i32*, i32** %9
%43 = icmp eq i32* %42, null
br i1 %43, label %44, label %45
44: ; preds = %39
br label %53
45: ; preds = %39
%46 = load i32*, i32** %9
%47 = load i8*, i8** %4
%48 = load i32, i32* @errno
%49 = call i8* @strerror(i32 %48)
%50 = call i32 @fprintf(i32* %46, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %47, i8* %49)
%51 = load i32*, i32** %9
%52 = call i32 @fclose(i32* %51)
br label %53
53: ; preds = %45, %44, %38, %25, %13
%54 = load i32, i32* @EXIT_FAILURE
%55 = call i32 @_exit(i32 %54)
unreachable
}

LLVM IR (-O2):
@STDERR_FILENO = global i32 0
@.str = [23 x i8] c"Failed to save stderr.\00"
@STDOUT_FILENO = global i32 0
@.str.1 = [38 x i8] c"Failed to redirect %s program output.\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [24 x i8] c"execv failed on %s, %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
define void @run_program(i8* %0, i32 %1, i8** %2) {
%4 = load i32, i32* @STDERR_FILENO
%5 = tail call i32 @dup(i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = tail call i32 (i8*, ...) @error(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %31
9: ; preds = %3
%10 = load i32, i32* @STDOUT_FILENO
%11 = tail call i32 @dup2(i32 %1, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %17, label %13
13: ; preds = %9
%14 = load i32, i32* @STDERR_FILENO
%15 = tail call i32 @dup2(i32 %1, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %13, %9
%18 = tail call i32 (i8*, ...) @error(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %31
19: ; preds = %13
%20 = tail call i8* @basename(i8* %0)
store i8* %20, i8** %2
%21 = tail call i32 @execv(i8* %0, i8** %2)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %31
23: ; preds = %19
%24 = tail call i32* @fdopen(i32 %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%25 = icmp eq i32* %24, null
br i1 %25, label %31, label %26
26: ; preds = %23
%27 = load i32, i32* @errno
%28 = tail call i8* @strerror(i32 %27)
%29 = tail call i32 @fprintf(i32* %24, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %28)
%30 = tail call i32 @fclose(i32* %24)
br label %31
31: ; preds = %23, %19, %26, %17, %7
%32 = load i32, i32* @EXIT_FAILURE
%33 = tail call i32 @_exit(i32 %32)
unreachable
}