#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 */
scalar_t__ close(int);
int dup2(int, int);
int /*<<< orphan*/ unix_fatal(char *);
void do_dup(int oldfd, int newfd) {
  if (oldfd != newfd) {
    if (dup2(oldfd, newfd) != newfd) {
      unix_fatal("dup2 error");
    }
    if (close(oldfd) < 0) {
      unix_fatal("close error");
    }
  }
}


x86 assembly (clang, -O0):
<do_dup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%eax
cmp    -0x8(%rbp),%eax
je     118d <do_dup+0x5d>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1040 <dup2@plt>
cmp    -0x8(%rbp),%eax
je     116a <do_dup+0x3a>
lea    0xe9b(%rip),%rdi
call   1030 <unix_fatal@plt>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
cmp    $0x0,%rax
jge    1188 <do_dup+0x58>
lea    0xe88(%rip),%rdi
call   1030 <unix_fatal@plt>
jmp    118d <do_dup+0x5d>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_dup>:
cmp    %esi,%edi
je     1163 <do_dup+0x33>
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %edi,%ebx
call   1040 <dup2@plt>
cmp    %ebp,%eax
je     1150 <do_dup+0x20>
lea    0xeb5(%rip),%rdi
call   1030 <unix_fatal@plt>
mov    %ebx,%edi
call   1050 <close@plt>
test   %rax,%rax
lea    0x8(%rsp),%rsp
pop    %rbx
pop    %rbp
js     1164 <do_dup+0x34>
ret
lea    0xea0(%rip),%rdi
jmp    1030 <unix_fatal@plt>

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

LLVM IR (-O2):
@.str = [11 x i8] c"dup2 error\00"
@.str.1 = [12 x i8] c"close error\00"
define void @do_dup(i32 %0, i32 %1) {
%3 = icmp eq i32 %0, %1
br i1 %3, label %14, label %4
4: ; preds = %2
%5 = tail call i32 @dup2(i32 %0, i32 %1)
%6 = icmp eq i32 %5, %1
br i1 %6, label %9, label %7
7: ; preds = %4
%8 = tail call i32 @unix_fatal(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %4
%10 = tail call i64 @close(i32 %0)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i32 @unix_fatal(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %9, %12, %2
ret void
}