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

/* Variables and functions */
scalar_t__ EINVAL;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ abort();
int /*<<< orphan*/ close(int);
int dup2(int, int /*<<< orphan*/);
int dup3(int, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ errno;
int open(char *, int /*<<< orphan*/);
int main() {
  int fd = open("/dev/null", O_RDONLY);
  if (fd == -1)
    abort();
  if (dup2(fd, STDIN_FILENO) == -1)
    return 13;
  if (dup2(fd, STDOUT_FILENO) == -1)
    return 15;
  if (dup2(fd, STDERR_FILENO) == -1)
    return 17;
  if (dup3(fd, STDIN_FILENO, 0) != -1 || errno != EINVAL)
    return 20;
  if (dup3(fd, STDOUT_FILENO, 0) == -1)
    return 22;
  if (dup3(fd, STDERR_FILENO, 0) == -1)
    return 24;
  int fd2 = open("/dev/null", O_RDONLY);
  if (fd2 == -1)
    abort();
  if (dup2(fd2, STDIN_FILENO) == -1)
    return 31;
  if (dup2(fd2, STDOUT_FILENO) == -1)
    return 33;
  if (dup2(fd2, STDERR_FILENO) == -1)
    return 35;
  if (dup3(fd2, STDIN_FILENO, 0) == -1)
    return 38;
  if (dup3(fd2, STDOUT_FILENO, 0) == -1)
    return 40;
  if (dup3(fd2, STDERR_FILENO, 0) == -1)
    return 42;
  close(fd2);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
lea    0xe91(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1188 <main+0x38>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x8(%rbp),%edi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
cmp    $0xffffffff,%eax
jne    11ae <main+0x5e>
movl   $0xd,-0x4(%rbp)
jmp    13b6 <main+0x266>
mov    -0x8(%rbp),%edi
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
cmp    $0xffffffff,%eax
jne    11d4 <main+0x84>
movl   $0xf,-0x4(%rbp)
jmp    13b6 <main+0x266>
mov    -0x8(%rbp),%edi
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
cmp    $0xffffffff,%eax
jne    11fa <main+0xaa>
movl   $0x11,-0x4(%rbp)
jmp    13b6 <main+0x266>
mov    -0x8(%rbp),%edi
mov    0x2db4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <dup3@plt>
cmp    $0xffffffff,%eax
jne    1230 <main+0xe0>
mov    0x2db3(%rip),%rax
mov    (%rax),%rax
mov    0x2d99(%rip),%rcx
cmp    (%rcx),%rax
je     123c <main+0xec>
movl   $0x14,-0x4(%rbp)
jmp    13b6 <main+0x266>
mov    -0x8(%rbp),%edi
mov    0x2daa(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <dup3@plt>
cmp    $0xffffffff,%eax
jne    1264 <main+0x114>
movl   $0x16,-0x4(%rbp)
jmp    13b6 <main+0x266>
mov    -0x8(%rbp),%edi
mov    0x2d5a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <dup3@plt>
cmp    $0xffffffff,%eax
jne    128c <main+0x13c>
movl   $0x18,-0x4(%rbp)
jmp    13b6 <main+0x266>
mov    0x2d4d(%rip),%rax
mov    (%rax),%esi
lea    0xd64(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    12b5 <main+0x165>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0xc(%rbp),%edi
mov    0x2cf9(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
cmp    $0xffffffff,%eax
jne    12db <main+0x18b>
movl   $0x1f,-0x4(%rbp)
jmp    13b6 <main+0x266>
mov    -0xc(%rbp),%edi
mov    0x2d0b(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
cmp    $0xffffffff,%eax
jne    1301 <main+0x1b1>
movl   $0x21,-0x4(%rbp)
jmp    13b6 <main+0x266>
mov    -0xc(%rbp),%edi
mov    0x2cbd(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
cmp    $0xffffffff,%eax
jne    1327 <main+0x1d7>
movl   $0x23,-0x4(%rbp)
jmp    13b6 <main+0x266>
mov    -0xc(%rbp),%edi
mov    0x2c87(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <dup3@plt>
cmp    $0xffffffff,%eax
jne    134f <main+0x1ff>
movl   $0x26,-0x4(%rbp)
jmp    13b6 <main+0x266>
mov    -0xc(%rbp),%edi
mov    0x2c97(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <dup3@plt>
cmp    $0xffffffff,%eax
jne    1377 <main+0x227>
movl   $0x28,-0x4(%rbp)
jmp    13b6 <main+0x266>
mov    -0xc(%rbp),%edi
mov    0x2c47(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <dup3@plt>
cmp    $0xffffffff,%eax
jne    139f <main+0x24f>
movl   $0x2a,-0x4(%rbp)
jmp    13b6 <main+0x266>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e7e(%rip),%r14
mov    (%r14),%esi
lea    0xe94(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     132b <main+0x1db>
mov    %eax,%ebx
mov    0x2e35(%rip),%r13
mov    0x0(%r13),%esi
mov    %eax,%edi
call   1050 <dup2@plt>
cmp    $0xffffffff,%eax
je     12cf <main+0x17f>
mov    0x2e52(%rip),%r12
mov    (%r12),%esi
mov    %ebx,%edi
call   1050 <dup2@plt>
cmp    $0xffffffff,%eax
je     12d6 <main+0x186>
mov    0x2e0f(%rip),%r15
mov    (%r15),%esi
mov    %ebx,%edi
call   1050 <dup2@plt>
cmp    $0xffffffff,%eax
je     12dd <main+0x18d>
mov    0x0(%r13),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <dup3@plt>
mov    $0x14,%ebp
cmp    $0xffffffff,%eax
jne    12e2 <main+0x192>
mov    0x2de2(%rip),%rax
mov    (%rax),%rax
mov    0x2dc8(%rip),%rcx
cmp    (%rcx),%rax
jne    12e2 <main+0x192>
mov    (%r12),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <dup3@plt>
cmp    $0xffffffff,%eax
je     12f3 <main+0x1a3>
mov    (%r15),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <dup3@plt>
cmp    $0xffffffff,%eax
je     12fa <main+0x1aa>
mov    (%r14),%esi
lea    0xdca(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     132b <main+0x1db>
mov    %eax,%r14d
mov    0x0(%r13),%esi
mov    %eax,%edi
call   1050 <dup2@plt>
cmp    $0xffffffff,%eax
je     1301 <main+0x1b1>
mov    (%r12),%esi
mov    %r14d,%edi
call   1050 <dup2@plt>
cmp    $0xffffffff,%eax
je     1308 <main+0x1b8>
mov    (%r15),%esi
mov    %r14d,%edi
call   1050 <dup2@plt>
cmp    $0xffffffff,%eax
je     130f <main+0x1bf>
mov    0x0(%r13),%esi
mov    %r14d,%edi
xor    %edx,%edx
call   1040 <dup3@plt>
cmp    $0xffffffff,%eax
je     1316 <main+0x1c6>
mov    (%r12),%esi
mov    %r14d,%edi
xor    %edx,%edx
call   1040 <dup3@plt>
cmp    $0xffffffff,%eax
je     131d <main+0x1cd>
mov    (%r15),%esi
xor    %ebp,%ebp
mov    %r14d,%edi
xor    %edx,%edx
call   1040 <dup3@plt>
cmp    $0xffffffff,%eax
je     1324 <main+0x1d4>
mov    %r14d,%edi
call   1060 <close@plt>
mov    %ebx,%edi
call   1060 <close@plt>
jmp    12e2 <main+0x192>
mov    $0xd,%ebp
jmp    12e2 <main+0x192>
mov    $0xf,%ebp
jmp    12e2 <main+0x192>
mov    $0x11,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x16,%ebp
jmp    12e2 <main+0x192>
mov    $0x18,%ebp
jmp    12e2 <main+0x192>
mov    $0x1f,%ebp
jmp    12e2 <main+0x192>
mov    $0x21,%ebp
jmp    12e2 <main+0x192>
mov    $0x23,%ebp
jmp    12e2 <main+0x192>
mov    $0x26,%ebp
jmp    12e2 <main+0x192>
mov    $0x28,%ebp
jmp    12e2 <main+0x192>
mov    $0x2a,%ebp
jmp    12e2 <main+0x192>
xor    %eax,%eax
call   1030 <abort@plt>

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

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/null\00"
@O_RDONLY = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@errno = global i64 0
@EINVAL = global i64 0
define i32 @main() {
%1 = load i32, i32* @O_RDONLY
%2 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp eq i32 %2, -1
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = tail call i32 (...) @abort()
unreachable
6: ; preds = %0
%7 = load i32, i32* @STDIN_FILENO
%8 = tail call i32 @dup2(i32 %2, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %67, label %10
10: ; preds = %6
%11 = load i32, i32* @STDOUT_FILENO
%12 = tail call i32 @dup2(i32 %2, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %67, label %14
14: ; preds = %10
%15 = load i32, i32* @STDERR_FILENO
%16 = tail call i32 @dup2(i32 %2, i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %67, label %18
18: ; preds = %14
%19 = load i32, i32* @STDIN_FILENO
%20 = tail call i32 @dup3(i32 %2, i32 %19, i32 0)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %67
22: ; preds = %18
%23 = load i64, i64* @errno
%24 = load i64, i64* @EINVAL
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %67
26: ; preds = %22
%27 = load i32, i32* @STDOUT_FILENO
%28 = tail call i32 @dup3(i32 %2, i32 %27, i32 0)
%29 = icmp eq i32 %28, -1
br i1 %29, label %67, label %30
30: ; preds = %26
%31 = load i32, i32* @STDERR_FILENO
%32 = tail call i32 @dup3(i32 %2, i32 %31, i32 0)
%33 = icmp eq i32 %32, -1
br i1 %33, label %67, label %34
34: ; preds = %30
%35 = load i32, i32* @O_RDONLY
%36 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %35)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = tail call i32 (...) @abort()
unreachable
40: ; preds = %34
%41 = load i32, i32* @STDIN_FILENO
%42 = tail call i32 @dup2(i32 %36, i32 %41)
%43 = icmp eq i32 %42, -1
br i1 %43, label %67, label %44
44: ; preds = %40
%45 = load i32, i32* @STDOUT_FILENO
%46 = tail call i32 @dup2(i32 %36, i32 %45)
%47 = icmp eq i32 %46, -1
br i1 %47, label %67, label %48
48: ; preds = %44
%49 = load i32, i32* @STDERR_FILENO
%50 = tail call i32 @dup2(i32 %36, i32 %49)
%51 = icmp eq i32 %50, -1
br i1 %51, label %67, label %52
52: ; preds = %48
%53 = load i32, i32* @STDIN_FILENO
%54 = tail call i32 @dup3(i32 %36, i32 %53, i32 0)
%55 = icmp eq i32 %54, -1
br i1 %55, label %67, label %56
56: ; preds = %52
%57 = load i32, i32* @STDOUT_FILENO
%58 = tail call i32 @dup3(i32 %36, i32 %57, i32 0)
%59 = icmp eq i32 %58, -1
br i1 %59, label %67, label %60
60: ; preds = %56
%61 = load i32, i32* @STDERR_FILENO
%62 = tail call i32 @dup3(i32 %36, i32 %61, i32 0)
%63 = icmp eq i32 %62, -1
br i1 %63, label %67, label %64
64: ; preds = %60
%65 = tail call i32 @close(i32 %36)
%66 = tail call i32 @close(i32 %2)
br label %67
67: ; preds = %64, %40, %44, %48, %52, %56, %60, %30, %26, %18, %22, %14, %10, %6
%68 = phi i32 [ 13, %6 ], [ 15, %10 ], [ 17, %14 ], [ 20, %22 ], [ 20, %18 ], [ 22, %26 ], [ 24, %30 ], [ 0, %64 ], [ 31, %40 ], [ 33, %44 ], [ 35, %48 ], [ 38, %52 ], [ 40, %56 ], [ 42, %60 ]
ret i32 %68
}