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


x86 assembly (clang, -O0):
<execute_dup3>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xec1(%rip),%rdi
call   1040 <open_rdonly@plt>
mov    %eax,-0x4(%rbp)
lea    0xebf(%rip),%rdi
call   1050 <open_rdwr@plt>
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    0x2e75(%rip),%rax
mov    (%rax),%edx
call   1030 <dup3@plt>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    $0xffffffff,%edx
call   1030 <dup3@plt>
mov    -0x4(%rbp),%edi
mov    -0x4(%rbp),%esi
mov    0x2e51(%rip),%rax
mov    (%rax),%edx
call   1030 <dup3@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<execute_dup3>:
push   %rbp
push   %r14
push   %rbx
lea    0xec5(%rip),%rdi
call   1040 <open_rdonly@plt>
mov    %eax,%ebx
lea    0xec4(%rip),%rdi
call   1050 <open_rdwr@plt>
mov    %eax,%ebp
mov    0x2e81(%rip),%r14
mov    (%r14),%edx
mov    %ebx,%edi
mov    %eax,%esi
call   1030 <dup3@plt>
mov    %ebx,%edi
mov    %ebp,%esi
mov    $0xffffffff,%edx
call   1030 <dup3@plt>
mov    (%r14),%edx
mov    %ebx,%edi
mov    %ebx,%esi
call   1030 <dup3@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"syscalls.txt\00"
@.str.1 = [14 x i8] c"syscalls2.txt\00"
@O_CLOEXEC = global i32 0
define i32 @execute_dup3() {
%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 = load i32, i32* @O_CLOEXEC
%8 = call i32 @dup3(i32 %5, i32 %6, i32 %7)
%9 = load i32, i32* %1
%10 = load i32, i32* %2
%11 = call i32 @dup3(i32 %9, i32 %10, i32 -1)
%12 = load i32, i32* %1
%13 = load i32, i32* %1
%14 = load i32, i32* @O_CLOEXEC
%15 = call i32 @dup3(i32 %12, i32 %13, i32 %14)
ret i32 0
}

LLVM IR (-O2):
@.str = [13 x i8] c"syscalls.txt\00"
@.str.1 = [14 x i8] c"syscalls2.txt\00"
@O_CLOEXEC = global i32 0
define i32 @execute_dup3() {
%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 = load i32, i32* @O_CLOEXEC
%4 = tail call i32 @dup3(i32 %1, i32 %2, i32 %3)
%5 = tail call i32 @dup3(i32 %1, i32 %2, i32 -1)
%6 = load i32, i32* @O_CLOEXEC
%7 = tail call i32 @dup3(i32 %1, i32 %1, i32 %6)
ret i32 0
}