#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ dup2(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stdout;
FILE *openlog(int daemon) {
  FILE *fd;
  fd = fopen("/var/log/filetoast.log", "a+");
  if (fd == NULL) {
    fprintf(stdout, "Can not open logfile!");
    exit(255);
  }
  if (daemon) {
    dup2(fileno(fd), STDOUT_FILENO);
    close(fileno(fd));
  }
  return fd;
}


x86 assembly (clang, -O0):
<openlog>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
lea    0xe8e(%rip),%rdi
lea    0xe9e(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ac <openlog+0x4c>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
lea    0xe7d(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xff,%edi
call   1080 <exit@plt>
cmpl   $0x0,-0x4(%rbp)
je     11df <openlog+0x7f>
mov    -0x10(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x10(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openlog>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
lea    0xe94(%rip),%rdi
lea    0xea4(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11b5 <openlog+0x55>
mov    %rax,%rbx
test   %ebp,%ebp
je     11ab <openlog+0x4b>
mov    %rbx,%rdi
call   1060 <fileno@plt>
mov    0x2e5d(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
call   1030 <dup2@plt>
mov    %rbx,%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
call   1040 <close@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
lea    0xe55(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [23 x i8] c"/var/log/filetoast.log\00"
@.str.1 = [3 x i8] c"a+\00"
@stdout = global i32 0
@.str.2 = [22 x i8] c"Can not open logfile!\00"
@STDOUT_FILENO = global i32 0
define i32* @openlog(i32 %0) {
%2 = alloca i32
%3 = alloca i32*
store i32 %0, i32* %2
%4 = call i32* @fopen(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %4, i32** %3
%5 = load i32*, i32** %3
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %11
7: ; preds = %1
%8 = load i32, i32* @stdout
%9 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%10 = call i32 @exit(i32 255)
unreachable
11: ; preds = %1
%12 = load i32, i32* %2
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %22
14: ; preds = %11
%15 = load i32*, i32** %3
%16 = call i32 @fileno(i32* %15)
%17 = load i32, i32* @STDOUT_FILENO
%18 = call i32 @dup2(i32 %16, i32 %17)
%19 = load i32*, i32** %3
%20 = call i32 @fileno(i32* %19)
%21 = call i32 @close(i32 %20)
br label %22
22: ; preds = %14, %11
%23 = load i32*, i32** %3
ret i32* %23
}

LLVM IR (-O2):
@.str = [23 x i8] c"/var/log/filetoast.log\00"
@.str.1 = [3 x i8] c"a+\00"
@stdout = global i32 0
@.str.2 = [22 x i8] c"Can not open logfile!\00"
@STDOUT_FILENO = global i32 0
define noalias i32* @openlog(i32 %0) {
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %8
4: ; preds = %1
%5 = load i32, i32* @stdout
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%7 = tail call i32 @exit(i32 255)
unreachable
8: ; preds = %1
%9 = icmp eq i32 %0, 0
br i1 %9, label %16, label %10
10: ; preds = %8
%11 = tail call i32 @fileno(i32* %2)
%12 = load i32, i32* @STDOUT_FILENO
%13 = tail call i32 @dup2(i32 %11, i32 %12)
%14 = tail call i32 @fileno(i32* %2)
%15 = tail call i32 @close(i32 %14)
br label %16
16: ; preds = %10, %8
ret i32* %2
}