#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*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
scalar_t__ freadline(int /*<<< orphan*/ *, char **, size_t *, size_t *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ *stdin;
scalar_t__ strcmp(char const *, char *);
void load_file(const char *path, char **buf, size_t *blen, size_t *bsize) {
  FILE *fh = stdin;
  if (strcmp(path, "-") != 0 && (fh = fopen(path, "r")) == NULL) {
    fprintf(stderr, "Cannot read file: %s\n", path);
    exit(-1);
  }
  while (freadline(fh, buf, blen, bsize) > 0) {
  }
  if (strcmp(path, "-") != 0)
    fclose(fh);
}


x86 assembly (clang, -O0):
<load_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    0x2e69(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe6f(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
je     11e1 <load_file+0x81>
mov    -0x8(%rbp),%rdi
lea    0xe57(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11e1 <load_file+0x81>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe32(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
jmp    11e6 <load_file+0x86>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1060 <freadline@plt>
cmp    $0x0,%rax
jle    120a <load_file+0xaa>
jmp    11e6 <load_file+0x86>
mov    -0x8(%rbp),%rdi
lea    0xdeb(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
je     122d <load_file+0xcd>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_file>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rcx,%r15
mov    %rdx,%r12
mov    %rsi,%r13
mov    %rdi,%r14
mov    0x2e6c(%rip),%rax
mov    (%rax),%rbx
lea    0xe7a(%rip),%rsi
call   1040 <strcmp@plt>
test   %rax,%rax
je     11b0 <load_file+0x50>
lea    0xe6b(%rip),%rsi
mov    %r14,%rdi
call   1070 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11f5 <load_file+0x95>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %r12,%rdx
mov    %r15,%rcx
call   1060 <freadline@plt>
test   %rax,%rax
jg     11b0 <load_file+0x50>
lea    0xe33(%rip),%rsi
mov    %r14,%rdi
call   1040 <strcmp@plt>
test   %rax,%rax
je     11eb <load_file+0x8b>
mov    %rbx,%rdi
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1030 <fclose@plt>
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
lea    0xdff(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stdin = global i32* null
@.str = [2 x i8] c"-\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [22 x i8] c"Cannot read file: %s\0A\00"
define void @load_file(i8* %0, i8** %1, i64* %2, i64* %3) {
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i64*
%8 = alloca i64*
%9 = alloca i32*
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i64* %2, i64** %7
store i64* %3, i64** %8
%10 = load i32*, i32** @stdin
store i32* %10, i32** %9
%11 = load i8*, i8** %5
%12 = call i64 @strcmp(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %23
14: ; preds = %4
%15 = load i8*, i8** %5
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %16, i32** %9
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %23
18: ; preds = %14
%19 = load i32, i32* @stderr
%20 = load i8*, i8** %5
%21 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %20)
%22 = call i32 @exit(i32 -1)
unreachable
23: ; preds = %14, %4
br label %24
24: ; preds = %31, %23
%25 = load i32*, i32** %9
%26 = load i8**, i8*** %6
%27 = load i64*, i64** %7
%28 = load i64*, i64** %8
%29 = call i64 @freadline(i32* %25, i8** %26, i64* %27, i64* %28)
%30 = icmp sgt i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %24
br label %24
32: ; preds = %24
%33 = load i8*, i8** %5
%34 = call i64 @strcmp(i8* %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %32
%37 = load i32*, i32** %9
%38 = call i32 @fclose(i32* %37)
br label %39
39: ; preds = %36, %32
ret void
}

LLVM IR (-O2):
@stdin = global i32* null
@.str = [2 x i8] c"-\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [22 x i8] c"Cannot read file: %s\0A\00"
define void @load_file(i8* %0, i8** %1, i64* %2, i64* %3) {
%5 = load i32*, i32** @stdin
%6 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i64 %6, 0
br i1 %7, label %15, label %8
8: ; preds = %4
%9 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %15
11: ; preds = %8
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%14 = tail call i32 @exit(i32 -1)
unreachable
15: ; preds = %8, %4
%16 = phi i32* [ %9, %8 ], [ %5, %4 ]
br label %17
17: ; preds = %17, %15
%18 = tail call i64 @freadline(i32* %16, i8** %1, i64* %2, i64* %3)
%19 = icmp sgt i64 %18, 0
br i1 %19, label %17, label %20
20: ; preds = %17
%21 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%22 = icmp eq i64 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %20
%24 = tail call i32 @fclose(i32* %16)
br label %25
25: ; preds = %23, %20
ret void
}