#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*/ EXIT_FAILURE;
int /*<<< orphan*/ F_DUPFD;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fcntl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int);
int /*<<< orphan*/ safeClose(int);
int /*<<< orphan*/ stderr;
int _dup2(int oldfd, int newfd) {
  int nextfd;
  int flags = fcntl(oldfd, F_GETFL);
  if (flags == -1) {
    errno = EBADF;
    return -1;
  }
  if (oldfd == newfd) {
    return newfd;
  }
  flags = fcntl(newfd, F_GETFL);
  if (flags != -1) {
    safeClose(newfd);
  }
  nextfd = fcntl(oldfd, F_DUPFD, newfd);
  if (nextfd != newfd) {
    fprintf(stderr,
            "_dup2 is not atomic, so sometimes it fails. It just did. Expected "
            "fd %d, got %d\n",
            newfd, nextfd);
    exit(EXIT_FAILURE);
  }
  return newfd;
}


x86 assembly (clang, -O0):
<_dup2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    118c <_dup2+0x4c>
mov    0x2e43(%rip),%rax
mov    (%rax),%ecx
mov    0x2e52(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    121f <_dup2+0xdf>
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jne    11a3 <_dup2+0x63>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    121f <_dup2+0xdf>
mov    -0xc(%rbp),%edi
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
je     11cb <_dup2+0x8b>
mov    -0xc(%rbp),%edi
call   1040 <safeClose@plt>
mov    -0x8(%rbp),%edi
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
je     1219 <_dup2+0xd9>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
mov    -0x10(%rbp),%ecx
lea    0xdfa(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_dup2>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebx
mov    %edi,%ebp
mov    0x2e79(%rip),%r14
mov    (%r14),%esi
xor    %eax,%eax
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1196 <_dup2+0x56>
cmp    %ebx,%ebp
je     11ad <_dup2+0x6d>
mov    (%r14),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     117a <_dup2+0x3a>
mov    %ebx,%edi
call   1040 <safeClose@plt>
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %ebx,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
cmp    %ebx,%eax
jne    11b4 <_dup2+0x74>
mov    %ebx,%ebp
jmp    11ad <_dup2+0x6d>
mov    0x2e1b(%rip),%rax
mov    (%rax),%eax
mov    0x2e2a(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2e1d(%rip),%rcx
mov    (%rcx),%edi
lea    0xe3c(%rip),%rsi
mov    %ebx,%edx
mov    %eax,%ecx
call   1050 <fprintf@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@F_GETFL = global i32 0
@EBADF = global i32 0
@errno = global i32 0
@F_DUPFD = global i32 0
@stderr = global i32 0
@.str = [81 x i8] c"_dup2 is not atomic, so sometimes it fails. It just did. Expected fd %d, got %d\0A\00"
@EXIT_FAILURE = global i32 0
define i32 @_dup2(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = load i32, i32* %4
%9 = load i32, i32* @F_GETFL
%10 = call i32 (i32, i32, ...) @fcntl(i32 %8, i32 %9)
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = load i32, i32* @EBADF
store i32 %14, i32* @errno
store i32 -1, i32* %3
br label %47
15: ; preds = %2
%16 = load i32, i32* %4
%17 = load i32, i32* %5
%18 = icmp eq i32 %16, %17
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = load i32, i32* %5
store i32 %20, i32* %3
br label %47
21: ; preds = %15
%22 = load i32, i32* %5
%23 = load i32, i32* @F_GETFL
%24 = call i32 (i32, i32, ...) @fcntl(i32 %22, i32 %23)
store i32 %24, i32* %7
%25 = load i32, i32* %7
%26 = icmp ne i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %21
%28 = load i32, i32* %5
%29 = call i32 @safeClose(i32 %28)
br label %30
30: ; preds = %27, %21
%31 = load i32, i32* %4
%32 = load i32, i32* @F_DUPFD
%33 = load i32, i32* %5
%34 = call i32 (i32, i32, ...) @fcntl(i32 %31, i32 %32, i32 %33)
store i32 %34, i32* %6
%35 = load i32, i32* %6
%36 = load i32, i32* %5
%37 = icmp ne i32 %35, %36
br i1 %37, label %38, label %45
38: ; preds = %30
%39 = load i32, i32* @stderr
%40 = load i32, i32* %5
%41 = load i32, i32* %6
%42 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([81 x i8], [81 x i8]* @.str, i64 0, i64 0), i32 %40, i32 %41)
%43 = load i32, i32* @EXIT_FAILURE
%44 = call i32 @exit(i32 %43)
unreachable
45: ; preds = %30
%46 = load i32, i32* %5
store i32 %46, i32* %3
br label %47
47: ; preds = %45, %19, %13
%48 = load i32, i32* %3
ret i32 %48
}

LLVM IR (-O2):
@F_GETFL = global i32 0
@EBADF = global i32 0
@errno = global i32 0
@F_DUPFD = global i32 0
@stderr = global i32 0
@.str = [81 x i8] c"_dup2 is not atomic, so sometimes it fails. It just did. Expected fd %d, got %d\0A\00"
@EXIT_FAILURE = global i32 0
define i32 @_dup2(i32 %0, i32 %1) {
%3 = load i32, i32* @F_GETFL
%4 = tail call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = load i32, i32* @EBADF
store i32 %7, i32* @errno
br label %25
8: ; preds = %2
%9 = icmp eq i32 %0, %1
br i1 %9, label %25, label %10
10: ; preds = %8
%11 = load i32, i32* @F_GETFL
%12 = tail call i32 (i32, i32, ...) @fcntl(i32 %1, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %16, label %14
14: ; preds = %10
%15 = tail call i32 @safeClose(i32 %1)
br label %16
16: ; preds = %14, %10
%17 = load i32, i32* @F_DUPFD
%18 = tail call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %17, i32 %1)
%19 = icmp eq i32 %18, %1
br i1 %19, label %25, label %20
20: ; preds = %16
%21 = load i32, i32* @stderr
%22 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([81 x i8], [81 x i8]* @.str, i64 0, i64 0), i32 %1, i32 %18)
%23 = load i32, i32* @EXIT_FAILURE
%24 = tail call i32 @exit(i32 %23)
unreachable
25: ; preds = %16, %8, %6
%26 = phi i32 [ -1, %6 ], [ %0, %8 ], [ %1, %16 ]
ret i32 %26
}