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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fileSize(char const *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(void *, int, int, int /*<<< orphan*/ *);
void *malloc(int);
void *readFileToS(const char *filePath) {
  FILE *f = NULL;
  void *r = NULL;
  ssize_t len;
  size_t readStatus;
  len = fileSize(filePath);
  ;
  r = malloc(len + 1);
  if (!r) {
    return (NULL);
  }
  f = fopen(filePath, "r");
  readStatus = fread(r, 1, len, f);
  fclose(f);
  if (readStatus != (size_t)len) {
    return (NULL);
  }
  *((char *)r + len) = 0;
  return (r);
}


x86 assembly (clang, -O0):
<readFileToS>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <fileSize@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    119f <readFileToS+0x4f>
movq   $0x0,-0x8(%rbp)
jmp    1207 <readFileToS+0xb7>
mov    -0x10(%rbp),%rdi
lea    0xe56(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
cmp    %rcx,%rax
je     11f3 <readFileToS+0xa3>
movq   $0x0,-0x8(%rbp)
jmp    1207 <readFileToS+0xb7>
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readFileToS>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
call   1040 <fileSize@plt>
mov    %eax,%r14d
lea    0x1(%r14),%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11b3 <readFileToS+0x63>
mov    %rax,%rbx
lea    0xe85(%rip),%rsi
mov    %r15,%rdi
call   1070 <fopen@plt>
mov    %rax,%r12
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
mov    %rax,%r15
mov    %r12,%rdi
call   1050 <fclose@plt>
movslq %r14d,%rax
cmp    %rax,%r15
jne    11b3 <readFileToS+0x63>
movb   $0x0,(%rbx,%r15,1)
jmp    11b5 <readFileToS+0x65>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
define i8* @readFileToS(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i64
store i8* %0, i8** %3
store i32* null, i32** %4
store i8* null, i8** %5
%8 = load i8*, i8** %3
%9 = call i32 @fileSize(i8* %8)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = add nsw i32 %10, 1
%12 = call i8* @malloc(i32 %11)
store i8* %12, i8** %5
%13 = load i8*, i8** %5
%14 = icmp ne i8* %13, null
br i1 %14, label %16, label %15
15: ; preds = %1
store i8* null, i8** %2
br label %36
16: ; preds = %1
%17 = load i8*, i8** %3
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %18, i32** %4
%19 = load i8*, i8** %5
%20 = load i32, i32* %6
%21 = load i32*, i32** %4
%22 = call i64 @fread(i8* %19, i32 1, i32 %20, i32* %21)
store i64 %22, i64* %7
%23 = load i32*, i32** %4
%24 = call i32 @fclose(i32* %23)
%25 = load i64, i64* %7
%26 = load i32, i32* %6
%27 = sext i32 %26 to i64
%28 = icmp ne i64 %25, %27
br i1 %28, label %29, label %30
29: ; preds = %16
store i8* null, i8** %2
br label %36
30: ; preds = %16
%31 = load i8*, i8** %5
%32 = load i32, i32* %6
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
store i8 0, i8* %34
%35 = load i8*, i8** %5
store i8* %35, i8** %2
br label %36
36: ; preds = %30, %29, %15
%37 = load i8*, i8** %2
ret i8* %37
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define noalias i8* @readFileToS(i8* %0) {
%2 = tail call i32 @fileSize(i8* %0)
%3 = add nsw i32 %2, 1
%4 = tail call i8* @malloc(i32 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %14, label %6
6: ; preds = %1
%7 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = tail call i64 @fread(i8* %4, i32 1, i32 %2, i32* %7)
%9 = tail call i32 @fclose(i32* %7)
%10 = sext i32 %2 to i64
%11 = icmp eq i64 %8, %10
br i1 %11, label %12, label %14
12: ; preds = %6
%13 = getelementptr inbounds i8, i8* %4, i64 %8
store i8 0, i8* %13
br label %14
14: ; preds = %6, %1, %12
%15 = phi i8* [ %4, %12 ], [ null, %1 ], [ null, %6 ]
ret i8* %15
}