#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 /*<<< orphan*/ dup2(int, int);
int open_rdonly(char *);
int open_rdwr(char *);
int execute_dup2() {
  int oldfd = open_rdonly("syscalls.txt");
  int newfd = open_rdwr("syscalls2.txt");
  dup2(oldfd, newfd);
  dup2(oldfd, oldfd);
  dup2(oldfd, -1);
  close(newfd);
  dup2(oldfd, newfd);
  close(oldfd);
  dup2(oldfd, newfd);
  return 0;
}


x86 assembly (clang, -O0):
<execute_dup2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xeb1(%rip),%rdi
call   1030 <open_rdonly@plt>
mov    %eax,-0x4(%rbp)
lea    0xeaf(%rip),%rdi
call   1060 <open_rdwr@plt>
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1040 <dup2@plt>
mov    -0x4(%rbp),%edi
mov    -0x4(%rbp),%esi
call   1040 <dup2@plt>
mov    -0x4(%rbp),%edi
mov    $0xffffffff,%esi
call   1040 <dup2@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1040 <dup2@plt>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1040 <dup2@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<execute_dup2>:
push   %rbp
push   %rbx
push   %rax
lea    0xeb6(%rip),%rdi
call   1030 <open_rdonly@plt>
mov    %eax,%ebx
lea    0xeb5(%rip),%rdi
call   1060 <open_rdwr@plt>
mov    %eax,%ebp
mov    %ebx,%edi
mov    %eax,%esi
call   1040 <dup2@plt>
mov    %ebx,%edi
mov    %ebx,%esi
call   1040 <dup2@plt>
mov    %ebx,%edi
mov    $0xffffffff,%esi
call   1040 <dup2@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %ebx,%edi
mov    %ebp,%esi
call   1040 <dup2@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %ebx,%edi
mov    %ebp,%esi
call   1040 <dup2@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"syscalls.txt\00"
@.str.1 = [14 x i8] c"syscalls2.txt\00"
define i32 @execute_dup2() {
%1 = alloca i32
%2 = alloca i32
%3 = call i32 @open_rdonly(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 %3, i32* %1
%4 = call i32 @open_rdwr(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i32 %4, i32* %2
%5 = load i32, i32* %1
%6 = load i32, i32* %2
%7 = call i32 @dup2(i32 %5, i32 %6)
%8 = load i32, i32* %1
%9 = load i32, i32* %1
%10 = call i32 @dup2(i32 %8, i32 %9)
%11 = load i32, i32* %1
%12 = call i32 @dup2(i32 %11, i32 -1)
%13 = load i32, i32* %2
%14 = call i32 @close(i32 %13)
%15 = load i32, i32* %1
%16 = load i32, i32* %2
%17 = call i32 @dup2(i32 %15, i32 %16)
%18 = load i32, i32* %1
%19 = call i32 @close(i32 %18)
%20 = load i32, i32* %1
%21 = load i32, i32* %2
%22 = call i32 @dup2(i32 %20, i32 %21)
ret i32 0
}

LLVM IR (-O2):
@.str = [13 x i8] c"syscalls.txt\00"
@.str.1 = [14 x i8] c"syscalls2.txt\00"
define i32 @execute_dup2() {
%1 = tail call i32 @open_rdonly(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%2 = tail call i32 @open_rdwr(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%3 = tail call i32 @dup2(i32 %1, i32 %2)
%4 = tail call i32 @dup2(i32 %1, i32 %1)
%5 = tail call i32 @dup2(i32 %1, i32 -1)
%6 = tail call i32 @close(i32 %2)
%7 = tail call i32 @dup2(i32 %1, i32 %2)
%8 = tail call i32 @close(i32 %1)
%9 = tail call i32 @dup2(i32 %1, i32 %2)
ret i32 0
}