#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_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close_on_exec(int);
int open(char const *, int, int);
void open_log_file(int *plogfd, const char *logfile) {
  int logfd = *plogfd;
  if (logfd < 0) {
    logfd = open(logfile, O_WRONLY | O_CREAT | O_TRUNC, 0755);
    if (logfd < 0) {
      *plogfd = -2;
      return;
    }
    close_on_exec(logfd);
    *plogfd = logfd;
  }
}


x86 assembly (clang, -O0):
<open_log_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1199 <open_log_file+0x79>
mov    -0x10(%rbp),%rdi
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
mov    0x2ea1(%rip),%rax
or     (%rax),%esi
mov    0x2e90(%rip),%rax
or     (%rax),%esi
mov    $0x1ed,%edx
call   1040 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1188 <open_log_file+0x68>
mov    -0x8(%rbp),%rax
movl   $0xfffffffe,(%rax)
jmp    1199 <open_log_file+0x79>
mov    -0x14(%rbp),%edi
call   1030 <close_on_exec@plt>
mov    -0x14(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_log_file>:
push   %rbp
push   %rbx
push   %rax
cmpl   $0x0,(%rdi)
jns    116b <open_log_file+0x4b>
mov    %rdi,%rbx
mov    0x2e96(%rip),%rcx
mov    0x2ebf(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    0x2eac(%rip),%rcx
or     (%rcx),%eax
mov    %rsi,%rdi
mov    %eax,%esi
mov    $0x1ed,%edx
call   1040 <open@plt>
test   %eax,%eax
js     1164 <open_log_file+0x44>
mov    %eax,%ebp
mov    %eax,%edi
call   1030 <close_on_exec@plt>
jmp    1169 <open_log_file+0x49>
mov    $0xfffffffe,%ebp
mov    %ebp,(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
define void @open_log_file(i32* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32
store i32* %0, i32** %3
store i8* %1, i8** %4
%6 = load i32*, i32** %3
%7 = load i32, i32* %6
store i32 %7, i32* %5
%8 = load i32, i32* %5
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %27
10: ; preds = %2
%11 = load i8*, i8** %4
%12 = load i32, i32* @O_WRONLY
%13 = load i32, i32* @O_CREAT
%14 = or i32 %12, %13
%15 = load i32, i32* @O_TRUNC
%16 = or i32 %14, %15
%17 = call i32 @open(i8* %11, i32 %16, i32 493)
store i32 %17, i32* %5
%18 = load i32, i32* %5
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %10
%21 = load i32*, i32** %3
store i32 -2, i32* %21
br label %27
22: ; preds = %10
%23 = load i32, i32* %5
%24 = call i32 @close_on_exec(i32 %23)
%25 = load i32, i32* %5
%26 = load i32*, i32** %3
store i32 %25, i32* %26
br label %27
27: ; preds = %20, %22, %2
ret void
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
define void @open_log_file(i32* nocapture %0, i8* nocapture %1) {
%3 = load i32, i32* %0
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %17
5: ; preds = %2
%6 = load i32, i32* @O_WRONLY
%7 = load i32, i32* @O_CREAT
%8 = or i32 %7, %6
%9 = load i32, i32* @O_TRUNC
%10 = or i32 %8, %9
%11 = tail call i32 @open(i8* %1, i32 %10, i32 493)
%12 = icmp slt i32 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %5
%14 = tail call i32 @close_on_exec(i32 %11)
br label %15
15: ; preds = %5, %13
%16 = phi i32 [ %11, %13 ], [ -2, %5 ]
store i32 %16, i32* %0
br label %17
17: ; preds = %15, %2
ret void
}