#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 main() {
  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):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
lea    0xe81(%rip),%rdi
xor    %edx,%edx
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
xor    %edi,%edi
call   1040 <dup@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
lea    -0x70(%rbp),%rsi
lea    0xe6d(%rip),%rdi
call   1070 <scanf@plt>
lea    -0x70(%rbp),%rdi
lea    0xe64(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0xc(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
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 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [100 x i8]
store i32 0, i32* %1
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %5, i32 0)
store i32 %6, i32* %2
%7 = call i32 @dup(i32 0)
store i32 %7, i32* %3
%8 = load i32, i32* %2
%9 = call i32 @dup2(i32 %8, i32 0)
%10 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%11 = call i32 @scanf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %10)
%12 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%13 = call i64 @strcmp(i8* %12, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%14 = icmp eq i64 %13, 0
%15 = zext i1 %14 to i32
%16 = call i32 @assert(i32 %15)
%17 = load i32, i32* %3
%18 = call i32 @dup2(i32 %17, 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 @main() {
%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
}