#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*/ close(int);
int dup(int);
int getdtablesize();
int /*<<< orphan*/ printf(char *, ...);
int chk_dup(int src_fd, int new_fd) {
  int tbl_size = getdtablesize();
  printf("[my_dup2]: parameter : src fd %d\n", src_fd);
  printf("[my_dup2]: parameter : new fd %d\n\n", new_fd);
  if (src_fd < 0 || new_fd < 0) {
    printf("[my_dup2]: error: src or des parameter < 0.\n");
    return -1;
  } else if (new_fd >= tbl_size) {
    printf("[my_dup2]: error: des_fd out of system limit: %d\n", tbl_size);
    return -1;
  }
  int index;
  if ((index = dup(src_fd)) == -1) {
    printf("[my_dup2]: parameter src_fd is inactive!\n");
    return -1;
  } else
    close(index);
  return 0;
}


x86 assembly (clang, -O0):
<chk_dup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    $0x0,%al
call   1060 <getdtablesize@plt>
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe9e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xc(%rbp),%esi
lea    0xeaf(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x0,-0x8(%rbp)
jl     118e <chk_dup+0x4e>
cmpl   $0x0,-0xc(%rbp)
jge    11a8 <chk_dup+0x68>
lea    0xeb0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1213 <chk_dup+0xd3>
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jl     11d1 <chk_dup+0x91>
mov    -0x10(%rbp),%esi
lea    0xeb4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1213 <chk_dup+0xd3>
jmp    11d6 <chk_dup+0x96>
mov    -0x8(%rbp),%edi
call   1040 <dup@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1204 <chk_dup+0xc4>
lea    0xeb3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1213 <chk_dup+0xd3>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chk_dup>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %edi,%ebx
xor    %eax,%eax
call   1070 <getdtablesize@plt>
mov    %eax,%r14d
lea    0xe97(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xea9(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%eax
or     %ebx,%eax
js     11a4 <chk_dup+0x54>
cmp    %ebp,%r14d
jle    11ad <chk_dup+0x5d>
mov    %ebx,%edi
call   1050 <dup@plt>
cmp    $0xffffffff,%eax
je     11c0 <chk_dup+0x70>
mov    %eax,%edi
call   1060 <close@plt>
xor    %eax,%eax
jmp    11d1 <chk_dup+0x81>
lea    0xef5(%rip),%rdi
jmp    11c7 <chk_dup+0x77>
lea    0xe91(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11cc <chk_dup+0x7c>
lea    0xeb0(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [34 x i8] c"[my_dup2]: parameter : src fd %d\0A\00"
@.str.1 = [35 x i8] c"[my_dup2]: parameter : new fd %d\0A\0A\00"
@.str.2 = [45 x i8] c"[my_dup2]: error: src or des parameter < 0.\0A\00"
@.str.3 = [50 x i8] c"[my_dup2]: error: des_fd out of system limit: %d\0A\00"
@.str.4 = [42 x i8] c"[my_dup2]: parameter src_fd is inactive!\0A\00"
define i32 @chk_dup(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = call i32 (...) @getdtablesize()
store i32 %8, i32* %6
%9 = load i32, i32* %4
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = load i32, i32* %5
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %2
%16 = load i32, i32* %5
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %15, %2
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %38
20: ; preds = %15
%21 = load i32, i32* %5
%22 = load i32, i32* %6
%23 = icmp sge i32 %21, %22
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = load i32, i32* %6
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.3, i64 0, i64 0), i32 %25)
store i32 -1, i32* %3
br label %38
27: ; preds = %20
br label %28
28: ; preds = %27
%29 = load i32, i32* %4
%30 = call i32 @dup(i32 %29)
store i32 %30, i32* %7
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %38
34: ; preds = %28
%35 = load i32, i32* %7
%36 = call i32 @close(i32 %35)
br label %37
37: ; preds = %34
store i32 0, i32* %3
br label %38
38: ; preds = %37, %32, %24, %18
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
@.str = [34 x i8] c"[my_dup2]: parameter : src fd %d\0A\00"
@.str.1 = [35 x i8] c"[my_dup2]: parameter : new fd %d\0A\0A\00"
@.str.3 = [50 x i8] c"[my_dup2]: error: des_fd out of system limit: %d\0A\00"
@str = [41 x i8] c"[my_dup2]: parameter src_fd is inactive!\00"
@str.5 = [44 x i8] c"[my_dup2]: error: src or des parameter < 0.\00"
define i32 @chk_dup(i32 %0, i32 %1) {
%3 = tail call i32 (...) @getdtablesize()
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %1)
%6 = or i32 %1, %0
%7 = icmp sgt i32 %6, -1
br i1 %7, label %10, label %8
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str.5, i64 0, i64 0))
br label %21
10: ; preds = %2
%11 = icmp sgt i32 %3, %1
br i1 %11, label %14, label %12
12: ; preds = %10
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @.str.3, i64 0, i64 0), i32 %3)
br label %21
14: ; preds = %10
%15 = tail call i32 @dup(i32 %0)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str, i64 0, i64 0))
br label %21
19: ; preds = %14
%20 = tail call i32 @close(i32 %15)
br label %21
21: ; preds = %17, %19, %12, %8
%22 = phi i32 [ -1, %8 ], [ -1, %12 ], [ -1, %17 ], [ 0, %19 ]
ret i32 %22
}