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

/* Variables and functions */
int /*<<< orphan*/ dup2(int const, int);
int /*<<< orphan*/ execvp(char *const, char *const *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fork();
int waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
int runiofd(char *const *argv, const int infile, const int outfile,
            const int errfile) {
  pid_t pid;
  int status;
  switch ((pid = fork())) {
  case -1:
    return -1;
  case 0:
    if (infile != 0)
      dup2(infile, 0);
    if (outfile != 1)
      dup2(outfile, 1);
    if (errfile != 2)
      dup2(errfile, 2);
    execvp(argv[0], argv);
    exit(1);
    return 0;
  default:
    if (waitpid(pid, &status, 0) == -1)
      return -1;
    return status;
  }
}


x86 assembly (clang, -O0):
<runiofd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
xor    %eax,%eax
call   1070 <fork@plt>
mov    %eax,-0x20(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x28(%rbp)
sub    $0xffffffff,%eax
je     1192 <runiofd+0x42>
jmp    1182 <runiofd+0x32>
mov    -0x28(%rbp),%eax
test   %eax,%eax
je     119e <runiofd+0x4e>
jmp    11fa <runiofd+0xaa>
movl   $0xffffffff,-0x4(%rbp)
jmp    1223 <runiofd+0xd3>
cmpl   $0x0,-0x14(%rbp)
je     11b2 <runiofd+0x62>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
cmpl   $0x1,-0x18(%rbp)
je     11c9 <runiofd+0x79>
mov    -0x18(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
cmpl   $0x2,-0x1c(%rbp)
je     11e0 <runiofd+0x90>
mov    -0x1c(%rbp),%edi
mov    $0x2,%esi
call   1030 <dup2@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <execvp@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x20(%rbp),%edi
lea    -0x24(%rbp),%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    121d <runiofd+0xcd>
movl   $0xffffffff,-0x4(%rbp)
jmp    1223 <runiofd+0xd3>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<runiofd>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r15d
mov    %edx,%ebx
mov    %esi,%ebp
mov    %rdi,%r14
xor    %eax,%eax
call   1070 <fork@plt>
cmp    $0xffffffff,%eax
je     1188 <runiofd+0x38>
test   %eax,%eax
je     1193 <runiofd+0x43>
lea    0x4(%rsp),%rsi
mov    %eax,%edi
xor    %edx,%edx
call   1040 <waitpid@plt>
cmp    $0xffffffff,%eax
je     1188 <runiofd+0x38>
mov    0x4(%rsp),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
test   %ebp,%ebp
jne    11c4 <runiofd+0x74>
cmp    $0x1,%ebx
jne    11d2 <runiofd+0x82>
cmp    $0x2,%r15d
je     11af <runiofd+0x5f>
mov    %r15d,%edi
mov    $0x2,%esi
call   1030 <dup2@plt>
mov    (%r14),%rdi
mov    %r14,%rsi
call   1050 <execvp@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    %ebp,%edi
xor    %esi,%esi
call   1030 <dup2@plt>
cmp    $0x1,%ebx
je     119c <runiofd+0x4c>
mov    %ebx,%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
cmp    $0x2,%r15d
jne    11a2 <runiofd+0x52>
jmp    11af <runiofd+0x5f>

LLVM IR (-O0):
define i32 @runiofd(i8** %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8** %0, i8*** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%12 = call i32 (...) @fork()
store i32 %12, i32* %10
switch i32 %12, label %39 [
i32 -1, label %13
i32 0, label %14
]
13: ; preds = %4
store i32 -1, i32* %5
br label %46
14: ; preds = %4
%15 = load i32, i32* %7
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i32, i32* %7
%19 = call i32 @dup2(i32 %18, i32 0)
br label %20
20: ; preds = %17, %14
%21 = load i32, i32* %8
%22 = icmp ne i32 %21, 1
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = load i32, i32* %8
%25 = call i32 @dup2(i32 %24, i32 1)
br label %26
26: ; preds = %23, %20
%27 = load i32, i32* %9
%28 = icmp ne i32 %27, 2
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = load i32, i32* %9
%31 = call i32 @dup2(i32 %30, i32 2)
br label %32
32: ; preds = %29, %26
%33 = load i8**, i8*** %6
%34 = getelementptr inbounds i8*, i8** %33, i64 0
%35 = load i8*, i8** %34
%36 = load i8**, i8*** %6
%37 = call i32 @execvp(i8* %35, i8** %36)
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %4
%40 = load i32, i32* %10
%41 = call i32 @waitpid(i32 %40, i32* %11, i32 0)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %44
43: ; preds = %39
store i32 -1, i32* %5
br label %46
44: ; preds = %39
%45 = load i32, i32* %11
store i32 %45, i32* %5
br label %46
46: ; preds = %44, %43, %13
%47 = load i32, i32* %5
ret i32 %47
}

LLVM IR (-O2):
define i32 @runiofd(i8** %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
%7 = tail call i32 (...) @fork()
switch i32 %7, label %24 [
i32 -1, label %29
i32 0, label %8
]
8: ; preds = %4
%9 = icmp eq i32 %1, 0
br i1 %9, label %12, label %10
10: ; preds = %8
%11 = tail call i32 @dup2(i32 %1, i32 0)
br label %12
12: ; preds = %10, %8
%13 = icmp eq i32 %2, 1
br i1 %13, label %16, label %14
14: ; preds = %12
%15 = tail call i32 @dup2(i32 %2, i32 1)
br label %16
16: ; preds = %14, %12
%17 = icmp eq i32 %3, 2
br i1 %17, label %20, label %18
18: ; preds = %16
%19 = tail call i32 @dup2(i32 %3, i32 2)
br label %20
20: ; preds = %18, %16
%21 = load i8*, i8** %0
%22 = tail call i32 @execvp(i8* %21, i8** %0)
%23 = tail call i32 @exit(i32 1)
unreachable
24: ; preds = %4
%25 = call i32 @waitpid(i32 %7, i32* %5, i32 0)
%26 = icmp eq i32 %25, -1
%27 = load i32, i32* %5
%28 = select i1 %26, i32 -1, i32 %27
br label %29
29: ; preds = %24, %4
%30 = phi i32 [ %7, %4 ], [ %28, %24 ]
ret i32 %30
}