#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 */
struct zproto_parser {
  int dummy;
};
struct stat {
  int st_size;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
int stat(char const *, struct stat *);
int zproto_parse(struct zproto_parser *, char *);
int zproto_load(struct zproto_parser *p, const char *path) {
  int err;
  char *buff;
  struct stat st;
  err = stat(path, &st);
  if (err == -1) {
    perror("zproto_load:");
    return -1;
  }
  FILE *fp = fopen(path, "rb");
  if (fp == NULL) {
    perror("zproto_load:");
    return -1;
  }
  buff = (char *)malloc(st.st_size + 1);
  err = fread(buff, 1, st.st_size, fp);
  if (err != st.st_size) {
    perror("zproto_load:");
    err = -1;
    goto end;
  }
  buff[st.st_size] = '\0';
  err = zproto_parse(p, buff);
end:
  if (fp)
    fclose(fp);
  if (buff)
    free(buff);
  return err;
}


x86 assembly (clang, -O0):
<zproto_load>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1070 <stat@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11c2 <zproto_load+0x42>
lea    0xe4f(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128e <zproto_load+0x10e>
mov    -0x18(%rbp),%rdi
lea    0xe40(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11f9 <zproto_load+0x79>
lea    0xe18(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128e <zproto_load+0x10e>
mov    -0x30(%rbp),%edi
add    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%edx
mov    -0x38(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x30(%rbp),%eax
je     1244 <zproto_load+0xc4>
lea    0xdcd(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x1c(%rbp)
jmp    1260 <zproto_load+0xe0>
mov    -0x28(%rbp),%rax
movslq -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1060 <zproto_parse@plt>
mov    %eax,-0x1c(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1274 <zproto_load+0xf4>
mov    -0x38(%rbp),%rdi
call   1050 <fclose@plt>
cmpq   $0x0,-0x28(%rbp)
je     1288 <zproto_load+0x108>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<zproto_load>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
je     120e <zproto_load+0x8e>
lea    0xe69(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     120e <zproto_load+0x8e>
mov    %rax,%r15
mov    (%rsp),%ebp
lea    0x1(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
cmp    %ebp,%eax
jne    1221 <zproto_load+0xa1>
movslq %ebp,%rax
movb   $0x0,(%rbx,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <zproto_parse@plt>
mov    %eax,%ebp
mov    %r15,%rdi
call   1050 <fclose@plt>
test   %rbx,%rbx
je     1201 <zproto_load+0x81>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdeb(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%ebp
jmp    1201 <zproto_load+0x81>
lea    0xdd8(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%ebp
jmp    11ec <zproto_load+0x6c>

LLVM IR (-O0):
%struct.zproto_parser = type { i32 }
%struct.stat = type { i32 }
@.str = [13 x i8] c"zproto_load:\00"
@.str.1 = [3 x i8] c"rb\00"
define i32 @zproto_load(%struct.zproto_parser* %0, i8* %1) {
%3 = alloca i32
%4 = alloca %struct.zproto_parser*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca %struct.stat
%9 = alloca i32*
store %struct.zproto_parser* %0, %struct.zproto_parser** %4
store i8* %1, i8** %5
%10 = load i8*, i8** %5
%11 = call i32 @stat(i8* %10, %struct.stat* %8)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %63
16: ; preds = %2
%17 = load i8*, i8** %5
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %18, i32** %9
%19 = load i32*, i32** %9
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %63
23: ; preds = %16
%24 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%25 = load i32, i32* %24
%26 = add nsw i32 %25, 1
%27 = call i64 @malloc(i32 %26)
%28 = inttoptr i64 %27 to i8*
store i8* %28, i8** %7
%29 = load i8*, i8** %7
%30 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%31 = load i32, i32* %30
%32 = load i32*, i32** %9
%33 = call i32 @fread(i8* %29, i32 1, i32 %31, i32* %32)
store i32 %33, i32* %6
%34 = load i32, i32* %6
%35 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%36 = load i32, i32* %35
%37 = icmp ne i32 %34, %36
br i1 %37, label %38, label %40
38: ; preds = %23
%39 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %6
br label %49
40: ; preds = %23
%41 = load i8*, i8** %7
%42 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%43 = load i32, i32* %42
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %41, i64 %44
store i8 0, i8* %45
%46 = load %struct.zproto_parser*, %struct.zproto_parser** %4
%47 = load i8*, i8** %7
%48 = call i32 @zproto_parse(%struct.zproto_parser* %46, i8* %47)
store i32 %48, i32* %6
br label %49
49: ; preds = %40, %38
%50 = load i32*, i32** %9
%51 = icmp ne i32* %50, null
br i1 %51, label %52, label %55
52: ; preds = %49
%53 = load i32*, i32** %9
%54 = call i32 @fclose(i32* %53)
br label %55
55: ; preds = %52, %49
%56 = load i8*, i8** %7
%57 = icmp ne i8* %56, null
br i1 %57, label %58, label %61
58: ; preds = %55
%59 = load i8*, i8** %7
%60 = call i32 @free(i8* %59)
br label %61
61: ; preds = %58, %55
%62 = load i32, i32* %6
store i32 %62, i32* %3
br label %63
63: ; preds = %61, %21, %14
%64 = load i32, i32* %3
ret i32 %64
}

LLVM IR (-O2):
%struct.zproto_parser = type { i32 }
%struct.stat = type { i32 }
@.str = [13 x i8] c"zproto_load:\00"
@.str.1 = [3 x i8] c"rb\00"
define i32 @zproto_load(%struct.zproto_parser* %0, i8* nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i32 @stat(i8* %1, %struct.stat* %3)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %34
9: ; preds = %2
%10 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %34
14: ; preds = %9
%15 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%16 = load i32, i32* %15
%17 = add nsw i32 %16, 1
%18 = tail call i64 @malloc(i32 %17)
%19 = inttoptr i64 %18 to i8*
%20 = tail call i32 @fread(i8* %19, i32 1, i32 %16, i32* %10)
%21 = icmp eq i32 %20, %16
br i1 %21, label %24, label %22
22: ; preds = %14
%23 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %28
24: ; preds = %14
%25 = sext i32 %16 to i64
%26 = getelementptr inbounds i8, i8* %19, i64 %25
store i8 0, i8* %26
%27 = tail call i32 @zproto_parse(%struct.zproto_parser* %0, i8* %19)
br label %28
28: ; preds = %22, %24
%29 = phi i32 [ -1, %22 ], [ %27, %24 ]
%30 = tail call i32 @fclose(i32* %10)
%31 = icmp eq i64 %18, 0
br i1 %31, label %34, label %32
32: ; preds = %28
%33 = tail call i32 @free(i8* %19)
br label %34
34: ; preds = %12, %32, %28, %7
%35 = phi i32 [ -1, %7 ], [ -1, %12 ], [ %29, %32 ], [ %29, %28 ]
ret i32 %35
}