#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 /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
scalar_t__ fread(char *, int, int, int /*<<< orphan*/ *);
char *malloc(int);
char *realloc(char *, int);
char *read_file(const char *path, int *len) {
  FILE *fd = fopen(path, "rt+");
  if (!fd)
    return NULL;
  int block = 512;
  int size = 512;
  int slen = 0;
  char *buf = malloc(size);
  if (!buf)
    goto err;
  while (!feof(fd)) {
    if (slen + block + 1 > size) {
      size *= 2;
      buf = realloc(buf, size);
      if (!buf)
        goto err;
    }
    slen += fread(&buf[slen], 1, block, fd);
    buf[slen] = '\0';
  }
err:
  if (len)
    *len = slen;
  fclose(fd);
  return buf;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe85(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    119c <read_file+0x3c>
movq   $0x0,-0x8(%rbp)
jmp    1294 <read_file+0x134>
movl   $0x200,-0x24(%rbp)
movl   $0x200,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x28(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11cd <read_file+0x6d>
jmp    126f <read_file+0x10f>
jmp    11d2 <read_file+0x72>
mov    -0x20(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11f0 <read_file+0x90>
jmp    126a <read_file+0x10a>
mov    -0x2c(%rbp),%eax
add    -0x24(%rbp),%eax
add    $0x1,%eax
cmp    -0x28(%rbp),%eax
jle    1230 <read_file+0xd0>
mov    -0x28(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%esi
call   1070 <realloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    122b <read_file+0xcb>
jmp    126f <read_file+0x10f>
jmp    1230 <read_file+0xd0>
mov    -0x38(%rbp),%rdi
movslq -0x2c(%rbp),%rax
add    %rax,%rdi
mov    -0x24(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,%rcx
movslq -0x2c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x38(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11d2 <read_file+0x72>
jmp    126f <read_file+0x10f>
cmpq   $0x0,-0x18(%rbp)
je     1283 <read_file+0x123>
mov    -0x2c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
lea    0xe8b(%rip),%rsi
call   1080 <fopen@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     121b <read_file+0xbb>
mov    %rax,%r15
mov    $0x200,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     1206 <read_file+0xa6>
mov    %rax,%r12
mov    %r15,%rdi
call   1050 <feof@plt>
test   %eax,%eax
jne    1209 <read_file+0xa9>
mov    $0x200,%r13d
xor    %ebp,%ebp
jmp    11e3 <read_file+0x83>
movslq %ebp,%rdi
add    %r12,%rdi
mov    $0x1,%esi
mov    $0x200,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
mov    %rax,%rbx
add    %ebp,%ebx
movslq %ebx,%rax
movb   $0x0,(%r12,%rax,1)
mov    %r15,%rdi
call   1050 <feof@plt>
mov    %ebx,%ebp
test   %eax,%eax
jne    1209 <read_file+0xa9>
lea    0x201(%rbp),%eax
cmp    %r13d,%eax
jle    11b0 <read_file+0x50>
add    %r13d,%r13d
mov    %r12,%rdi
mov    %r13d,%esi
call   1070 <realloc@plt>
mov    %rax,%r12
test   %rax,%rax
jne    11b0 <read_file+0x50>
mov    %ebp,%ebx
xor    %r12d,%r12d
test   %r14,%r14
je     1211 <read_file+0xb1>
mov    %ebx,(%r14)
mov    %r15,%rdi
call   1040 <fclose@plt>
jmp    121e <read_file+0xbe>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [4 x i8] c"rt+\00"
define i8* @read_file(i8* nocapture %0, i32* writeonly %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %40, label %5
5: ; preds = %2
%6 = tail call dereferenceable_or_null(512) i8* @malloc(i32 512)
%7 = icmp eq i8* %6, null
br i1 %7, label %33, label %8
8: ; preds = %5
%9 = tail call i32 @feof(i32* %3)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %33
11: ; preds = %8, %21
%12 = phi i8* [ %23, %21 ], [ %6, %8 ]
%13 = phi i32 [ %28, %21 ], [ 0, %8 ]
%14 = phi i32 [ %22, %21 ], [ 512, %8 ]
%15 = add nsw i32 %13, 513
%16 = icmp sgt i32 %15, %14
br i1 %16, label %17, label %21
17: ; preds = %11
%18 = shl nsw i32 %14, 1
%19 = tail call i8* @realloc(i8* %12, i32 %18)
%20 = icmp eq i8* %19, null
br i1 %20, label %33, label %21
21: ; preds = %17, %11
%22 = phi i32 [ %18, %17 ], [ %14, %11 ]
%23 = phi i8* [ %19, %17 ], [ %12, %11 ]
%24 = sext i32 %13 to i64
%25 = getelementptr inbounds i8, i8* %23, i64 %24
%26 = tail call i64 @fread(i8* %25, i32 1, i32 512, i32* %3)
%27 = trunc i64 %26 to i32
%28 = add i32 %13, %27
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %23, i64 %29
store i8 0, i8* %30
%31 = tail call i32 @feof(i32* %3)
%32 = icmp eq i32 %31, 0
br i1 %32, label %11, label %33
33: ; preds = %17, %21, %8, %5
%34 = phi i32 [ 0, %5 ], [ 0, %8 ], [ %13, %17 ], [ %28, %21 ]
%35 = phi i8* [ null, %5 ], [ %6, %8 ], [ null, %17 ], [ %23, %21 ]
%36 = icmp eq i32* %1, null
br i1 %36, label %38, label %37
37: ; preds = %33
store i32 %34, i32* %1
br label %38
38: ; preds = %37, %33
%39 = tail call i32 @fclose(i32* %3)
br label %40
40: ; preds = %2, %38
%41 = phi i8* [ %35, %38 ], [ null, %2 ]
ret i8* %41
}