#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 size_t off_t;

/* Variables and functions */
int check(int /*<<< orphan*/, char *);
char *check_malloc(size_t);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
int /*<<< orphan*/ read(int, char *, int);
char *reads(int fd) {
  size_t buf_sz = 64;
  char *buf = check_malloc(buf_sz);
  off_t off = 0;
  char next;
  while (check(read(fd, &next, 1), "reads") == 1) {
    if (off >= buf_sz - 1) {
      buf_sz *= 2;
      char *tmp = check_malloc(buf_sz);
      memcpy(tmp, buf, off);
      free(buf);
      buf = tmp;
    }
    buf[off] = next;
    off++;
  }
  buf[off] = 0;
  return buf;
}


x86 assembly (clang, -O0):
<reads>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
movq   $0x40,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <check_malloc@plt>
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,%edi
lea    0xe6e(%rip),%rsi
call   1070 <check@plt>
cmp    $0x1,%eax
jne    120f <reads+0xbf>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jb     11f0 <reads+0xa0>
mov    -0x10(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <check_malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x21(%rbp),%dl
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1178 <reads+0x28>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reads>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r15d
mov    $0x40,%ebx
mov    $0x40,%edi
call   1060 <check_malloc@plt>
mov    %rax,%r12
lea    0xf(%rsp),%rsi
mov    %r15d,%edi
mov    $0x1,%edx
call   1040 <read@plt>
lea    0xe74(%rip),%rsi
mov    %eax,%edi
call   1070 <check@plt>
xor    %ebp,%ebp
cmp    $0x1,%eax
jne    1218 <reads+0xc8>
lea    0xf(%rsp),%r13
lea    0xe5a(%rip),%r14
jmp    11dc <reads+0x8c>
nopl   0x0(%rax,%rax,1)
movzbl 0xf(%rsp),%eax
mov    %al,(%r12,%rbp,1)
add    $0x1,%rbp
mov    %r15d,%edi
mov    %r13,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,%edi
mov    %r14,%rsi
call   1070 <check@plt>
cmp    $0x1,%eax
jne    1218 <reads+0xc8>
lea    -0x1(%rbx),%rax
cmp    %rax,%rbp
jb     11b0 <reads+0x60>
add    %rbx,%rbx
mov    %rbx,0x10(%rsp)
mov    %rbx,%rdi
call   1060 <check_malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r12,%rsi
mov    %rbp,%rdx
call   1050 <memcpy@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %rbx,%r12
mov    0x10(%rsp),%rbx
jmp    11b0 <reads+0x60>
movb   $0x0,(%r12,%rbp,1)
mov    %r12,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"reads\00"
define i8* @reads(i32 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8
%7 = alloca i8*
store i32 %0, i32* %2
store i64 64, i64* %3
%8 = load i64, i64* %3
%9 = call i8* @check_malloc(i64 %8)
store i8* %9, i8** %4
store i64 0, i64* %5
br label %10
10: ; preds = %32, %1
%11 = load i32, i32* %2
%12 = call i32 @read(i32 %11, i8* %6, i32 1)
%13 = call i32 @check(i32 %12, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%14 = icmp eq i32 %13, 1
br i1 %14, label %15, label %39
15: ; preds = %10
%16 = load i64, i64* %5
%17 = load i64, i64* %3
%18 = sub i64 %17, 1
%19 = icmp uge i64 %16, %18
br i1 %19, label %20, label %32
20: ; preds = %15
%21 = load i64, i64* %3
%22 = mul i64 %21, 2
store i64 %22, i64* %3
%23 = load i64, i64* %3
%24 = call i8* @check_malloc(i64 %23)
store i8* %24, i8** %7
%25 = load i8*, i8** %7
%26 = load i8*, i8** %4
%27 = load i64, i64* %5
%28 = call i32 @memcpy(i8* %25, i8* %26, i64 %27)
%29 = load i8*, i8** %4
%30 = call i32 @free(i8* %29)
%31 = load i8*, i8** %7
store i8* %31, i8** %4
br label %32
32: ; preds = %20, %15
%33 = load i8, i8* %6
%34 = load i8*, i8** %4
%35 = load i64, i64* %5
%36 = getelementptr inbounds i8, i8* %34, i64 %35
store i8 %33, i8* %36
%37 = load i64, i64* %5
%38 = add i64 %37, 1
store i64 %38, i64* %5
br label %10
39: ; preds = %10
%40 = load i8*, i8** %4
%41 = load i64, i64* %5
%42 = getelementptr inbounds i8, i8* %40, i64 %41
store i8 0, i8* %42
%43 = load i8*, i8** %4
ret i8* %43
}

LLVM IR (-O2):
@.str = [6 x i8] c"reads\00"
define i8* @reads(i32 %0) {
%2 = alloca i8
%3 = tail call i8* @check_malloc(i64 64)
%4 = call i32 @read(i32 %0, i8* %2, i32 1)
%5 = tail call i32 @check(i32 %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32 %5, 1
br i1 %6, label %7, label %27
7: ; preds = %1, %18
%8 = phi i64 [ %20, %18 ], [ 64, %1 ]
%9 = phi i64 [ %23, %18 ], [ 0, %1 ]
%10 = phi i8* [ %19, %18 ], [ %3, %1 ]
%11 = add i64 %8, -1
%12 = icmp ult i64 %9, %11
br i1 %12, label %18, label %13
13: ; preds = %7
%14 = shl i64 %8, 1
%15 = tail call i8* @check_malloc(i64 %14)
%16 = tail call i32 @memcpy(i8* %15, i8* %10, i64 %9)
%17 = tail call i32 @free(i8* %10)
br label %18
18: ; preds = %13, %7
%19 = phi i8* [ %15, %13 ], [ %10, %7 ]
%20 = phi i64 [ %14, %13 ], [ %8, %7 ]
%21 = load i8, i8* %2
%22 = getelementptr inbounds i8, i8* %19, i64 %9
store i8 %21, i8* %22
%23 = add i64 %9, 1
%24 = call i32 @read(i32 %0, i8* %2, i32 1)
%25 = tail call i32 @check(i32 %24, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%26 = icmp eq i32 %25, 1
br i1 %26, label %7, label %27
27: ; preds = %18, %1
%28 = phi i8* [ %3, %1 ], [ %19, %18 ]
%29 = phi i64 [ 0, %1 ], [ %23, %18 ]
%30 = getelementptr inbounds i8, i8* %28, i64 %29
store i8 0, i8* %30
ret i8* %28
}