#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;
typedef int /*<<< orphan*/ BIO;

/* Variables and functions */
int /*<<< orphan*/ BIO_CLOSE;
int /*<<< orphan*/ BIO_F_BIO_NEW_FILE;
int /*<<< orphan*/ BIO_R_NO_SUCH_FILE;
int /*<<< orphan*/ *BIO_new(int /*<<< orphan*/);
int /*<<< orphan*/ BIO_s_file();
int /*<<< orphan*/ BIO_set_fp(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                              int /*<<< orphan*/);
int /*<<< orphan*/ BIOerr(int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ ENOENT;
int /*<<< orphan*/ ERR_R_SYS_LIB;
int /*<<< orphan*/ ERR_asprintf_error_data(char *, char const *, char const *);
int /*<<< orphan*/ SYS_F_FOPEN;
int /*<<< orphan*/ SYSerr(int /*<<< orphan*/, scalar_t__);
scalar_t__ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char const *);
BIO *BIO_new_file(const char *filename, const char *mode) {
  BIO *ret;
  FILE *file = NULL;
  file = fopen(filename, mode);
  if (file == NULL) {
    SYSerr(SYS_F_FOPEN, errno);
    ERR_asprintf_error_data("fopen('%s', '%s')", filename, mode);
    if (errno == ENOENT)
      BIOerr(BIO_F_BIO_NEW_FILE, BIO_R_NO_SUCH_FILE);
    else
      BIOerr(BIO_F_BIO_NEW_FILE, ERR_R_SYS_LIB);
    return (NULL);
  }
  if ((ret = BIO_new(BIO_s_file())) == NULL) {
    fclose(file);
    return (NULL);
  }
  BIO_set_fp(ret, file, BIO_CLOSE);
  return (ret);
}


x86 assembly (clang, -O0):
<BIO_new_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    123a <BIO_new_file+0xba>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
mov    0x2e04(%rip),%rax
mov    (%rax),%rsi
call   1040 <SYSerr@plt>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe25(%rip),%rdi
call   1030 <ERR_asprintf_error_data@plt>
mov    0x2de1(%rip),%rax
mov    (%rax),%rax
mov    0x2db7(%rip),%rcx
cmp    (%rcx),%rax
jne    1216 <BIO_new_file+0x96>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
call   1060 <BIOerr@plt>
jmp    122d <BIO_new_file+0xad>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
call   1060 <BIOerr@plt>
movq   $0x0,-0x8(%rbp)
jmp    128a <BIO_new_file+0x10a>
mov    $0x0,%al
call   10a0 <BIO_s_file@plt>
mov    %eax,%edi
call   1070 <BIO_new@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    126c <BIO_new_file+0xec>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    128a <BIO_new_file+0x10a>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    0x2d3d(%rip),%rax
mov    (%rax),%edx
call   1080 <BIO_set_fp@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BIO_new_file>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r15
mov    %rdi,%rbx
call   1090 <fopen@plt>
test   %rax,%rax
je     11c4 <BIO_new_file+0x44>
mov    %rax,%r14
xor    %eax,%eax
call   10a0 <BIO_s_file@plt>
mov    %eax,%edi
call   1070 <BIO_new@plt>
test   %rax,%rax
je     120f <BIO_new_file+0x8f>
mov    %rax,%rbx
mov    0x2e03(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <BIO_set_fp@plt>
jmp    1229 <BIO_new_file+0xa9>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
mov    0x2df4(%rip),%r14
mov    (%r14),%rsi
call   1040 <SYSerr@plt>
lea    0xe1d(%rip),%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1030 <ERR_asprintf_error_data@plt>
mov    (%r14),%rax
mov    0x2db0(%rip),%rcx
mov    0x2dc1(%rip),%rdx
mov    (%rdx),%edi
cmp    (%rcx),%rax
jne    1219 <BIO_new_file+0x99>
mov    0x2ddb(%rip),%rax
jmp    1220 <BIO_new_file+0xa0>
mov    %r14,%rdi
call   1050 <fclose@plt>
jmp    1227 <BIO_new_file+0xa7>
mov    0x2db8(%rip),%rax
mov    (%rax),%esi
call   1060 <BIOerr@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@SYS_F_FOPEN = global i32 0
@errno = global i64 0
@.str = [18 x i8] c"fopen('%s', '%s')\00"
@ENOENT = global i64 0
@BIO_F_BIO_NEW_FILE = global i32 0
@BIO_R_NO_SUCH_FILE = global i32 0
@ERR_R_SYS_LIB = global i32 0
@BIO_CLOSE = global i32 0
define i32* @BIO_new_file(i8* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32* null, i32** %7
%8 = load i8*, i8** %4
%9 = load i8*, i8** %5
%10 = call i32* @fopen(i8* %8, i8* %9)
store i32* %10, i32** %7
%11 = load i32*, i32** %7
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %32
13: ; preds = %2
%14 = load i32, i32* @SYS_F_FOPEN
%15 = load i64, i64* @errno
%16 = call i32 @SYSerr(i32 %14, i64 %15)
%17 = load i8*, i8** %4
%18 = load i8*, i8** %5
%19 = call i32 @ERR_asprintf_error_data(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %17, i8* %18)
%20 = load i64, i64* @errno
%21 = load i64, i64* @ENOENT
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %27
23: ; preds = %13
%24 = load i32, i32* @BIO_F_BIO_NEW_FILE
%25 = load i32, i32* @BIO_R_NO_SUCH_FILE
%26 = call i32 @BIOerr(i32 %24, i32 %25)
br label %31
27: ; preds = %13
%28 = load i32, i32* @BIO_F_BIO_NEW_FILE
%29 = load i32, i32* @ERR_R_SYS_LIB
%30 = call i32 @BIOerr(i32 %28, i32 %29)
br label %31
31: ; preds = %27, %23
store i32* null, i32** %3
br label %45
32: ; preds = %2
%33 = call i32 (...) @BIO_s_file()
%34 = call i32* @BIO_new(i32 %33)
store i32* %34, i32** %6
%35 = icmp eq i32* %34, null
br i1 %35, label %36, label %39
36: ; preds = %32
%37 = load i32*, i32** %7
%38 = call i32 @fclose(i32* %37)
store i32* null, i32** %3
br label %45
39: ; preds = %32
%40 = load i32*, i32** %6
%41 = load i32*, i32** %7
%42 = load i32, i32* @BIO_CLOSE
%43 = call i32 @BIO_set_fp(i32* %40, i32* %41, i32 %42)
%44 = load i32*, i32** %6
store i32* %44, i32** %3
br label %45
45: ; preds = %39, %36, %31
%46 = load i32*, i32** %3
ret i32* %46
}

LLVM IR (-O2):
@SYS_F_FOPEN = global i32 0
@errno = global i64 0
@.str = [18 x i8] c"fopen('%s', '%s')\00"
@ENOENT = global i64 0
@BIO_F_BIO_NEW_FILE = global i32 0
@BIO_R_NO_SUCH_FILE = global i32 0
@ERR_R_SYS_LIB = global i32 0
@BIO_CLOSE = global i32 0
define i32* @BIO_new_file(i8* %0, i8* %1) {
%3 = tail call i32* @fopen(i8* %0, i8* %1)
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %20
5: ; preds = %2
%6 = load i32, i32* @SYS_F_FOPEN
%7 = load i64, i64* @errno
%8 = tail call i32 @SYSerr(i32 %6, i64 %7)
%9 = tail call i32 @ERR_asprintf_error_data(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%10 = load i64, i64* @errno
%11 = load i64, i64* @ENOENT
%12 = icmp eq i64 %10, %11
%13 = load i32, i32* @BIO_F_BIO_NEW_FILE
br i1 %12, label %14, label %17
14: ; preds = %5
%15 = load i32, i32* @BIO_R_NO_SUCH_FILE
%16 = tail call i32 @BIOerr(i32 %13, i32 %15)
br label %29
17: ; preds = %5
%18 = load i32, i32* @ERR_R_SYS_LIB
%19 = tail call i32 @BIOerr(i32 %13, i32 %18)
br label %29
20: ; preds = %2
%21 = tail call i32 (...) @BIO_s_file()
%22 = tail call i32* @BIO_new(i32 %21)
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = tail call i32 @fclose(i32* %3)
br label %29
26: ; preds = %20
%27 = load i32, i32* @BIO_CLOSE
%28 = tail call i32 @BIO_set_fp(i32* %22, i32* %3, i32 %27)
br label %29
29: ; preds = %14, %17, %26, %24
%30 = phi i32* [ null, %24 ], [ %22, %26 ], [ null, %17 ], [ null, %14 ]
ret i32* %30
}