#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*/ argv;
typedef int /*<<< orphan*/ args;

/* Variables and functions */
int STDERR_FILENO;
int STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ close_all_fds(int, int);
int dup2(int, int);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int make_child_args(char const *, char *, int, char **, int);
int strlen(char *);
int write(int, char *, int);
int exec_image(const char *image, int infd, int outfd) {
  int sts;
  int argc;
  char *argv[128] = {NULL};
  char args[1024] = {'\0'};
  argc = make_child_args(image, args, sizeof(args), argv,
                         sizeof(argv) / sizeof(argv[0]));
  if (!argv[0] || argc <= 0) {
    return 105;
  }
  if (infd != STDIN_FILENO) {
    close(STDIN_FILENO);
    if (dup2(infd, STDIN_FILENO) != STDIN_FILENO) {
      exit(106);
    }
  }
  if (outfd != STDOUT_FILENO) {
    close(STDOUT_FILENO);
    if (dup2(outfd, STDOUT_FILENO) != STDOUT_FILENO) {
      exit(107);
    }
  }
  if (outfd != STDERR_FILENO) {
    close(STDERR_FILENO);
    if (dup2(outfd, STDERR_FILENO) != STDERR_FILENO) {
      exit(108);
    }
  }
  close_all_fds(STDERR_FILENO + 1, -1);
  execvp(argv[0], argv);
  sts = strlen("exec failed");
  if (write(STDERR_FILENO, "exec failed", sts) != sts) {
    exit(109);
  }
  return 110;
}


x86 assembly (clang, -O0):
<exec_image>:
push   %rbp
mov    %rsp,%rbp
sub    $0x820,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
lea    -0x820(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    -0x10(%rbp),%rdi
lea    -0x820(%rbp),%rsi
lea    -0x420(%rbp),%rcx
mov    $0x400,%edx
mov    $0x80,%r8d
call   1080 <make_child_args@plt>
mov    %eax,-0x20(%rbp)
cmpq   $0x0,-0x420(%rbp)
je     1208 <exec_image+0x78>
cmpl   $0x0,-0x20(%rbp)
jg     1214 <exec_image+0x84>
movl   $0x69,-0x4(%rbp)
jmp    136b <exec_image+0x1db>
mov    -0x14(%rbp),%eax
mov    0x2db2(%rip),%rcx
cmp    (%rcx),%eax
je     1263 <exec_image+0xd3>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
call   1070 <close@plt>
mov    -0x14(%rbp),%edi
mov    0x2d92(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    0x2d84(%rip),%rcx
cmp    (%rcx),%eax
je     125e <exec_image+0xce>
mov    $0x6a,%edi
call   10b0 <exit@plt>
jmp    1263 <exec_image+0xd3>
mov    -0x18(%rbp),%eax
mov    0x2d83(%rip),%rcx
cmp    (%rcx),%eax
je     12b2 <exec_image+0x122>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
call   1070 <close@plt>
mov    -0x18(%rbp),%edi
mov    0x2d63(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    0x2d55(%rip),%rcx
cmp    (%rcx),%eax
je     12ad <exec_image+0x11d>
mov    $0x6b,%edi
call   10b0 <exit@plt>
jmp    12b2 <exec_image+0x122>
mov    -0x18(%rbp),%eax
mov    0x2d1c(%rip),%rcx
cmp    (%rcx),%eax
je     1301 <exec_image+0x171>
mov    0x2d0d(%rip),%rax
mov    (%rax),%edi
call   1070 <close@plt>
mov    -0x18(%rbp),%edi
mov    0x2cfc(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    0x2cee(%rip),%rcx
cmp    (%rcx),%eax
je     12fc <exec_image+0x16c>
mov    $0x6c,%edi
call   10b0 <exit@plt>
jmp    1301 <exec_image+0x171>
mov    0x2cd0(%rip),%rax
mov    (%rax),%edi
add    $0x1,%edi
mov    $0xffffffff,%esi
call   1090 <close_all_fds@plt>
mov    -0x420(%rbp),%rdi
lea    -0x420(%rbp),%rsi
call   10a0 <execvp@plt>
lea    0xccf(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2c98(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
lea    0xcb4(%rip),%rsi
call   1030 <write@plt>
cmp    -0x1c(%rbp),%eax
je     1364 <exec_image+0x1d4>
mov    $0x6d,%edi
call   10b0 <exit@plt>
movl   $0x6e,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x820,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<exec_image>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x800,%rsp
mov    %edx,%r12d
mov    %esi,%r14d
mov    %rdi,%rbp
mov    %rsp,%r15
mov    $0x400,%edx
mov    %r15,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
lea    0x400(%rsp),%rbx
mov    $0x400,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    $0x400,%edx
mov    %r15,%rcx
mov    $0x80,%r8d
call   1080 <make_child_args@plt>
mov    %eax,%ecx
cmpq   $0x0,(%rsp)
mov    $0x69,%eax
je     12bb <exec_image+0x12b>
test   %ecx,%ecx
jle    12bb <exec_image+0x12b>
mov    0x2dc5(%rip),%rbx
mov    (%rbx),%edi
cmp    %r14d,%edi
je     1229 <exec_image+0x99>
call   1070 <close@plt>
mov    (%rbx),%esi
mov    %r14d,%edi
call   1050 <dup2@plt>
cmp    (%rbx),%eax
jne    12d5 <exec_image+0x145>
mov    0x2dc0(%rip),%rbx
mov    (%rbx),%edi
cmp    %r12d,%edi
je     124e <exec_image+0xbe>
call   1070 <close@plt>
mov    (%rbx),%esi
mov    %r12d,%edi
call   1050 <dup2@plt>
cmp    (%rbx),%eax
jne    12df <exec_image+0x14f>
mov    0x2d83(%rip),%r14
mov    (%r14),%edi
cmp    %r12d,%edi
je     1275 <exec_image+0xe5>
call   1070 <close@plt>
mov    (%r14),%esi
mov    %r12d,%edi
call   1050 <dup2@plt>
mov    %eax,%r12d
cmp    (%r14),%eax
jne    12e9 <exec_image+0x159>
add    $0x1,%r12d
mov    %r12d,%edi
mov    $0xffffffff,%esi
call   1090 <close_all_fds@plt>
mov    (%rsp),%rdi
mov    %rsp,%rsi
call   10a0 <execvp@plt>
lea    0xd67(%rip),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    (%r14),%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,%ecx
mov    $0x6e,%eax
cmp    %ebp,%ecx
jne    12cb <exec_image+0x13b>
add    $0x800,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x6d,%edi
call   10b0 <exit@plt>
mov    $0x6a,%edi
call   10b0 <exit@plt>
mov    $0x6b,%edi
call   10b0 <exit@plt>
mov    $0x6c,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str = [12 x i8] c"exec failed\00"
define i32 @exec_image(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [128 x i8*]
%11 = alloca [1024 x i8]
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%12 = bitcast [128 x i8*]* %10 to i8*
%13 = bitcast [1024 x i8]* %11 to i8*
%14 = load i8*, i8** %5
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%16 = getelementptr inbounds [128 x i8*], [128 x i8*]* %10, i64 0, i64 0
%17 = call i32 @make_child_args(i8* %14, i8* %15, i32 1024, i8** %16, i32 128)
store i32 %17, i32* %9
%18 = getelementptr inbounds [128 x i8*], [128 x i8*]* %10, i64 0, i64 0
%19 = load i8*, i8** %18
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %24
21: ; preds = %3
%22 = load i32, i32* %9
%23 = icmp sle i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %21, %3
store i32 105, i32* %4
br label %87
25: ; preds = %21
%26 = load i32, i32* %6
%27 = load i32, i32* @STDIN_FILENO
%28 = icmp ne i32 %26, %27
br i1 %28, label %29, label %40
29: ; preds = %25
%30 = load i32, i32* @STDIN_FILENO
%31 = call i32 @close(i32 %30)
%32 = load i32, i32* %6
%33 = load i32, i32* @STDIN_FILENO
%34 = call i32 @dup2(i32 %32, i32 %33)
%35 = load i32, i32* @STDIN_FILENO
%36 = icmp ne i32 %34, %35
br i1 %36, label %37, label %39
37: ; preds = %29
%38 = call i32 @exit(i32 106)
unreachable
39: ; preds = %29
br label %40
40: ; preds = %39, %25
%41 = load i32, i32* %7
%42 = load i32, i32* @STDOUT_FILENO
%43 = icmp ne i32 %41, %42
br i1 %43, label %44, label %55
44: ; preds = %40
%45 = load i32, i32* @STDOUT_FILENO
%46 = call i32 @close(i32 %45)
%47 = load i32, i32* %7
%48 = load i32, i32* @STDOUT_FILENO
%49 = call i32 @dup2(i32 %47, i32 %48)
%50 = load i32, i32* @STDOUT_FILENO
%51 = icmp ne i32 %49, %50
br i1 %51, label %52, label %54
52: ; preds = %44
%53 = call i32 @exit(i32 107)
unreachable
54: ; preds = %44
br label %55
55: ; preds = %54, %40
%56 = load i32, i32* %7
%57 = load i32, i32* @STDERR_FILENO
%58 = icmp ne i32 %56, %57
br i1 %58, label %59, label %70
59: ; preds = %55
%60 = load i32, i32* @STDERR_FILENO
%61 = call i32 @close(i32 %60)
%62 = load i32, i32* %7
%63 = load i32, i32* @STDERR_FILENO
%64 = call i32 @dup2(i32 %62, i32 %63)
%65 = load i32, i32* @STDERR_FILENO
%66 = icmp ne i32 %64, %65
br i1 %66, label %67, label %69
67: ; preds = %59
%68 = call i32 @exit(i32 108)
unreachable
69: ; preds = %59
br label %70
70: ; preds = %69, %55
%71 = load i32, i32* @STDERR_FILENO
%72 = add nsw i32 %71, 1
%73 = call i32 @close_all_fds(i32 %72, i32 -1)
%74 = getelementptr inbounds [128 x i8*], [128 x i8*]* %10, i64 0, i64 0
%75 = load i8*, i8** %74
%76 = getelementptr inbounds [128 x i8*], [128 x i8*]* %10, i64 0, i64 0
%77 = call i32 @execvp(i8* %75, i8** %76)
%78 = call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32 %78, i32* %8
%79 = load i32, i32* @STDERR_FILENO
%80 = load i32, i32* %8
%81 = call i32 @write(i32 %79, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %80)
%82 = load i32, i32* %8
%83 = icmp ne i32 %81, %82
br i1 %83, label %84, label %86
84: ; preds = %70
%85 = call i32 @exit(i32 109)
unreachable
86: ; preds = %70
store i32 110, i32* %4
br label %87
87: ; preds = %86, %24
%88 = load i32, i32* %4
ret i32 %88
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str = [12 x i8] c"exec failed\00"
define i32 @exec_image(i8* %0, i32 %1, i32 %2) {
%4 = alloca [128 x i8*]
%5 = alloca [1024 x i8]
%6 = bitcast [128 x i8*]* %4 to i8*
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%8 = getelementptr inbounds [128 x i8*], [128 x i8*]* %4, i64 0, i64 0
%9 = call i32 @make_child_args(i8* %0, i8* %7, i32 1024, i8** %8, i32 128)
%10 = load i8*, i8** %8
%11 = icmp eq i8* %10, null
%12 = icmp slt i32 %9, 1
%13 = select i1 %11, i1 true, i1 %12
br i1 %13, label %59, label %14
14: ; preds = %3
%15 = load i32, i32* @STDIN_FILENO
%16 = icmp eq i32 %15, %1
br i1 %16, label %25, label %17
17: ; preds = %14
%18 = call i32 @close(i32 %15)
%19 = load i32, i32* @STDIN_FILENO
%20 = call i32 @dup2(i32 %1, i32 %19)
%21 = load i32, i32* @STDIN_FILENO
%22 = icmp eq i32 %20, %21
br i1 %22, label %25, label %23
23: ; preds = %17
%24 = call i32 @exit(i32 106)
unreachable
25: ; preds = %17, %14
%26 = load i32, i32* @STDOUT_FILENO
%27 = icmp eq i32 %26, %2
br i1 %27, label %36, label %28
28: ; preds = %25
%29 = call i32 @close(i32 %26)
%30 = load i32, i32* @STDOUT_FILENO
%31 = call i32 @dup2(i32 %2, i32 %30)
%32 = load i32, i32* @STDOUT_FILENO
%33 = icmp eq i32 %31, %32
br i1 %33, label %36, label %34
34: ; preds = %28
%35 = call i32 @exit(i32 107)
unreachable
36: ; preds = %28, %25
%37 = load i32, i32* @STDERR_FILENO
%38 = icmp eq i32 %37, %2
br i1 %38, label %47, label %39
39: ; preds = %36
%40 = call i32 @close(i32 %37)
%41 = load i32, i32* @STDERR_FILENO
%42 = call i32 @dup2(i32 %2, i32 %41)
%43 = load i32, i32* @STDERR_FILENO
%44 = icmp eq i32 %42, %43
br i1 %44, label %47, label %45
45: ; preds = %39
%46 = call i32 @exit(i32 108)
unreachable
47: ; preds = %39, %36
%48 = phi i32 [ %42, %39 ], [ %2, %36 ]
%49 = add nsw i32 %48, 1
%50 = call i32 @close_all_fds(i32 %49, i32 -1)
%51 = load i8*, i8** %8
%52 = call i32 @execvp(i8* %51, i8** %8)
%53 = call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%54 = load i32, i32* @STDERR_FILENO
%55 = call i32 @write(i32 %54, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %53)
%56 = icmp eq i32 %55, %53
br i1 %56, label %59, label %57
57: ; preds = %47
%58 = call i32 @exit(i32 109)
unreachable
59: ; preds = %47, %3
%60 = phi i32 [ 105, %3 ], [ 110, %47 ]
ret i32 %60
}