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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
bool read_text_file(char *dst, size_t buffer_size, size_t *bytes_read,
                    const char *file) {
  if (!dst || !file || buffer_size == 0)
    return false;
  *dst = '\0';
  *bytes_read = 0;
  FILE *f = fopen(file, "rt");
  if (!f)
    return false;
  char buff[1024];
  size_t current_index = 0;
  while (!feof(f) && current_index < (buffer_size - 1)) {
    size_t n = fread(buff, sizeof(buff[0]), sizeof(buff) / sizeof(buff[0]), f);
    size_t space_available = buffer_size - 1 - current_index;
    size_t to_copy = n > space_available ? space_available : n;
    memcpy(&dst[current_index], buff, to_copy);
    current_index += to_copy;
  }
  fclose(f);
  *bytes_read = current_index;
  dst[current_index] = '\0';
  return true;
}


x86 assembly (clang, -O0):
<read_text_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x460,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     118c <read_text_file+0x3c>
cmpq   $0x0,-0x28(%rbp)
je     118c <read_text_file+0x3c>
cmpq   $0x0,-0x18(%rbp)
jne    1198 <read_text_file+0x48>
movl   $0x0,-0x4(%rbp)
jmp    1309 <read_text_file+0x1b9>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x28(%rbp),%rdi
lea    0xe4b(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11d5 <read_text_file+0x85>
movl   $0x0,-0x4(%rbp)
jmp    1309 <read_text_file+0x1b9>
movq   $0x0,-0x438(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <feof@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x451(%rbp)
jne    1217 <read_text_file+0xc7>
mov    -0x438(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
setb   %al
mov    %al,-0x451(%rbp)
mov    -0x451(%rbp),%al
test   $0x1,%al
jne    122a <read_text_file+0xda>
jmp    12dc <read_text_file+0x18c>
lea    -0x430(%rbp),%rdi
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
mov    $0x400,%edx
call   1030 <fread@plt>
mov    %rax,-0x440(%rbp)
mov    -0x18(%rbp),%rax
sub    $0x1,%rax
sub    -0x438(%rbp),%rax
mov    %rax,-0x448(%rbp)
mov    -0x440(%rbp),%rax
cmp    -0x448(%rbp),%rax
jbe    1288 <read_text_file+0x138>
mov    -0x448(%rbp),%rax
mov    %rax,-0x460(%rbp)
jmp    1296 <read_text_file+0x146>
mov    -0x440(%rbp),%rax
mov    %rax,-0x460(%rbp)
mov    -0x460(%rbp),%rax
mov    %rax,-0x450(%rbp)
mov    -0x10(%rbp),%rdi
add    -0x438(%rbp),%rdi
lea    -0x430(%rbp),%rsi
mov    -0x450(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x450(%rbp),%rax
add    -0x438(%rbp),%rax
mov    %rax,-0x438(%rbp)
jmp    11e0 <read_text_file+0x90>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x438(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    -0x438(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x460,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_text_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
xor    %ebx,%ebx
test   %rsi,%rsi
je     1232 <read_text_file+0xe2>
mov    %rdi,%r15
test   %rdi,%rdi
je     1232 <read_text_file+0xe2>
test   %rcx,%rcx
je     1232 <read_text_file+0xe2>
mov    %rdx,%rbp
mov    %rsi,%r12
movb   $0x0,(%r15)
movq   $0x0,(%rdx)
lea    0xe67(%rip),%rsi
mov    %rcx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     1232 <read_text_file+0xe2>
mov    %rax,%r13
mov    %rbp,0x8(%rsp)
mov    %rax,%rdi
call   1050 <feof@plt>
xor    %ebx,%ebx
test   %eax,%eax
jne    1218 <read_text_file+0xc8>
add    $0xffffffffffffffff,%r12
je     1218 <read_text_file+0xc8>
xor    %ebx,%ebx
lea    0x10(%rsp),%rbp
nopl   (%rax)
mov    %rbx,%r14
mov    %rbp,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %r13,%rcx
call   1030 <fread@plt>
mov    %r12,%rbx
sub    %r14,%rbx
cmp    %rbx,%rax
cmovbe %rax,%rbx
lea    (%r15,%r14,1),%rdi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1060 <memcpy@plt>
add    %r14,%rbx
mov    %r13,%rdi
call   1050 <feof@plt>
test   %eax,%eax
jne    1218 <read_text_file+0xc8>
cmp    %rbx,%r12
ja     11d0 <read_text_file+0x80>
mov    %r13,%rdi
call   1040 <fclose@plt>
mov    0x8(%rsp),%rax
mov    %rbx,(%rax)
movb   $0x0,(%r15,%rbx,1)
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rt\00"
define i32 @read_text_file(i8* %0, i64 %1, i64* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64*
%9 = alloca i8*
%10 = alloca i32*
%11 = alloca [1024 x i8]
%12 = alloca i64
%13 = alloca i64
%14 = alloca i64
%15 = alloca i64
store i8* %0, i8** %6
store i64 %1, i64* %7
store i64* %2, i64** %8
store i8* %3, i8** %9
%16 = load i8*, i8** %6
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %24
18: ; preds = %4
%19 = load i8*, i8** %9
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = load i64, i64* %7
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %21, %18, %4
store i32 0, i32* %5
br label %79
25: ; preds = %21
%26 = load i8*, i8** %6
store i8 0, i8* %26
%27 = load i64*, i64** %8
store i64 0, i64* %27
%28 = load i8*, i8** %9
%29 = call i32* @fopen(i8* %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %29, i32** %10
%30 = load i32*, i32** %10
%31 = icmp ne i32* %30, null
br i1 %31, label %33, label %32
32: ; preds = %25
store i32 0, i32* %5
br label %79
33: ; preds = %25
store i64 0, i64* %12
br label %34
34: ; preds = %60, %33
%35 = load i32*, i32** %10
%36 = call i32 @feof(i32* %35)
%37 = icmp ne i32 %36, 0
br i1 %37, label %43, label %38
38: ; preds = %34
%39 = load i64, i64* %12
%40 = load i64, i64* %7
%41 = sub i64 %40, 1
%42 = icmp ult i64 %39, %41
br label %43
43: ; preds = %38, %34
%44 = phi i1 [ false, %34 ], [ %42, %38 ]
br i1 %44, label %45, label %71
45: ; preds = %43
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%47 = load i32*, i32** %10
%48 = call i64 @fread(i8* %46, i32 1, i32 1024, i32* %47)
store i64 %48, i64* %13
%49 = load i64, i64* %7
%50 = sub i64 %49, 1
%51 = load i64, i64* %12
%52 = sub i64 %50, %51
store i64 %52, i64* %14
%53 = load i64, i64* %13
%54 = load i64, i64* %14
%55 = icmp ugt i64 %53, %54
br i1 %55, label %56, label %58
56: ; preds = %45
%57 = load i64, i64* %14
br label %60
58: ; preds = %45
%59 = load i64, i64* %13
br label %60
60: ; preds = %58, %56
%61 = phi i64 [ %57, %56 ], [ %59, %58 ]
store i64 %61, i64* %15
%62 = load i8*, i8** %6
%63 = load i64, i64* %12
%64 = getelementptr inbounds i8, i8* %62, i64 %63
%65 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%66 = load i64, i64* %15
%67 = call i32 @memcpy(i8* %64, i8* %65, i64 %66)
%68 = load i64, i64* %15
%69 = load i64, i64* %12
%70 = add i64 %69, %68
store i64 %70, i64* %12
br label %34
71: ; preds = %43
%72 = load i32*, i32** %10
%73 = call i32 @fclose(i32* %72)
%74 = load i64, i64* %12
%75 = load i64*, i64** %8
store i64 %74, i64* %75
%76 = load i8*, i8** %6
%77 = load i64, i64* %12
%78 = getelementptr inbounds i8, i8* %76, i64 %77
store i8 0, i8* %78
store i32 1, i32* %5
br label %79
79: ; preds = %71, %32, %24
%80 = load i32, i32* %5
ret i32 %80
}

LLVM IR (-O2):
@.str = [3 x i8] c"rt\00"
define i32 @read_text_file(i8* %0, i64 %1, i64* nocapture writeonly %2, i8* %3) {
%5 = alloca [1024 x i8]
%6 = icmp eq i8* %0, null
%7 = icmp eq i8* %3, null
%8 = or i1 %6, %7
%9 = icmp eq i64 %1, 0
%10 = or i1 %9, %8
br i1 %10, label %38, label %11
11: ; preds = %4
store i8 0, i8* %0
store i64 0, i64* %2
%12 = tail call i32* @fopen(i8* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %38, label %14
14: ; preds = %11
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%16 = call i32 @feof(i32* %12)
%17 = icmp eq i32 %16, 0
%18 = add i64 %1, -1
%19 = icmp ne i64 %18, 0
%20 = and i1 %17, %19
br i1 %20, label %21, label %34
21: ; preds = %14, %21
%22 = phi i64 [ %29, %21 ], [ 0, %14 ]
%23 = call i64 @fread(i8* %15, i32 1, i32 1024, i32* %12)
%24 = sub i64 %18, %22
%25 = icmp ugt i64 %23, %24
%26 = select i1 %25, i64 %24, i64 %23
%27 = getelementptr inbounds i8, i8* %0, i64 %22
%28 = call i32 @memcpy(i8* %27, i8* %15, i64 %26)
%29 = add i64 %26, %22
%30 = call i32 @feof(i32* %12)
%31 = icmp eq i32 %30, 0
%32 = icmp ugt i64 %18, %29
%33 = select i1 %31, i1 %32, i1 false
br i1 %33, label %21, label %34
34: ; preds = %21, %14
%35 = phi i64 [ 0, %14 ], [ %29, %21 ]
%36 = call i32 @fclose(i32* %12)
store i64 %35, i64* %2
%37 = getelementptr inbounds i8, i8* %0, i64 %35
store i8 0, i8* %37
br label %38
38: ; preds = %34, %11, %4
%39 = phi i32 [ 0, %4 ], [ 1, %34 ], [ 0, %11 ]
ret i32 %39
}