#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 EBADF;
int /*<<< orphan*/ F_DUPFD;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ __set_errno(int);
int /*<<< orphan*/ close(int);
int errno;
int fcntl(int, int /*<<< orphan*/, ...);
int __dup2(int fd, int fd2) {
  int save;
  if (fd2 < 0) {
    __set_errno(EBADF);
    return -1;
  }
  if (fcntl(fd, F_GETFL) < 0)
    return -1;
  if (fd == fd2)
    return fd2;
  save = errno;
  (void)close(fd2);
  __set_errno(save);
  return fcntl(fd, F_DUPFD, fd2);
}


x86 assembly (clang, -O0):
<__dup2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1162 <__dup2+0x32>
mov    0x2e79(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d6 <__dup2+0xa6>
mov    -0x8(%rbp),%edi
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1040 <fcntl@plt>
cmp    $0x0,%eax
jge    118a <__dup2+0x5a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d6 <__dup2+0xa6>
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jne    11a1 <__dup2+0x71>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d6 <__dup2+0xa6>
mov    0x2e30(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%edi
call   1030 <__set_errno@plt>
mov    -0x8(%rbp),%edi
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
mov    $0x0,%al
call   1040 <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
test   %esi,%esi
js     1185 <__dup2+0x55>
mov    %esi,%ebx
mov    %edi,%ebp
mov    0x2e8d(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1040 <fcntl@plt>
test   %eax,%eax
js     1193 <__dup2+0x63>
cmp    %ebx,%ebp
je     1198 <__dup2+0x68>
mov    0x2e7d(%rip),%rax
mov    (%rax),%r14d
mov    %ebx,%edi
call   1050 <close@plt>
mov    %r14d,%edi
call   1030 <__set_errno@plt>
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %ebx,%edx
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <fcntl@plt>
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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