#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 chk_dup(int, int);
int /*<<< orphan*/ close(int);
int do_dup2(int, int);
int /*<<< orphan*/ printf(char *);
int my_dup2(int src_fd, int new_fd) {
  int ret;
  if ((ret = chk_dup(src_fd, new_fd)) == -1)
    return -1;
  if (src_fd == new_fd)
    return src_fd;
  close(new_fd);
  if ((ret = do_dup2(src_fd, new_fd)) == -1) {
    printf("[my_dup2]: do dup failed!\n");
    return -1;
  } else
    return ret;
}


x86 assembly (clang, -O0):
<my_dup2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1030 <chk_dup@plt>
mov    %eax,-0x10(%rbp)
cmp    $0xffffffff,%eax
jne    1171 <my_dup2+0x31>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c5 <my_dup2+0x85>
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jne    1188 <my_dup2+0x48>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11c5 <my_dup2+0x85>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1050 <do_dup2@plt>
mov    %eax,-0x10(%rbp)
cmp    $0xffffffff,%eax
jne    11bf <my_dup2+0x7f>
lea    0xe52(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c5 <my_dup2+0x85>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_dup2>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %edi,%ebp
call   1030 <chk_dup@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    $0xffffffff,%ecx
je     1186 <my_dup2+0x46>
cmp    %ebx,%ebp
jne    1160 <my_dup2+0x20>
mov    %ebp,%eax
jmp    1186 <my_dup2+0x46>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %ebp,%edi
mov    %ebx,%esi
call   1050 <do_dup2@plt>
cmp    $0xffffffff,%eax
jne    1186 <my_dup2+0x46>
lea    0xe84(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
@str = [26 x i8] c"[my_dup2]: do dup failed!\00"
define i32 @my_dup2(i32 %0, i32 %1) {
%3 = tail call i32 @chk_dup(i32 %0, i32 %1)
%4 = icmp eq i32 %3, -1
br i1 %4, label %13, label %5
5: ; preds = %2
%6 = icmp eq i32 %0, %1
br i1 %6, label %13, label %7
7: ; preds = %5
%8 = tail call i32 @close(i32 %1)
%9 = tail call i32 @do_dup2(i32 %0, i32 %1)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
br label %13
13: ; preds = %7, %5, %2, %11
%14 = phi i32 [ -1, %11 ], [ -1, %2 ], [ %0, %5 ], [ %9, %7 ]
ret i32 %14
}