#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 close(int);
int dup2(int, int);
int /*<<< orphan*/ syserr(char *);
void replace(int desc, int target) {
  if (close(target) == -1) {
    syserr("Error in close target");
  }
  if (dup2(desc, target) == -1) {
    syserr("Error in dup2");
  }
  if (close(desc) == -1) {
    syserr("Error in close desc");
  }
}


x86 assembly (clang, -O0):
<replace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    115b <replace+0x2b>
lea    0xeaa(%rip),%rdi
call   1040 <syserr@plt>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    117b <replace+0x4b>
lea    0xea0(%rip),%rdi
call   1040 <syserr@plt>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    1198 <replace+0x68>
lea    0xe91(%rip),%rdi
call   1040 <syserr@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %edi,%ebx
mov    %esi,%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    114f <replace+0x1f>
lea    0xeb6(%rip),%rdi
call   1040 <syserr@plt>
mov    %ebx,%edi
mov    %ebp,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    1169 <replace+0x39>
lea    0xeb2(%rip),%rdi
call   1040 <syserr@plt>
mov    %ebx,%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
je     117c <replace+0x4c>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xea1(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <syserr@plt>

LLVM IR (-O0):
@.str = [22 x i8] c"Error in close target\00"
@.str.1 = [14 x i8] c"Error in dup2\00"
@.str.2 = [20 x i8] c"Error in close desc\00"
define void @replace(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 = call i32 @close(i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = call i32 @syserr(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %2
%11 = load i32, i32* %3
%12 = load i32, i32* %4
%13 = call i32 @dup2(i32 %11, i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = call i32 @syserr(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %15, %10
%18 = load i32, i32* %3
%19 = call i32 @close(i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 @syserr(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %23
23: ; preds = %21, %17
ret void
}

LLVM IR (-O2):
@.str = [22 x i8] c"Error in close target\00"
@.str.1 = [14 x i8] c"Error in dup2\00"
@.str.2 = [20 x i8] c"Error in close desc\00"
define void @replace(i32 %0, i32 %1) {
%3 = tail call i32 @close(i32 %1)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @syserr(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %5, %2
%8 = tail call i32 @dup2(i32 %0, i32 %1)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = tail call i32 @syserr(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %10, %7
%13 = tail call i32 @close(i32 %0)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = tail call i32 @syserr(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %17
17: ; preds = %15, %12
ret void
}