#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 */
scalar_t__ gc_alloc(int, char *);
int /*<<< orphan*/ gc_free(char *);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
char *file_read(int fd) {
  int i = 0;
  int len = 128;
  char *buff = (char *)gc_alloc(sizeof(char) * len, "file_read");
  char foo;
  while (1) {
    if (read(fd, &foo, 1) <= 0) {
      break;
    }
    if (i >= len - 2) {
      char *tmp = (char *)gc_alloc(sizeof(char) * len * 2, "file_read");
      len *= 2;
      strcpy(tmp, buff);
      gc_free(buff);
      buff = tmp;
    }
    buff[i] = foo;
    i++;
  }
  buff[i] = '\0';
  return buff;
}


x86 assembly (clang, -O0):
<file_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x80,-0xc(%rbp)
movslq -0xc(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
lea    0xe96(%rip),%rsi
call   1050 <gc_alloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x0,%rax
jg     1193 <file_read+0x53>
jmp    1205 <file_read+0xc5>
mov    -0x8(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x2,%ecx
cmp    %ecx,%eax
jl     11e9 <file_read+0xa9>
movslq -0xc(%rbp),%rax
shl    $0x0,%rax
shl    $0x1,%rax
mov    %eax,%edi
lea    0xe47(%rip),%rsi
call   1050 <gc_alloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0xc(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rdi
call   1060 <gc_free@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x19(%rbp),%dl
mov    -0x18(%rbp),%rax
movslq -0x8(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1173 <file_read+0x33>
mov    -0x18(%rbp),%rax
movslq -0x8(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r14d
lea    0xeab(%rip),%rsi
mov    $0x80,%ebp
mov    $0x80,%edi
call   1050 <gc_alloc@plt>
mov    %rax,%r13
lea    0x7(%rsp),%rsi
mov    %r14d,%edi
mov    $0x1,%edx
call   1040 <read@plt>
test   %rax,%rax
jle    11ed <file_read+0xad>
xor    %r12d,%r12d
lea    0x7(%rsp),%r15
jmp    11b3 <file_read+0x73>
nopl   0x0(%rax,%rax,1)
movzbl 0x7(%rsp),%eax
mov    %al,0x0(%r13,%r12,1)
add    $0x1,%r12
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %rax,%rax
jle    11e8 <file_read+0xa8>
lea    -0x2(%rbp),%eax
cltq
cmp    %rax,%r12
jl     1190 <file_read+0x50>
add    %ebp,%ebp
mov    %ebp,%edi
lea    0xe38(%rip),%rsi
call   1050 <gc_alloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r13,%rsi
call   1030 <strcpy@plt>
mov    %r13,%rdi
call   1060 <gc_free@plt>
mov    %rbx,%r13
jmp    1190 <file_read+0x50>
mov    %r12d,%eax
jmp    11ef <file_read+0xaf>
xor    %eax,%eax
movb   $0x0,0x0(%r13,%rax,1)
mov    %r13,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"file_read\00"
define i8* @file_read(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8
%7 = alloca i8*
store i32 %0, i32* %2
store i32 0, i32* %3
store i32 128, i32* %4
%8 = load i32, i32* %4
%9 = sext i32 %8 to i64
%10 = mul i64 1, %9
%11 = trunc i64 %10 to i32
%12 = call i64 @gc_alloc(i32 %11, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%13 = inttoptr i64 %12 to i8*
store i8* %13, i8** %5
br label %14
14: ; preds = %1, %40
%15 = load i32, i32* %2
%16 = call i64 @read(i32 %15, i8* %6, i32 1)
%17 = icmp sle i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %14
br label %48
19: ; preds = %14
%20 = load i32, i32* %3
%21 = load i32, i32* %4
%22 = sub nsw i32 %21, 2
%23 = icmp sge i32 %20, %22
br i1 %23, label %24, label %40
24: ; preds = %19
%25 = load i32, i32* %4
%26 = sext i32 %25 to i64
%27 = mul i64 1, %26
%28 = mul i64 %27, 2
%29 = trunc i64 %28 to i32
%30 = call i64 @gc_alloc(i32 %29, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%31 = inttoptr i64 %30 to i8*
store i8* %31, i8** %7
%32 = load i32, i32* %4
%33 = mul nsw i32 %32, 2
store i32 %33, i32* %4
%34 = load i8*, i8** %7
%35 = load i8*, i8** %5
%36 = call i32 @strcpy(i8* %34, i8* %35)
%37 = load i8*, i8** %5
%38 = call i32 @gc_free(i8* %37)
%39 = load i8*, i8** %7
store i8* %39, i8** %5
br label %40
40: ; preds = %24, %19
%41 = load i8, i8* %6
%42 = load i8*, i8** %5
%43 = load i32, i32* %3
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
store i8 %41, i8* %45
%46 = load i32, i32* %3
%47 = add nsw i32 %46, 1
store i32 %47, i32* %3
br label %14
48: ; preds = %18
%49 = load i8*, i8** %5
%50 = load i32, i32* %3
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %49, i64 %51
store i8 0, i8* %52
%53 = load i8*, i8** %5
ret i8* %53
}

LLVM IR (-O2):
@.str = [10 x i8] c"file_read\00"
define i8* @file_read(i32 %0) {
%2 = alloca i8
%3 = tail call i64 @gc_alloc(i32 128, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%4 = inttoptr i64 %3 to i8*
%5 = call i64 @read(i32 %0, i8* %2, i32 1)
%6 = icmp slt i64 %5, 1
br i1 %6, label %30, label %7
7: ; preds = %1, %20
%8 = phi i64 [ %25, %20 ], [ 0, %1 ]
%9 = phi i8* [ %22, %20 ], [ %4, %1 ]
%10 = phi i32 [ %21, %20 ], [ 128, %1 ]
%11 = add nsw i32 %10, -2
%12 = sext i32 %11 to i64
%13 = icmp slt i64 %8, %12
br i1 %13, label %20, label %14
14: ; preds = %7
%15 = shl i32 %10, 1
%16 = tail call i64 @gc_alloc(i32 %15, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%17 = inttoptr i64 %16 to i8*
%18 = tail call i32 @strcpy(i8* %17, i8* %9)
%19 = tail call i32 @gc_free(i8* %9)
br label %20
20: ; preds = %14, %7
%21 = phi i32 [ %15, %14 ], [ %10, %7 ]
%22 = phi i8* [ %17, %14 ], [ %9, %7 ]
%23 = load i8, i8* %2
%24 = getelementptr inbounds i8, i8* %22, i64 %8
store i8 %23, i8* %24
%25 = add nuw i64 %8, 1
%26 = call i64 @read(i32 %0, i8* %2, i32 1)
%27 = icmp slt i64 %26, 1
br i1 %27, label %28, label %7
28: ; preds = %20
%29 = and i64 %25, 4294967295
br label %30
30: ; preds = %28, %1
%31 = phi i8* [ %4, %1 ], [ %22, %28 ]
%32 = phi i64 [ 0, %1 ], [ %29, %28 ]
%33 = getelementptr inbounds i8, i8* %31, i64 %32
store i8 0, i8* %33
ret i8* %31
}