#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 fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
char *realloc(char *, size_t);
char *create_buffer(FILE *file, size_t *size) {
  char *buffer = malloc(0x1000);
  char *buffer_tmp = NULL;
  size_t buffer_max = 0x1000;
  size_t buffer_size = 0;
  int r;
  while ((r = fread(buffer + buffer_size, 1, 0x1000, file)) > 0) {
    buffer_size += r;
    while (buffer_size + 0x1000 > buffer_max) {
      buffer_max *= 2;
      buffer_tmp = realloc(buffer, buffer_max);
      if (buffer_tmp == NULL) {
        goto error_free;
      }
      buffer = buffer_tmp;
    }
  }
  if (size != NULL) {
    *size = buffer_size;
  }
  return buffer;
error_free:
  free(buffer);
  if (size != NULL) {
    *size = buffer_size;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<create_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    $0x1000,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x1000,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
add    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1000,%edx
call   1040 <fread@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0x0,%eax
jle    11fc <create_buffer+0xbc>
movslq -0x3c(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
add    $0x1000,%rax
cmp    -0x30(%rbp),%rax
jbe    11f7 <create_buffer+0xb7>
mov    -0x30(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1060 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11ea <create_buffer+0xaa>
jmp    121f <create_buffer+0xdf>
mov    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    11a9 <create_buffer+0x69>
jmp    1176 <create_buffer+0x36>
cmpq   $0x0,-0x18(%rbp)
je     1212 <create_buffer+0xd2>
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1246 <create_buffer+0x106>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x18(%rbp)
je     123e <create_buffer+0xfe>
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_buffer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    $0x1000,%edi
call   1050 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
test   %eax,%eax
jle    1201 <create_buffer+0xc1>
mov    $0x1000,%ebx
xor    %r13d,%r13d
mov    %eax,%eax
lea    (%rax,%r13,1),%rbp
add    $0x1000,%rbp
add    %rax,%r13
mov    %r12,%rax
cs nopw 0x0(%rax,%rax,1)
mov    %rax,%r12
cmp    %rbx,%rbp
jbe    11c0 <create_buffer+0x80>
add    %rbx,%rbx
mov    %r12,%rdi
mov    %rbx,%rsi
call   1060 <realloc@plt>
test   %rax,%rax
jne    11a0 <create_buffer+0x60>
jmp    11dc <create_buffer+0x9c>
nopl   (%rax)
lea    (%r12,%r13,1),%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
test   %eax,%eax
jg     1183 <create_buffer+0x43>
jmp    11e7 <create_buffer+0xa7>
mov    %r12,%rdi
call   1030 <free@plt>
xor    %r12d,%r12d
test   %r14,%r14
je     11ef <create_buffer+0xaf>
mov    %r13,(%r14)
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %r13d,%r13d
test   %r14,%r14
jne    11ec <create_buffer+0xac>
jmp    11ef <create_buffer+0xaf>

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

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