#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 S_IRGRP;
int S_IRUSR;
int S_IWGRP;
int S_IWUSR;
int dup2(int, int);
int open(char const *, int, int);
int aeternum_redirect(const char *dest, int fileno) {
  int out;
  const char *filename;
  if (dest == NULL) {
    filename = "/dev/null";
  } else {
    filename = dest;
  }
  out = open(filename, O_WRONLY | O_APPEND | O_CREAT,
             S_IRUSR | S_IRGRP | S_IWGRP | S_IWUSR);
  if (out == -1) {
    return -1;
  }
  if (dup2(out, fileno) == -1) {
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<aeternum_redirect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    114a <aeternum_redirect+0x2a>
lea    0xebf(%rip),%rax
mov    %rax,-0x20(%rbp)
jmp    1152 <aeternum_redirect+0x32>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
mov    0x2e42(%rip),%rax
or     (%rax),%esi
mov    0x2e89(%rip),%rax
or     (%rax),%esi
mov    0x2e48(%rip),%rax
mov    (%rax),%edx
mov    0x2e47(%rip),%rax
or     (%rax),%edx
mov    0x2e56(%rip),%rax
or     (%rax),%edx
mov    0x2e45(%rip),%rax
or     (%rax),%edx
call   1040 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11b3 <aeternum_redirect+0x93>
movl   $0xffffffff,-0x4(%rbp)
jmp    11da <aeternum_redirect+0xba>
mov    -0x18(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    11d3 <aeternum_redirect+0xb3>
movl   $0xffffffff,-0x4(%rbp)
jmp    11da <aeternum_redirect+0xba>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<aeternum_redirect>:
push   %rbx
test   %rdi,%rdi
lea    0xed5(%rip),%rax
cmove  %rax,%rdi
mov    0x2e7a(%rip),%rcx
mov    0x2e6b(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    0x2eb0(%rip),%rcx
or     (%rcx),%eax
mov    0x2e6f(%rip),%rcx
mov    0x2e70(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2e7d(%rip),%rcx
or     (%rcx),%edx
mov    %esi,%ebx
mov    0x2e6a(%rip),%rcx
or     (%rcx),%edx
mov    %eax,%esi
call   1040 <open@plt>
cmp    $0xffffffff,%eax
je     1193 <aeternum_redirect+0x73>
mov    %eax,%edi
mov    %ebx,%esi
call   1030 <dup2@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
sete   %al
neg    %eax
pop    %rbx
ret
mov    $0xffffffff,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/null\00"
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IWUSR = global i32 0
define i32 @aeternum_redirect(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i8*, i8** %4
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %11
10: ; preds = %2
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %7
br label %13
11: ; preds = %2
%12 = load i8*, i8** %4
store i8* %12, i8** %7
br label %13
13: ; preds = %11, %10
%14 = load i8*, i8** %7
%15 = load i32, i32* @O_WRONLY
%16 = load i32, i32* @O_APPEND
%17 = or i32 %15, %16
%18 = load i32, i32* @O_CREAT
%19 = or i32 %17, %18
%20 = load i32, i32* @S_IRUSR
%21 = load i32, i32* @S_IRGRP
%22 = or i32 %20, %21
%23 = load i32, i32* @S_IWGRP
%24 = or i32 %22, %23
%25 = load i32, i32* @S_IWUSR
%26 = or i32 %24, %25
%27 = call i32 @open(i8* %14, i32 %19, i32 %26)
store i32 %27, i32* %6
%28 = load i32, i32* %6
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %31
30: ; preds = %13
store i32 -1, i32* %3
br label %38
31: ; preds = %13
%32 = load i32, i32* %6
%33 = load i32, i32* %5
%34 = call i32 @dup2(i32 %32, i32 %33)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %37
36: ; preds = %31
store i32 -1, i32* %3
br label %38
37: ; preds = %31
store i32 0, i32* %3
br label %38
38: ; preds = %37, %36, %30
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/null\00"
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IWUSR = global i32 0
define i32 @aeternum_redirect(i8* %0, i32 %1) {
%3 = icmp eq i8* %0, null
%4 = select i1 %3, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %0
%5 = load i32, i32* @O_WRONLY
%6 = load i32, i32* @O_APPEND
%7 = or i32 %6, %5
%8 = load i32, i32* @O_CREAT
%9 = or i32 %7, %8
%10 = load i32, i32* @S_IRUSR
%11 = load i32, i32* @S_IRGRP
%12 = or i32 %11, %10
%13 = load i32, i32* @S_IWGRP
%14 = or i32 %12, %13
%15 = load i32, i32* @S_IWUSR
%16 = or i32 %14, %15
%17 = tail call i32 @open(i8* %4, i32 %9, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %23, label %19
19: ; preds = %2
%20 = tail call i32 @dup2(i32 %17, i32 %1)
%21 = icmp eq i32 %20, -1
%22 = sext i1 %21 to i32
br label %23
23: ; preds = %19, %2
%24 = phi i32 [ -1, %2 ], [ %22, %19 ]
ret i32 %24
}