#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
char *malloc(int);
int open(char const *, int /*<<< orphan*/);
int read(int, char *, int);
char *realloc(char *, int);
char *get_file(const char *path) {
  int i = 0;
  int r = 0;
  int fd = open(path, O_RDONLY);
  char *str = malloc(1024 + 1);
  while ((r = read(fd, &str[i], 1024)) > 0) {
    i = r + i;
    str = realloc(str, i + 1024 + 1);
  }
  str[i] = '\0';
  return (str);
}


x86 assembly (clang, -O0):
<get_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
mov    $0x401,%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
movslq -0xc(%rbp),%rax
add    %rax,%rsi
mov    $0x400,%edx
call   1030 <read@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jle    11c8 <get_file+0x88>
mov    -0x10(%rbp),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%esi
add    $0x400,%esi
add    $0x1,%esi
call   1050 <realloc@plt>
mov    %rax,-0x20(%rbp)
jmp    117d <get_file+0x3d>
mov    -0x20(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e9a(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,%r14d
mov    $0x401,%edi
call   1040 <malloc@plt>
mov    %rax,%rbp
mov    %r14d,%edi
mov    %rax,%rsi
mov    $0x400,%edx
call   1030 <read@plt>
test   %eax,%eax
jle    11b7 <get_file+0x77>
xor    %ecx,%ecx
nopl   0x0(%rax,%rax,1)
lea    (%rax,%rcx,1),%r15d
lea    (%rax,%rcx,1),%esi
add    $0x401,%esi
mov    %rbp,%rdi
call   1050 <realloc@plt>
mov    %rax,%rbp
movslq %r15d,%rbx
add    %rax,%rbx
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1030 <read@plt>
mov    %r15d,%ecx
test   %eax,%eax
jg     1180 <get_file+0x40>
jmp    11ba <get_file+0x7a>
mov    %rbp,%rbx
movb   $0x0,(%rbx)
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
define i8* @get_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %2
store i32 0, i32* %3
store i32 0, i32* %4
%7 = load i8*, i8** %2
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %7, i32 %8)
store i32 %9, i32* %5
%10 = call i8* @malloc(i32 1025)
store i8* %10, i8** %6
br label %11
11: ; preds = %19, %1
%12 = load i32, i32* %5
%13 = load i8*, i8** %6
%14 = load i32, i32* %3
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %13, i64 %15
%17 = call i32 @read(i32 %12, i8* %16, i32 1024)
store i32 %17, i32* %4
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %28
19: ; preds = %11
%20 = load i32, i32* %4
%21 = load i32, i32* %3
%22 = add nsw i32 %20, %21
store i32 %22, i32* %3
%23 = load i8*, i8** %6
%24 = load i32, i32* %3
%25 = add nsw i32 %24, 1024
%26 = add nsw i32 %25, 1
%27 = call i8* @realloc(i8* %23, i32 %26)
store i8* %27, i8** %6
br label %11
28: ; preds = %11
%29 = load i8*, i8** %6
%30 = load i32, i32* %3
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
store i8 0, i8* %32
%33 = load i8*, i8** %6
ret i8* %33
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
define i8* @get_file(i8* nocapture %0) {
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* %0, i32 %2)
%4 = tail call dereferenceable_or_null(1025) i8* @malloc(i32 1025)
%5 = tail call i32 @read(i32 %3, i8* %4, i32 1024)
%6 = icmp sgt i32 %5, 0
br i1 %6, label %7, label %18
7: ; preds = %1, %7
%8 = phi i32 [ %16, %7 ], [ %5, %1 ]
%9 = phi i8* [ %13, %7 ], [ %4, %1 ]
%10 = phi i32 [ %11, %7 ], [ 0, %1 ]
%11 = add nsw i32 %8, %10
%12 = add nsw i32 %11, 1025
%13 = tail call i8* @realloc(i8* %9, i32 %12)
%14 = sext i32 %11 to i64
%15 = getelementptr inbounds i8, i8* %13, i64 %14
%16 = tail call i32 @read(i32 %3, i8* %15, i32 1024)
%17 = icmp sgt i32 %16, 0
br i1 %17, label %7, label %18
18: ; preds = %7, %1
%19 = phi i8* [ %4, %1 ], [ %13, %7 ]
%20 = phi i8* [ %4, %1 ], [ %15, %7 ]
store i8 0, i8* %20
ret i8* %19
}