#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 char FILE;

/* Variables and functions */
int errno;
char *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
char *strerror(int);
int open_file(const char *path, FILE **file) {
  int result = 0;
  if (!path || !file) {
    fprintf(stderr, "[ERROR] Invalid open file parameter\n");
    result = 1;
    goto exit;
  }
  *file = fopen(path, "r");
  if (!*file) {
    fprintf(stderr, "[ERROR] Failed to open input file: %s: %s\n", path,
            strerror(errno));
    result = errno;
    goto exit;
  }
  fprintf(stdout, "[INIT] %s opened on 0x%p\n", path, *file);
exit:
  return result;
}


x86 assembly (clang, -O0):
<open_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     115d <open_file+0x2d>
cmpq   $0x0,-0x10(%rbp)
jne    1180 <open_file+0x50>
mov    0x2e7c(%rip),%rax
mov    (%rax),%edi
lea    0xe93(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0x1,-0x14(%rbp)
jmp    1215 <open_file+0xe5>
mov    -0x8(%rbp),%rdi
lea    0xe9a(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11f3 <open_file+0xc3>
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rdx
mov    %rax,%rcx
lea    0xe4c(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
jmp    1215 <open_file+0xe5>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
lea    0xe44(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_file>:
push   %rbp
push   %r14
push   %rbx
test   %rdi,%rdi
je     117a <open_file+0x4a>
mov    %rsi,%rbp
test   %rsi,%rsi
je     117a <open_file+0x4a>
mov    %rdi,%rbx
lea    0xeda(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,0x0(%rbp)
test   %rax,%rax
je     1198 <open_file+0x68>
mov    0x2e70(%rip),%rcx
mov    (%rcx),%edi
lea    0xee9(%rip),%rsi
xor    %ebp,%ebp
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1030 <fprintf@plt>
jmp    11c9 <open_file+0x99>
mov    0x2e5f(%rip),%rax
mov    (%rax),%edi
lea    0xe76(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0x1,%ebp
jmp    11c9 <open_file+0x99>
mov    0x2e41(%rip),%rax
mov    (%rax),%ebp
mov    0x2e30(%rip),%r14
mov    (%r14),%edi
call   1050 <strerror@plt>
lea    0xe70(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    (%r14),%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [37 x i8] c"[ERROR] Invalid open file parameter\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [43 x i8] c"[ERROR] Failed to open input file: %s: %s\0A\00"
@errno = global i32 0
@stdout = global i32 0
@.str.3 = [26 x i8] c"[INIT] %s opened on 0x%p\0A\00"
define i32 @open_file(i8* %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i8**
%5 = alloca i32
store i8* %0, i8** %3
store i8** %1, i8*** %4
store i32 0, i32* %5
%6 = load i8*, i8** %3
%7 = icmp ne i8* %6, null
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = load i8**, i8*** %4
%10 = icmp ne i8** %9, null
br i1 %10, label %14, label %11
11: ; preds = %8, %2
%12 = load i32, i32* @stderr
%13 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %5
br label %34
14: ; preds = %8
%15 = load i8*, i8** %3
%16 = call i8* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%17 = load i8**, i8*** %4
store i8* %16, i8** %17
%18 = load i8**, i8*** %4
%19 = load i8*, i8** %18
%20 = icmp ne i8* %19, null
br i1 %20, label %28, label %21
21: ; preds = %14
%22 = load i32, i32* @stderr
%23 = load i8*, i8** %3
%24 = load i32, i32* @errno
%25 = call i8* @strerror(i32 %24)
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i8* %23, i8* %25)
%27 = load i32, i32* @errno
store i32 %27, i32* %5
br label %34
28: ; preds = %14
%29 = load i32, i32* @stdout
%30 = load i8*, i8** %3
%31 = load i8**, i8*** %4
%32 = load i8*, i8** %31
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %30, i8* %32)
br label %34
34: ; preds = %28, %21, %11
%35 = load i32, i32* %5
ret i32 %35
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [37 x i8] c"[ERROR] Invalid open file parameter\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [43 x i8] c"[ERROR] Failed to open input file: %s: %s\0A\00"
@errno = global i32 0
@stdout = global i32 0
@.str.3 = [26 x i8] c"[INIT] %s opened on 0x%p\0A\00"
define i32 @open_file(i8* %0, i8** writeonly %1) {
%3 = icmp ne i8* %0, null
%4 = icmp ne i8** %1, null
%5 = and i1 %3, %4
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
br label %21
9: ; preds = %2
%10 = tail call i8* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %10, i8** %1
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %18
12: ; preds = %9
%13 = load i32, i32* @stderr
%14 = load i32, i32* @errno
%15 = tail call i8* @strerror(i32 %14)
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %15)
%17 = load i32, i32* @errno
br label %21
18: ; preds = %9
%19 = load i32, i32* @stdout
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %10)
br label %21
21: ; preds = %18, %12, %6
%22 = phi i32 [ 0, %18 ], [ %17, %12 ], [ 1, %6 ]
ret i32 %22
}