#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 */
scalar_t__ ENOENT;
scalar_t__ errno;
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
FILE *open_create_if_not_exist(const char *file) {
  errno = 0;
  FILE *fp = fopen(file, "r+");
  if (fp == NULL) {
    fprintf(stderr, "%s; fopen: %s\n", file, strerror(errno));
    if (errno == ENOENT) {
      errno = 0;
      fp = fopen(file, "w+");
      if (fp == NULL)
        fprintf(stderr, "can't create file: %s; fopen: %s\n", file,
                strerror(errno));
    }
  }
  return fp;
}


x86 assembly (clang, -O0):
<open_create_if_not_exist>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e95(%rip),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
lea    0xeab(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    122c <open_create_if_not_exist+0xfc>
mov    0x2e70(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e54(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rdx
mov    %rax,%rcx
lea    0xe7c(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2e2f(%rip),%rax
mov    (%rax),%rax
mov    0x2e15(%rip),%rcx
cmp    (%rcx),%rax
jne    1227 <open_create_if_not_exist+0xf7>
mov    0x2e15(%rip),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
lea    0xe2e(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1222 <open_create_if_not_exist+0xf2>
mov    0x2df0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2dd4(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rdx
mov    %rax,%rcx
lea    0xde9(%rip),%rsi
call   1030 <fprintf@plt>
jmp    1227 <open_create_if_not_exist+0xf7>
jmp    122c <open_create_if_not_exist+0xfc>
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_create_if_not_exist>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e97(%rip),%r14
movq   $0x0,(%r14)
lea    0xeb1(%rip),%rsi
call   1040 <fopen@plt>
test   %rax,%rax
jne    11ca <open_create_if_not_exist+0x9a>
mov    0x2e80(%rip),%r15
mov    (%r15),%ebp
mov    (%r14),%rdi
call   1050 <strerror@plt>
lea    0xea7(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1030 <fprintf@plt>
mov    (%r14),%rax
mov    0x2e3f(%rip),%rcx
cmp    (%rcx),%rax
jne    11c8 <open_create_if_not_exist+0x98>
movq   $0x0,(%r14)
lea    0xe67(%rip),%rsi
mov    %rbx,%rdi
call   1040 <fopen@plt>
test   %rax,%rax
jne    11ca <open_create_if_not_exist+0x9a>
mov    (%r15),%ebp
mov    (%r14),%rdi
call   1050 <strerror@plt>
lea    0xe4b(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1030 <fprintf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@.str = [3 x i8] c"r+\00"
@stderr = global i32 0
@.str.1 = [15 x i8] c"%s; fopen: %s\0A\00"
@ENOENT = global i64 0
@.str.2 = [3 x i8] c"w+\00"
@.str.3 = [34 x i8] c"can't create file: %s; fopen: %s\0A\00"
define i32* @open_create_if_not_exist(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
store i8* %0, i8** %2
store i64 0, i64* @errno
%4 = load i8*, i8** %2
%5 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %5, i32** %3
%6 = load i32*, i32** %3
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %30
8: ; preds = %1
%9 = load i32, i32* @stderr
%10 = load i8*, i8** %2
%11 = load i64, i64* @errno
%12 = call i8* @strerror(i64 %11)
%13 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %10, i8* %12)
%14 = load i64, i64* @errno
%15 = load i64, i64* @ENOENT
%16 = icmp eq i64 %14, %15
br i1 %16, label %17, label %29
17: ; preds = %8
store i64 0, i64* @errno
%18 = load i8*, i8** %2
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %19, i32** %3
%20 = load i32*, i32** %3
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %28
22: ; preds = %17
%23 = load i32, i32* @stderr
%24 = load i8*, i8** %2
%25 = load i64, i64* @errno
%26 = call i8* @strerror(i64 %25)
%27 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %24, i8* %26)
br label %28
28: ; preds = %22, %17
br label %29
29: ; preds = %28, %8
br label %30
30: ; preds = %29, %1
%31 = load i32*, i32** %3
ret i32* %31
}

LLVM IR (-O2):
@errno = global i64 0
@.str = [3 x i8] c"r+\00"
@stderr = global i32 0
@.str.1 = [15 x i8] c"%s; fopen: %s\0A\00"
@ENOENT = global i64 0
@.str.2 = [3 x i8] c"w+\00"
@.str.3 = [34 x i8] c"can't create file: %s; fopen: %s\0A\00"
define noalias i32* @open_create_if_not_exist(i8* %0) {
store i64 0, i64* @errno
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %20
4: ; preds = %1
%5 = load i32, i32* @stderr
%6 = load i64, i64* @errno
%7 = tail call i8* @strerror(i64 %6)
%8 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %7)
%9 = load i64, i64* @errno
%10 = load i64, i64* @ENOENT
%11 = icmp eq i64 %9, %10
br i1 %11, label %12, label %20
12: ; preds = %4
store i64 0, i64* @errno
%13 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %20
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = load i64, i64* @errno
%18 = tail call i8* @strerror(i64 %17)
%19 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %18)
br label %20
20: ; preds = %4, %15, %12, %1
%21 = phi i32* [ null, %15 ], [ %13, %12 ], [ null, %4 ], [ %2, %1 ]
ret i32* %21
}