#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 *);
long fread(char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(long);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, long);
int /*<<< orphan*/ printf(char *, ...);
void load_shader_from_file(const char *filepath, char **content) {
  FILE *fp = fopen(filepath, "rb");
  long length = -1;
  if (NULL == fp) {
    printf("filepath: %s doesn't exist\n", filepath);
    return;
  }
  fseek(fp, 0, SEEK_END);
  length = ftell(fp);
  fseek(fp, 0, SEEK_SET);
  *content = (char *)malloc(length + 1);
  if (NULL == *content) {
    printf("out of memory\n");
    goto exit;
  }
  memset(*content, 0, length + 1);
  if (length != fread(*content, 1, length, fp))
    printf("fail to read all content\n");
exit:
  fclose(fp);
}


x86 assembly (clang, -O0):
<load_shader_from_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe65(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
movq   $0xffffffffffffffff,-0x20(%rbp)
ff
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    11cf <load_shader_from_file+0x4f>
mov    -0x8(%rbp),%rsi
lea    0xe40(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12a0 <load_shader_from_file+0x120>
mov    -0x18(%rbp),%rdi
mov    0x2e0e(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2ddd(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1080 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rcx
xor    %eax,%eax
cmp    (%rcx),%rax
jne    123d <load_shader_from_file+0xbd>
lea    0xdee(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1297 <load_shader_from_file+0x117>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rdx
add    $0x1,%rdx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
cmp    %rcx,%rax
je     1292 <load_shader_from_file+0x112>
lea    0xda3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1297 <load_shader_from_file+0x117>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_shader_from_file>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xe5a(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1226 <load_shader_from_file+0x96>
mov    %rax,%r13
mov    0x2e2e(%rip),%rax
mov    (%rax),%edx
mov    %r13,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %r13,%rdi
call   1080 <ftell@plt>
mov    %rax,%r15
mov    0x2e00(%rip),%rax
mov    (%rax),%edx
mov    %r13,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
lea    0x1(%r15),%r12
mov    %r12,%rdi
call   1090 <malloc@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     1240 <load_shader_from_file+0xb0>
mov    %rax,%rdi
xor    %esi,%esi
mov    %r12,%rdx
call   1070 <memset@plt>
mov    (%r14),%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %r13,%rcx
call   1040 <fread@plt>
cmp    %rax,%r15
je     124c <load_shader_from_file+0xbc>
lea    0xdfb(%rip),%rdi
jmp    1247 <load_shader_from_file+0xb7>
lea    0xdd6(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1060 <printf@plt>
lea    0xdf1(%rip),%rdi
call   1030 <puts@plt>
mov    %r13,%rdi
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1050 <fclose@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [28 x i8] c"filepath: %s doesn't exist\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [15 x i8] c"out of memory\0A\00"
@.str.3 = [26 x i8] c"fail to read all content\0A\00"
define void @load_shader_from_file(i8* %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i8**
%5 = alloca i32*
%6 = alloca i64
store i8* %0, i8** %3
store i8** %1, i8*** %4
%7 = load i8*, i8** %3
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %5
store i64 -1, i64* %6
%9 = load i32*, i32** %5
%10 = icmp eq i32* null, %9
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = load i8*, i8** %3
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %12)
br label %52
14: ; preds = %2
%15 = load i32*, i32** %5
%16 = load i32, i32* @SEEK_END
%17 = call i32 @fseek(i32* %15, i32 0, i32 %16)
%18 = load i32*, i32** %5
%19 = call i64 @ftell(i32* %18)
store i64 %19, i64* %6
%20 = load i32*, i32** %5
%21 = load i32, i32* @SEEK_SET
%22 = call i32 @fseek(i32* %20, i32 0, i32 %21)
%23 = load i64, i64* %6
%24 = add nsw i64 %23, 1
%25 = call i64 @malloc(i64 %24)
%26 = inttoptr i64 %25 to i8*
%27 = load i8**, i8*** %4
store i8* %26, i8** %27
%28 = load i8**, i8*** %4
%29 = load i8*, i8** %28
%30 = icmp eq i8* null, %29
br i1 %30, label %31, label %33
31: ; preds = %14
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %49
33: ; preds = %14
%34 = load i8**, i8*** %4
%35 = load i8*, i8** %34
%36 = load i64, i64* %6
%37 = add nsw i64 %36, 1
%38 = call i32 @memset(i8* %35, i32 0, i64 %37)
%39 = load i64, i64* %6
%40 = load i8**, i8*** %4
%41 = load i8*, i8** %40
%42 = load i64, i64* %6
%43 = load i32*, i32** %5
%44 = call i64 @fread(i8* %41, i32 1, i64 %42, i32* %43)
%45 = icmp ne i64 %39, %44
br i1 %45, label %46, label %48
46: ; preds = %33
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %48
48: ; preds = %46, %33
br label %49
49: ; preds = %48, %31
%50 = load i32*, i32** %5
%51 = call i32 @fclose(i32* %50)
br label %52
52: ; preds = %49, %11
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [28 x i8] c"filepath: %s doesn't exist\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@str = [25 x i8] c"fail to read all content\00"
@str.4 = [14 x i8] c"out of memory\00"
define void @load_shader_from_file(i8* %0, i8** nocapture %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %27
7: ; preds = %2
%8 = load i32, i32* @SEEK_END
%9 = tail call i32 @fseek(i32* %3, i32 0, i32 %8)
%10 = tail call i64 @ftell(i32* %3)
%11 = load i32, i32* @SEEK_SET
%12 = tail call i32 @fseek(i32* %3, i32 0, i32 %11)
%13 = add nsw i64 %10, 1
%14 = tail call i64 @malloc(i64 %13)
%15 = inttoptr i64 %14 to i8*
store i8* %15, i8** %1
%16 = icmp eq i64 %14, 0
br i1 %16, label %22, label %17
17: ; preds = %7
%18 = tail call i32 @memset(i8* %15, i32 0, i64 %13)
%19 = load i8*, i8** %1
%20 = tail call i64 @fread(i8* %19, i32 1, i64 %10, i32* %3)
%21 = icmp eq i64 %10, %20
br i1 %21, label %25, label %22
22: ; preds = %17, %7
%23 = phi i8* [ getelementptr inbounds ([14 x i8], [14 x i8]* @str.4, i64 0, i64 0), %7 ], [ getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0), %17 ]
%24 = tail call i32 @puts(i8* dereferenceable(1) %23)
br label %25
25: ; preds = %22, %17
%26 = tail call i32 @fclose(i32* %3)
br label %27
27: ; preds = %25, %5
ret void
}