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


x86 assembly (clang, -O0):
<aeternum_redirect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1165 <aeternum_redirect+0x25>
lea    0xe9f(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
mov    0x2e27(%rip),%rax
or     (%rax),%esi
mov    0x2e76(%rip),%rax
or     (%rax),%esi
mov    0x2e2d(%rip),%rax
mov    (%rax),%edx
mov    0x2e2c(%rip),%rax
or     (%rax),%edx
mov    0x2e43(%rip),%rax
or     (%rax),%edx
mov    0x2e32(%rip),%rax
or     (%rax),%edx
call   1040 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11d4 <aeternum_redirect+0x94>
lea    0xe4a(%rip),%rdi
call   1050 <perror@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x10(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    1202 <aeternum_redirect+0xc2>
lea    0xe23(%rip),%rdi
call   1050 <perror@plt>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<aeternum_redirect>:
push   %rbx
test   %rdi,%rdi
lea    0xeb5(%rip),%rax
cmove  %rax,%rdi
mov    0x2e52(%rip),%rcx
mov    0x2e43(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    0x2e90(%rip),%rcx
or     (%rcx),%eax
mov    0x2e47(%rip),%rcx
mov    0x2e48(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2e5d(%rip),%rcx
or     (%rcx),%edx
mov    %esi,%ebx
mov    0x2e4a(%rip),%rcx
or     (%rcx),%edx
mov    %eax,%esi
call   1040 <open@plt>
cmp    $0xffffffff,%eax
je     11ac <aeternum_redirect+0x6c>
mov    %eax,%edi
mov    %ebx,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
je     11b5 <aeternum_redirect+0x75>
pop    %rbx
ret
lea    0xe58(%rip),%rdi
jmp    11bc <aeternum_redirect+0x7c>
lea    0xe56(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

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

LLVM IR (-O2):
@.str = [11 x i8] c"/dev/null\00\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
@.str.1 = [7 x i8] c"open()\00"
@errno = global i32 0
@.str.2 = [7 x i8] c"dup2()\00"
define void @aeternum_redirect(i8* %0, i32 %1) {
%3 = icmp eq i8* %0, null
%4 = select i1 %3, i8* getelementptr inbounds ([11 x i8], [11 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 %19, label %23
19: ; preds = %2
%20 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%21 = load i32, i32* @errno
%22 = tail call i32 @exit(i32 %21)
unreachable
23: ; preds = %2
%24 = tail call i32 @dup2(i32 %17, i32 %1)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %30
26: ; preds = %23
%27 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%28 = load i32, i32* @errno
%29 = tail call i32 @exit(i32 %28)
unreachable
30: ; preds = %23
ret void
}