#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 newdup2(int fd1, int fd2) {
  if (fd1 == fd2) {
    if (fcntl(fd1, F_GETFL) == -1) {
      return -1;
    }
    return fd1;
  }
  if (close(fd2) == -1 && errno != EBADF) {
    return -1;
  }
  return fcntl(fd1, F_DUPFD, fd2);
}


x86 assembly (clang, -O0):
<newdup2>:
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    116d <newdup2+0x4d>
mov    -0x8(%rbp),%edi
mov    0x2e8c(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    1162 <newdup2+0x42>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bd <newdup2+0x9d>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11bd <newdup2+0x9d>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    11a4 <newdup2+0x84>
mov    0x2e53(%rip),%rax
mov    (%rax),%rax
mov    0x2e39(%rip),%rcx
cmp    (%rcx),%rax
je     11a4 <newdup2+0x84>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bd <newdup2+0x9d>
mov    -0x8(%rbp),%edi
mov    0x2e3a(%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):
<newdup2>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
cmp    %esi,%edi
jne    114a <newdup2+0x2a>
mov    0x2ea0(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
cmove  %eax,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    %esi,%ebp
mov    %esi,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    1173 <newdup2+0x53>
mov    0x2e79(%rip),%rax
mov    (%rax),%rcx
mov    0x2e5f(%rip),%rdx
mov    $0xffffffff,%eax
cmp    (%rdx),%rcx
jne    1143 <newdup2+0x23>
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %ebp,%edx
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <fcntl@plt>

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

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