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


x86 assembly (clang, -O0):
<substitute>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <dup@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    116f <substitute+0x2f>
lea    0xe96(%rip),%rdi
call   1040 <syserr@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
jne    118c <substitute+0x4c>
lea    0xe86(%rip),%rdi
call   1040 <syserr@plt>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    11ac <substitute+0x6c>
lea    0xe7c(%rip),%rdi
call   1040 <syserr@plt>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
jne    11c9 <substitute+0x89>
lea    0xe6d(%rip),%rdi
call   1040 <syserr@plt>
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<substitute>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebx
mov    %edi,%ebp
mov    %esi,%edi
call   1050 <dup@plt>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
jne    1163 <substitute+0x23>
lea    0xea2(%rip),%rdi
call   1040 <syserr@plt>
mov    %ebx,%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
jne    117b <substitute+0x3b>
lea    0xe97(%rip),%rdi
call   1040 <syserr@plt>
mov    %ebp,%edi
mov    %ebx,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    1195 <substitute+0x55>
lea    0xe93(%rip),%rdi
call   1040 <syserr@plt>
mov    %ebp,%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
jne    11ad <substitute+0x6d>
lea    0xe89(%rip),%rdi
call   1040 <syserr@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"Error in dup\00"
@.str.1 = [22 x i8] c"Error in close target\00"
@.str.2 = [14 x i8] c"Error in dup2\00"
@.str.3 = [20 x i8] c"Error in close desc\00"
define i32 @substitute(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%6 = load i32, i32* %4
%7 = call i32 @dup(i32 %6)
store i32 %7, i32* %5
%8 = load i32, i32* %5
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @syserr(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %2
%13 = load i32, i32* %4
%14 = call i32 @close(i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = call i32 @syserr(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %16, %12
%19 = load i32, i32* %3
%20 = load i32, i32* %4
%21 = call i32 @dup2(i32 %19, i32 %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = call i32 @syserr(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %25
25: ; preds = %23, %18
%26 = load i32, i32* %3
%27 = call i32 @close(i32 %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = call i32 @syserr(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %31
31: ; preds = %29, %25
%32 = load i32, i32* %5
ret i32 %32
}

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