#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_RDONLY;
int /*<<< orphan*/ assert(int);
int dup(int /*<<< orphan*/);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ scanf(char *, char *);
scalar_t__ strcmp(char *, char *);
int test_fd_dup() {
  int x = open("/tests/dup_test_file", O_RDONLY, 0);
  int y = dup(0);
  dup2(x, 0);
  char word[100];
  scanf("%[^\n]s", word);
  assert(strcmp(word, "Hello, World!") == 0);
  dup2(y, 0);
  return 0;
}


x86 assembly (clang, -O0):
<test_fd_dup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
lea    0xe88(%rip),%rdi
xor    %edx,%edx
call   1080 <open@plt>
mov    %eax,-0x4(%rbp)
xor    %edi,%edi
call   1040 <dup@plt>
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
lea    -0x70(%rbp),%rsi
lea    0xe74(%rip),%rdi
call   1070 <scanf@plt>
lea    -0x70(%rbp),%rdi
lea    0xe6b(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_fd_dup>:
push   %rbp
push   %rbx
sub    $0x68,%rsp
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
lea    0xe8a(%rip),%rdi
xor    %edx,%edx
call   1080 <open@plt>
mov    %eax,%ebx
xor    %edi,%edi
call   1040 <dup@plt>
mov    %eax,%ebp
mov    %ebx,%edi
xor    %esi,%esi
call   1030 <dup2@plt>
lea    0xe7d(%rip),%rdi
mov    %rsp,%rbx
mov    %rbx,%rsi
call   1070 <scanf@plt>
lea    0xe72(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strcmp@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1060 <assert@plt>
mov    %ebp,%edi
xor    %esi,%esi
call   1030 <dup2@plt>
xor    %eax,%eax
add    $0x68,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [21 x i8] c"/tests/dup_test_file\00"
@O_RDONLY = global i32 0
@.str.1 = [7 x i8] c"%[^\0A]s\00"
@.str.2 = [14 x i8] c"Hello, World!\00"
define i32 @test_fd_dup() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [100 x i8]
%4 = load i32, i32* @O_RDONLY
%5 = call i32 @open(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %4, i32 0)
store i32 %5, i32* %1
%6 = call i32 @dup(i32 0)
store i32 %6, i32* %2
%7 = load i32, i32* %1
%8 = call i32 @dup2(i32 %7, i32 0)
%9 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%10 = call i32 @scanf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %9)
%11 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%12 = call i64 @strcmp(i8* %11, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%13 = icmp eq i64 %12, 0
%14 = zext i1 %13 to i32
%15 = call i32 @assert(i32 %14)
%16 = load i32, i32* %2
%17 = call i32 @dup2(i32 %16, i32 0)
ret i32 0
}

LLVM IR (-O2):
@.str = [21 x i8] c"/tests/dup_test_file\00"
@O_RDONLY = global i32 0
@.str.1 = [7 x i8] c"%[^\0A]s\00"
@.str.2 = [14 x i8] c"Hello, World!\00"
define i32 @test_fd_dup() {
%1 = alloca [100 x i8]
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %2, i32 0)
%4 = tail call i32 @dup(i32 0)
%5 = tail call i32 @dup2(i32 %3, i32 0)
%6 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%7 = call i32 @scanf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %6)
%8 = call i64 @strcmp(i8* %6, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%9 = icmp eq i64 %8, 0
%10 = zext i1 %9 to i32
%11 = call i32 @assert(i32 %10)
%12 = call i32 @dup2(i32 %4, i32 0)
ret i32 0
}