#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 /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ execve(char const *, char **, char *const *);
scalar_t__ fork();
int open_rd(char const *);
int open_wr(char const *);
int /*<<< orphan*/ t_error(char *, char const *);
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
char *xbasename(char const *);
int /*<<< orphan*/ xclose(int);
int /*<<< orphan*/ xdup2(int, int /*<<< orphan*/);
int t_exec(const char *path, char *argv[], const char *in, const char *out,
           const char *err) {
  int in_fd;
  int out_fd;
  int err_fd;
  pid_t pid;
  int status;
  char *const envp[1] = {NULL};
  in_fd = open_rd(in);
  out_fd = open_wr(out);
  err_fd = open_wr(err);
  pid = fork();
  if (pid == 0) {
    xdup2(in_fd, STDIN_FILENO);
    xdup2(out_fd, STDOUT_FILENO);
    xdup2(err_fd, STDERR_FILENO);
  }
  xclose(in_fd);
  xclose(out_fd);
  xclose(err_fd);
  if (pid == 0) {
    argv[0] = xbasename(path);
    (void)execve(path, argv, envp);
    _exit(66);
  }
  for (;;) {
    if (waitpid(pid, &status, 0) == pid) {
      if (WIFEXITED(status) || WIFSIGNALED(status)) {
        if (WIFEXITED(status) && WEXITSTATUS(status) == 66) {
          t_error("failed to execve %s", path);
        }
        return status;
      }
    }
  }
}


x86 assembly (clang, -O0):
<t_exec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1090 <memset@plt>
mov    -0x18(%rbp),%rdi
call   1080 <open_rd@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <open_wr@plt>
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <open_wr@plt>
mov    %eax,-0x34(%rbp)
mov    $0x0,%al
call   1100 <fork@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1279 <t_exec+0x99>
mov    -0x2c(%rbp),%edi
mov    0x2d80(%rip),%rax
mov    (%rax),%esi
call   1070 <xdup2@plt>
mov    -0x30(%rbp),%edi
mov    0x2d8f(%rip),%rax
mov    (%rax),%esi
call   1070 <xdup2@plt>
mov    -0x34(%rbp),%edi
mov    0x2d66(%rip),%rax
mov    (%rax),%esi
call   1070 <xdup2@plt>
mov    -0x2c(%rbp),%edi
call   1040 <xclose@plt>
mov    -0x30(%rbp),%edi
call   1040 <xclose@plt>
mov    -0x34(%rbp),%edi
call   1040 <xclose@plt>
cmpq   $0x0,-0x40(%rbp)
jne    12ca <t_exec+0xea>
mov    -0x8(%rbp),%rdi
call   1060 <xbasename@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x50(%rbp),%rdx
call   10a0 <execve@plt>
mov    $0x42,%edi
call   1030 <_exit@plt>
jmp    12cf <t_exec+0xef>
mov    -0x40(%rbp),%rdi
lea    -0x44(%rbp),%rsi
xor    %edx,%edx
call   10d0 <waitpid@plt>
cmp    -0x40(%rbp),%rax
jne    134d <t_exec+0x16d>
mov    -0x44(%rbp),%edi
call   10e0 <WIFEXITED@plt>
cmp    $0x0,%rax
jne    130c <t_exec+0x12c>
mov    -0x44(%rbp),%edi
call   10b0 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     1348 <t_exec+0x168>
mov    -0x44(%rbp),%edi
call   10e0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     133f <t_exec+0x15f>
mov    -0x44(%rbp),%edi
call   10c0 <WEXITSTATUS@plt>
cmp    $0x42,%eax
jne    133f <t_exec+0x15f>
mov    -0x8(%rbp),%rsi
lea    0xcc6(%rip),%rdi
call   10f0 <t_error@plt>
mov    -0x44(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret
jmp    134d <t_exec+0x16d>
jmp    12cf <t_exec+0xef>

x86 assembly (clang, -O2):
<t_exec>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8,%rbp
mov    %rcx,%rbx
mov    %rsi,%r15
mov    %rdi,0x8(%rsp)
movq   $0x0,0x10(%rsp)
mov    %rdx,%rdi
call   1080 <open_rd@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
call   1050 <open_wr@plt>
mov    %eax,%r13d
mov    %rbp,%rdi
call   1050 <open_wr@plt>
mov    %eax,%r12d
xor    %eax,%eax
call   10f0 <fork@plt>
test   %rax,%rax
je     12be <t_exec+0xee>
mov    %rax,%rbp
mov    %r14d,%edi
call   1040 <xclose@plt>
mov    %r13d,%edi
call   1040 <xclose@plt>
mov    %r12d,%edi
call   1040 <xclose@plt>
lea    0x4(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    %rbx,%rsi
xor    %edx,%edx
call   10c0 <waitpid@plt>
cmp    %rbp,%rax
jne    1250 <t_exec+0x80>
mov    0x4(%rsp),%edi
call   10d0 <WIFEXITED@plt>
test   %rax,%rax
jne    127e <t_exec+0xae>
mov    0x4(%rsp),%edi
call   10a0 <WIFSIGNALED@plt>
test   %rax,%rax
je     1250 <t_exec+0x80>
mov    0x4(%rsp),%edi
call   10d0 <WIFEXITED@plt>
test   %rax,%rax
je     12ab <t_exec+0xdb>
mov    0x4(%rsp),%edi
call   10b0 <WEXITSTATUS@plt>
cmp    $0x42,%eax
jne    12ab <t_exec+0xdb>
lea    0xd5f(%rip),%rdi
mov    0x8(%rsp),%rsi
call   10e0 <t_error@plt>
mov    0x4(%rsp),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d0b(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
call   1070 <xdup2@plt>
mov    0x2d1a(%rip),%rax
mov    (%rax),%esi
mov    %r13d,%edi
call   1070 <xdup2@plt>
mov    0x2cf1(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
call   1070 <xdup2@plt>
mov    %r14d,%edi
call   1040 <xclose@plt>
mov    %r13d,%edi
call   1040 <xclose@plt>
mov    %r12d,%edi
call   1040 <xclose@plt>
mov    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <xbasename@plt>
mov    %rax,(%r15)
lea    0x10(%rsp),%rdx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1090 <execve@plt>
mov    $0x42,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str = [20 x i8] c"failed to execve %s\00"
define i32 @t_exec(i8* %0, i8** %1, i8* %2, i8* %3, i8* %4) {
%6 = alloca i8*
%7 = alloca i8**
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i64
%15 = alloca i32
%16 = alloca [1 x i8*]
store i8* %0, i8** %6
store i8** %1, i8*** %7
store i8* %2, i8** %8
store i8* %3, i8** %9
store i8* %4, i8** %10
%17 = bitcast [1 x i8*]* %16 to i8*
%18 = load i8*, i8** %8
%19 = call i32 @open_rd(i8* %18)
store i32 %19, i32* %11
%20 = load i8*, i8** %9
%21 = call i32 @open_wr(i8* %20)
store i32 %21, i32* %12
%22 = load i8*, i8** %10
%23 = call i32 @open_wr(i8* %22)
store i32 %23, i32* %13
%24 = call i64 (...) @fork()
store i64 %24, i64* %14
%25 = load i64, i64* %14
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %37
27: ; preds = %5
%28 = load i32, i32* %11
%29 = load i32, i32* @STDIN_FILENO
%30 = call i32 @xdup2(i32 %28, i32 %29)
%31 = load i32, i32* %12
%32 = load i32, i32* @STDOUT_FILENO
%33 = call i32 @xdup2(i32 %31, i32 %32)
%34 = load i32, i32* %13
%35 = load i32, i32* @STDERR_FILENO
%36 = call i32 @xdup2(i32 %34, i32 %35)
br label %37
37: ; preds = %27, %5
%38 = load i32, i32* %11
%39 = call i32 @xclose(i32 %38)
%40 = load i32, i32* %12
%41 = call i32 @xclose(i32 %40)
%42 = load i32, i32* %13
%43 = call i32 @xclose(i32 %42)
%44 = load i64, i64* %14
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %56
46: ; preds = %37
%47 = load i8*, i8** %6
%48 = call i8* @xbasename(i8* %47)
%49 = load i8**, i8*** %7
%50 = getelementptr inbounds i8*, i8** %49, i64 0
store i8* %48, i8** %50
%51 = load i8*, i8** %6
%52 = load i8**, i8*** %7
%53 = getelementptr inbounds [1 x i8*], [1 x i8*]* %16, i64 0, i64 0
%54 = call i32 @execve(i8* %51, i8** %52, i8** %53)
%55 = call i32 @_exit(i32 66)
unreachable
56: ; preds = %37
br label %57
57: ; preds = %84, %56
%58 = load i64, i64* %14
%59 = call i64 @waitpid(i64 %58, i32* %15, i32 0)
%60 = load i64, i64* %14
%61 = icmp eq i64 %59, %60
br i1 %61, label %62, label %84
62: ; preds = %57
%63 = load i32, i32* %15
%64 = call i64 @WIFEXITED(i32 %63)
%65 = icmp ne i64 %64, 0
br i1 %65, label %70, label %66
66: ; preds = %62
%67 = load i32, i32* %15
%68 = call i64 @WIFSIGNALED(i32 %67)
%69 = icmp ne i64 %68, 0
br i1 %69, label %70, label %83
70: ; preds = %66, %62
%71 = load i32, i32* %15
%72 = call i64 @WIFEXITED(i32 %71)
%73 = icmp ne i64 %72, 0
br i1 %73, label %74, label %81
74: ; preds = %70
%75 = load i32, i32* %15
%76 = call i32 @WEXITSTATUS(i32 %75)
%77 = icmp eq i32 %76, 66
br i1 %77, label %78, label %81
78: ; preds = %74
%79 = load i8*, i8** %6
%80 = call i32 @t_error(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %79)
br label %81
81: ; preds = %78, %74, %70
%82 = load i32, i32* %15
ret i32 %82
83: ; preds = %66
br label %84
84: ; preds = %83, %57
br label %57
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str = [20 x i8] c"failed to execve %s\00"
define i32 @t_exec(i8* %0, i8** %1, i8* %2, i8* %3, i8* %4) {
%6 = alloca i32
%7 = alloca [1 x i8*]
%8 = bitcast i32* %6 to i8*
%9 = bitcast [1 x i8*]* %7 to i8*
%10 = bitcast [1 x i8*]* %7 to i64*
store i64 0, i64* %10
%11 = tail call i32 @open_rd(i8* %2)
%12 = tail call i32 @open_wr(i8* %3)
%13 = tail call i32 @open_wr(i8* %4)
%14 = tail call i64 (...) @fork()
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %30
16: ; preds = %5
%17 = load i32, i32* @STDIN_FILENO
%18 = tail call i32 @xdup2(i32 %11, i32 %17)
%19 = load i32, i32* @STDOUT_FILENO
%20 = tail call i32 @xdup2(i32 %12, i32 %19)
%21 = load i32, i32* @STDERR_FILENO
%22 = tail call i32 @xdup2(i32 %13, i32 %21)
%23 = tail call i32 @xclose(i32 %11)
%24 = tail call i32 @xclose(i32 %12)
%25 = tail call i32 @xclose(i32 %13)
%26 = tail call i8* @xbasename(i8* %0)
store i8* %26, i8** %1
%27 = getelementptr inbounds [1 x i8*], [1 x i8*]* %7, i64 0, i64 0
%28 = call i32 @execve(i8* %0, i8** %1, i8** %27)
%29 = call i32 @_exit(i32 66)
unreachable
30: ; preds = %5
%31 = tail call i32 @xclose(i32 %11)
%32 = tail call i32 @xclose(i32 %12)
%33 = tail call i32 @xclose(i32 %13)
br label %34
34: ; preds = %45, %30
%35 = call i64 @waitpid(i64 %14, i32* %6, i32 0)
%36 = icmp eq i64 %35, %14
br i1 %36, label %37, label %45
37: ; preds = %34
%38 = load i32, i32* %6
%39 = call i64 @WIFEXITED(i32 %38)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %46
41: ; preds = %37
%42 = load i32, i32* %6
%43 = call i64 @WIFSIGNALED(i32 %42)
%44 = icmp eq i64 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %41, %34
br label %34
46: ; preds = %41, %37
%47 = load i32, i32* %6
%48 = call i64 @WIFEXITED(i32 %47)
%49 = icmp eq i64 %48, 0
br i1 %49, label %56, label %50
50: ; preds = %46
%51 = load i32, i32* %6
%52 = call i32 @WEXITSTATUS(i32 %51)
%53 = icmp eq i32 %52, 66
br i1 %53, label %54, label %56
54: ; preds = %50
%55 = call i32 @t_error(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %56
56: ; preds = %54, %50, %46
%57 = load i32, i32* %6
ret i32 %57
}