#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__ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(char *, int, unsigned long, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(unsigned long);
scalar_t__ realloc(char *, unsigned long);
void *sea_platform_buffer_construct_with_buffer(char *, unsigned long);
void *sea_platform_read_file(const char *filepath) {
  FILE *file = fopen(filepath, "rb");
  if (file) {
    unsigned long file_size = 0;
    unsigned long buffer_size = 1024;
    char *buffer = (char *)malloc(buffer_size);
    while (feof(file) == 0) {
      int read_bytes =
          fread(buffer + file_size, 1, buffer_size - file_size, file);
      file_size += read_bytes;
      if (file_size == buffer_size) {
        char *resized_buffer;
        buffer_size *= 1.6f;
        resized_buffer = (char *)realloc(buffer, buffer_size);
        if (!resized_buffer) {
          free(buffer);
          fclose(file);
          return NULL;
        } else {
          buffer = resized_buffer;
        }
      }
    }
    fclose(file);
    return sea_platform_buffer_construct_with_buffer(buffer, file_size);
  } else {
    return NULL;
  }
}


x86 assembly (clang, -O0):
<sea_platform_read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe71(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12fa <sea_platform_read_file+0x17a>
movq   $0x0,-0x20(%rbp)
movq   $0x400,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%rax
jne    12db <sea_platform_read_file+0x15b>
mov    -0x30(%rbp),%rdi
add    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rdx
sub    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %eax,-0x34(%rbp)
movslq -0x34(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x28(%rbp),%rax
jne    12d6 <sea_platform_read_file+0x156>
mov    -0x28(%rbp),%rax
mov    %rax,%rdx
shr    %rdx
mov    %eax,%ecx
and    $0x1,%ecx
or     %rdx,%rcx
cvtsi2ss %rcx,%xmm0
addss  %xmm0,%xmm0
cvtsi2ss %rax,%xmm1
movss  %xmm1,-0x48(%rbp)
test   %rax,%rax
movss  %xmm0,-0x44(%rbp)
js     1253 <sea_platform_read_file+0xd3>
movss  -0x48(%rbp),%xmm0
movss  %xmm0,-0x44(%rbp)
movss  -0x44(%rbp),%xmm0
movss  0xda0(%rip),%xmm1
mulss  %xmm1,%xmm0
movaps %xmm0,%xmm1
cvttss2si %xmm1,%rax
mov    %rax,%rdx
sar    $0x3f,%rdx
movss  0xd89(%rip),%xmm1
subss  %xmm1,%xmm0
cvttss2si %xmm0,%rcx
and    %rdx,%rcx
or     %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1090 <realloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    12c9 <sea_platform_read_file+0x149>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    1302 <sea_platform_read_file+0x182>
mov    -0x40(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    12d6 <sea_platform_read_file+0x156>
jmp    11c8 <sea_platform_read_file+0x48>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <sea_platform_buffer_construct_with_buffer@plt>
mov    %rax,-0x8(%rbp)
jmp    1302 <sea_platform_read_file+0x182>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sea_platform_read_file>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
lea    0xe78(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1293 <sea_platform_read_file+0x113>
mov    %rax,%r15
mov    $0x400,%ebx
mov    $0x400,%edi
call   1080 <malloc@plt>
mov    %rax,%r14
mov    %r15,%rdi
call   1070 <feof@plt>
xor    %r12d,%r12d
jmp    11c8 <sea_platform_read_file+0x48>
mov    %r15,%rdi
call   1070 <feof@plt>
test   %rax,%rax
jne    1267 <sea_platform_read_file+0xe7>
mov    %r12,%r13
lea    (%r14,%r12,1),%rdi
mov    %rbx,%rdx
sub    %r12,%rdx
mov    $0x1,%esi
mov    %r15,%rcx
call   1040 <fread@plt>
movslq %eax,%r12
add    %r13,%r12
cmp    %rbx,%r12
jne    11c0 <sea_platform_read_file+0x40>
test   %rbx,%rbx
js     1210 <sea_platform_read_file+0x90>
xorps  %xmm0,%xmm0
cvtsi2ss %rbx,%xmm0
jmp    1228 <sea_platform_read_file+0xa8>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rax
shr    %rax
and    $0x1,%ebx
or     %rax,%rbx
xorps  %xmm0,%xmm0
cvtsi2ss %rbx,%xmm0
addss  %xmm0,%xmm0
mulss  0xdd0(%rip),%xmm0
cvttss2si %xmm0,%rax
mov    %rax,%rcx
sar    $0x3f,%rcx
subss  0xdc0(%rip),%xmm0
cvttss2si %xmm0,%rbx
and    %rcx,%rbx
or     %rax,%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1090 <realloc@plt>
test   %rax,%rax
je     1283 <sea_platform_read_file+0x103>
mov    %rax,%r14
jmp    11c0 <sea_platform_read_file+0x40>
mov    %r15,%rdi
call   1060 <fclose@plt>
mov    %r14,%rdi
mov    %r12,%rsi
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1050 <sea_platform_buffer_construct_with_buffer@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1060 <fclose@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
define i8* @sea_platform_read_file(i8* nocapture %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %40, label %4
4: ; preds = %1
%5 = tail call i64 @malloc(i64 1024)
%6 = inttoptr i64 %5 to i8*
%7 = tail call i64 @feof(i32* %2)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %35
9: ; preds = %4, %30
%10 = phi i64 [ %17, %30 ], [ 0, %4 ]
%11 = phi i8* [ %32, %30 ], [ %6, %4 ]
%12 = phi i64 [ %31, %30 ], [ 1024, %4 ]
%13 = getelementptr inbounds i8, i8* %11, i64 %10
%14 = sub i64 %12, %10
%15 = tail call i32 @fread(i8* %13, i32 1, i64 %14, i32* %2)
%16 = sext i32 %15 to i64
%17 = add i64 %10, %16
%18 = icmp eq i64 %17, %12
br i1 %18, label %19, label %30
19: ; preds = %9
%20 = uitofp i64 %12 to float
%21 = fmul float %20, 0x3FF99999A0000000
%22 = fptoui float %21 to i64
%23 = tail call i64 @realloc(i8* %11, i64 %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %19
%26 = tail call i32 @free(i8* %11)
%27 = tail call i32 @fclose(i32* %2)
br label %40
28: ; preds = %19
%29 = inttoptr i64 %23 to i8*
br label %30
30: ; preds = %9, %28
%31 = phi i64 [ %22, %28 ], [ %12, %9 ]
%32 = phi i8* [ %29, %28 ], [ %11, %9 ]
%33 = tail call i64 @feof(i32* %2)
%34 = icmp eq i64 %33, 0
br i1 %34, label %9, label %35
35: ; preds = %30, %4
%36 = phi i8* [ %6, %4 ], [ %32, %30 ]
%37 = phi i64 [ 0, %4 ], [ %17, %30 ]
%38 = tail call i32 @fclose(i32* %2)
%39 = tail call i8* @sea_platform_buffer_construct_with_buffer(i8* %36, i64 %37)
br label %40
40: ; preds = %25, %1, %35
%41 = phi i8* [ %39, %35 ], [ null, %1 ], [ null, %25 ]
ret i8* %41
}