#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_APPEND;
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ STDOUT_FILENO;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *);
int q1p3() {
  int fileDesc =
      open("redirect.txt", O_CREAT | O_WRONLY | O_APPEND, S_IRUSR | S_IWUSR);
  dup2(fileDesc, STDOUT_FILENO);
  printf("A simple program output.\n");
  return 0;
}


x86 assembly (clang, -O0):
<q1p3>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2eb9(%rip),%rax
mov    (%rax),%esi
mov    0x2e70(%rip),%rax
or     (%rax),%esi
mov    0x2e5f(%rip),%rax
or     (%rax),%esi
mov    0x2e6e(%rip),%rax
mov    (%rax),%edx
mov    0x2e75(%rip),%rax
or     (%rax),%edx
lea    0xe94(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
lea    0xe81(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<q1p3>:
push   %rax
mov    0x2ec0(%rip),%rax
mov    0x2e79(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e66(%rip),%rax
or     (%rax),%esi
mov    0x2e75(%rip),%rax
mov    0x2e7e(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe9b(%rip),%rdi
call   1050 <open@plt>
mov    0x2e77(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
call   1040 <dup2@plt>
lea    0xe8c(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [13 x i8] c"redirect.txt\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@STDOUT_FILENO = global i32 0
@.str.1 = [26 x i8] c"A simple program output.\0A\00"
define i32 @q1p3() {
%1 = alloca i32
%2 = load i32, i32* @O_CREAT
%3 = load i32, i32* @O_WRONLY
%4 = or i32 %2, %3
%5 = load i32, i32* @O_APPEND
%6 = or i32 %4, %5
%7 = load i32, i32* @S_IRUSR
%8 = load i32, i32* @S_IWUSR
%9 = or i32 %7, %8
%10 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %6, i32 %9)
store i32 %10, i32* %1
%11 = load i32, i32* %1
%12 = load i32, i32* @STDOUT_FILENO
%13 = call i32 @dup2(i32 %11, i32 %12)
%14 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [13 x i8] c"redirect.txt\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@STDOUT_FILENO = global i32 0
@str = [25 x i8] c"A simple program output.\00"
define i32 @q1p3() {
%1 = load i32, i32* @O_CREAT
%2 = load i32, i32* @O_WRONLY
%3 = or i32 %2, %1
%4 = load i32, i32* @O_APPEND
%5 = or i32 %3, %4
%6 = load i32, i32* @S_IRUSR
%7 = load i32, i32* @S_IWUSR
%8 = or i32 %7, %6
%9 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %8)
%10 = load i32, i32* @STDOUT_FILENO
%11 = tail call i32 @dup2(i32 %9, i32 %10)
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
ret i32 0
}