#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 */
struct stat {
  int dummy;
};

/* Variables and functions */
scalar_t__ ENOENT;
int O_APPEND;
int O_CREAT;
int O_SYNC;
int O_WRONLY;
int S_IRGRP;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
scalar_t__ dup2(int, int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fileno(int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
scalar_t__ strcmp(char *, char *);
int redirect(char *outfile) {
  int n;
  int flags = O_WRONLY | O_APPEND | O_SYNC;
  struct stat buf;
  if (strcmp(outfile, "-")) {
    if (stat(outfile, &buf)) {
      if (errno == ENOENT) {
        flags = O_WRONLY | O_CREAT | O_SYNC;
      } else {
        exit(14);
      }
    }
    n = open(outfile, flags, S_IRUSR | S_IWUSR | S_IRGRP);
    if (n < 0) {
      perror(" error opening file ");
      exit(10);
    }
    if (dup2(n, fileno(stdout)) < 0) {
      perror(" error dupping stdout ");
      exit(11);
    }
    if (dup2(n, fileno(stderr)) < 0) {
      exit(12);
    }
    close(n);
  }
  return 0;
}


x86 assembly (clang, -O0):
<redirect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2dfd(%rip),%rax
mov    (%rax),%eax
mov    0x2dec(%rip),%rcx
or     (%rcx),%eax
mov    0x2e3b(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe4b(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     12e9 <redirect+0x169>
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
je     1227 <redirect+0xa7>
mov    0x2dde(%rip),%rax
mov    (%rax),%rax
mov    0x2dac(%rip),%rcx
cmp    (%rcx),%rax
jne    1218 <redirect+0x98>
mov    0x2d94(%rip),%rax
mov    (%rax),%eax
mov    0x2df3(%rip),%rcx
or     (%rcx),%eax
mov    0x2dd2(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x10(%rbp)
jmp    1222 <redirect+0xa2>
mov    $0xe,%edi
call   10a0 <exit@plt>
jmp    1227 <redirect+0xa7>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    0x2d7b(%rip),%rax
mov    (%rax),%edx
mov    0x2d9a(%rip),%rax
or     (%rax),%edx
mov    0x2d71(%rip),%rax
or     (%rax),%edx
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1271 <redirect+0xf1>
lea    0xda0(%rip),%rdi
call   1090 <perror@plt>
mov    $0xa,%edi
call   10a0 <exit@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2d2a(%rip),%rax
mov    (%rax),%edi
call   1070 <fileno@plt>
mov    -0x1c(%rbp),%edi
mov    %eax,%esi
call   1030 <dup2@plt>
cmp    $0x0,%rax
jge    12af <redirect+0x12f>
lea    0xd77(%rip),%rdi
call   1090 <perror@plt>
mov    $0xb,%edi
call   10a0 <exit@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d0c(%rip),%rax
mov    (%rax),%edi
call   1070 <fileno@plt>
mov    -0x20(%rbp),%edi
mov    %eax,%esi
call   1030 <dup2@plt>
cmp    $0x0,%rax
jge    12e1 <redirect+0x161>
mov    $0xc,%edi
call   10a0 <exit@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<redirect>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2dfb(%rip),%r14
mov    (%r14),%r12d
mov    0x2de9(%rip),%rax
mov    (%rax),%ebp
mov    0x2e38(%rip),%r15
mov    (%r15),%r13d
lea    0xe4e(%rip),%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     126b <redirect+0xeb>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1060 <stat@plt>
test   %rax,%rax
je     11fb <redirect+0x7b>
mov    0x2de9(%rip),%rax
mov    (%rax),%rax
mov    0x2db7(%rip),%rcx
cmp    (%rcx),%rax
jne    12b2 <redirect+0x132>
mov    0x2e07(%rip),%rax
mov    (%rax),%ebp
or     (%r14),%ebp
or     (%r15),%ebp
jmp    1201 <redirect+0x81>
or     %r12d,%ebp
or     %r13d,%ebp
mov    0x2da8(%rip),%rax
mov    0x2dc9(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d9e(%rip),%rax
or     (%rax),%edx
mov    %rbx,%rdi
mov    %ebp,%esi
call   1080 <open@plt>
test   %eax,%eax
js     127c <redirect+0xfc>
mov    %eax,%ebx
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
call   1070 <fileno@plt>
mov    %ebx,%edi
mov    %eax,%esi
call   1030 <dup2@plt>
test   %rax,%rax
js     1292 <redirect+0x112>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
call   1070 <fileno@plt>
mov    %ebx,%edi
mov    %eax,%esi
call   1030 <dup2@plt>
test   %rax,%rax
js     12a8 <redirect+0x128>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd7f(%rip),%rdi
call   1090 <perror@plt>
mov    $0xa,%edi
call   10a0 <exit@plt>
lea    0xd7e(%rip),%rdi
call   1090 <perror@plt>
mov    $0xb,%edi
call   10a0 <exit@plt>
mov    $0xc,%edi
call   10a0 <exit@plt>
mov    $0xe,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@O_SYNC = global i32 0
@.str = [2 x i8] c"-\00"
@errno = global i64 0
@ENOENT = global i64 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@.str.1 = [21 x i8] c" error opening file \00"
@stdout = global i32 0
@.str.2 = [23 x i8] c" error dupping stdout \00"
@stderr = global i32 0
define i32 @redirect(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.stat
store i8* %0, i8** %2
%6 = load i32, i32* @O_WRONLY
%7 = load i32, i32* @O_APPEND
%8 = or i32 %6, %7
%9 = load i32, i32* @O_SYNC
%10 = or i32 %8, %9
store i32 %10, i32* %4
%11 = load i8*, i8** %2
%12 = call i64 @strcmp(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %65
14: ; preds = %1
%15 = load i8*, i8** %2
%16 = call i64 @stat(i8* %15, %struct.stat* %5)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %31
18: ; preds = %14
%19 = load i64, i64* @errno
%20 = load i64, i64* @ENOENT
%21 = icmp eq i64 %19, %20
br i1 %21, label %22, label %28
22: ; preds = %18
%23 = load i32, i32* @O_WRONLY
%24 = load i32, i32* @O_CREAT
%25 = or i32 %23, %24
%26 = load i32, i32* @O_SYNC
%27 = or i32 %25, %26
store i32 %27, i32* %4
br label %30
28: ; preds = %18
%29 = call i32 @exit(i32 14)
unreachable
30: ; preds = %22
br label %31
31: ; preds = %30, %14
%32 = load i8*, i8** %2
%33 = load i32, i32* %4
%34 = load i32, i32* @S_IRUSR
%35 = load i32, i32* @S_IWUSR
%36 = or i32 %34, %35
%37 = load i32, i32* @S_IRGRP
%38 = or i32 %36, %37
%39 = call i32 @open(i8* %32, i32 %33, i32 %38)
store i32 %39, i32* %3
%40 = load i32, i32* %3
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %31
%43 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%44 = call i32 @exit(i32 10)
unreachable
45: ; preds = %31
%46 = load i32, i32* %3
%47 = load i32, i32* @stdout
%48 = call i32 @fileno(i32 %47)
%49 = call i64 @dup2(i32 %46, i32 %48)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %45
%52 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%53 = call i32 @exit(i32 11)
unreachable
54: ; preds = %45
%55 = load i32, i32* %3
%56 = load i32, i32* @stderr
%57 = call i32 @fileno(i32 %56)
%58 = call i64 @dup2(i32 %55, i32 %57)
%59 = icmp slt i64 %58, 0
br i1 %59, label %60, label %62
60: ; preds = %54
%61 = call i32 @exit(i32 12)
unreachable
62: ; preds = %54
%63 = load i32, i32* %3
%64 = call i32 @close(i32 %63)
br label %65
65: ; preds = %62, %1
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@O_SYNC = global i32 0
@.str = [2 x i8] c"-\00"
@errno = global i64 0
@ENOENT = global i64 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@.str.1 = [21 x i8] c" error opening file \00"
@stdout = global i32 0
@.str.2 = [23 x i8] c" error dupping stdout \00"
@stderr = global i32 0
define i32 @redirect(i8* %0) {
%2 = alloca %struct.stat
%3 = load i32, i32* @O_WRONLY
%4 = load i32, i32* @O_APPEND
%5 = load i32, i32* @O_SYNC
%6 = bitcast %struct.stat* %2 to i8*
%7 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i64 %7, 0
br i1 %8, label %55, label %9
9: ; preds = %1
%10 = or i32 %4, %3
%11 = or i32 %10, %5
%12 = call i64 @stat(i8* %0, %struct.stat* %2)
%13 = icmp eq i64 %12, 0
br i1 %13, label %26, label %14
14: ; preds = %9
%15 = load i64, i64* @errno
%16 = load i64, i64* @ENOENT
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %24
18: ; preds = %14
%19 = load i32, i32* @O_WRONLY
%20 = load i32, i32* @O_CREAT
%21 = or i32 %20, %19
%22 = load i32, i32* @O_SYNC
%23 = or i32 %21, %22
br label %26
24: ; preds = %14
%25 = call i32 @exit(i32 14)
unreachable
26: ; preds = %18, %9
%27 = phi i32 [ %23, %18 ], [ %11, %9 ]
%28 = load i32, i32* @S_IRUSR
%29 = load i32, i32* @S_IWUSR
%30 = or i32 %29, %28
%31 = load i32, i32* @S_IRGRP
%32 = or i32 %30, %31
%33 = call i32 @open(i8* %0, i32 %27, i32 %32)
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %26
%36 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%37 = call i32 @exit(i32 10)
unreachable
38: ; preds = %26
%39 = load i32, i32* @stdout
%40 = call i32 @fileno(i32 %39)
%41 = call i64 @dup2(i32 %33, i32 %40)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %38
%44 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%45 = call i32 @exit(i32 11)
unreachable
46: ; preds = %38
%47 = load i32, i32* @stderr
%48 = call i32 @fileno(i32 %47)
%49 = call i64 @dup2(i32 %33, i32 %48)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %46
%52 = call i32 @exit(i32 12)
unreachable
53: ; preds = %46
%54 = call i32 @close(i32 %33)
br label %55
55: ; preds = %53, %1
ret i32 0
}