#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 */
size_t BUFSIZ;
int EOF;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(unsigned char *);
int getc(int /*<<< orphan*/ *);
unsigned char *malloc(int);
unsigned char *realloc(unsigned char *, size_t);
unsigned char *readline(FILE *f) {
  unsigned char *buf, *p;
  size_t buflen = BUFSIZ;
  int c;
  buf = p = malloc(sizeof(char) * buflen);
  if (buf == NULL)
    exit(1);
  for (;;) {
    c = getc(f);
    if (c == EOF) {
      if (buf == p) {
        free(buf);
        return NULL;
      }
      break;
    }
    *p++ = c;
    if (p >= buf + buflen - 1) {
      buflen *= 2;
      buf = realloc(buf, buflen);
      if (buf == NULL)
        exit(1);
    }
    if (c == '\n')
      break;
  }
  *p++ = '\0';
  return buf;
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1196 <readline+0x46>
mov    $0x1,%edi
call   1060 <exit@plt>
jmp    119b <readline+0x4b>
mov    -0x10(%rbp),%rdi
call   1070 <getc@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    0x2e37(%rip),%rcx
cmp    (%rcx),%eax
jne    11e2 <readline+0x92>
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
jne    11dd <readline+0x8d>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1276 <readline+0x126>
jmp    125c <readline+0x10c>
mov    -0x2c(%rbp),%eax
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
add    -0x28(%rbp),%rcx
add    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jb     1248 <readline+0xf8>
mov    -0x28(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1050 <realloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1243 <readline+0xf3>
mov    $0x1,%edi
call   1060 <exit@plt>
jmp    1248 <readline+0xf8>
cmpl   $0xa,-0x2c(%rbp)
jne    1257 <readline+0x107>
jmp    125c <readline+0x10c>
jmp    119b <readline+0x4b>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e73(%rip),%rax
mov    (%rax),%rbx
mov    %ebx,%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     11d0 <readline+0x80>
mov    %rax,%r15
mov    0x2e6a(%rip),%r13
mov    %rax,%r12
jmp    1195 <readline+0x45>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmp    $0xa,%ebp
je     11df <readline+0x8f>
mov    %r14,%rdi
call   1070 <getc@plt>
cmp    0x0(%r13),%eax
je     11da <readline+0x8a>
mov    %eax,%ebp
mov    %bpl,(%r12)
add    $0x1,%r12
lea    (%r15,%rbx,1),%rax
add    $0xffffffffffffffff,%rax
cmp    %rax,%r12
jb     1190 <readline+0x40>
add    %rbx,%rbx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1050 <realloc@plt>
mov    %rax,%r15
test   %rax,%rax
jne    1190 <readline+0x40>
mov    $0x1,%edi
call   1060 <exit@plt>
cmp    %r15,%r12
je     11f6 <readline+0xa6>
movb   $0x0,(%r12)
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r15,%rdi
call   1030 <free@plt>
xor    %r15d,%r15d
jmp    11e4 <readline+0x94>

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

LLVM IR (-O2):
@BUFSIZ = global i64 0
@EOF = global i32 0
define i8* @readline(i32* nocapture %0) {
%2 = load i64, i64* @BUFSIZ
%3 = trunc i64 %2 to i32
%4 = tail call i8* @malloc(i32 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %1, %31
%9 = phi i8* [ %32, %31 ], [ %4, %1 ]
%10 = phi i8* [ %21, %31 ], [ %4, %1 ]
%11 = phi i64 [ %33, %31 ], [ %2, %1 ]
%12 = tail call i32 @getc(i32* %0)
%13 = load i32, i32* @EOF
%14 = icmp eq i32 %12, %13
br i1 %14, label %15, label %19
15: ; preds = %8
%16 = icmp eq i8* %9, %10
br i1 %16, label %17, label %35
17: ; preds = %15
%18 = tail call i32 @free(i8* %9)
br label %38
19: ; preds = %8
%20 = trunc i32 %12 to i8
%21 = getelementptr inbounds i8, i8* %10, i64 1
store i8 %20, i8* %10
%22 = getelementptr inbounds i8, i8* %9, i64 -1
%23 = getelementptr inbounds i8, i8* %22, i64 %11
%24 = icmp ult i8* %21, %23
br i1 %24, label %31, label %25
25: ; preds = %19
%26 = shl i64 %11, 1
%27 = tail call i8* @realloc(i8* %9, i64 %26)
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = tail call i32 @exit(i32 1)
unreachable
31: ; preds = %25, %19
%32 = phi i8* [ %27, %25 ], [ %9, %19 ]
%33 = phi i64 [ %26, %25 ], [ %11, %19 ]
%34 = icmp eq i32 %12, 10
br i1 %34, label %35, label %8
35: ; preds = %31, %15
%36 = phi i8* [ %9, %15 ], [ %32, %31 ]
%37 = phi i8* [ %10, %15 ], [ %21, %31 ]
store i8 0, i8* %37
br label %38
38: ; preds = %35, %17
%39 = phi i8* [ null, %17 ], [ %36, %35 ]
ret i8* %39
}