#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_RDONLY;
int /*<<< orphan*/ O_WRONLY;
int close(int);
int dup2(int, int);
int open(char *, int /*<<< orphan*/);
int aeternum_dup(int oldfd, int newfd) {
  if (oldfd == -1) {
    oldfd = open("/dev/null", newfd == 0 ? O_RDONLY : O_WRONLY);
  }
  if (close(newfd) == -1) {
    return -1;
  }
  if (dup2(oldfd, newfd) == -1) {
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<aeternum_dup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1181 <aeternum_dup+0x51>
cmpl   $0x0,-0xc(%rbp)
jne    1163 <aeternum_dup+0x33>
mov    0x2e8f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
jmp    116f <aeternum_dup+0x3f>
mov    0x2e66(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%esi
lea    0xe87(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    119e <aeternum_dup+0x6e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c5 <aeternum_dup+0x95>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    11be <aeternum_dup+0x8e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c5 <aeternum_dup+0x95>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<aeternum_dup>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %edi,%ebp
cmp    $0xffffffff,%edi
jne    1160 <aeternum_dup+0x30>
test   %ebx,%ebx
jne    1149 <aeternum_dup+0x19>
mov    0x2ea1(%rip),%rax
jmp    1150 <aeternum_dup+0x20>
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
lea    0xea7(%rip),%rdi
call   1050 <open@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
je     1183 <aeternum_dup+0x53>
mov    %ebp,%edi
mov    %ebx,%esi
call   1030 <dup2@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
sete   %al
neg    %eax
jmp    1188 <aeternum_dup+0x58>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/null\00"
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
define i32 @aeternum_dup(i32 %0, i32 %1) {
%3 = icmp eq i32 %0, -1
br i1 %3, label %4, label %10
4: ; preds = %2
%5 = icmp eq i32 %1, 0
%6 = load i32, i32* @O_RDONLY
%7 = load i32, i32* @O_WRONLY
%8 = select i1 %5, i32 %6, i32 %7
%9 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %8)
br label %10
10: ; preds = %4, %2
%11 = phi i32 [ %9, %4 ], [ %0, %2 ]
%12 = tail call i32 @close(i32 %1)
%13 = icmp eq i32 %12, -1
br i1 %13, label %18, label %14
14: ; preds = %10
%15 = tail call i32 @dup2(i32 %11, i32 %1)
%16 = icmp eq i32 %15, -1
%17 = sext i1 %16 to i32
br label %18
18: ; preds = %14, %10
%19 = phi i32 [ -1, %10 ], [ %17, %14 ]
ret i32 %19
}