#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*/ F_GETFL;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int fcntl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int my_dup2(int old_fd, int new_fd) {
  if (fcntl(old_fd, F_GETFL) == -1) {
    fprintf(stderr, "%s %d\n", strerror(errno), old_fd);
    errno = EBADF;
    return -1;
  }
  if ((new_fd != old_fd)) {
    close(new_fd);
  }
  new_fd = fcntl(old_fd, F_DUPFD, new_fd);
  return new_fd;
}


x86 assembly (clang, -O0):
<my_dup2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%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>
cmp    $0xffffffff,%eax
jne    11b7 <my_dup2+0x77>
mov    0x2e67(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x10(%rbp),%edi
mov    %rax,%rdx
mov    -0x8(%rbp),%ecx
lea    0xe6c(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e20(%rip),%rax
mov    (%rax),%ecx
mov    0x2e27(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11ea <my_dup2+0xaa>
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
je     11cb <my_dup2+0x8b>
mov    -0xc(%rbp),%edi
call   1040 <close@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,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_dup2>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %edi,%ebx
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1180 <my_dup2+0x40>
cmp    %ebx,%ebp
je     1168 <my_dup2+0x28>
mov    %ebp,%edi
call   1040 <close@plt>
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %ebp,%edx
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <fcntl@plt>
mov    0x2e51(%rip),%rax
mov    (%rax),%ebp
mov    0x2e40(%rip),%r14
mov    (%r14),%edi
call   1060 <strerror@plt>
lea    0xe61(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
mov    %ebx,%ecx
call   1050 <fprintf@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%r14)
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@F_GETFL = global i32 0
@stderr = global i32 0
@.str = [7 x i8] c"%s %d\0A\00"
@errno = global i32 0
@EBADF = global i32 0
@F_DUPFD = global i32 0
define i32 @my_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* @F_GETFL
%8 = call i32 (i32, i32, ...) @fcntl(i32 %6, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %17
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = call i8* @strerror(i32 %12)
%14 = load i32, i32* %4
%15 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %13, i32 %14)
%16 = load i32, i32* @EBADF
store i32 %16, i32* @errno
store i32 -1, i32* %3
br label %30
17: ; preds = %2
%18 = load i32, i32* %5
%19 = load i32, i32* %4
%20 = icmp ne i32 %18, %19
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = load i32, i32* %5
%23 = call i32 @close(i32 %22)
br label %24
24: ; preds = %21, %17
%25 = load i32, i32* %4
%26 = load i32, i32* @F_DUPFD
%27 = load i32, i32* %5
%28 = call i32 (i32, i32, ...) @fcntl(i32 %25, i32 %26, i32 %27)
store i32 %28, i32* %5
%29 = load i32, i32* %5
store i32 %29, i32* %3
br label %30
30: ; preds = %24, %10
%31 = load i32, i32* %3
ret i32 %31
}

LLVM IR (-O2):
@F_GETFL = global i32 0
@stderr = global i32 0
@.str = [7 x i8] c"%s %d\0A\00"
@errno = global i32 0
@EBADF = global i32 0
@F_DUPFD = global i32 0
define i32 @my_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 %12
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i32, i32* @errno
%9 = tail call i8* @strerror(i32 %8)
%10 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %9, i32 %0)
%11 = load i32, i32* @EBADF
store i32 %11, i32* @errno
br label %19
12: ; preds = %2
%13 = icmp eq i32 %1, %0
br i1 %13, label %16, label %14
14: ; preds = %12
%15 = tail call i32 @close(i32 %1)
br label %16
16: ; preds = %14, %12
%17 = load i32, i32* @F_DUPFD
%18 = tail call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %17, i32 %1)
br label %19
19: ; preds = %16, %6
%20 = phi i32 [ -1, %6 ], [ %18, %16 ]
ret i32 %20
}