#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*/ F_DUPFD;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ close(int);
scalar_t__ fcntl(int, int /*<<< orphan*/, ...);
int dup2(int fd1, int fd2) {
  if (fd1 != fd2) {
    if (fcntl(fd1, F_GETFL) < 0)
      return -1;
    if (fcntl(fd2, F_GETFL) >= 0)
      close(fd2);
    if (fcntl(fd1, F_DUPFD, fd2) < 0)
      return -1;
  }
  return fd2;
}


x86 assembly (clang, -O0):
<dup2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
je     11b9 <dup2+0x99>
mov    -0x8(%rbp),%edi
mov    0x2e94(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    1163 <dup2+0x43>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bf <dup2+0x9f>
mov    -0xc(%rbp),%edi
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jl     1188 <dup2+0x68>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%edi
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    11b4 <dup2+0x94>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bf <dup2+0x9f>
jmp    11b9 <dup2+0x99>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dup2>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebx
cmp    %esi,%edi
je     117c <dup2+0x5c>
mov    %edi,%ebp
mov    0x2ea2(%rip),%r15
mov    (%r15),%esi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
js     117f <dup2+0x5f>
mov    (%r15),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %rax,%rax
js     1163 <dup2+0x43>
mov    %ebx,%edi
call   1040 <close@plt>
mov    0x2e7e(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %ebx,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %rax,%rax
js     117f <dup2+0x5f>
mov    %ebx,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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