#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 /*<<< orphan*/ free(char *);
int getc(int /*<<< orphan*/ *);
char *malloc(size_t);
char *realloc(char *, size_t);
char *read_line(char *buffer, size_t *size, FILE *fp) {
  size_t pos = 0;
  if (buffer == NULL) {
    *size = 4096;
    buffer = malloc(*size);
  }
  for (;;) {
    char c;
    {
      int t = getc(fp);
      if (t == EOF) {
        free(buffer);
        return NULL;
      }
      c = t;
    }
    if (c == '\n') {
      buffer[pos] = '\0';
      return buffer;
    } else {
      if (pos + 1 >= *size) {
        *size <<= 2;
        buffer = realloc(buffer, *size);
      }
      buffer[pos++] = c;
    }
  }
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1182 <read_line+0x42>
mov    -0x18(%rbp),%rax
movq   $0x1000,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
jmp    1187 <read_line+0x47>
mov    -0x20(%rbp),%rdi
call   1060 <getc@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    0x2e4b(%rip),%rcx
cmp    (%rcx),%eax
jne    11bb <read_line+0x7b>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    123c <read_line+0xfc>
mov    -0x30(%rbp),%eax
mov    %al,-0x29(%rbp)
movsbl -0x29(%rbp),%eax
cmp    $0xa,%eax
jne    11e7 <read_line+0xa7>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    123c <read_line+0xfc>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%rax
jb     121e <read_line+0xde>
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
shl    $0x2,%rcx
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
call   1050 <realloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x29(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x28(%rbp)
mov    %dl,(%rax,%rcx,1)
jmp    1187 <read_line+0x47>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r13
test   %rdi,%rdi
jne    116d <read_line+0x2d>
movq   $0x1000,(%r15)
mov    $0x1000,%edi
call   1040 <malloc@plt>
mov    %rax,%r13
mov    %r14,%rdi
call   1060 <getc@plt>
mov    0x2e6c(%rip),%r12
cmp    (%r12),%eax
jne    118f <read_line+0x4f>
mov    %r13,%rdi
call   1030 <free@plt>
xor    %r13d,%r13d
jmp    11e1 <read_line+0xa1>
mov    %eax,%ebp
mov    $0x1,%ebx
jmp    11b9 <read_line+0x79>
nopl   0x0(%rax,%rax,1)
mov    %bpl,-0x1(%r13,%rbx,1)
mov    %r14,%rdi
call   1060 <getc@plt>
mov    %eax,%ebp
add    $0x1,%rbx
cmp    (%r12),%eax
je     1182 <read_line+0x42>
cmp    $0xa,%bpl
je     11db <read_line+0x9b>
mov    (%r15),%rsi
cmp    %rsi,%rbx
jb     11a0 <read_line+0x60>
shl    $0x2,%rsi
mov    %rsi,(%r15)
mov    %r13,%rdi
call   1050 <realloc@plt>
mov    %rax,%r13
jmp    11a0 <read_line+0x60>
movb   $0x0,-0x1(%r13,%rbx,1)
mov    %r13,%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* @read_line(i8* %0, i64* %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64*
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i8
%10 = alloca i32
store i8* %0, i8** %5
store i64* %1, i64** %6
store i32* %2, i32** %7
store i64 0, i64* %8
%11 = load i8*, i8** %5
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %18
13: ; preds = %3
%14 = load i64*, i64** %6
store i64 4096, i64* %14
%15 = load i64*, i64** %6
%16 = load i64, i64* %15
%17 = call i8* @malloc(i64 %16)
store i8* %17, i8** %5
br label %18
18: ; preds = %13, %3
br label %19
19: ; preds = %59, %18
%20 = load i32*, i32** %7
%21 = call i32 @getc(i32* %20)
store i32 %21, i32* %10
%22 = load i32, i32* %10
%23 = load i32, i32* @EOF
%24 = icmp eq i32 %22, %23
br i1 %24, label %25, label %28
25: ; preds = %19
%26 = load i8*, i8** %5
%27 = call i32 @free(i8* %26)
store i8* null, i8** %4
br label %60
28: ; preds = %19
%29 = load i32, i32* %10
%30 = trunc i32 %29 to i8
store i8 %30, i8* %9
%31 = load i8, i8* %9
%32 = sext i8 %31 to i32
%33 = icmp eq i32 %32, 10
br i1 %33, label %34, label %39
34: ; preds = %28
%35 = load i8*, i8** %5
%36 = load i64, i64* %8
%37 = getelementptr inbounds i8, i8* %35, i64 %36
store i8 0, i8* %37
%38 = load i8*, i8** %5
store i8* %38, i8** %4
br label %60
39: ; preds = %28
%40 = load i64, i64* %8
%41 = add i64 %40, 1
%42 = load i64*, i64** %6
%43 = load i64, i64* %42
%44 = icmp uge i64 %41, %43
br i1 %44, label %45, label %53
45: ; preds = %39
%46 = load i64*, i64** %6
%47 = load i64, i64* %46
%48 = shl i64 %47, 2
store i64 %48, i64* %46
%49 = load i8*, i8** %5
%50 = load i64*, i64** %6
%51 = load i64, i64* %50
%52 = call i8* @realloc(i8* %49, i64 %51)
store i8* %52, i8** %5
br label %53
53: ; preds = %45, %39
%54 = load i8, i8* %9
%55 = load i8*, i8** %5
%56 = load i64, i64* %8
%57 = add i64 %56, 1
store i64 %57, i64* %8
%58 = getelementptr inbounds i8, i8* %55, i64 %56
store i8 %54, i8* %58
br label %59
59: ; preds = %53
br label %19
60: ; preds = %34, %25
%61 = load i8*, i8** %4
ret i8* %61
}

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