#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*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
char *readJSONFile(char const *fileName) {
  FILE *fp;
  fp = fopen(fileName, "r");
  if (!fp) {
    printf("error open %s", fileName);
    return NULL;
  }
  fseek(fp, 0, SEEK_END);
  long length = ftell(fp);
  fseek(fp, 0, SEEK_SET);
  printf("read %ld   %ld \n", length, ftell(fp));
  char *buffer = malloc(sizeof(char) * length);
  while (length > 0) {
    int read = fread(buffer, sizeof(char), length, fp);
    length -= read;
  }
  fclose(fp);
  printf("read end %ld \n", length);
  return buffer;
}


x86 assembly (clang, -O0):
<readJSONFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe79(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ba <readJSONFile+0x4a>
mov    -0x10(%rbp),%rsi
lea    0xe5c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1283 <readJSONFile+0x113>
mov    -0x18(%rbp),%rdi
mov    0x2e23(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2df2(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <ftell@plt>
mov    -0x38(%rbp),%rsi
mov    %rax,%rdx
lea    0xe02(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
jle    1260 <readJSONFile+0xf0>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %eax,-0x2c(%rbp)
movslq -0x2c(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
jmp    1228 <readJSONFile+0xb8>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rsi
lea    0xdad(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readJSONFile>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
lea    0xe81(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     121b <readJSONFile+0xab>
mov    %rax,%r14
mov    0x2e51(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %r14,%rdi
call   1060 <ftell@plt>
mov    %rax,%rbx
mov    0x2e23(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %r14,%rdi
call   1060 <ftell@plt>
lea    0xe40(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
call   1070 <malloc@plt>
mov    %rax,%r15
test   %rbx,%rbx
jle    120a <readJSONFile+0x9a>
nopl   0x0(%rax)
mov    %r15,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1030 <fread@plt>
cltq
sub    %rax,%rbx
jg     11f0 <readJSONFile+0x80>
mov    %r14,%rdi
call   1040 <fclose@plt>
lea    0xe08(%rip),%rdi
jmp    1225 <readJSONFile+0xb5>
lea    0xde0(%rip),%rdi
xor    %r15d,%r15d
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [14 x i8] c"error open %s\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [17 x i8] c"read %ld %ld \0A\00"
@.str.3 = [15 x i8] c"read end %ld \0A\00"
define i8* @readJSONFile(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %4
%10 = load i32*, i32** %4
%11 = icmp ne i32* %10, null
br i1 %11, label %15, label %12
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %13)
store i8* null, i8** %2
br label %50
15: ; preds = %1
%16 = load i32*, i32** %4
%17 = load i32, i32* @SEEK_END
%18 = call i32 @fseek(i32* %16, i32 0, i32 %17)
%19 = load i32*, i32** %4
%20 = call i64 @ftell(i32* %19)
store i64 %20, i64* %5
%21 = load i32*, i32** %4
%22 = load i32, i32* @SEEK_SET
%23 = call i32 @fseek(i32* %21, i32 0, i32 %22)
%24 = load i64, i64* %5
%25 = load i32*, i32** %4
%26 = call i64 @ftell(i32* %25)
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i64 %24, i64 %26)
%28 = load i64, i64* %5
%29 = mul i64 1, %28
%30 = trunc i64 %29 to i32
%31 = call i8* @malloc(i32 %30)
store i8* %31, i8** %6
br label %32
32: ; preds = %35, %15
%33 = load i64, i64* %5
%34 = icmp sgt i64 %33, 0
br i1 %34, label %35, label %44
35: ; preds = %32
%36 = load i8*, i8** %6
%37 = load i64, i64* %5
%38 = load i32*, i32** %4
%39 = call i32 @fread(i8* %36, i32 1, i64 %37, i32* %38)
store i32 %39, i32* %7
%40 = load i32, i32* %7
%41 = sext i32 %40 to i64
%42 = load i64, i64* %5
%43 = sub nsw i64 %42, %41
store i64 %43, i64* %5
br label %32
44: ; preds = %32
%45 = load i32*, i32** %4
%46 = call i32 @fclose(i32* %45)
%47 = load i64, i64* %5
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i64 %47)
%49 = load i8*, i8** %6
store i8* %49, i8** %2
br label %50
50: ; preds = %44, %12
%51 = load i8*, i8** %2
ret i8* %51
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [14 x i8] c"error open %s\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [17 x i8] c"read %ld %ld \0A\00"
@.str.3 = [15 x i8] c"read end %ld \0A\00"
define noalias i8* @readJSONFile(i8* %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %27
6: ; preds = %1
%7 = load i32, i32* @SEEK_END
%8 = tail call i32 @fseek(i32* %2, i32 0, i32 %7)
%9 = tail call i64 @ftell(i32* %2)
%10 = load i32, i32* @SEEK_SET
%11 = tail call i32 @fseek(i32* %2, i32 0, i32 %10)
%12 = tail call i64 @ftell(i32* %2)
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i64 %9, i64 %12)
%14 = trunc i64 %9 to i32
%15 = tail call i8* @malloc(i32 %14)
%16 = icmp sgt i64 %9, 0
br i1 %16, label %17, label %23
17: ; preds = %6, %17
%18 = phi i64 [ %21, %17 ], [ %9, %6 ]
%19 = tail call i32 @fread(i8* %15, i32 1, i64 %18, i32* %2)
%20 = sext i32 %19 to i64
%21 = sub nsw i64 %18, %20
%22 = icmp sgt i64 %21, 0
br i1 %22, label %17, label %23
23: ; preds = %17, %6
%24 = phi i64 [ %9, %6 ], [ %21, %17 ]
%25 = tail call i32 @fclose(i32* %2)
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i64 %24)
br label %27
27: ; preds = %23, %4
%28 = phi i8* [ %15, %23 ], [ null, %4 ]
ret i8* %28
}