#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;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ stat(char *, struct stat *);
int read_json_file(char **buf, char *filename) {
  struct stat st;
  if (stat(filename, &st) != 0) {
    return 1;
  }
  printf("file size: %lld \n", st.st_size);
  *buf = malloc(sizeof(char) * (st.st_size + 1));
  if (*buf == NULL) {
    printf("failed to malloc buf\n");
    return 2;
  }
  FILE *fp = fopen(filename, "rb");
  fread((*buf), 1, st.st_size, fp);
  (*buf)[st.st_size] = '\0';
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<read_json_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
je     1193 <read_json_file+0x33>
movl   $0x1,-0x4(%rbp)
jmp    1234 <read_json_file+0xd4>
mov    -0x20(%rbp),%esi
lea    0xe63(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11e9 <read_json_file+0x89>
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1234 <read_json_file+0xd4>
mov    -0x18(%rbp),%rdi
lea    0xe34(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_json_file>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1070 <stat@plt>
mov    $0x1,%ebp
test   %rax,%rax
jne    120a <read_json_file+0x9a>
mov    0x8(%rsp),%esi
lea    0xe60(%rip),%rdi
xor    %ebp,%ebp
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x8(%rsp),%edi
add    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     11f9 <read_json_file+0x89>
lea    0xe4e(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
mov    %rax,%rbx
mov    (%r14),%rdi
mov    0x8(%rsp),%edx
mov    $0x1,%esi
mov    %rax,%rcx
call   1040 <fread@plt>
mov    (%r14),%rax
movslq 0x8(%rsp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    120a <read_json_file+0x9a>
lea    0xe15(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [18 x i8] c"file size: %lld \0A\00"
@.str.1 = [22 x i8] c"failed to malloc buf\0A\00"
@.str.2 = [3 x i8] c"rb\00"
define i32 @read_json_file(i8** %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca %struct.stat
%7 = alloca i32*
store i8** %0, i8*** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %5
%9 = call i64 @stat(i8* %8, %struct.stat* %6)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 1, i32* %3
br label %46
12: ; preds = %2
%13 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%14 = load i32, i32* %13
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%17 = load i32, i32* %16
%18 = add nsw i32 %17, 1
%19 = sext i32 %18 to i64
%20 = mul i64 1, %19
%21 = trunc i64 %20 to i32
%22 = call i8* @malloc(i32 %21)
%23 = load i8**, i8*** %4
store i8* %22, i8** %23
%24 = load i8**, i8*** %4
%25 = load i8*, i8** %24
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %29
27: ; preds = %12
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %3
br label %46
29: ; preds = %12
%30 = load i8*, i8** %5
%31 = call i32* @fopen(i8* %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %31, i32** %7
%32 = load i8**, i8*** %4
%33 = load i8*, i8** %32
%34 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%35 = load i32, i32* %34
%36 = load i32*, i32** %7
%37 = call i32 @fread(i8* %33, i32 1, i32 %35, i32* %36)
%38 = load i8**, i8*** %4
%39 = load i8*, i8** %38
%40 = getelementptr inbounds %struct.stat, %struct.stat* %6, 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 i32*, i32** %7
%45 = call i32 @fclose(i32* %44)
store i32 0, i32* %3
br label %46
46: ; preds = %29, %27, %11
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [18 x i8] c"file size: %lld \0A\00"
@.str.2 = [3 x i8] c"rb\00"
@str = [21 x i8] c"failed to malloc buf\00"
define i32 @read_json_file(i8** nocapture %0, i8* %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i64 @stat(i8* %1, %struct.stat* %3)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %27
7: ; preds = %2
%8 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%9 = load i32, i32* %8
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = load i32, i32* %8
%12 = add nsw i32 %11, 1
%13 = call i8* @malloc(i32 %12)
store i8* %13, i8** %0
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %17
15: ; preds = %7
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
br label %27
17: ; preds = %7
%18 = call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%19 = load i8*, i8** %0
%20 = load i32, i32* %8
%21 = call i32 @fread(i8* %19, i32 1, i32 %20, i32* %18)
%22 = load i8*, i8** %0
%23 = load i32, i32* %8
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
store i8 0, i8* %25
%26 = call i32 @fclose(i32* %18)
br label %27
27: ; preds = %2, %17, %15
%28 = phi i32 [ 2, %15 ], [ 0, %17 ], [ 1, %2 ]
ret i32 %28
}