#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 O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_RDWR;
int O_TRUNC;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execvp(char *const, char *const *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fork();
int open(char const *, int, ...);
int waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
int runio(char *const *argv, const char *infile, const char *outfile,
          const char *errfile) {
  int fd;
  pid_t pid;
  int status;
  switch ((pid = fork())) {
  case -1:
    return -1;
  case 0:
    if (infile) {
      (void)close(0);
      if ((fd = open(infile, O_RDONLY)) == -1)
        exit(1);
      if (fd != 0)
        (void)dup2(fd, 0);
    }
    if (outfile) {
      (void)close(1);
      if ((fd = open(outfile, O_RDWR | O_CREAT | O_TRUNC, 0666)) == -1)
        exit(1);
      if (fd != 1)
        (void)dup2(fd, 1);
    }
    if (errfile) {
      (void)close(2);
      if ((fd = open(errfile, O_RDWR | O_CREAT | O_TRUNC, 0666)) == -1)
        exit(1);
      if (fd != 2)
        (void)dup2(fd, 2);
    }
    execvp(argv[0], argv);
    exit(1);
    return 0;
  default:
    if (waitpid(pid, &status, 0) == -1)
      return -1;
    return status;
  }
}


x86 assembly (clang, -O0):
<runio>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,-0x30(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x38(%rbp)
sub    $0xffffffff,%eax
je     11b5 <runio+0x45>
jmp    11a5 <runio+0x35>
mov    -0x38(%rbp),%eax
test   %eax,%eax
je     11c1 <runio+0x51>
jmp    1314 <runio+0x1a4>
movl   $0xffffffff,-0x4(%rbp)
jmp    133d <runio+0x1cd>
cmpq   $0x0,-0x18(%rbp)
je     1216 <runio+0xa6>
xor    %edi,%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%rdi
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
jne    11fd <runio+0x8d>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpl   $0x0,-0x2c(%rbp)
je     1211 <runio+0xa1>
mov    -0x2c(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
jmp    1216 <runio+0xa6>
cmpq   $0x0,-0x20(%rbp)
je     1288 <runio+0x118>
mov    $0x1,%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%rdi
mov    0x2da2(%rip),%rax
mov    (%rax),%esi
mov    0x2db9(%rip),%rax
or     (%rax),%esi
mov    0x2da8(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
jne    126c <runio+0xfc>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpl   $0x1,-0x2c(%rbp)
je     1283 <runio+0x113>
mov    -0x2c(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
jmp    1288 <runio+0x118>
cmpq   $0x0,-0x28(%rbp)
je     12fa <runio+0x18a>
mov    $0x2,%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%rdi
mov    0x2d30(%rip),%rax
mov    (%rax),%esi
mov    0x2d47(%rip),%rax
or     (%rax),%esi
mov    0x2d36(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
jne    12de <runio+0x16e>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpl   $0x2,-0x2c(%rbp)
je     12f5 <runio+0x185>
mov    -0x2c(%rbp),%edi
mov    $0x2,%esi
call   1030 <dup2@plt>
jmp    12fa <runio+0x18a>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <execvp@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x30(%rbp),%edi
lea    -0x34(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    1337 <runio+0x1c7>
movl   $0xffffffff,-0x4(%rbp)
jmp    133d <runio+0x1cd>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<runio>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r15
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %rdi,%r14
xor    %eax,%eax
call   1090 <fork@plt>
cmp    $0xffffffff,%eax
je     11ab <runio+0x3b>
test   %eax,%eax
je     11b7 <runio+0x47>
lea    0x4(%rsp),%rsi
mov    %eax,%edi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmp    $0xffffffff,%eax
je     11ab <runio+0x3b>
mov    0x4(%rsp),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
test   %rbx,%rbx
je     11ec <runio+0x7c>
xor    %edi,%edi
call   1040 <close@plt>
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1060 <open@plt>
test   %eax,%eax
je     11ec <runio+0x7c>
cmp    $0xffffffff,%eax
je     127e <runio+0x10e>
mov    %eax,%edi
xor    %esi,%esi
call   1030 <dup2@plt>
test   %r12,%r12
je     123b <runio+0xcb>
mov    $0x1,%edi
call   1040 <close@plt>
mov    0x2dd6(%rip),%rax
mov    0x2def(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2ddc(%rip),%rax
or     (%rax),%esi
mov    %r12,%rdi
mov    $0x1b6,%edx
xor    %eax,%eax
call   1060 <open@plt>
cmp    $0x1,%eax
je     123b <runio+0xcb>
cmp    $0xffffffff,%eax
je     127e <runio+0x10e>
mov    %eax,%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
test   %r15,%r15
je     1294 <runio+0x124>
mov    $0x2,%edi
call   1040 <close@plt>
mov    0x2d87(%rip),%rax
mov    0x2da0(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d8d(%rip),%rax
or     (%rax),%esi
mov    %r15,%rdi
mov    $0x1b6,%edx
xor    %eax,%eax
call   1060 <open@plt>
cmp    $0x2,%eax
je     1294 <runio+0x124>
cmp    $0xffffffff,%eax
jne    1288 <runio+0x118>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    %eax,%edi
mov    $0x2,%esi
call   1030 <dup2@plt>
mov    (%r14),%rdi
mov    %r14,%rsi
call   1070 <execvp@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
define i32 @runio(i8** %0, i8* %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i8** %0, i8*** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i8* %3, i8** %9
%13 = call i32 (...) @fork()
store i32 %13, i32* %11
switch i32 %13, label %84 [
i32 -1, label %14
i32 0, label %15
]
14: ; preds = %4
store i32 -1, i32* %5
br label %91
15: ; preds = %4
%16 = load i8*, i8** %7
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %33
18: ; preds = %15
%19 = call i32 @close(i32 0)
%20 = load i8*, i8** %7
%21 = load i32, i32* @O_RDONLY
%22 = call i32 (i8*, i32, ...) @open(i8* %20, i32 %21)
store i32 %22, i32* %10
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %18
%27 = load i32, i32* %10
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = load i32, i32* %10
%31 = call i32 @dup2(i32 %30, i32 0)
br label %32
32: ; preds = %29, %26
br label %33
33: ; preds = %32, %15
%34 = load i8*, i8** %8
%35 = icmp ne i8* %34, null
br i1 %35, label %36, label %55
36: ; preds = %33
%37 = call i32 @close(i32 1)
%38 = load i8*, i8** %8
%39 = load i32, i32* @O_RDWR
%40 = load i32, i32* @O_CREAT
%41 = or i32 %39, %40
%42 = load i32, i32* @O_TRUNC
%43 = or i32 %41, %42
%44 = call i32 (i8*, i32, ...) @open(i8* %38, i32 %43, i32 438)
store i32 %44, i32* %10
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %48
46: ; preds = %36
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %36
%49 = load i32, i32* %10
%50 = icmp ne i32 %49, 1
br i1 %50, label %51, label %54
51: ; preds = %48
%52 = load i32, i32* %10
%53 = call i32 @dup2(i32 %52, i32 1)
br label %54
54: ; preds = %51, %48
br label %55
55: ; preds = %54, %33
%56 = load i8*, i8** %9
%57 = icmp ne i8* %56, null
br i1 %57, label %58, label %77
58: ; preds = %55
%59 = call i32 @close(i32 2)
%60 = load i8*, i8** %9
%61 = load i32, i32* @O_RDWR
%62 = load i32, i32* @O_CREAT
%63 = or i32 %61, %62
%64 = load i32, i32* @O_TRUNC
%65 = or i32 %63, %64
%66 = call i32 (i8*, i32, ...) @open(i8* %60, i32 %65, i32 438)
store i32 %66, i32* %10
%67 = icmp eq i32 %66, -1
br i1 %67, label %68, label %70
68: ; preds = %58
%69 = call i32 @exit(i32 1)
unreachable
70: ; preds = %58
%71 = load i32, i32* %10
%72 = icmp ne i32 %71, 2
br i1 %72, label %73, label %76
73: ; preds = %70
%74 = load i32, i32* %10
%75 = call i32 @dup2(i32 %74, i32 2)
br label %76
76: ; preds = %73, %70
br label %77
77: ; preds = %76, %55
%78 = load i8**, i8*** %6
%79 = getelementptr inbounds i8*, i8** %78, i64 0
%80 = load i8*, i8** %79
%81 = load i8**, i8*** %6
%82 = call i32 @execvp(i8* %80, i8** %81)
%83 = call i32 @exit(i32 1)
unreachable
84: ; preds = %4
%85 = load i32, i32* %11
%86 = call i32 @waitpid(i32 %85, i32* %12, i32 0)
%87 = icmp eq i32 %86, -1
br i1 %87, label %88, label %89
88: ; preds = %84
store i32 -1, i32* %5
br label %91
89: ; preds = %84
%90 = load i32, i32* %12
store i32 %90, i32* %5
br label %91
91: ; preds = %89, %88, %14
%92 = load i32, i32* %5
ret i32 %92
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
define i32 @runio(i8** %0, i8* %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
%7 = tail call i32 (...) @fork()
switch i32 %7, label %50 [
i32 -1, label %55
i32 0, label %8
]
8: ; preds = %4
%9 = icmp eq i8* %1, null
br i1 %9, label %18, label %10
10: ; preds = %8
%11 = tail call i32 @close(i32 0)
%12 = load i32, i32* @O_RDONLY
%13 = tail call i32 (i8*, i32, ...) @open(i8* %1, i32 %12)
switch i32 %13, label %16 [
i32 -1, label %14
i32 0, label %18
]
14: ; preds = %10
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %10
%17 = tail call i32 @dup2(i32 %13, i32 0)
br label %18
18: ; preds = %10, %16, %8
%19 = icmp eq i8* %2, null
br i1 %19, label %32, label %20
20: ; preds = %18
%21 = tail call i32 @close(i32 1)
%22 = load i32, i32* @O_RDWR
%23 = load i32, i32* @O_CREAT
%24 = or i32 %23, %22
%25 = load i32, i32* @O_TRUNC
%26 = or i32 %24, %25
%27 = tail call i32 (i8*, i32, ...) @open(i8* %2, i32 %26, i32 438)
switch i32 %27, label %30 [
i32 -1, label %28
i32 1, label %32
]
28: ; preds = %20
%29 = tail call i32 @exit(i32 1)
unreachable
30: ; preds = %20
%31 = tail call i32 @dup2(i32 %27, i32 1)
br label %32
32: ; preds = %20, %30, %18
%33 = icmp eq i8* %3, null
br i1 %33, label %46, label %34
34: ; preds = %32
%35 = tail call i32 @close(i32 2)
%36 = load i32, i32* @O_RDWR
%37 = load i32, i32* @O_CREAT
%38 = or i32 %37, %36
%39 = load i32, i32* @O_TRUNC
%40 = or i32 %38, %39
%41 = tail call i32 (i8*, i32, ...) @open(i8* %3, i32 %40, i32 438)
switch i32 %41, label %44 [
i32 -1, label %42
i32 2, label %46
]
42: ; preds = %34
%43 = tail call i32 @exit(i32 1)
unreachable
44: ; preds = %34
%45 = tail call i32 @dup2(i32 %41, i32 2)
br label %46
46: ; preds = %34, %44, %32
%47 = load i8*, i8** %0
%48 = tail call i32 @execvp(i8* %47, i8** %0)
%49 = tail call i32 @exit(i32 1)
unreachable
50: ; preds = %4
%51 = call i32 @waitpid(i32 %7, i32* %5, i32 0)
%52 = icmp eq i32 %51, -1
%53 = load i32, i32* %5
%54 = select i1 %52, i32 -1, i32 %53
br label %55
55: ; preds = %50, %4
%56 = phi i32 [ %7, %4 ], [ %54, %50 ]
ret i32 %56
}