#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 close(int);
int /*<<< orphan*/ errno;
int fcntl(int, int /*<<< orphan*/, ...);
int _dup2(int oldfd, int newfd) {
  int flags;
  if (oldfd == newfd)
    return newfd;
  flags = fcntl(oldfd, F_GETFL);
  if (flags == -1 || newfd < 0) {
    errno = EBADF;
    return -1;
  }
  flags = fcntl(newfd, F_GETFL);
  if (flags != -1) {
    if (close(newfd) == -1)
      return -1;
  }
  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),%eax
cmp    -0xc(%rbp),%eax
jne    1145 <_dup2+0x25>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11e8 <_dup2+0xc8>
mov    -0x8(%rbp),%edi
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
je     116f <_dup2+0x4f>
cmpl   $0x0,-0xc(%rbp)
jge    118d <_dup2+0x6d>
mov    0x2e52(%rip),%rax
mov    (%rax),%ecx
mov    0x2e59(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11e8 <_dup2+0xc8>
mov    -0xc(%rbp),%edi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
je     11cf <_dup2+0xaf>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    11ca <_dup2+0xaa>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e8 <_dup2+0xc8>
jmp    11cf <_dup2+0xaf>
mov    -0x8(%rbp),%edi
mov    0x2e0f(%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    %edi,%ebx
cmp    %esi,%edi
je     1194 <_dup2+0x74>
mov    %esi,%ebp
mov    0x2e9d(%rip),%r14
mov    (%r14),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %ebp,%ebp
js     117d <_dup2+0x5d>
cmp    $0xffffffff,%eax
je     117d <_dup2+0x5d>
mov    (%r14),%esi
mov    %ebp,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1165 <_dup2+0x45>
mov    %ebp,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
je     118f <_dup2+0x6f>
mov    0x2e7c(%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    0x2e44(%rip),%rax
mov    (%rax),%eax
mov    0x2e4b(%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
%6 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%7 = load i32, i32* %4
%8 = load i32, i32* %5
%9 = icmp eq i32 %7, %8
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = load i32, i32* %5
store i32 %11, i32* %3
br label %40
12: ; preds = %2
%13 = load i32, i32* %4
%14 = load i32, i32* @F_GETFL
%15 = call i32 (i32, i32, ...) @fcntl(i32 %13, i32 %14)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp eq i32 %16, -1
br i1 %17, label %21, label %18
18: ; preds = %12
%19 = load i32, i32* %5
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %18, %12
%22 = load i32, i32* @EBADF
store i32 %22, i32* @errno
store i32 -1, i32* %3
br label %40
23: ; preds = %18
%24 = load i32, i32* %5
%25 = load i32, i32* @F_GETFL
%26 = call i32 (i32, i32, ...) @fcntl(i32 %24, i32 %25)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = icmp ne i32 %27, -1
br i1 %28, label %29, label %35
29: ; preds = %23
%30 = load i32, i32* %5
%31 = call i32 @close(i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %34
33: ; preds = %29
store i32 -1, i32* %3
br label %40
34: ; preds = %29
br label %35
35: ; preds = %34, %23
%36 = load i32, i32* %4
%37 = load i32, i32* @F_DUPFD
%38 = load i32, i32* %5
%39 = call i32 (i32, i32, ...) @fcntl(i32 %36, i32 %37, i32 %38)
store i32 %39, i32* %3
br label %40
40: ; preds = %35, %33, %21, %10
%41 = load i32, i32* %3
ret i32 %41
}

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 = icmp eq i32 %0, %1
br i1 %3, label %22, label %4
4: ; preds = %2
%5 = load i32, i32* @F_GETFL
%6 = tail call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %5)
%7 = icmp eq i32 %6, -1
%8 = icmp slt i32 %1, 0
%9 = or i1 %8, %7
br i1 %9, label %10, label %12
10: ; preds = %4
%11 = load i32, i32* @EBADF
store i32 %11, i32* @errno
br label %22
12: ; preds = %4
%13 = load i32, i32* @F_GETFL
%14 = tail call i32 (i32, i32, ...) @fcntl(i32 %1, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %19, label %16
16: ; preds = %12
%17 = tail call i32 @close(i32 %1)
%18 = icmp eq i32 %17, -1
br i1 %18, label %22, label %19
19: ; preds = %16, %12
%20 = load i32, i32* @F_DUPFD
%21 = tail call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %20, i32 %1)
br label %22
22: ; preds = %16, %2, %19, %10
%23 = phi i32 [ -1, %10 ], [ %21, %19 ], [ %0, %2 ], [ -1, %16 ]
ret i32 %23
}