#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 */
struct stat {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ ENOENT;
int FILENAME_MAX;
int O_CREAT;
int O_WRONLY;
int S_IRGRP;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
char *edesc(scalar_t__);
int /*<<< orphan*/ ensure_data_dir_exists();
scalar_t__ errno;
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, scalar_t__, char *,
                           char *);
int /*<<< orphan*/ get_log_file_path(char *);
int open(char *, int, int);
int stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
FILE *open_log_file(void) {
  if (!ensure_data_dir_exists()) {
    return NULL;
  }
  char path[FILENAME_MAX];
  get_log_file_path(path);
  struct stat finfo;
  int success = stat(path, &finfo);
  if (-1 == success) {
    if (ENOENT == errno) {
      success = open(path, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP);
      if (-1 == success) {
        goto fail;
      } else {
        close(success);
      }
    } else {
      goto fail;
    }
  }
  FILE *fh = fopen(path, "r+");
  if (NULL == fh) {
    goto fail;
  }
  return fh;
fail:
  fprintf(stderr, "Error %d (%s) opening %s\n", errno, edesc(errno), path);
  return NULL;
}


x86 assembly (clang, -O0):
<open_log_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    $0x0,%al
call   1060 <ensure_data_dir_exists@plt>
cmp    $0x0,%eax
jne    11a5 <open_log_file+0x25>
movq   $0x0,-0x8(%rbp)
jmp    1303 <open_log_file+0x183>
mov    0x2e2c(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x40(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x18(%rbp)
call   1030 <get_log_file_path@plt>
mov    -0x40(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1080 <stat@plt>
mov    %eax,-0x24(%rbp)
mov    $0xffffffff,%eax
cmp    -0x24(%rbp),%eax
jne    1271 <open_log_file+0xf1>
mov    0x2da5(%rip),%rax
mov    (%rax),%rax
mov    0x2dbb(%rip),%rcx
cmp    (%rcx),%rax
jne    1267 <open_log_file+0xe7>
mov    -0x40(%rbp),%rdi
mov    0x2d7f(%rip),%rax
mov    (%rax),%esi
mov    0x2dd6(%rip),%rax
or     (%rax),%esi
mov    0x2d85(%rip),%rax
mov    (%rax),%edx
mov    0x2dac(%rip),%rax
or     (%rax),%edx
mov    0x2d7b(%rip),%rax
or     (%rax),%edx
call   1090 <open@plt>
mov    %eax,-0x24(%rbp)
mov    $0xffffffff,%eax
cmp    -0x24(%rbp),%eax
jne    125a <open_log_file+0xda>
jmp    12aa <open_log_file+0x12a>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
jmp    126c <open_log_file+0xec>
jmp    12aa <open_log_file+0x12a>
jmp    1271 <open_log_file+0xf1>
mov    -0x40(%rbp),%rdi
lea    0xd84(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x30(%rbp)
xor    %eax,%eax
cmp    -0x30(%rbp),%rax
jne    1296 <open_log_file+0x116>
jmp    12aa <open_log_file+0x12a>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    12fc <open_log_file+0x17c>
mov    0x2d17(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    0x2d03(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
mov    0x2cf5(%rip),%rax
mov    (%rax),%rdi
call   1040 <edesc@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rdx
mov    -0x40(%rbp),%r8
mov    %rax,%rcx
lea    0xd1b(%rip),%rsi
call   1070 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_log_file>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <ensure_data_dir_exists@plt>
test   %eax,%eax
je     1285 <open_log_file+0x105>
mov    %rsp,%r12
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r14
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
mov    %r14,%rdi
call   1030 <get_log_file_path@plt>
lea    -0x28(%rbp),%rsi
mov    %r14,%rdi
call   1080 <stat@plt>
cmp    $0xffffffff,%eax
je     11f3 <open_log_file+0x73>
lea    0xe23(%rip),%rsi
mov    %r14,%rdi
call   10a0 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1282 <open_log_file+0x102>
jmp    124c <open_log_file+0xcc>
mov    0x2da6(%rip),%rax
mov    (%rax),%rax
mov    0x2dbc(%rip),%rcx
cmp    (%rcx),%rax
jne    124c <open_log_file+0xcc>
mov    0x2d88(%rip),%rax
mov    0x2de1(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d8e(%rip),%rax
mov    0x2db7(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d84(%rip),%rax
or     (%rax),%edx
mov    %r14,%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     124c <open_log_file+0xcc>
mov    %eax,%edi
call   1050 <close@plt>
jmp    11d6 <open_log_file+0x56>
mov    0x2d75(%rip),%rax
mov    (%rax),%r15d
mov    0x2d63(%rip),%rax
mov    (%rax),%rbx
mov    %rbx,%rdi
call   1040 <edesc@plt>
lea    0xd94(%rip),%rsi
mov    %r15d,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
mov    %r14,%r8
call   1070 <fprintf@plt>
xor    %ebx,%ebx
mov    %r12,%rsp
mov    %rbx,%rax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@FILENAME_MAX = global i32 0
@ENOENT = global i64 0
@errno = global i64 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@.str = [3 x i8] c"r+\00"
@stderr = global i32 0
@.str.1 = [26 x i8] c"Error %d (%s) opening %s\0A\00"
define i32* @open_log_file() {
%1 = alloca i32*
%2 = alloca i8*
%3 = alloca i64
%4 = alloca %struct.stat
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = call i32 (...) @ensure_data_dir_exists()
%9 = icmp ne i32 %8, 0
br i1 %9, label %11, label %10
10: ; preds = %0
store i32* null, i32** %1
br label %58
11: ; preds = %0
%12 = load i32, i32* @FILENAME_MAX
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %2
%15 = alloca i8, i64 %13
store i64 %13, i64* %3
%16 = call i32 @get_log_file_path(i8* %15)
%17 = call i32 @stat(i8* %15, %struct.stat* %4)
store i32 %17, i32* %5
%18 = load i32, i32* %5
%19 = icmp eq i32 -1, %18
br i1 %19, label %20, label %43
20: ; preds = %11
%21 = load i64, i64* @ENOENT
%22 = load i64, i64* @errno
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %41
24: ; preds = %20
%25 = load i32, i32* @O_WRONLY
%26 = load i32, i32* @O_CREAT
%27 = or i32 %25, %26
%28 = load i32, i32* @S_IRUSR
%29 = load i32, i32* @S_IWUSR
%30 = or i32 %28, %29
%31 = load i32, i32* @S_IRGRP
%32 = or i32 %30, %31
%33 = call i32 @open(i8* %15, i32 %27, i32 %32)
store i32 %33, i32* %5
%34 = load i32, i32* %5
%35 = icmp eq i32 -1, %34
br i1 %35, label %36, label %37
36: ; preds = %24
br label %50
37: ; preds = %24
%38 = load i32, i32* %5
%39 = call i32 @close(i32 %38)
br label %40
40: ; preds = %37
br label %42
41: ; preds = %20
br label %50
42: ; preds = %40
br label %43
43: ; preds = %42, %11
%44 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %44, i32** %6
%45 = load i32*, i32** %6
%46 = icmp eq i32* null, %45
br i1 %46, label %47, label %48
47: ; preds = %43
br label %50
48: ; preds = %43
%49 = load i32*, i32** %6
store i32* %49, i32** %1
store i32 1, i32* %7
br label %56
50: ; preds = %47, %41, %36
%51 = load i32, i32* @stderr
%52 = load i64, i64* @errno
%53 = load i64, i64* @errno
%54 = call i8* @edesc(i64 %53)
%55 = call i32 @fprintf(i32 %51, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i64 %52, i8* %54, i8* %15)
store i32* null, i32** %1
store i32 1, i32* %7
br label %56
56: ; preds = %50, %48
%57 = load i8*, i8** %2
br label %58
58: ; preds = %56, %10
%59 = load i32*, i32** %1
ret i32* %59
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@FILENAME_MAX = global i32 0
@ENOENT = global i64 0
@errno = global i64 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@.str = [3 x i8] c"r+\00"
@stderr = global i32 0
@.str.1 = [26 x i8] c"Error %d (%s) opening %s\0A\00"
define noalias i32* @open_log_file() {
%1 = alloca %struct.stat
%2 = tail call i32 (...) @ensure_data_dir_exists()
%3 = icmp eq i32 %2, 0
br i1 %3, label %40, label %4
4: ; preds = %0
%5 = load i32, i32* @FILENAME_MAX
%6 = zext i32 %5 to i64
%7 = tail call i8* @llvm.stacksave()
%8 = alloca i8, i64 %6
%9 = call i32 @get_log_file_path(i8* %8)
%10 = bitcast %struct.stat* %1 to i8*
%11 = call i32 @stat(i8* %8, %struct.stat* %1)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %30
13: ; preds = %4
%14 = load i64, i64* @ENOENT
%15 = load i64, i64* @errno
%16 = icmp eq i64 %14, %15
br i1 %16, label %17, label %33
17: ; preds = %13
%18 = load i32, i32* @O_WRONLY
%19 = load i32, i32* @O_CREAT
%20 = or i32 %19, %18
%21 = load i32, i32* @S_IRUSR
%22 = load i32, i32* @S_IWUSR
%23 = or i32 %22, %21
%24 = load i32, i32* @S_IRGRP
%25 = or i32 %23, %24
%26 = call i32 @open(i8* %8, i32 %20, i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %33, label %28
28: ; preds = %17
%29 = call i32 @close(i32 %26)
br label %30
30: ; preds = %28, %4
%31 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%32 = icmp eq i32* %31, null
br i1 %32, label %33, label %38
33: ; preds = %30, %13, %17
%34 = load i32, i32* @stderr
%35 = load i64, i64* @errno
%36 = call i8* @edesc(i64 %35)
%37 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i64 %35, i8* %36, i8* %8)
br label %38
38: ; preds = %30, %33
%39 = phi i32* [ null, %33 ], [ %31, %30 ]
br label %40
40: ; preds = %0, %38
%41 = phi i32* [ %39, %38 ], [ null, %0 ]
ret i32* %41
}