#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 errno;
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int /*<<< orphan*/ stderr;
int open_file(char *filename, char *mode, FILE **out_fp) {
  FILE *fp;
  fp = fopen(filename, mode);
  if (fp == NULL) {
    fprintf(stderr, "Can't open file '%s` with mode \"%s\"\n", filename, mode);
    return -errno;
  }
  *out_fp = fp;
  return 0;
}


x86 assembly (clang, -O0):
<open_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1180 <open_file+0x60>
mov    0x2e89(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xe98(%rip),%rsi
call   1030 <fprintf@plt>
xor    %eax,%eax
mov    0x2e62(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1192 <open_file+0x72>
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_file>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
call   1040 <fopen@plt>
test   %rax,%rax
je     113f <open_file+0x1f>
mov    %rax,(%r15)
xor    %eax,%eax
jmp    1165 <open_file+0x45>
mov    0x2e9a(%rip),%rax
mov    (%rax),%edi
lea    0xeb1(%rip),%rsi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1030 <fprintf@plt>
mov    0x2e77(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [37 x i8] c"Can't open file '%s` with mode \22%s\22\0A\00"
@errno = global i32 0
define i32 @open_file(i8* %0, i8* %1, i32** %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32**
%8 = alloca i32*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32** %2, i32*** %7
%9 = load i8*, i8** %5
%10 = load i8*, i8** %6
%11 = call i32* @fopen(i8* %9, i8* %10)
store i32* %11, i32** %8
%12 = load i32*, i32** %8
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %21
14: ; preds = %3
%15 = load i32, i32* @stderr
%16 = load i8*, i8** %5
%17 = load i8*, i8** %6
%18 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %16, i8* %17)
%19 = load i32, i32* @errno
%20 = sub nsw i32 0, %19
store i32 %20, i32* %4
br label %24
21: ; preds = %3
%22 = load i32*, i32** %8
%23 = load i32**, i32*** %7
store i32* %22, i32** %23
store i32 0, i32* %4
br label %24
24: ; preds = %21, %14
%25 = load i32, i32* %4
ret i32 %25
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [37 x i8] c"Can't open file '%s` with mode \22%s\22\0A\00"
@errno = global i32 0
define i32 @open_file(i8* %0, i8* %1, i32** nocapture writeonly %2) {
%4 = tail call i32* @fopen(i8* %0, i8* %1)
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %11
6: ; preds = %3
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%9 = load i32, i32* @errno
%10 = sub nsw i32 0, %9
br label %12
11: ; preds = %3
store i32* %4, i32** %2
br label %12
12: ; preds = %11, %6
%13 = phi i32 [ %10, %6 ], [ 0, %11 ]
ret i32 %13
}