#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*/ EXIT_FAILURE;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
scalar_t__ close(int);
int dup2(int, int);
int /*<<< orphan*/ perror(char *);
void copy_fd(int old, int new) {
  if (new != old) {
    if (dup2(old, new) == -1) {
      perror("dup2");
      _exit(EXIT_FAILURE);
    } else if (close(old)) {
      perror("close");
      _exit(EXIT_FAILURE);
    }
  }
}


x86 assembly (clang, -O0):
<copy_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x4(%rbp),%eax
je     11be <copy_fd+0x7e>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
jne    1188 <copy_fd+0x48>
lea    0xe8b(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e5f(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
cmp    $0x0,%rax
je     11b4 <copy_fd+0x74>
lea    0xe64(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
jmp    11b9 <copy_fd+0x79>
jmp    11be <copy_fd+0x7e>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_fd>:
push   %rbx
cmp    %edi,%esi
je     115d <copy_fd+0x1d>
mov    %edi,%ebx
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
je     115f <copy_fd+0x1f>
mov    %ebx,%edi
call   1050 <close@plt>
test   %rax,%rax
jne    1168 <copy_fd+0x28>
pop    %rbx
ret
lea    0xe9a(%rip),%rdi
jmp    116f <copy_fd+0x2f>
lea    0xe96(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"dup2\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [6 x i8] c"close\00"
define void @copy_fd(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%5 = load i32, i32* %4
%6 = load i32, i32* %3
%7 = icmp ne i32 %5, %6
br i1 %7, label %8, label %27
8: ; preds = %2
%9 = load i32, i32* %3
%10 = load i32, i32* %4
%11 = call i32 @dup2(i32 %9, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %17
13: ; preds = %8
%14 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @_exit(i32 %15)
unreachable
17: ; preds = %8
%18 = load i32, i32* %3
%19 = call i64 @close(i32 %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %17
%22 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @_exit(i32 %23)
unreachable
25: ; preds = %17
br label %26
26: ; preds = %25
br label %27
27: ; preds = %26, %2
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"dup2\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [6 x i8] c"close\00"
define void @copy_fd(i32 %0, i32 %1) {
%3 = icmp eq i32 %1, %0
br i1 %3, label %18, label %4
4: ; preds = %2
%5 = tail call i32 @dup2(i32 %0, i32 %1)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %11
7: ; preds = %4
%8 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @_exit(i32 %9)
unreachable
11: ; preds = %4
%12 = tail call i64 @close(i32 %0)
%13 = icmp eq i64 %12, 0
br i1 %13, label %18, label %14
14: ; preds = %11
%15 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = tail call i32 @_exit(i32 %16)
unreachable
18: ; preds = %11, %2
ret void
}