#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_CREAT;
int O_RDWR;
int STDOUT_FILENO;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ logerr(char *, char const *);
int open(char const *, int, int);
int /*<<< orphan*/ stderr;
int open_start_log(const char *path, int debug) {
  int fd;
  if (path == NULL) {
    fd = STDOUT_FILENO;
  } else if ((fd = open(path, O_RDWR | O_CREAT, 0600)) == -1)
    logerr("open(%s)", path);
  if (debug)
    fprintf(stderr, "Writing new logfile header\n");
  return (fd);
}


x86 assembly (clang, -O0):
<open_start_log>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    115b <open_start_log+0x2b>
mov    0x2e97(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
jmp    119c <open_start_log+0x6c>
mov    -0x8(%rbp),%rdi
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
mov    0x2e89(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   1050 <open@plt>
mov    %eax,-0x10(%rbp)
cmp    $0xffffffff,%eax
jne    1197 <open_start_log+0x67>
mov    -0x8(%rbp),%rsi
lea    0xe6e(%rip),%rdi
call   1040 <logerr@plt>
jmp    119c <open_start_log+0x6c>
cmpl   $0x0,-0xc(%rbp)
je     11bb <open_start_log+0x8b>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
lea    0xe53(%rip),%rsi
call   1030 <fprintf@plt>
mov    -0x10(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_start_log>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
test   %rdi,%rdi
je     1178 <open_start_log+0x48>
mov    %rdi,%rbx
mov    0x2e92(%rip),%rax
mov    0x2eab(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x180,%edx
call   1050 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1181 <open_start_log+0x51>
lea    0xe97(%rip),%rdi
mov    %rbx,%rsi
call   1040 <logerr@plt>
mov    $0xffffffff,%ebp
jmp    1181 <open_start_log+0x51>
mov    0x2e69(%rip),%rax
mov    (%rax),%ebp
test   %r14d,%r14d
je     119b <open_start_log+0x6b>
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
lea    0xe73(%rip),%rsi
call   1030 <fprintf@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@STDOUT_FILENO = global i32 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str = [9 x i8] c"open(%s)\00"
@stderr = global i32 0
@.str.1 = [28 x i8] c"Writing new logfile header\0A\00"
define i32 @open_start_log(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 %10
8: ; preds = %2
%9 = load i32, i32* @STDOUT_FILENO
store i32 %9, i32* %5
br label %21
10: ; preds = %2
%11 = load i8*, i8** %3
%12 = load i32, i32* @O_RDWR
%13 = load i32, i32* @O_CREAT
%14 = or i32 %12, %13
%15 = call i32 @open(i8* %11, i32 %14, i32 384)
store i32 %15, i32* %5
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %10
%18 = load i8*, i8** %3
%19 = call i32 @logerr(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %18)
br label %20
20: ; preds = %17, %10
br label %21
21: ; preds = %20, %8
%22 = load i32, i32* %4
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %24, %21
%28 = load i32, i32* %5
ret i32 %28
}

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str = [9 x i8] c"open(%s)\00"
@stderr = global i32 0
@.str.1 = [28 x i8] c"Writing new logfile header\0A\00"
define i32 @open_start_log(i8* %0, i32 %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = load i32, i32* @STDOUT_FILENO
br label %14
6: ; preds = %2
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_CREAT
%9 = or i32 %8, %7
%10 = tail call i32 @open(i8* %0, i32 %9, i32 384)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %6
%13 = tail call i32 @logerr(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %14
14: ; preds = %6, %12, %4
%15 = phi i32 [ %5, %4 ], [ -1, %12 ], [ %10, %6 ]
%16 = icmp eq i32 %1, 0
br i1 %16, label %20, label %17
17: ; preds = %14
%18 = load i32, i32* @stderr
%19 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %17, %14
ret i32 %15
}