#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 /*<<< orphan*/ EXIT_FAILURE;
int O_APPEND;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int STDERR_FILENO;
int STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ close(int);
int dup(int);
int /*<<< orphan*/ err(int /*<<< orphan*/, char *, ...);
int open(char const *, int, ...);
void SetupStdio(const char *log_path, bool log_append) {
  close(STDIN_FILENO);
  int fd = open("/dev/null", O_RDONLY);
  if (fd == -1) {
    err(EXIT_FAILURE, "Failed to open /dev/null");
  }
  assert(fd == STDIN_FILENO);
  close(STDOUT_FILENO);
  int flags = O_WRONLY | O_CREAT | (log_append ? O_APPEND : O_TRUNC);
  fd = open(log_path, flags, 0666);
  if (fd == -1) {
    err(EXIT_FAILURE, "Failed to create log file %s", log_path);
  }
  assert(fd == STDOUT_FILENO);
  close(STDERR_FILENO);
  fd = dup(STDOUT_FILENO);
  if (fd == -1) {
    err(EXIT_FAILURE, "dup failed");
  }
  assert(fd == STDERR_FILENO);
}


x86 assembly (clang, -O0):
<SetupStdio>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
lea    0xe92(%rip),%rdi
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11a8 <SetupStdio+0x58>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
lea    0xe5f(%rip),%rsi
mov    $0x0,%al
call   1060 <err@plt>
mov    -0x10(%rbp),%eax
mov    0x2dfe(%rip),%rcx
cmp    (%rcx),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2dca(%rip),%rax
mov    (%rax),%eax
mov    0x2e19(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     11ff <SetupStdio+0xaf>
mov    0x2da3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    120b <SetupStdio+0xbb>
mov    0x2dea(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
mov    -0x1c(%rbp),%ecx
or     %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    $0x1b6,%edx
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    1251 <SetupStdio+0x101>
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdcf(%rip),%rsi
mov    $0x0,%al
call   1060 <err@plt>
mov    -0x10(%rbp),%eax
mov    0x2d85(%rip),%rcx
cmp    (%rcx),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
call   1030 <dup@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    12aa <SetupStdio+0x15a>
mov    0x2d1e(%rip),%rax
mov    (%rax),%edi
lea    0xd93(%rip),%rsi
mov    $0x0,%al
call   1060 <err@plt>
mov    -0x10(%rbp),%eax
mov    0x2d0c(%rip),%rcx
cmp    (%rcx),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SetupStdio>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2e4d(%rip),%r15
mov    (%r15),%edi
call   1040 <close@plt>
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
lea    0xe94(%rip),%rdi
xor    %eax,%eax
call   1070 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11a0 <SetupStdio+0x50>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
lea    0xe67(%rip),%rsi
xor    %eax,%eax
call   1060 <err@plt>
xor    %edi,%edi
cmp    (%r15),%ebx
sete   %dil
call   1050 <assert@plt>
mov    0x2e2b(%rip),%rbx
mov    (%rbx),%edi
call   1040 <close@plt>
mov    0x2ddd(%rip),%rax
mov    0x2e2e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
test   %ebp,%ebp
jne    11db <SetupStdio+0x8b>
mov    0x2e17(%rip),%rax
jmp    11e2 <SetupStdio+0x92>
mov    0x2db6(%rip),%rax
or     (%rax),%esi
mov    %r14,%rdi
mov    $0x1b6,%edx
xor    %eax,%eax
call   1070 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1214 <SetupStdio+0xc4>
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
lea    0xe0f(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <err@plt>
xor    %edi,%edi
cmp    (%rbx),%ebp
sete   %dil
call   1050 <assert@plt>
mov    0x2d98(%rip),%rbp
mov    0x0(%rbp),%edi
call   1040 <close@plt>
mov    (%rbx),%edi
call   1030 <dup@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    1255 <SetupStdio+0x105>
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
lea    0xde8(%rip),%rsi
xor    %eax,%eax
call   1060 <err@plt>
xor    %edi,%edi
cmp    0x0(%rbp),%ebx
sete   %dil
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <assert@plt>

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@.str = [10 x i8] c"/dev/null\00"
@O_RDONLY = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [25 x i8] c"Failed to open /dev/null\00"
@STDOUT_FILENO = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@O_TRUNC = global i32 0
@.str.2 = [29 x i8] c"Failed to create log file %s\00"
@STDERR_FILENO = global i32 0
@.str.3 = [11 x i8] c"dup failed\00"
define void @SetupStdio(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = load i32, i32* @STDIN_FILENO
%8 = call i32 @close(i32 %7)
%9 = load i32, i32* @O_RDONLY
%10 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i32, i32* @EXIT_FAILURE
%15 = call i32 (i32, i8*, ...) @err(i32 %14, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %13, %2
%17 = load i32, i32* %5
%18 = load i32, i32* @STDIN_FILENO
%19 = icmp eq i32 %17, %18
%20 = zext i1 %19 to i32
%21 = call i32 @assert(i32 %20)
%22 = load i32, i32* @STDOUT_FILENO
%23 = call i32 @close(i32 %22)
%24 = load i32, i32* @O_WRONLY
%25 = load i32, i32* @O_CREAT
%26 = or i32 %24, %25
%27 = load i32, i32* %4
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %16
%30 = load i32, i32* @O_APPEND
br label %33
31: ; preds = %16
%32 = load i32, i32* @O_TRUNC
br label %33
33: ; preds = %31, %29
%34 = phi i32 [ %30, %29 ], [ %32, %31 ]
%35 = or i32 %26, %34
store i32 %35, i32* %6
%36 = load i8*, i8** %3
%37 = load i32, i32* %6
%38 = call i32 (i8*, i32, ...) @open(i8* %36, i32 %37, i32 438)
store i32 %38, i32* %5
%39 = load i32, i32* %5
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %45
41: ; preds = %33
%42 = load i32, i32* @EXIT_FAILURE
%43 = load i8*, i8** %3
%44 = call i32 (i32, i8*, ...) @err(i32 %42, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %43)
br label %45
45: ; preds = %41, %33
%46 = load i32, i32* %5
%47 = load i32, i32* @STDOUT_FILENO
%48 = icmp eq i32 %46, %47
%49 = zext i1 %48 to i32
%50 = call i32 @assert(i32 %49)
%51 = load i32, i32* @STDERR_FILENO
%52 = call i32 @close(i32 %51)
%53 = load i32, i32* @STDOUT_FILENO
%54 = call i32 @dup(i32 %53)
store i32 %54, i32* %5
%55 = load i32, i32* %5
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %60
57: ; preds = %45
%58 = load i32, i32* @EXIT_FAILURE
%59 = call i32 (i32, i8*, ...) @err(i32 %58, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %60
60: ; preds = %57, %45
%61 = load i32, i32* %5
%62 = load i32, i32* @STDERR_FILENO
%63 = icmp eq i32 %61, %62
%64 = zext i1 %63 to i32
%65 = call i32 @assert(i32 %64)
ret void
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@.str = [10 x i8] c"/dev/null\00"
@O_RDONLY = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [25 x i8] c"Failed to open /dev/null\00"
@STDOUT_FILENO = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@O_TRUNC = global i32 0
@.str.2 = [29 x i8] c"Failed to create log file %s\00"
@STDERR_FILENO = global i32 0
@.str.3 = [11 x i8] c"dup failed\00"
define void @SetupStdio(i8* %0, i32 %1) {
%3 = load i32, i32* @STDIN_FILENO
%4 = tail call i32 @close(i32 %3)
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 (i32, i8*, ...) @err(i32 %9, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %11
11: ; preds = %8, %2
%12 = load i32, i32* @STDIN_FILENO
%13 = icmp eq i32 %6, %12
%14 = zext i1 %13 to i32
%15 = tail call i32 @assert(i32 %14)
%16 = load i32, i32* @STDOUT_FILENO
%17 = tail call i32 @close(i32 %16)
%18 = load i32, i32* @O_WRONLY
%19 = load i32, i32* @O_CREAT
%20 = or i32 %19, %18
%21 = icmp eq i32 %1, 0
%22 = load i32, i32* @O_APPEND
%23 = load i32, i32* @O_TRUNC
%24 = select i1 %21, i32 %23, i32 %22
%25 = or i32 %20, %24
%26 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %25, i32 438)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %11
%29 = load i32, i32* @EXIT_FAILURE
%30 = tail call i32 (i32, i8*, ...) @err(i32 %29, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %31
31: ; preds = %28, %11
%32 = load i32, i32* @STDOUT_FILENO
%33 = icmp eq i32 %26, %32
%34 = zext i1 %33 to i32
%35 = tail call i32 @assert(i32 %34)
%36 = load i32, i32* @STDERR_FILENO
%37 = tail call i32 @close(i32 %36)
%38 = load i32, i32* @STDOUT_FILENO
%39 = tail call i32 @dup(i32 %38)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %44
41: ; preds = %31
%42 = load i32, i32* @EXIT_FAILURE
%43 = tail call i32 (i32, i8*, ...) @err(i32 %42, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %44
44: ; preds = %41, %31
%45 = load i32, i32* @STDERR_FILENO
%46 = icmp eq i32 %39, %45
%47 = zext i1 %46 to i32
%48 = tail call i32 @assert(i32 %47)
ret void
}