#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 /*<<< orphan*/ *fopen(char const *, char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
int /*<<< orphan*/ warn(char *);
FILE *ensure_open_file(char const *filename) {
  FILE *file;
  if ((file = fopen(filename, "r")) == NULL) {
    char *buff = (char *)malloc(128);
    snprintf(buff, 128,
             "open file %s failed, file may not exist or permission denied.",
             filename);
    warn(buff);
    return (FILE *)0;
  }
  return file;
}


x86 assembly (clang, -O0):
<ensure_open_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea9(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11a7 <ensure_open_file+0x67>
mov    $0x80,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x80,%esi
lea    0xe76(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <warn@plt>
movq   $0x0,-0x8(%rbp)
jmp    11af <ensure_open_file+0x6f>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ensure_open_file>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
lea    0xeb1(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1188 <ensure_open_file+0x48>
mov    $0x80,%edi
call   1050 <malloc@plt>
mov    %rax,%r15
lea    0xe92(%rip),%rdx
mov    %rax,%rdi
mov    $0x80,%esi
mov    %r14,%rcx
call   1040 <snprintf@plt>
mov    %r15,%rdi
call   1030 <warn@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [62 x i8] c"open file %s failed, file may not exist or permission denied.\00"
define i32* @ensure_open_file(i8* %0) {
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %4
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %17
9: ; preds = %1
%10 = call i64 @malloc(i32 128)
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %5
%12 = load i8*, i8** %5
%13 = load i8*, i8** %3
%14 = call i32 @snprintf(i8* %12, i32 128, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%15 = load i8*, i8** %5
%16 = call i32 @warn(i8* %15)
store i32* null, i32** %2
br label %19
17: ; preds = %1
%18 = load i32*, i32** %4
store i32* %18, i32** %2
br label %19
19: ; preds = %17, %9
%20 = load i32*, i32** %2
ret i32* %20
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [62 x i8] c"open file %s failed, file may not exist or permission denied.\00"
define noalias i32* @ensure_open_file(i8* %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %9
4: ; preds = %1
%5 = tail call i64 @malloc(i32 128)
%6 = inttoptr i64 %5 to i8*
%7 = tail call i32 @snprintf(i8* %6, i32 128, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%8 = tail call i32 @warn(i8* %6)
br label %9
9: ; preds = %1, %4
ret i32* %2
}