#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 EOF;
int fgetc(int /*<<< orphan*/ *);
char *malloc(size_t);
char *realloc(char *, size_t);
char *read_file_contents(FILE *fp) {
  char *content;
  size_t content_length;
  size_t content_bufsize;
  int ch;
  content_bufsize = 1024;
  content = malloc(content_bufsize);
  content_length = 0;
  while ((ch = fgetc(fp)) != EOF) {
    if (content_length + 1 >= content_bufsize - 1) {
      content_bufsize *= 2;
      content = realloc(content, content_bufsize);
    }
    content[content_length] = ch;
    content_length++;
  }
  content[content_length] = '\0';
  return content;
}


x86 assembly (clang, -O0):
<read_file_contents>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x400,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %eax,-0x24(%rbp)
mov    0x2e7c(%rip),%rcx
cmp    (%rcx),%eax
je     11cb <read_file_contents+0x9b>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jb     11aa <read_file_contents+0x7a>
mov    -0x20(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <realloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x24(%rbp),%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1159 <read_file_contents+0x29>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file_contents>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    $0x400,%r13d
mov    $0x400,%edi
call   1040 <malloc@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1030 <fgetc@plt>
mov    %eax,%ebp
mov    0x2e86(%rip),%rax
cmp    (%rax),%ebp
jne    1180 <read_file_contents+0x50>
xor    %r12d,%r12d
movb   $0x0,(%r15,%r12,1)
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %ebx,%ebx
jmp    11ac <read_file_contents+0x7c>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %bpl,(%r15,%rbx,1)
mov    %r14,%rdi
call   1030 <fgetc@plt>
mov    %eax,%ebp
mov    %r12,%rbx
mov    0x2e40(%rip),%rax
cmp    (%rax),%ebp
je     1169 <read_file_contents+0x39>
lea    0x1(%rbx),%r12
lea    -0x1(%r13),%rax
cmp    %rax,%r12
jb     1190 <read_file_contents+0x60>
add    %r13,%r13
mov    %r15,%rdi
mov    %r13,%rsi
call   1050 <realloc@plt>
mov    %rax,%r15
jmp    1190 <read_file_contents+0x60>

LLVM IR (-O0):
@EOF = global i32 0
define i8* @read_file_contents(i32* %0) {
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i32
store i32* %0, i32** %2
store i64 1024, i64* %5
%7 = load i64, i64* %5
%8 = call i8* @malloc(i64 %7)
store i8* %8, i8** %3
store i64 0, i64* %4
br label %9
9: ; preds = %26, %1
%10 = load i32*, i32** %2
%11 = call i32 @fgetc(i32* %10)
store i32 %11, i32* %6
%12 = load i32, i32* @EOF
%13 = icmp ne i32 %11, %12
br i1 %13, label %14, label %34
14: ; preds = %9
%15 = load i64, i64* %4
%16 = add i64 %15, 1
%17 = load i64, i64* %5
%18 = sub i64 %17, 1
%19 = icmp uge i64 %16, %18
br i1 %19, label %20, label %26
20: ; preds = %14
%21 = load i64, i64* %5
%22 = mul i64 %21, 2
store i64 %22, i64* %5
%23 = load i8*, i8** %3
%24 = load i64, i64* %5
%25 = call i8* @realloc(i8* %23, i64 %24)
store i8* %25, i8** %3
br label %26
26: ; preds = %20, %14
%27 = load i32, i32* %6
%28 = trunc i32 %27 to i8
%29 = load i8*, i8** %3
%30 = load i64, i64* %4
%31 = getelementptr inbounds i8, i8* %29, i64 %30
store i8 %28, i8* %31
%32 = load i64, i64* %4
%33 = add i64 %32, 1
store i64 %33, i64* %4
br label %9
34: ; preds = %9
%35 = load i8*, i8** %3
%36 = load i64, i64* %4
%37 = getelementptr inbounds i8, i8* %35, i64 %36
store i8 0, i8* %37
%38 = load i8*, i8** %3
ret i8* %38
}

LLVM IR (-O2):
@EOF = global i32 0
define noalias i8* @read_file_contents(i32* nocapture %0) {
%2 = tail call dereferenceable_or_null(1024) i8* @malloc(i64 1024)
%3 = tail call i32 @fgetc(i32* %0)
%4 = load i32, i32* @EOF
%5 = icmp eq i32 %3, %4
br i1 %5, label %25, label %6
6: ; preds = %1, %17
%7 = phi i32 [ %22, %17 ], [ %3, %1 ]
%8 = phi i8* [ %19, %17 ], [ %2, %1 ]
%9 = phi i64 [ %18, %17 ], [ 1024, %1 ]
%10 = phi i64 [ %11, %17 ], [ 0, %1 ]
%11 = add i64 %10, 1
%12 = add i64 %9, -1
%13 = icmp ult i64 %11, %12
br i1 %13, label %17, label %14
14: ; preds = %6
%15 = shl i64 %9, 1
%16 = tail call i8* @realloc(i8* %8, i64 %15)
br label %17
17: ; preds = %14, %6
%18 = phi i64 [ %15, %14 ], [ %9, %6 ]
%19 = phi i8* [ %16, %14 ], [ %8, %6 ]
%20 = trunc i32 %7 to i8
%21 = getelementptr inbounds i8, i8* %19, i64 %10
store i8 %20, i8* %21
%22 = tail call i32 @fgetc(i32* %0)
%23 = load i32, i32* @EOF
%24 = icmp eq i32 %22, %23
br i1 %24, label %25, label %6
25: ; preds = %17, %1
%26 = phi i64 [ 0, %1 ], [ %11, %17 ]
%27 = phi i8* [ %2, %1 ], [ %19, %17 ]
%28 = getelementptr inbounds i8, i8* %27, i64 %26
store i8 0, i8* %28
ret i8* %27
}