#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*/ close(int);
int /*<<< orphan*/ errno;
int fcntl(int, int /*<<< orphan*/, int);
int dup2(int fildes, int fildes2) {
  int dup_fd;
  if (fildes2 < 0) {
    errno = EBADF;
    return -1;
  }
  if (fildes == fildes2) {
    return fildes2;
  }
  close(fildes2);
  dup_fd = fcntl(fildes, F_DUPFD, fildes2);
  if (dup_fd != fildes2) {
    if (dup_fd >= 0) {
      close(dup_fd);
    }
    errno = EBADF;
    return -1;
  }
  return dup_fd;
}


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    1156 <dup2+0x36>
mov    0x2e91(%rip),%rax
mov    (%rax),%ecx
mov    0x2e90(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11ce <dup2+0xae>
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jne    116d <dup2+0x4d>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ce <dup2+0xae>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%edi
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
call   1030 <fcntl@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
je     11c8 <dup2+0xa8>
cmpl   $0x0,-0x10(%rbp)
jl     11aa <dup2+0x8a>
mov    -0x10(%rbp),%edi
call   1040 <close@plt>
mov    0x2e1f(%rip),%rax
mov    (%rax),%ecx
mov    0x2e1e(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11ce <dup2+0xae>
mov    -0x10(%rbp),%eax
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
test   %esi,%esi
js     1157 <dup2+0x37>
mov    %esi,%ebx
mov    %edi,%ebp
cmp    %esi,%edi
je     116e <dup2+0x4e>
mov    %ebx,%edi
call   1040 <close@plt>
mov    0x2eab(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %ebx,%edx
call   1030 <fcntl@plt>
cmp    %ebx,%eax
je     116e <dup2+0x4e>
test   %eax,%eax
js     1157 <dup2+0x37>
mov    %eax,%edi
call   1040 <close@plt>
mov    0x2e72(%rip),%rax
mov    (%rax),%eax
mov    0x2e71(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
@EBADF = 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 %15, label %4
4: ; preds = %2
%5 = icmp eq i32 %0, %1
br i1 %5, label %17, label %6
6: ; preds = %4
%7 = tail call i32 @close(i32 %1)
%8 = load i32, i32* @F_DUPFD
%9 = tail call i32 @fcntl(i32 %0, i32 %8, i32 %1)
%10 = icmp eq i32 %9, %1
br i1 %10, label %17, label %11
11: ; preds = %6
%12 = icmp sgt i32 %9, -1
br i1 %12, label %13, label %15
13: ; preds = %11
%14 = tail call i32 @close(i32 %9)
br label %15
15: ; preds = %11, %13, %2
%16 = load i32, i32* @EBADF
store i32 %16, i32* @errno
br label %17
17: ; preds = %15, %6, %4
%18 = phi i32 [ %1, %4 ], [ %1, %6 ], [ -1, %15 ]
ret i32 %18
}