#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);
int fcntl(int, int /*<<< orphan*/, int);
int fd_copy(int to, int from) {
  if (to == from)
    return 0;
  if (fcntl(from, F_GETFL, 0) == -1)
    return -1;
  close(to);
  if (fcntl(from, F_DUPFD, to) == -1)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<fd_copy>:
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    1146 <fd_copy+0x26>
movl   $0x0,-0x4(%rbp)
jmp    11a6 <fd_copy+0x86>
mov    -0xc(%rbp),%edi
mov    0x2e88(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    116e <fd_copy+0x4e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a6 <fd_copy+0x86>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0xc(%rbp),%edi
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%edx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    119f <fd_copy+0x7f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a6 <fd_copy+0x86>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fd_copy>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
cmp    %esi,%edi
je     1170 <fd_copy+0x50>
mov    %esi,%ebp
mov    %edi,%ebx
mov    0x2ea4(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     116b <fd_copy+0x4b>
mov    %ebx,%edi
call   1040 <close@plt>
mov    0x2e96(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %ebx,%edx
call   1030 <fcntl@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
sete   %al
neg    %eax
jmp    1170 <fd_copy+0x50>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
@F_GETFL = global i32 0
@F_DUPFD = global i32 0
define i32 @fd_copy(i32 %0, i32 %1) {
%3 = icmp eq i32 %0, %1
br i1 %3, label %14, label %4
4: ; preds = %2
%5 = load i32, i32* @F_GETFL
%6 = tail call i32 @fcntl(i32 %1, i32 %5, i32 0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %14, label %8
8: ; preds = %4
%9 = tail call i32 @close(i32 %0)
%10 = load i32, i32* @F_DUPFD
%11 = tail call i32 @fcntl(i32 %1, i32 %10, i32 %0)
%12 = icmp eq i32 %11, -1
%13 = sext i1 %12 to i32
br label %14
14: ; preds = %8, %4, %2
%15 = phi i32 [ 0, %2 ], [ -1, %4 ], [ %13, %8 ]
ret i32 %15
}