#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 getc(int /*<<< orphan*/ *);
char *realloc(char *, unsigned int);
char *read_line(FILE *fp) {
  char *line = NULL;
  unsigned length = 0;
  int c;
  while (1) {
    c = getc(fp);
    if ((c == EOF) || (c == '\n'))
      break;
    line = realloc(line, length + 1);
    line[length++] = c;
  }
  line = realloc(line, length + 1);
  line[length] = 0;
  return line;
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <getc@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    0x2e97(%rip),%rcx
cmp    (%rcx),%eax
je     1163 <read_line+0x43>
cmpl   $0xa,-0x18(%rbp)
jne    1168 <read_line+0x48>
jmp    1199 <read_line+0x79>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
add    $0x1,%esi
call   1030 <realloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x14(%rbp)
mov    %ecx,%ecx
mov    %dl,(%rax,%rcx,1)
jmp    113b <read_line+0x1b>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
add    $0x1,%esi
call   1030 <realloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x20,%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    %rdi,%r14
call   1040 <getc@plt>
xor    %r15d,%r15d
mov    $0x0,%r13d
cmp    $0xa,%eax
je     1191 <read_line+0x71>
mov    %eax,%ebp
mov    0x2e9e(%rip),%r12
cmp    (%r12),%eax
je     1191 <read_line+0x71>
xor    %r13d,%r13d
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
mov    %r15d,%ebx
lea    0x1(%rbx),%r15d
mov    %r13,%rdi
mov    %r15d,%esi
call   1030 <realloc@plt>
mov    %rax,%r13
mov    %ebx,%eax
mov    %bpl,0x0(%r13,%rax,1)
mov    %r14,%rdi
call   1040 <getc@plt>
cmp    $0xa,%eax
je     1191 <read_line+0x71>
mov    %eax,%ebp
cmp    (%r12),%eax
jne    1160 <read_line+0x40>
lea    0x1(%r15),%esi
mov    %r13,%rdi
call   1030 <realloc@plt>
mov    %r15d,%ecx
movb   $0x0,(%rax,%rcx,1)
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(i32* %0) {
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i32* %0, i32** %2
store i8* null, i8** %3
store i32 0, i32* %4
br label %6
6: ; preds = %1, %16
%7 = load i32*, i32** %2
%8 = call i32 @getc(i32* %7)
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = load i32, i32* @EOF
%11 = icmp eq i32 %9, %10
br i1 %11, label %15, label %12
12: ; preds = %6
%13 = load i32, i32* %5
%14 = icmp eq i32 %13, 10
br i1 %14, label %15, label %16
15: ; preds = %12, %6
br label %28
16: ; preds = %12
%17 = load i8*, i8** %3
%18 = load i32, i32* %4
%19 = add i32 %18, 1
%20 = call i8* @realloc(i8* %17, i32 %19)
store i8* %20, i8** %3
%21 = load i32, i32* %5
%22 = trunc i32 %21 to i8
%23 = load i8*, i8** %3
%24 = load i32, i32* %4
%25 = add i32 %24, 1
store i32 %25, i32* %4
%26 = zext i32 %24 to i64
%27 = getelementptr inbounds i8, i8* %23, i64 %26
store i8 %22, i8* %27
br label %6
28: ; preds = %15
%29 = load i8*, i8** %3
%30 = load i32, i32* %4
%31 = add i32 %30, 1
%32 = call i8* @realloc(i8* %29, i32 %31)
store i8* %32, i8** %3
%33 = load i8*, i8** %3
%34 = load i32, i32* %4
%35 = zext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
store i8 0, i8* %36
%37 = load i8*, i8** %3
ret i8* %37
}

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