#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*/ O_RDWR;
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ err_sys(char *);
int mydup2(int, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int);
int main(int argc, char **argv) {
  int fd;
  if (argc != 3) {
    err_sys("Usage: ./mydup2 <file> <newfd>\n");
    return -1;
  }
  char *name = argv[1];
  fd = open((const char *)name, O_RDWR);
  if (fd < 0) {
    err_sys("open file error\n");
    return -1;
  }
  fd = mydup2(fd, atoi(argv[2]));
  printf("new fd is %d\n", fd);
  return fd;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     1188 <main+0x38>
lea    0xe89(%rip),%rdi
call   1030 <err_sys@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1200 <main+0xb0>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11cb <main+0x7b>
lea    0xe66(%rip),%rdi
call   1030 <err_sys@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1200 <main+0xb0>
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1060 <atoi@plt>
mov    -0x24(%rbp),%edi
mov    %eax,%esi
call   1070 <mydup2@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe3c(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
cmp    $0x3,%edi
jne    1197 <main+0x47>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
js     11a0 <main+0x50>
mov    %eax,%ebp
mov    0x10(%rbx),%rdi
call   1060 <atoi@plt>
mov    %ebp,%edi
mov    %eax,%esi
call   1070 <mydup2@plt>
mov    %eax,%ebx
lea    0xea3(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
jmp    11b1 <main+0x61>
lea    0xe62(%rip),%rdi
jmp    11a7 <main+0x57>
lea    0xe79(%rip),%rdi
call   1030 <err_sys@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [32 x i8] c"Usage: ./mydup2 <file> <newfd>\0A\00"
@O_RDWR = global i32 0
@.str.1 = [17 x i8] c"open file error\0A\00"
@.str.2 = [14 x i8] c"new fd is %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 3
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @err_sys(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %33
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
store i8* %15, i8** %7
%16 = load i8*, i8** %7
%17 = load i32, i32* @O_RDWR
%18 = call i32 @open(i8* %16, i32 %17)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %12
%22 = call i32 @err_sys(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %33
23: ; preds = %12
%24 = load i32, i32* %6
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 2
%27 = load i8*, i8** %26
%28 = call i32 @atoi(i8* %27)
%29 = call i32 @mydup2(i32 %24, i32 %28)
store i32 %29, i32* %6
%30 = load i32, i32* %6
%31 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %30)
%32 = load i32, i32* %6
store i32 %32, i32* %3
br label %33
33: ; preds = %23, %21, %10
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
@.str = [32 x i8] c"Usage: ./mydup2 <file> <newfd>\0A\00"
@O_RDWR = global i32 0
@.str.1 = [17 x i8] c"open file error\0A\00"
@.str.2 = [14 x i8] c"new fd is %d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 3
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = tail call i32 @err_sys(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %20
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = load i32, i32* @O_RDWR
%10 = tail call i32 @open(i8* %8, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %6
%13 = tail call i32 @err_sys(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %20
14: ; preds = %6
%15 = getelementptr inbounds i8*, i8** %1, i64 2
%16 = load i8*, i8** %15
%17 = tail call i32 @atoi(i8* %16)
%18 = tail call i32 @mydup2(i32 %10, i32 %17)
%19 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %18)
br label %20
20: ; preds = %12, %14, %4
%21 = phi i32 [ -1, %4 ], [ -1, %12 ], [ %18, %14 ]
ret i32 %21
}