#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 */
int /*<<< orphan*/ O_CLOEXEC;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int dup3(int, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int main(void) {
  int fd1, fd2, fd3, ret = 1;
  fd1 = open("/dev/zero", O_RDONLY);
  if (fd1 < 0)
    goto err0;
  fd2 = open("/dev/null", O_WRONLY);
  if (fd2 < 0)
    goto err1;
  fd3 = dup3(fd1, fd2, O_CLOEXEC);
  if (fd3 < 0)
    goto err2;
  fd2 = fd3;
  ret = 0;
err2:
  (void)close(fd2);
err1:
  (void)close(fd1);
err0:
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x14(%rbp)
mov    0x2e9b(%rip),%rax
mov    (%rax),%esi
lea    0xeaa(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    116d <main+0x3d>
jmp    11d7 <main+0xa7>
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
lea    0xe8d(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1194 <main+0x64>
jmp    11cf <main+0x9f>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    0x2e27(%rip),%rax
mov    (%rax),%edx
call   1030 <dup3@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11ba <main+0x8a>
jmp    11c7 <main+0x97>
mov    -0x10(%rbp),%eax
mov    %eax,-0xc(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    0x2ead(%rip),%rax
mov    (%rax),%esi
lea    0xebc(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
js     1191 <main+0x61>
mov    %eax,%r14d
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
lea    0xeaa(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
js     1198 <main+0x68>
mov    %eax,%ebx
mov    0x2e56(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
mov    %ebx,%esi
call   1030 <dup3@plt>
mov    %eax,%ebp
test   %eax,%eax
cmovns %eax,%ebx
shr    $0x1f,%ebp
mov    %ebx,%edi
call   1040 <close@plt>
jmp    119d <main+0x6d>
mov    $0x1,%ebp
jmp    11a5 <main+0x75>
mov    $0x1,%ebp
mov    %r14d,%edi
call   1040 <close@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/zero\00"
@O_RDONLY = global i32 0
@.str.1 = [10 x i8] c"/dev/null\00"
@O_WRONLY = global i32 0
@O_CLOEXEC = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
store i32 1, i32* %5
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %0
br label %33
11: ; preds = %0
%12 = load i32, i32* @O_WRONLY
%13 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %12)
store i32 %13, i32* %3
%14 = load i32, i32* %3
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %11
br label %30
17: ; preds = %11
%18 = load i32, i32* %2
%19 = load i32, i32* %3
%20 = load i32, i32* @O_CLOEXEC
%21 = call i32 @dup3(i32 %18, i32 %19, i32 %20)
store i32 %21, i32* %4
%22 = load i32, i32* %4
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %17
br label %27
25: ; preds = %17
%26 = load i32, i32* %4
store i32 %26, i32* %3
store i32 0, i32* %5
br label %27
27: ; preds = %25, %24
%28 = load i32, i32* %3
%29 = call i32 @close(i32 %28)
br label %30
30: ; preds = %27, %16
%31 = load i32, i32* %2
%32 = call i32 @close(i32 %31)
br label %33
33: ; preds = %30, %10
%34 = load i32, i32* %5
ret i32 %34
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/zero\00"
@O_RDONLY = global i32 0
@.str.1 = [10 x i8] c"/dev/null\00"
@O_WRONLY = global i32 0
@O_CLOEXEC = global i32 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 slt i32 %2, 0
br i1 %3, label %18, label %4
4: ; preds = %0
%5 = load i32, i32* @O_WRONLY
%6 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %15, label %8
8: ; preds = %4
%9 = load i32, i32* @O_CLOEXEC
%10 = tail call i32 @dup3(i32 %2, i32 %6, i32 %9)
%11 = icmp slt i32 %10, 0
%12 = select i1 %11, i32 %6, i32 %10
%13 = lshr i32 %10, 31
%14 = tail call i32 @close(i32 %12)
br label %15
15: ; preds = %4, %8
%16 = phi i32 [ 1, %4 ], [ %13, %8 ]
%17 = tail call i32 @close(i32 %2)
br label %18
18: ; preds = %0, %15
%19 = phi i32 [ 1, %0 ], [ %16, %15 ]
ret i32 %19
}