#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 */
char *calloc(size_t, int);
scalar_t__ ferror(int /*<<< orphan*/ *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ puts(char *);
char *realloc(char *, size_t);
char *slurp(FILE *file) {
  size_t size = 1024;
  size_t offset = 0;
  size_t nr = 0;
  char *buf = 0;
  if (!file) {
    return 0;
  }
  buf = calloc(size, 1);
  if (!buf) {
    return 0;
  }
  while (1024 == (nr = fread(&(buf[offset]), 1, 1024, file))) {
    offset = size;
    size += 1024;
    buf = realloc(buf, size);
    if (!buf) {
      return 0;
    }
  }
  if (ferror(file)) {
    puts("slurp: file read failed");
    return 0;
  }
  return buf;
}


x86 assembly (clang, -O0):
<slurp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x400,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1194 <slurp+0x44>
movq   $0x0,-0x8(%rbp)
jmp    126b <slurp+0x11b>
mov    -0x18(%rbp),%rdi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11be <slurp+0x6e>
movq   $0x0,-0x8(%rbp)
jmp    126b <slurp+0x11b>
jmp    11c3 <slurp+0x73>
mov    -0x30(%rbp),%rdi
add    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x400,%edx
call   1050 <fread@plt>
mov    %rax,%rcx
mov    %rcx,-0x28(%rbp)
mov    $0x400,%eax
cmp    %rcx,%rax
jne    1237 <slurp+0xe7>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
add    $0x400,%rax
mov    %rax,-0x18(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <realloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1232 <slurp+0xe2>
movq   $0x0,-0x8(%rbp)
jmp    126b <slurp+0x11b>
jmp    11c3 <slurp+0x73>
mov    -0x10(%rbp),%rdi
call   1040 <ferror@plt>
cmp    $0x0,%rax
je     1263 <slurp+0x113>
lea    0xdaf(%rip),%rdi
call   1030 <puts@plt>
movq   $0x0,-0x8(%rbp)
jmp    126b <slurp+0x11b>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<slurp>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     11d7 <slurp+0x87>
mov    %rdi,%r14
mov    $0x400,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
test   %rax,%rax
je     11d7 <slurp+0x87>
mov    %rax,%r15
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
lea    (%r15,%rbx,1),%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %r14,%rcx
call   1050 <fread@plt>
cmp    $0x400,%rax
jne    11be <slurp+0x6e>
lea    0x800(%rbx),%rsi
mov    %r15,%rdi
call   1070 <realloc@plt>
mov    %rax,%r15
add    $0x400,%rbx
test   %rax,%rax
jne    1180 <slurp+0x30>
jmp    11d7 <slurp+0x87>
mov    %r14,%rdi
call   1040 <ferror@plt>
test   %rax,%rax
je     11da <slurp+0x8a>
lea    0xe2e(%rip),%rdi
call   1030 <puts@plt>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [24 x i8] c"slurp: file read failed\00"
define i8* @slurp(i32* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i8*
store i32* %0, i32** %3
store i64 1024, i64* %4
store i64 0, i64* %5
store i64 0, i64* %6
store i8* null, i8** %7
%8 = load i32*, i32** %3
%9 = icmp ne i32* %8, null
br i1 %9, label %11, label %10
10: ; preds = %1
store i8* null, i8** %2
br label %44
11: ; preds = %1
%12 = load i64, i64* %4
%13 = call i8* @calloc(i64 %12, i32 1)
store i8* %13, i8** %7
%14 = load i8*, i8** %7
%15 = icmp ne i8* %14, null
br i1 %15, label %17, label %16
16: ; preds = %11
store i8* null, i8** %2
br label %44
17: ; preds = %11
br label %18
18: ; preds = %35, %17
%19 = load i8*, i8** %7
%20 = load i64, i64* %5
%21 = getelementptr inbounds i8, i8* %19, i64 %20
%22 = load i32*, i32** %3
%23 = call i64 @fread(i8* %21, i32 1, i32 1024, i32* %22)
store i64 %23, i64* %6
%24 = icmp eq i64 1024, %23
br i1 %24, label %25, label %36
25: ; preds = %18
%26 = load i64, i64* %4
store i64 %26, i64* %5
%27 = load i64, i64* %4
%28 = add i64 %27, 1024
store i64 %28, i64* %4
%29 = load i8*, i8** %7
%30 = load i64, i64* %4
%31 = call i8* @realloc(i8* %29, i64 %30)
store i8* %31, i8** %7
%32 = load i8*, i8** %7
%33 = icmp ne i8* %32, null
br i1 %33, label %35, label %34
34: ; preds = %25
store i8* null, i8** %2
br label %44
35: ; preds = %25
br label %18
36: ; preds = %18
%37 = load i32*, i32** %3
%38 = call i64 @ferror(i32* %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = call i32 @puts(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %2
br label %44
42: ; preds = %36
%43 = load i8*, i8** %7
store i8* %43, i8** %2
br label %44
44: ; preds = %42, %40, %34, %16, %10
%45 = load i8*, i8** %2
ret i8* %45
}

LLVM IR (-O2):
@.str = [24 x i8] c"slurp: file read failed\00"
define i8* @slurp(i32* %0) {
%2 = icmp eq i32* %0, null
br i1 %2, label %22, label %3
3: ; preds = %1
%4 = tail call i8* @calloc(i64 1024, i32 1)
%5 = icmp eq i8* %4, null
br i1 %5, label %22, label %6
6: ; preds = %3, %13
%7 = phi i64 [ %14, %13 ], [ 1024, %3 ]
%8 = phi i64 [ %7, %13 ], [ 0, %3 ]
%9 = phi i8* [ %15, %13 ], [ %4, %3 ]
%10 = getelementptr inbounds i8, i8* %9, i64 %8
%11 = tail call i64 @fread(i8* %10, i32 1, i32 1024, i32* %0)
%12 = icmp eq i64 %11, 1024
br i1 %12, label %13, label %17
13: ; preds = %6
%14 = add i64 %7, 1024
%15 = tail call i8* @realloc(i8* %9, i64 %14)
%16 = icmp eq i8* %15, null
br i1 %16, label %22, label %6
17: ; preds = %6
%18 = tail call i64 @ferror(i32* %0)
%19 = icmp eq i64 %18, 0
br i1 %19, label %22, label %20
20: ; preds = %17
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %13, %17, %3, %1, %20
%23 = phi i8* [ null, %20 ], [ null, %1 ], [ null, %3 ], [ %9, %17 ], [ null, %13 ]
ret i8* %23
}