#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 off_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ malloc(int);
int open(char const *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ strcat(char *, char *);
char *util_loadFile(const char *path) {
  char *buff;
  char *part;
  int f;
  int bytesRead;
  off_t size;
  f = open(path, O_RDONLY);
  if (f == -1) {
    return NULL;
  }
  size = lseek(f, 0, SEEK_END);
  if (size == -1) {
    close(f);
    return NULL;
  }
  if (lseek(f, 0, SEEK_SET) == -1) {
    close(f);
    return NULL;
  }
  buff = (char *)malloc(size + 1);
  part = (char *)malloc(256 + 1);
  buff[0] = '\0';
  do {
    bytesRead = read(f, part, 256);
    if (bytesRead < 0) {
      free(buff);
      free(part);
      close(f);
      return NULL;
    }
    part[bytesRead] = '\0';
    strcat(buff, part);
  } while (bytesRead != 0);
  free(part);
  close(f);
  return buff;
}


x86 assembly (clang, -O0):
<util_loadFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11a8 <util_loadFile+0x38>
movq   $0x0,-0x8(%rbp)
jmp    12b3 <util_loadFile+0x143>
mov    -0x24(%rbp),%edi
mov    0x2e36(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11dd <util_loadFile+0x6d>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    12b3 <util_loadFile+0x143>
mov    -0x24(%rbp),%edi
mov    0x2de9(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
cmp    $0xffffffff,%eax
jne    120e <util_loadFile+0x9e>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    12b3 <util_loadFile+0x143>
mov    -0x2c(%rbp),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    $0x101,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x100,%edx
call   1060 <read@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    1277 <util_loadFile+0x107>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    12b3 <util_loadFile+0x143>
mov    -0x20(%rbp),%rax
movslq -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1090 <strcat@plt>
cmpl   $0x0,-0x28(%rbp)
jne    1232 <util_loadFile+0xc2>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<util_loadFile>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1226 <util_loadFile+0xb6>
mov    %eax,%r14d
mov    0x2e50(%rip),%rax
mov    (%rax),%edx
xor    %r15d,%r15d
mov    %r14d,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
cmp    $0xffffffff,%eax
je     123e <util_loadFile+0xce>
mov    %eax,%ebp
mov    0x2e17(%rip),%rax
mov    (%rax),%edx
xor    %r15d,%r15d
mov    %r14d,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
cmp    $0xffffffff,%eax
je     123e <util_loadFile+0xce>
add    $0x1,%ebp
mov    %ebp,%edi
call   1070 <malloc@plt>
mov    %rax,%r15
mov    $0x101,%edi
call   1070 <malloc@plt>
mov    %rax,%rbp
movb   $0x0,(%r15)
nopl   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0x100,%edx
call   1060 <read@plt>
test   %eax,%eax
js     122b <util_loadFile+0xbb>
mov    %eax,%ebx
mov    %eax,%eax
movb   $0x0,0x0(%rbp,%rax,1)
mov    %r15,%rdi
mov    %rbp,%rsi
call   1090 <strcat@plt>
test   %ebx,%ebx
jne    11f0 <util_loadFile+0x80>
mov    %rbp,%rdi
call   1030 <free@plt>
jmp    123e <util_loadFile+0xce>
xor    %r15d,%r15d
jmp    1246 <util_loadFile+0xd6>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
xor    %r15d,%r15d
mov    %r14d,%edi
call   1050 <close@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i8* @util_loadFile(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
%9 = load i8*, i8** %3
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %15
14: ; preds = %1
store i8* null, i8** %2
br label %71
15: ; preds = %1
%16 = load i32, i32* %6
%17 = load i32, i32* @SEEK_END
%18 = call i32 @lseek(i32 %16, i32 0, i32 %17)
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %15
%22 = load i32, i32* %6
%23 = call i32 @close(i32 %22)
store i8* null, i8** %2
br label %71
24: ; preds = %15
%25 = load i32, i32* %6
%26 = load i32, i32* @SEEK_SET
%27 = call i32 @lseek(i32 %25, i32 0, i32 %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %24
%30 = load i32, i32* %6
%31 = call i32 @close(i32 %30)
store i8* null, i8** %2
br label %71
32: ; preds = %24
%33 = load i32, i32* %8
%34 = add nsw i32 %33, 1
%35 = call i64 @malloc(i32 %34)
%36 = inttoptr i64 %35 to i8*
store i8* %36, i8** %4
%37 = call i64 @malloc(i32 257)
%38 = inttoptr i64 %37 to i8*
store i8* %38, i8** %5
%39 = load i8*, i8** %4
%40 = getelementptr inbounds i8, i8* %39, i64 0
store i8 0, i8* %40
br label %41
41: ; preds = %62, %32
%42 = load i32, i32* %6
%43 = load i8*, i8** %5
%44 = call i32 @read(i32 %42, i8* %43, i32 256)
store i32 %44, i32* %7
%45 = load i32, i32* %7
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %54
47: ; preds = %41
%48 = load i8*, i8** %4
%49 = call i32 @free(i8* %48)
%50 = load i8*, i8** %5
%51 = call i32 @free(i8* %50)
%52 = load i32, i32* %6
%53 = call i32 @close(i32 %52)
store i8* null, i8** %2
br label %71
54: ; preds = %41
%55 = load i8*, i8** %5
%56 = load i32, i32* %7
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %55, i64 %57
store i8 0, i8* %58
%59 = load i8*, i8** %4
%60 = load i8*, i8** %5
%61 = call i32 @strcat(i8* %59, i8* %60)
br label %62
62: ; preds = %54
%63 = load i32, i32* %7
%64 = icmp ne i32 %63, 0
br i1 %64, label %41, label %65
65: ; preds = %62
%66 = load i8*, i8** %5
%67 = call i32 @free(i8* %66)
%68 = load i32, i32* %6
%69 = call i32 @close(i32 %68)
%70 = load i8*, i8** %4
store i8* %70, i8** %2
br label %71
71: ; preds = %65, %47, %29, %21, %14
%72 = load i8*, i8** %2
ret i8* %72
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i8* @util_loadFile(i8* nocapture %0) {
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* %0, i32 %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %35, label %5
5: ; preds = %1
%6 = load i32, i32* @SEEK_END
%7 = tail call i32 @lseek(i32 %3, i32 0, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %32, label %9
9: ; preds = %5
%10 = load i32, i32* @SEEK_SET
%11 = tail call i32 @lseek(i32 %3, i32 0, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %32, label %13
13: ; preds = %9
%14 = add nuw nsw i32 %7, 1
%15 = tail call i64 @malloc(i32 %14)
%16 = inttoptr i64 %15 to i8*
%17 = tail call i64 @malloc(i32 257)
%18 = inttoptr i64 %17 to i8*
store i8 0, i8* %16
br label %19
19: ; preds = %25, %13
%20 = tail call i32 @read(i32 %3, i8* %18, i32 256)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = tail call i32 @free(i8* %16)
%24 = tail call i32 @free(i8* %18)
br label %32
25: ; preds = %19
%26 = zext i32 %20 to i64
%27 = getelementptr inbounds i8, i8* %18, i64 %26
store i8 0, i8* %27
%28 = tail call i32 @strcat(i8* %16, i8* %18)
%29 = icmp eq i32 %20, 0
br i1 %29, label %30, label %19
30: ; preds = %25
%31 = tail call i32 @free(i8* %18)
br label %32
32: ; preds = %9, %5, %22, %30
%33 = phi i8* [ %16, %30 ], [ null, %22 ], [ null, %5 ], [ null, %9 ]
%34 = tail call i32 @close(i32 %3)
br label %35
35: ; preds = %32, %1
%36 = phi i8* [ null, %1 ], [ %33, %32 ]
ret i8* %36
}