#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 ssize_t;

/* Variables and functions */
char *newbuf();
int read(int, char *, int);
char *sizeset(char *, int);
int readtobuf(char **buf, int f) {
  ssize_t w, i;
  *buf = sizeset(newbuf(), 2);
  for (i = 0; (w = read(f, *buf + i, 1)) && w != -1; i++) {
    *buf = sizeset(*buf, i + 2);
  }
  return i;
}


x86 assembly (clang, -O0):
<readtobuf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    $0x0,%al
call   1030 <newbuf@plt>
mov    %rax,%rdi
mov    $0x2,%esi
call   1050 <sizeset@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x14(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
movslq -0x14(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,%ecx
mov    %ecx,-0x10(%rbp)
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x15(%rbp)
je     119c <readtobuf+0x6c>
cmpl   $0xffffffff,-0x10(%rbp)
setne  %al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
test   $0x1,%al
jne    11ac <readtobuf+0x7c>
jmp    11d6 <readtobuf+0xa6>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x14(%rbp),%esi
add    $0x2,%esi
call   1050 <sizeset@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1164 <readtobuf+0x34>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readtobuf>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %rdi,%rbp
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <newbuf@plt>
mov    %rax,%rdi
mov    $0x2,%esi
call   1050 <sizeset@plt>
mov    %rax,0x0(%rbp)
mov    %r14d,%edi
mov    %rax,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
add    $0xffffffff,%eax
cmp    $0xfffffffd,%eax
ja     11a1 <readtobuf+0x71>
xor    %ebx,%ebx
xchg   %ax,%ax
mov    0x0(%rbp),%rdi
lea    0x2(%rbx),%esi
call   1050 <sizeset@plt>
mov    %rax,0x0(%rbp)
lea    (%rax,%rbx,1),%rsi
add    $0x1,%rsi
add    $0x1,%rbx
mov    %r14d,%edi
mov    $0x1,%edx
call   1040 <read@plt>
add    $0xffffffff,%eax
cmp    $0xfffffffe,%eax
jb     1170 <readtobuf+0x40>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @readtobuf(i8** %0, i32 %1) {
%3 = alloca i8**
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8** %0, i8*** %3
store i32 %1, i32* %4
%7 = call i8* (...) @newbuf()
%8 = call i8* @sizeset(i8* %7, i32 2)
%9 = load i8**, i8*** %3
store i8* %8, i8** %9
store i32 0, i32* %6
br label %10
10: ; preds = %31, %2
%11 = load i32, i32* %4
%12 = load i8**, i8*** %3
%13 = load i8*, i8** %12
%14 = load i32, i32* %6
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %13, i64 %15
%17 = call i32 @read(i32 %11, i8* %16, i32 1)
store i32 %17, i32* %5
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %10
%20 = load i32, i32* %5
%21 = icmp ne i32 %20, -1
br label %22
22: ; preds = %19, %10
%23 = phi i1 [ false, %10 ], [ %21, %19 ]
br i1 %23, label %24, label %34
24: ; preds = %22
%25 = load i8**, i8*** %3
%26 = load i8*, i8** %25
%27 = load i32, i32* %6
%28 = add nsw i32 %27, 2
%29 = call i8* @sizeset(i8* %26, i32 %28)
%30 = load i8**, i8*** %3
store i8* %29, i8** %30
br label %31
31: ; preds = %24
%32 = load i32, i32* %6
%33 = add nsw i32 %32, 1
store i32 %33, i32* %6
br label %10
34: ; preds = %22
%35 = load i32, i32* %6
ret i32 %35
}

LLVM IR (-O2):
define i32 @readtobuf(i8** nocapture %0, i32 %1) {
%3 = tail call i8* (...) @newbuf()
%4 = tail call i8* @sizeset(i8* %3, i32 2)
store i8* %4, i8** %0
%5 = tail call i32 @read(i32 %1, i8* %4, i32 1)
%6 = add i32 %5, -1
%7 = icmp ult i32 %6, -2
br i1 %7, label %8, label %21
8: ; preds = %2, %8
%9 = phi i64 [ %14, %8 ], [ 0, %2 ]
%10 = load i8*, i8** %0
%11 = trunc i64 %9 to i32
%12 = add i32 %11, 2
%13 = tail call i8* @sizeset(i8* %10, i32 %12)
store i8* %13, i8** %0
%14 = add nuw i64 %9, 1
%15 = getelementptr inbounds i8, i8* %13, i64 %14
%16 = tail call i32 @read(i32 %1, i8* %15, i32 1)
%17 = add i32 %16, -1
%18 = icmp ult i32 %17, -2
br i1 %18, label %8, label %19
19: ; preds = %8
%20 = trunc i64 %14 to i32
br label %21
21: ; preds = %19, %2
%22 = phi i32 [ 0, %2 ], [ %20, %19 ]
ret i32 %22
}