#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 */
typedef int /*<<< orphan*/ off_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close_fd(int);
int dup_one(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ info(int);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char const *);
int /*<<< orphan*/ puts(char *);
int main(int argc, char **argv) {
  int oldfd, newfd;
  off_t ret, position;
  const char *file;
  if (argc != 2) {
    printf("Exercise 5-3: Implement dup(2) and dup2(2) using fcntl\nUsage: %s "
           "<file>\n",
           argv[0]);
    exit(EXIT_FAILURE);
  }
  file = argv[1];
  if ((oldfd = open(file, O_RDONLY)) == -1) {
    perror("open");
    exit(EXIT_FAILURE);
  }
  printf("Original file descriptor (%s):\n", file);
  info(oldfd);
  newfd = dup_one(oldfd);
  puts("Duplication of original file descriptor:");
  info(newfd);
  close_fd(oldfd);
  close_fd(newfd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x4(%rbp)
je     11ba <main+0x3a>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe59(%rip),%rdi
call   1050 <printf@plt>
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11fe <main+0x7e>
lea    0xe5e(%rip),%rdi
call   1080 <perror@plt>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x28(%rbp),%rsi
lea    0xe45(%rip),%rdi
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
call   10a0 <info@plt>
mov    -0x14(%rbp),%edi
call   1060 <dup_one@plt>
mov    %eax,-0x18(%rbp)
lea    0xe46(%rip),%rdi
call   1030 <puts@plt>
mov    -0x18(%rbp),%edi
call   10a0 <info@plt>
mov    -0x14(%rbp),%edi
call   1040 <close_fd@plt>
mov    -0x18(%rbp),%edi
call   1040 <close_fd@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x2,%edi
jne    11ec <main+0x6c>
mov    0x8(%rsi),%r14
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11fd <main+0x7d>
mov    %eax,%ebx
lea    0xea2(%rip),%rdi
mov    %r14,%rsi
call   1050 <printf@plt>
mov    %ebx,%edi
call   10a0 <info@plt>
mov    %ebx,%edi
call   1060 <dup_one@plt>
mov    %eax,%ebp
lea    0xea3(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   10a0 <info@plt>
mov    %ebx,%edi
call   1040 <close_fd@plt>
mov    %ebp,%edi
call   1040 <close_fd@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    (%rsi),%rsi
lea    0xe0a(%rip),%rdi
call   1050 <printf@plt>
jmp    1209 <main+0x89>
lea    0xe45(%rip),%rdi
call   1080 <perror@plt>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [73 x i8] c"Exercise 5-3: Implement dup(2) and dup2(2) using fcntl\0AUsage: %s <file>\0A\00"
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [32 x i8] c"Original file descriptor (%s):\0A\00"
@.str.3 = [41 x i8] c"Duplication of original file descriptor:\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i32 %0, i32* %3
store i8** %1, i8*** %4
%10 = load i32, i32* %3
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %19
12: ; preds = %2
%13 = load i8**, i8*** %4
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 @printf(i8* getelementptr inbounds ([73 x i8], [73 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %2
%20 = load i8**, i8*** %4
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
store i8* %22, i8** %9
%23 = load i8*, i8** %9
%24 = load i32, i32* @O_RDONLY
%25 = call i32 @open(i8* %23, i32 %24)
store i32 %25, i32* %5
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %31
27: ; preds = %19
%28 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %19
%32 = load i8*, i8** %9
%33 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %32)
%34 = load i32, i32* %5
%35 = call i32 @info(i32 %34)
%36 = load i32, i32* %5
%37 = call i32 @dup_one(i32 %36)
store i32 %37, i32* %6
%38 = call i32 @puts(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0))
%39 = load i32, i32* %6
%40 = call i32 @info(i32 %39)
%41 = load i32, i32* %5
%42 = call i32 @close_fd(i32 %41)
%43 = load i32, i32* %6
%44 = call i32 @close_fd(i32 %43)
ret i32 0
}

LLVM IR (-O2):
@.str = [73 x i8] c"Exercise 5-3: Implement dup(2) and dup2(2) using fcntl\0AUsage: %s <file>\0A\00"
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [32 x i8] c"Original file descriptor (%s):\0A\00"
@.str.3 = [41 x i8] c"Duplication of original file descriptor:\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %9, label %4
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 @printf(i8* getelementptr inbounds ([73 x i8], [73 x i8]* @.str, i64 0, i64 0), i8* %5)
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = load i32, i32* @O_RDONLY
%13 = tail call i32 @open(i8* %11, i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %19
15: ; preds = %9
%16 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %9
%20 = tail call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %11)
%21 = tail call i32 @info(i32 %13)
%22 = tail call i32 @dup_one(i32 %13)
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0))
%24 = tail call i32 @info(i32 %22)
%25 = tail call i32 @close_fd(i32 %13)
%26 = tail call i32 @close_fd(i32 %22)
ret i32 0
}