#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*/ EBADF;
int /*<<< orphan*/ F_DUPFD;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int fcntl(int, int /*<<< orphan*/, ...);
int dup2(int oldfd, int newfd) {
  if (fcntl(oldfd, F_GETFL) == -1) {
    errno = EBADF;
    return -1;
  }
  if (oldfd == newfd)
    return oldfd;
  if (fcntl(newfd, F_GETFL) != -1)
    close(newfd);
  return fcntl(oldfd, F_DUPFD, newfd);
}


x86 assembly (clang, -O0):
<dup2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e98(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    1168 <dup2+0x48>
mov    0x2e77(%rip),%rax
mov    (%rax),%ecx
mov    0x2e7e(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11bc <dup2+0x9c>
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jne    117f <dup2+0x5f>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11bc <dup2+0x9c>
mov    -0xc(%rbp),%edi
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     11a3 <dup2+0x83>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%edi
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dup2>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %edi,%ebx
mov    0x2ea1(%rip),%r14
mov    (%r14),%esi
xor    %eax,%eax
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1172 <dup2+0x52>
cmp    %ebp,%ebx
je     1189 <dup2+0x69>
mov    (%r14),%esi
mov    %ebp,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     115a <dup2+0x3a>
mov    %ebp,%edi
call   1040 <close@plt>
mov    0x2e87(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %ebp,%edx
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <fcntl@plt>
mov    0x2e4f(%rip),%rax
mov    (%rax),%eax
mov    0x2e56(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@F_GETFL = global i32 0
@EBADF = global i32 0
@errno = global i32 0
@F_DUPFD = global i32 0
define i32 @dup2(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 = load i32, i32* @F_GETFL
%8 = call i32 (i32, i32, ...) @fcntl(i32 %6, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = load i32, i32* @EBADF
store i32 %11, i32* @errno
store i32 -1, i32* %3
br label %31
12: ; preds = %2
%13 = load i32, i32* %4
%14 = load i32, i32* %5
%15 = icmp eq i32 %13, %14
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = load i32, i32* %4
store i32 %17, i32* %3
br label %31
18: ; preds = %12
%19 = load i32, i32* %5
%20 = load i32, i32* @F_GETFL
%21 = call i32 (i32, i32, ...) @fcntl(i32 %19, i32 %20)
%22 = icmp ne i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = load i32, i32* %5
%25 = call i32 @close(i32 %24)
br label %26
26: ; preds = %23, %18
%27 = load i32, i32* %4
%28 = load i32, i32* @F_DUPFD
%29 = load i32, i32* %5
%30 = call i32 (i32, i32, ...) @fcntl(i32 %27, i32 %28, i32 %29)
store i32 %30, i32* %3
br label %31
31: ; preds = %26, %16, %10
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
@F_GETFL = global i32 0
@EBADF = global i32 0
@errno = global i32 0
@F_DUPFD = global i32 0
define i32 @dup2(i32 %0, i32 %1) {
%3 = load i32, i32* @F_GETFL
%4 = tail call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = load i32, i32* @EBADF
store i32 %7, i32* @errno
br label %19
8: ; preds = %2
%9 = icmp eq i32 %0, %1
br i1 %9, label %19, label %10
10: ; preds = %8
%11 = load i32, i32* @F_GETFL
%12 = tail call i32 (i32, i32, ...) @fcntl(i32 %1, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %16, label %14
14: ; preds = %10
%15 = tail call i32 @close(i32 %1)
br label %16
16: ; preds = %14, %10
%17 = load i32, i32* @F_DUPFD
%18 = tail call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %17, i32 %1)
br label %19
19: ; preds = %8, %16, %6
%20 = phi i32 [ -1, %6 ], [ %18, %16 ], [ %0, %8 ]
ret i32 %20
}