#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 */
typedef int mode_t;

/* Variables and functions */
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ STDOUT_FILENO;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int dup2(int, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ write(int, char *, int);
void main(void) {
  int fd;
  int n;
  char buffer[100];
  mode_t fd_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
  if ((fd = open("myfile", O_WRONLY | O_CREAT, fd_mode)) == -1)
    perror("cound not open myfile");
  else {
    if (dup2(fd, STDOUT_FILENO) == -1)
      perror("could not redirect STDOUT");
    n = read(0, buffer, 100);
    write(1, buffer, n);
    close(fd);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
mov    0x2e5d(%rip),%rcx
or     (%rcx),%eax
mov    0x2e44(%rip),%rcx
or     (%rcx),%eax
mov    0x2e33(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x74(%rbp)
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
mov    0x2e56(%rip),%rax
or     (%rax),%esi
mov    -0x74(%rbp),%edx
lea    0xe61(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x4(%rbp)
cmp    $0xffffffff,%eax
jne    11d0 <main+0x70>
lea    0xe3a(%rip),%rdi
call   1080 <perror@plt>
jmp    1222 <main+0xc2>
mov    -0x4(%rbp),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
jne    11f6 <main+0x96>
lea    0xe25(%rip),%rdi
call   1080 <perror@plt>
lea    -0x70(%rbp),%rsi
xor    %edi,%edi
mov    $0x64,%edx
call   1060 <read@plt>
mov    %eax,-0x8(%rbp)
lea    -0x70(%rbp),%rsi
mov    -0x8(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x68,%rsp
mov    0x2e4a(%rip),%rax
mov    0x2e63(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e48(%rip),%rax
or     (%rax),%edx
mov    0x2e37(%rip),%rax
or     (%rax),%edx
mov    0x2e16(%rip),%rax
mov    0x2e5f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe6b(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11f4 <main+0x94>
mov    %eax,%ebx
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
je     1207 <main+0xa7>
mov    %rsp,%r14
xor    %edi,%edi
mov    %r14,%rsi
mov    $0x64,%edx
call   1060 <read@plt>
mov    $0x1,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebx,%edi
add    $0x68,%rsp
pop    %rbx
pop    %r14
jmp    1050 <close@plt>
lea    0xe05(%rip),%rdi
add    $0x68,%rsp
pop    %rbx
pop    %r14
jmp    1080 <perror@plt>
lea    0xe08(%rip),%rdi
call   1080 <perror@plt>
jmp    11c5 <main+0x65>

LLVM IR (-O0):
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str = [7 x i8] c"myfile\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.1 = [22 x i8] c"cound not open myfile\00"
@STDOUT_FILENO = global i32 0
@.str.2 = [26 x i8] c"could not redirect STDOUT\00"
define void @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [100 x i8]
%4 = alloca i32
%5 = load i32, i32* @S_IRUSR
%6 = load i32, i32* @S_IWUSR
%7 = or i32 %5, %6
%8 = load i32, i32* @S_IRGRP
%9 = or i32 %7, %8
%10 = load i32, i32* @S_IROTH
%11 = or i32 %9, %10
store i32 %11, i32* %4
%12 = load i32, i32* @O_WRONLY
%13 = load i32, i32* @O_CREAT
%14 = or i32 %12, %13
%15 = load i32, i32* %4
%16 = call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %14, i32 %15)
store i32 %16, i32* %1
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %0
%19 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %35
20: ; preds = %0
%21 = load i32, i32* %1
%22 = load i32, i32* @STDOUT_FILENO
%23 = call i32 @dup2(i32 %21, i32 %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %27
27: ; preds = %25, %20
%28 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%29 = call i32 @read(i32 0, i8* %28, i32 100)
store i32 %29, i32* %2
%30 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%31 = load i32, i32* %2
%32 = call i32 @write(i32 1, i8* %30, i32 %31)
%33 = load i32, i32* %1
%34 = call i32 @close(i32 %33)
br label %35
35: ; preds = %27, %18
ret void
}

LLVM IR (-O2):
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str = [7 x i8] c"myfile\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.1 = [22 x i8] c"cound not open myfile\00"
@STDOUT_FILENO = global i32 0
@.str.2 = [26 x i8] c"could not redirect STDOUT\00"
define void @main() {
%1 = alloca [100 x i8]
%2 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @S_IRUSR
%4 = load i32, i32* @S_IWUSR
%5 = or i32 %4, %3
%6 = load i32, i32* @S_IRGRP
%7 = or i32 %5, %6
%8 = load i32, i32* @S_IROTH
%9 = or i32 %7, %8
%10 = load i32, i32* @O_WRONLY
%11 = load i32, i32* @O_CREAT
%12 = or i32 %11, %10
%13 = tail call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %12, i32 %9)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %27
17: ; preds = %0
%18 = load i32, i32* @STDOUT_FILENO
%19 = tail call i32 @dup2(i32 %13, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %23
23: ; preds = %21, %17
%24 = call i32 @read(i32 0, i8* %2, i32 100)
%25 = call i32 @write(i32 1, i8* %2, i32 %24)
%26 = tail call i32 @close(i32 %13)
br label %27
27: ; preds = %23, %15
ret void
}