#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 stat {
  int st_size;
};
struct json {
  int dummy;
};
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 *);
int json_loadstring(struct json *, char *);
scalar_t__ malloc(int);
int stat(char const *, struct stat *);
int json_load(struct json *J, const char *path) {
  int err;
  FILE *fp;
  char *buff;
  struct stat st;
  err = stat(path, &st);
  if (err < 0)
    return -1;
  fp = fopen(path, "rb");
  if (fp == NULL)
    return -1;
  buff = (char *)malloc(st.st_size + 1);
  if (fread(buff, st.st_size, 1, fp) != 1) {
    free(buff);
    fclose(fp);
    return -1;
  }
  buff[st.st_size] = 0;
  err = json_loadstring(J, buff);
  fclose(fp);
  free(buff);
  return err;
}


x86 assembly (clang, -O0):
<json_load>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1070 <stat@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11a6 <json_load+0x36>
movl   $0xffffffff,-0x4(%rbp)
jmp    1250 <json_load+0xe0>
mov    -0x18(%rbp),%rdi
lea    0xe4f(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11d1 <json_load+0x61>
movl   $0xffffffff,-0x4(%rbp)
jmp    1250 <json_load+0xe0>
mov    -0x38(%rbp),%edi
add    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%esi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
cmp    $0x1,%eax
je     121c <json_load+0xac>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1250 <json_load+0xe0>
mov    -0x30(%rbp),%rax
movslq -0x38(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1060 <json_loadstring@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x30(%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):
<json_load>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1070 <stat@plt>
test   %eax,%eax
js     120d <json_load+0x9d>
lea    0xe66(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     120d <json_load+0x9d>
mov    %rax,%rbx
mov    0x8(%rsp),%r12d
lea    0x1(%r12),%edi
call   1080 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r12d,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
cmp    $0x1,%eax
jne    11fd <json_load+0x8d>
movslq %r12d,%rax
movb   $0x0,(%r15,%rax,1)
mov    %r14,%rdi
mov    %r15,%rsi
call   1060 <json_loadstring@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
jmp    1212 <json_load+0xa2>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.json = type { i32 }
%struct.stat = type { i32 }
@.str = [3 x i8] c"rb\00"
define i32 @json_load(%struct.json* %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 slt i32 %5, 0
br i1 %6, label %27, label %7
7: ; preds = %2
%8 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %27, label %10
10: ; preds = %7
%11 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%12 = load i32, i32* %11
%13 = add nsw i32 %12, 1
%14 = tail call i64 @malloc(i32 %13)
%15 = inttoptr i64 %14 to i8*
%16 = tail call i32 @fread(i8* %15, i32 %12, i32 1, i32* %8)
%17 = icmp eq i32 %16, 1
br i1 %17, label %21, label %18
18: ; preds = %10
%19 = tail call i32 @free(i8* %15)
%20 = tail call i32 @fclose(i32* %8)
br label %27
21: ; preds = %10
%22 = sext i32 %12 to i64
%23 = getelementptr inbounds i8, i8* %15, i64 %22
store i8 0, i8* %23
%24 = tail call i32 @json_loadstring(%struct.json* %0, i8* %15)
%25 = tail call i32 @fclose(i32* %8)
%26 = tail call i32 @free(i8* %15)
br label %27
27: ; preds = %7, %2, %21, %18
%28 = phi i32 [ -1, %18 ], [ %24, %21 ], [ -1, %2 ], [ -1, %7 ]
ret i32 %28
}