#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 EOF;
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
char *realloc(char *, int);
char *string_read(FILE *f) {
  char *res;
  int size = 16, v = 1, i = 0;
  res = malloc(size);
  while (v != 0) {
    v = fgetc(f);
    if (v == EOF) {
      free(res);
      return NULL;
    }
    res[i++] = (char)v;
    if (i == size) {
      size += 16;
      res = realloc(res, size);
    }
  }
  return res;
}


x86 assembly (clang, -O0):
<string_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x10,-0x1c(%rbp)
movl   $0x1,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     11ef <string_read+0xaf>
mov    -0x10(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    0x2e5b(%rip),%rcx
cmp    (%rcx),%eax
jne    11ab <string_read+0x6b>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11f7 <string_read+0xb7>
mov    -0x20(%rbp),%eax
mov    %al,%dl
mov    -0x18(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x24(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jne    11ea <string_read+0xaa>
mov    -0x1c(%rbp),%eax
add    $0x10,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1060 <realloc@plt>
mov    %rax,-0x18(%rbp)
jmp    116d <string_read+0x2d>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    $0x10,%r12d
mov    $0x10,%edi
call   1050 <malloc@plt>
mov    %rax,%r15
mov    $0x1,%ebp
mov    0x2e7b(%rip),%r13
jmp    1178 <string_read+0x38>
nop
add    $0x1,%rbp
test   %ebx,%ebx
je     11b4 <string_read+0x74>
mov    %r14,%rdi
call   1040 <fgetc@plt>
cmp    0x0(%r13),%eax
je     11a9 <string_read+0x69>
mov    %eax,%ebx
mov    %bl,-0x1(%r15,%rbp,1)
mov    %r12d,%eax
cmp    %rax,%rbp
jne    1170 <string_read+0x30>
add    $0x10,%r12d
mov    %r15,%rdi
mov    %r12d,%esi
call   1060 <realloc@plt>
mov    %rax,%r15
jmp    1170 <string_read+0x30>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EOF = global i32 0
define i8* @string_read(i32* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32* %0, i32** %3
store i32 16, i32* %5
store i32 1, i32* %6
store i32 0, i32* %7
%8 = load i32, i32* %5
%9 = call i8* @malloc(i32 %8)
store i8* %9, i8** %4
br label %10
10: ; preds = %39, %1
%11 = load i32, i32* %6
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %40
13: ; preds = %10
%14 = load i32*, i32** %3
%15 = call i32 @fgetc(i32* %14)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = load i32, i32* @EOF
%18 = icmp eq i32 %16, %17
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = load i8*, i8** %4
%21 = call i32 @free(i8* %20)
store i8* null, i8** %2
br label %42
22: ; preds = %13
%23 = load i32, i32* %6
%24 = trunc i32 %23 to i8
%25 = load i8*, i8** %4
%26 = load i32, i32* %7
%27 = add nsw i32 %26, 1
store i32 %27, i32* %7
%28 = sext i32 %26 to i64
%29 = getelementptr inbounds i8, i8* %25, i64 %28
store i8 %24, i8* %29
%30 = load i32, i32* %7
%31 = load i32, i32* %5
%32 = icmp eq i32 %30, %31
br i1 %32, label %33, label %39
33: ; preds = %22
%34 = load i32, i32* %5
%35 = add nsw i32 %34, 16
store i32 %35, i32* %5
%36 = load i8*, i8** %4
%37 = load i32, i32* %5
%38 = call i8* @realloc(i8* %36, i32 %37)
store i8* %38, i8** %4
br label %39
39: ; preds = %33, %22
br label %10
40: ; preds = %10
%41 = load i8*, i8** %4
store i8* %41, i8** %2
br label %42
42: ; preds = %40, %19
%43 = load i8*, i8** %2
ret i8* %43
}

LLVM IR (-O2):
@EOF = global i32 0
define i8* @string_read(i32* nocapture %0) {
%2 = tail call dereferenceable_or_null(16) i8* @malloc(i32 16)
br label %3
3: ; preds = %1, %21
%4 = phi i64 [ 0, %1 ], [ %14, %21 ]
%5 = phi i32 [ 16, %1 ], [ %23, %21 ]
%6 = phi i8* [ %2, %1 ], [ %22, %21 ]
%7 = tail call i32 @fgetc(i32* %0)
%8 = load i32, i32* @EOF
%9 = icmp eq i32 %7, %8
br i1 %9, label %10, label %12
10: ; preds = %3
%11 = tail call i32 @free(i8* %6)
br label %25
12: ; preds = %3
%13 = trunc i32 %7 to i8
%14 = add nuw i64 %4, 1
%15 = getelementptr inbounds i8, i8* %6, i64 %4
store i8 %13, i8* %15
%16 = zext i32 %5 to i64
%17 = icmp eq i64 %14, %16
br i1 %17, label %18, label %21
18: ; preds = %12
%19 = add nsw i32 %5, 16
%20 = tail call i8* @realloc(i8* %6, i32 %19)
br label %21
21: ; preds = %18, %12
%22 = phi i8* [ %20, %18 ], [ %6, %12 ]
%23 = phi i32 [ %19, %18 ], [ %5, %12 ]
%24 = icmp eq i32 %7, 0
br i1 %24, label %25, label %3
25: ; preds = %21, %10
%26 = phi i8* [ null, %10 ], [ %22, %21 ]
ret i8* %26
}