#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*/ feof(int /*<<< orphan*/);
int /*<<< orphan*/ ferror(int /*<<< orphan*/);
scalar_t__ fread(char *, int, int, int /*<<< orphan*/);
char *malloc(int);
char *realloc(char *, int);
int /*<<< orphan*/ stdin;
int read_file(char **edit_buffer, int *fails) {
  size_t file_size;
  size_t data_read = 0;
  *edit_buffer = malloc(0);
  while (!ferror(stdin) && !feof(stdin)) {
    *edit_buffer = realloc(*edit_buffer, 4096 + data_read);
    if (*edit_buffer == NULL) {
      *fails = 1;
      return 0;
    }
    data_read += fread(*edit_buffer, 1, 4096, stdin);
  }
  if (data_read == 0) {
    *edit_buffer = malloc(4096);
    if (*edit_buffer == NULL) {
      *fails = 1;
      return 0;
    }
  }
  return data_read;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
xor    %edi,%edi
call   1060 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    0x2e68(%rip),%rax
mov    (%rax),%edi
call   1030 <ferror@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x29(%rbp)
jne    11b0 <read_file+0x60>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
call   1050 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    11c0 <read_file+0x70>
jmp    1232 <read_file+0xe2>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%rax
add    $0x1000,%rax
mov    %eax,%esi
call   1070 <realloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1206 <read_file+0xb6>
mov    -0x18(%rbp),%rax
movl   $0x1,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1281 <read_file+0x131>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    0x2dd4(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
mov    $0x1000,%edx
call   1040 <fread@plt>
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    1179 <read_file+0x29>
cmpq   $0x0,-0x28(%rbp)
jne    127a <read_file+0x12a>
mov    $0x1000,%edi
call   1060 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1275 <read_file+0x125>
mov    -0x18(%rbp),%rax
movl   $0x1,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1281 <read_file+0x131>
jmp    127a <read_file+0x12a>
mov    -0x28(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
xor    %edi,%edi
call   1060 <malloc@plt>
mov    %rax,(%r15)
mov    0x2e79(%rip),%r12
mov    (%r12),%edi
call   1030 <ferror@plt>
test   %eax,%eax
je     1199 <read_file+0x49>
mov    $0x1000,%edi
call   1060 <malloc@plt>
mov    %rax,(%r15)
xor    %ebx,%ebx
test   %rax,%rax
jne    11f9 <read_file+0xa9>
movl   $0x1,(%r14)
jmp    11f9 <read_file+0xa9>
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    (%r12),%edi
call   1050 <feof@plt>
test   %eax,%eax
jne    11e9 <read_file+0x99>
mov    (%r15),%rdi
lea    0x1000(%rbx),%esi
call   1070 <realloc@plt>
mov    %rax,(%r15)
test   %rax,%rax
je     11f0 <read_file+0xa0>
mov    (%r12),%ecx
mov    %rax,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
call   1040 <fread@plt>
add    %rax,%rbx
mov    (%r12),%edi
call   1030 <ferror@plt>
test   %eax,%eax
je     11a0 <read_file+0x50>
test   %rbx,%rbx
jne    11f9 <read_file+0xa9>
jmp    117c <read_file+0x2c>
movl   $0x1,(%r14)
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stdin = global i32 0
define i32 @read_file(i8** %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i64
store i8** %0, i8*** %4
store i32* %1, i32** %5
store i64 0, i64* %7
%8 = call i8* @malloc(i32 0)
%9 = load i8**, i8*** %4
store i8* %8, i8** %9
br label %10
10: ; preds = %34, %2
%11 = load i32, i32* @stdin
%12 = call i32 @ferror(i32 %11)
%13 = icmp ne i32 %12, 0
br i1 %13, label %19, label %14
14: ; preds = %10
%15 = load i32, i32* @stdin
%16 = call i32 @feof(i32 %15)
%17 = icmp ne i32 %16, 0
%18 = xor i1 %17, true
br label %19
19: ; preds = %14, %10
%20 = phi i1 [ false, %10 ], [ %18, %14 ]
br i1 %20, label %21, label %41
21: ; preds = %19
%22 = load i8**, i8*** %4
%23 = load i8*, i8** %22
%24 = load i64, i64* %7
%25 = add i64 4096, %24
%26 = trunc i64 %25 to i32
%27 = call i8* @realloc(i8* %23, i32 %26)
%28 = load i8**, i8*** %4
store i8* %27, i8** %28
%29 = load i8**, i8*** %4
%30 = load i8*, i8** %29
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %34
32: ; preds = %21
%33 = load i32*, i32** %5
store i32 1, i32* %33
store i32 0, i32* %3
br label %56
34: ; preds = %21
%35 = load i8**, i8*** %4
%36 = load i8*, i8** %35
%37 = load i32, i32* @stdin
%38 = call i64 @fread(i8* %36, i32 1, i32 4096, i32 %37)
%39 = load i64, i64* %7
%40 = add i64 %39, %38
store i64 %40, i64* %7
br label %10
41: ; preds = %19
%42 = load i64, i64* %7
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %53
44: ; preds = %41
%45 = call i8* @malloc(i32 4096)
%46 = load i8**, i8*** %4
store i8* %45, i8** %46
%47 = load i8**, i8*** %4
%48 = load i8*, i8** %47
%49 = icmp eq i8* %48, null
br i1 %49, label %50, label %52
50: ; preds = %44
%51 = load i32*, i32** %5
store i32 1, i32* %51
store i32 0, i32* %3
br label %56
52: ; preds = %44
br label %53
53: ; preds = %52, %41
%54 = load i64, i64* %7
%55 = trunc i64 %54 to i32
store i32 %55, i32* %3
br label %56
56: ; preds = %53, %50, %32
%57 = load i32, i32* %3
ret i32 %57
}

LLVM IR (-O2):
@stdin = global i32 0
define i32 @read_file(i8** nocapture %0, i32* nocapture writeonly %1) {
%3 = tail call i8* @malloc(i32 0)
store i8* %3, i8** %0
%4 = load i32, i32* @stdin
%5 = tail call i32 @ferror(i32 %4)
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %29
7: ; preds = %2, %19
%8 = phi i64 [ %22, %19 ], [ 0, %2 ]
%9 = load i32, i32* @stdin
%10 = tail call i32 @feof(i32 %9)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %26
12: ; preds = %7
%13 = load i8*, i8** %0
%14 = trunc i64 %8 to i32
%15 = add i32 %14, 4096
%16 = tail call i8* @realloc(i8* %13, i32 %15)
store i8* %16, i8** %0
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %19
18: ; preds = %12
store i32 1, i32* %1
br label %36
19: ; preds = %12
%20 = load i32, i32* @stdin
%21 = tail call i64 @fread(i8* %16, i32 1, i32 4096, i32 %20)
%22 = add i64 %21, %8
%23 = load i32, i32* @stdin
%24 = tail call i32 @ferror(i32 %23)
%25 = icmp eq i32 %24, 0
br i1 %25, label %7, label %26
26: ; preds = %7, %19
%27 = phi i64 [ %8, %7 ], [ %22, %19 ]
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %2, %26
%30 = tail call dereferenceable_or_null(4096) i8* @malloc(i32 4096)
store i8* %30, i8** %0
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %33
32: ; preds = %29
store i32 1, i32* %1
br label %36
33: ; preds = %29, %26
%34 = phi i64 [ 0, %29 ], [ %27, %26 ]
%35 = trunc i64 %34 to i32
br label %36
36: ; preds = %33, %32, %18
%37 = phi i32 [ 0, %32 ], [ %35, %33 ], [ 0, %18 ]
ret i32 %37
}