#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*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int parse_buffer(char *, size_t);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ *stdin;
int /*<<< orphan*/ strcmp(char *, char const *);
int parse_file(const char *filename) {
  FILE *fp;
  if (!strcmp("-", filename))
    fp = stdin;
  else
    fp = fopen(filename, "rb");
  if (!fp) {
    perror("fopen");
    return 1;
  }
  char buf[1024];
  size_t ret = fread(buf, 1, 1024, fp);
  if (ferror(fp)) {
    perror("read");
    fclose(fp);
    return 1;
  }
  fclose(fp);
  return parse_buffer(buf, ret);
}


x86 assembly (clang, -O0):
<parse_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe76(%rip),%rdi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    11ab <parse_file+0x3b>
mov    0x2e49(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    11bf <parse_file+0x4f>
mov    -0x10(%rbp),%rdi
lea    0xe4c(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11e2 <parse_file+0x72>
lea    0xe34(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1256 <parse_file+0xe6>
lea    -0x420(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x400,%edx
call   1040 <fread@plt>
mov    %rax,-0x428(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     1237 <parse_file+0xc7>
lea    0xdee(%rip),%rdi
call   1090 <perror@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    1256 <parse_file+0xe6>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
lea    -0x420(%rbp),%rdi
mov    -0x428(%rbp),%rsi
call   1070 <parse_buffer@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_file>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %rdi,%rbx
lea    0xe7b(%rip),%rdi
mov    %rbx,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     11f5 <parse_file+0x85>
lea    0xe6a(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
mov    %rax,%rbx
test   %rbx,%rbx
je     1204 <parse_file+0x94>
mov    %rsp,%rdi
mov    $0x1,%ebp
mov    $0x1,%esi
mov    $0x400,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
jne    1217 <parse_file+0xa7>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %rsp,%rdi
mov    %r14,%rsi
call   1070 <parse_buffer@plt>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2dec(%rip),%rax
mov    (%rax),%rbx
test   %rbx,%rbx
jne    11a8 <parse_file+0x38>
lea    0xdfa(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%ebp
jmp    11e7 <parse_file+0x77>
lea    0xded(%rip),%rdi
call   1090 <perror@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    11e7 <parse_file+0x77>

LLVM IR (-O0):
@.str = [2 x i8] c"-\00"
@stdin = global i32* null
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [6 x i8] c"fopen\00"
@.str.3 = [5 x i8] c"read\00"
define i32 @parse_file(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca [1024 x i8]
%6 = alloca i64
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i32 @strcmp(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = icmp ne i32 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %1
%11 = load i32*, i32** @stdin
store i32* %11, i32** %4
br label %15
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %14, i32** %4
br label %15
15: ; preds = %12, %10
%16 = load i32*, i32** %4
%17 = icmp ne i32* %16, null
br i1 %17, label %20, label %18
18: ; preds = %15
%19 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %2
br label %37
20: ; preds = %15
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%22 = load i32*, i32** %4
%23 = call i64 @fread(i8* %21, i32 1, i32 1024, i32* %22)
store i64 %23, i64* %6
%24 = load i32*, i32** %4
%25 = call i64 @ferror(i32* %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %20
%28 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%29 = load i32*, i32** %4
%30 = call i32 @fclose(i32* %29)
store i32 1, i32* %2
br label %37
31: ; preds = %20
%32 = load i32*, i32** %4
%33 = call i32 @fclose(i32* %32)
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%35 = load i64, i64* %6
%36 = call i32 @parse_buffer(i8* %34, i64 %35)
store i32 %36, i32* %2
br label %37
37: ; preds = %31, %27, %18
%38 = load i32, i32* %2
ret i32 %38
}

LLVM IR (-O2):
@.str = [2 x i8] c"-\00"
@stdin = global i32* null
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [6 x i8] c"fopen\00"
@.str.3 = [5 x i8] c"read\00"
define i32 @parse_file(i8* nocapture %0) {
%2 = alloca [1024 x i8]
%3 = tail call i32 @strcmp(i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* dereferenceable(1) %0)
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = load i32*, i32** @stdin
br label %9
7: ; preds = %1
%8 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %9
9: ; preds = %7, %5
%10 = phi i32* [ %8, %7 ], [ %6, %5 ]
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %27
14: ; preds = %9
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%16 = call i64 @fread(i8* %15, i32 1, i32 1024, i32* %10)
%17 = tail call i64 @ferror(i32* %10)
%18 = icmp eq i64 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %14
%20 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%21 = tail call i32 @fclose(i32* %10)
br label %25
22: ; preds = %14
%23 = tail call i32 @fclose(i32* %10)
%24 = call i32 @parse_buffer(i8* %15, i64 %16)
br label %25
25: ; preds = %22, %19
%26 = phi i32 [ 1, %19 ], [ %24, %22 ]
br label %27
27: ; preds = %25, %12
%28 = phi i32 [ %26, %25 ], [ 1, %12 ]
ret i32 %28
}