#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 LOG_CONS;
int /*<<< orphan*/ LOG_DAEMON;
int LOG_NDELAY;
int LOG_PID;
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ log_file(int /*<<< orphan*/ *);
int /*<<< orphan*/ openlog(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ *stdout;
int log_open(const char *file, int foreground) {
  FILE *fp = NULL;
  if (foreground) {
    log_file(stdout);
  } else {
    if (file) {
      if ((fp = fopen(file, "a+")) == 0) {
        fprintf(stderr, "can't open logfile: %s\n", file);
        return -1;
      }
      log_file(fp);
    }
  }
  openlog(NULL, LOG_CONS | LOG_NDELAY | LOG_PID, LOG_DAEMON);
  return 0;
}


x86 assembly (clang, -O0):
<log_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1175 <log_open+0x35>
mov    0x2e58(%rip),%rax
mov    (%rax),%rdi
call   1030 <log_file@plt>
jmp    11d1 <log_open+0x91>
cmpq   $0x0,-0x10(%rbp)
je     11cc <log_open+0x8c>
mov    -0x10(%rbp),%rdi
lea    0xe75(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11c3 <log_open+0x83>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe51(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1205 <log_open+0xc5>
mov    -0x20(%rbp),%rdi
call   1030 <log_file@plt>
jmp    11d1 <log_open+0x91>
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
mov    0x2dcf(%rip),%rax
or     (%rax),%esi
mov    0x2dee(%rip),%rax
or     (%rax),%esi
mov    0x2df5(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%edi
call   1060 <openlog@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<log_open>:
push   %rbx
test   %esi,%esi
je     1185 <log_open+0x45>
mov    0x2e74(%rip),%rax
mov    (%rax),%rdi
call   1030 <log_file@plt>
mov    0x2e6d(%rip),%rax
mov    0x2e4e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e6b(%rip),%rax
or     (%rax),%esi
mov    0x2e72(%rip),%rax
mov    (%rax),%edx
xor    %ebx,%ebx
xor    %edi,%edi
call   1060 <openlog@plt>
mov    %ebx,%eax
pop    %rbx
ret
mov    %rdi,%rbx
test   %rdi,%rdi
je     1154 <log_open+0x14>
lea    0xe6c(%rip),%rsi
mov    %rbx,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     11a6 <log_open+0x66>
mov    %rax,%rdi
jmp    114f <log_open+0xf>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
lea    0xe4d(%rip),%rsi
mov    %rbx,%rdx
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    1181 <log_open+0x41>

LLVM IR (-O0):
@stdout = global i32* null
@.str = [3 x i8] c"a+\00"
@stderr = global i32 0
@.str.1 = [24 x i8] c"can't open logfile: %s\0A\00"
@LOG_CONS = global i32 0
@LOG_NDELAY = global i32 0
@LOG_PID = global i32 0
@LOG_DAEMON = global i32 0
define i32 @log_open(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32* null, i32** %6
%7 = load i32, i32* %5
%8 = icmp ne i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i32*, i32** @stdout
%11 = call i32 @log_file(i32* %10)
br label %27
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %26
15: ; preds = %12
%16 = load i8*, i8** %4
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %17, i32** %6
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %23
19: ; preds = %15
%20 = load i32, i32* @stderr
%21 = load i8*, i8** %4
%22 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %21)
store i32 -1, i32* %3
br label %35
23: ; preds = %15
%24 = load i32*, i32** %6
%25 = call i32 @log_file(i32* %24)
br label %26
26: ; preds = %23, %12
br label %27
27: ; preds = %26, %9
%28 = load i32, i32* @LOG_CONS
%29 = load i32, i32* @LOG_NDELAY
%30 = or i32 %28, %29
%31 = load i32, i32* @LOG_PID
%32 = or i32 %30, %31
%33 = load i32, i32* @LOG_DAEMON
%34 = call i32 @openlog(i32* null, i32 %32, i32 %33)
store i32 0, i32* %3
br label %35
35: ; preds = %27, %19
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
@stdout = global i32* null
@.str = [3 x i8] c"a+\00"
@stderr = global i32 0
@.str.1 = [24 x i8] c"can't open logfile: %s\0A\00"
@LOG_CONS = global i32 0
@LOG_NDELAY = global i32 0
@LOG_PID = global i32 0
@LOG_DAEMON = global i32 0
define i32 @log_open(i8* %0, i32 %1) {
%3 = icmp eq i32 %1, 0
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = load i32*, i32** @stdout
%6 = tail call i32 @log_file(i32* %5)
br label %17
7: ; preds = %2
%8 = icmp eq i8* %0, null
br i1 %8, label %17, label %9
9: ; preds = %7
%10 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %25
15: ; preds = %9
%16 = tail call i32 @log_file(i32* %10)
br label %17
17: ; preds = %7, %15, %4
%18 = load i32, i32* @LOG_CONS
%19 = load i32, i32* @LOG_NDELAY
%20 = or i32 %19, %18
%21 = load i32, i32* @LOG_PID
%22 = or i32 %20, %21
%23 = load i32, i32* @LOG_DAEMON
%24 = tail call i32 @openlog(i32* null, i32 %22, i32 %23)
br label %25
25: ; preds = %17, %12
%26 = phi i32 [ 0, %17 ], [ -1, %12 ]
ret i32 %26
}