#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*/ close(int);
int dup(int);
int errno;
int dupfd(int fd, int desired_fd) {
  int duplicated_fd = dup(fd);
  if (duplicated_fd < 0 || duplicated_fd == desired_fd)
    return duplicated_fd;
  else {
    int r = dupfd(fd, desired_fd);
    int e = errno;
    close(duplicated_fd);
    errno = e;
    return r;
  }
}


x86 assembly (clang, -O0):
<dupfd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <dup@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jl     115f <dupfd+0x2f>
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jne    116a <dupfd+0x3a>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    119e <dupfd+0x6e>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1030 <dupfd@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e61(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%ecx
mov    0x2e4a(%rip),%rax
mov    %ecx,(%rax)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dupfd>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %edi,%ebp
call   1040 <dup@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1172 <dupfd+0x42>
cmp    %r14d,%ebx
je     1172 <dupfd+0x42>
mov    %ebp,%edi
mov    %r14d,%esi
call   1030 <dupfd@plt>
mov    %eax,%r15d
mov    0x2e80(%rip),%r14
mov    (%r14),%ebp
mov    %ebx,%edi
call   1050 <close@plt>
mov    %ebp,(%r14)
mov    %r15d,%eax
jmp    1174 <dupfd+0x44>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
define i32 @dupfd(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%9 = load i32, i32* %4
%10 = call i32 @dup(i32 %9)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %17, label %13
13: ; preds = %2
%14 = load i32, i32* %6
%15 = load i32, i32* %5
%16 = icmp eq i32 %14, %15
br i1 %16, label %17, label %19
17: ; preds = %13, %2
%18 = load i32, i32* %6
store i32 %18, i32* %3
br label %28
19: ; preds = %13
%20 = load i32, i32* %4
%21 = load i32, i32* %5
%22 = call i32 @dupfd(i32 %20, i32 %21)
store i32 %22, i32* %7
%23 = load i32, i32* @errno
store i32 %23, i32* %8
%24 = load i32, i32* %6
%25 = call i32 @close(i32 %24)
%26 = load i32, i32* %8
store i32 %26, i32* @errno
%27 = load i32, i32* %7
store i32 %27, i32* %3
br label %28
28: ; preds = %19, %17
%29 = load i32, i32* %3
ret i32 %29
}

LLVM IR (-O2):
@errno = global i32 0
define i32 @dupfd(i32 %0, i32 %1) {
%3 = tail call i32 @dup(i32 %0)
%4 = icmp slt i32 %3, 0
%5 = icmp eq i32 %3, %1
%6 = or i1 %4, %5
br i1 %6, label %7, label %9
7: ; preds = %2, %9
%8 = phi i32 [ %10, %9 ], [ %3, %2 ]
ret i32 %8
9: ; preds = %2
%10 = tail call i32 @dupfd(i32 %0, i32 %1)
%11 = load i32, i32* @errno
%12 = tail call i32 @close(i32 %3)
store i32 %11, i32* @errno
br label %7
}