#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__ EBADF;
int /*<<< orphan*/ F_DUPFD;
int /*<<< orphan*/ F_GETFL;
int close(int);
scalar_t__ errno;
int fcntl(int, int /*<<< orphan*/, ...);
int dup2(int old, int new) {
  if (old == new) {
    return old;
  }
  if (fcntl(old, F_GETFL) == -1) {
    errno = EBADF;
    return -1;
  }
  if (close(new) == -1) {
    if (errno == EBADF) {
      errno = 0;
    } else {
      return -1;
    }
  }
  return fcntl(old, F_DUPFD, new);
}


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    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11e9 <dup2+0xc9>
mov    -0x8(%rbp),%edi
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    1181 <dup2+0x61>
mov    0x2e60(%rip),%rax
mov    (%rax),%rcx
mov    0x2e66(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11e9 <dup2+0xc9>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    11d0 <dup2+0xb0>
mov    0x2e3f(%rip),%rax
mov    (%rax),%rax
mov    0x2e25(%rip),%rcx
cmp    (%rcx),%rax
jne    11bf <dup2+0x9f>
mov    0x2e25(%rip),%rax
movq   $0x0,(%rax)
jmp    11cb <dup2+0xab>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e9 <dup2+0xc9>
jmp    11d0 <dup2+0xb0>
mov    -0x8(%rbp),%edi
mov    0x2e0e(%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   %rbx
push   %rax
mov    %edi,%ebp
cmp    %esi,%edi
jne    112d <dup2+0xd>
mov    %ebp,%eax
jmp    11a7 <dup2+0x87>
mov    %esi,%ebx
mov    0x2e9a(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     118e <dup2+0x6e>
mov    %ebx,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    1174 <dup2+0x54>
mov    0x2e7f(%rip),%rcx
mov    (%rcx),%rdx
mov    0x2e65(%rip),%rsi
mov    $0xffffffff,%eax
cmp    (%rsi),%rdx
jne    11a7 <dup2+0x87>
movq   $0x0,(%rcx)
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %ebx,%edx
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <fcntl@plt>
mov    0x2e33(%rip),%rax
mov    (%rax),%rax
mov    0x2e39(%rip),%rcx
mov    %rax,(%rcx)
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

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