#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 */
char EOF;
scalar_t__ emalloc(size_t);
char *erealloc(char *, size_t);
scalar_t__ feof(int /*<<< orphan*/ *);
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
char *efreadline(FILE *input) {
  size_t size = 8;
  size_t index = 0;
  char *str = (char *)emalloc(size);
  memset(str, 0, 8);
  if (feof(input)) {
    free(str);
    return NULL;
  }
  while (true) {
    char ch = fgetc(input);
    if (ch == EOF)
      break;
    str[index++] = ch;
    str[index] = '\0';
    if (index + 1 >= size) {
      size = size << 1;
      str = erealloc(str, size);
    }
    if (ch == '\n')
      break;
  }
  return str;
}


x86 assembly (clang, -O0):
<efreadline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x8,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <emalloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    -0x10(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%rax
je     11c2 <efreadline+0x62>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1261 <efreadline+0x101>
jmp    11c7 <efreadline+0x67>
mov    -0x10(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %al,-0x29(%rbp)
movsbl -0x29(%rbp),%eax
mov    0x2e0a(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    11ee <efreadline+0x8e>
jmp    1259 <efreadline+0xf9>
mov    -0x29(%rbp),%dl
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x20(%rbp)
mov    %dl,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
cmp    -0x18(%rbp),%rax
jb     1242 <efreadline+0xe2>
mov    -0x18(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <erealloc@plt>
mov    %rax,-0x28(%rbp)
movsbl -0x29(%rbp),%eax
cmp    $0xa,%eax
jne    1254 <efreadline+0xf4>
jmp    1259 <efreadline+0xf9>
jmp    11c7 <efreadline+0x67>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<efreadline>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    $0x8,%r15d
mov    $0x8,%edi
call   1080 <emalloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    %r14,%rdi
call   1070 <feof@plt>
test   %rax,%rax
je     11ba <efreadline+0x5a>
mov    %r12,%rdi
call   1030 <free@plt>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x2,%ebx
mov    0x2e22(%rip),%r13
jmp    11da <efreadline+0x7a>
nopl   0x0(%rax,%rax,1)
add    $0x1,%rbx
cmp    $0xa,%bpl
je     11a8 <efreadline+0x48>
mov    %r14,%rdi
call   1060 <fgetc@plt>
cmp    0x0(%r13),%al
je     11a8 <efreadline+0x48>
mov    %eax,%ebp
mov    %al,-0x2(%r12,%rbx,1)
movb   $0x0,-0x1(%r12,%rbx,1)
cmp    %r15,%rbx
jb     11d0 <efreadline+0x70>
add    %r15,%r15
mov    %r12,%rdi
mov    %r15,%rsi
call   1040 <erealloc@plt>
mov    %rax,%r12
jmp    11d0 <efreadline+0x70>

LLVM IR (-O0):
@EOF = global i8 0
define i8* @efreadline(i32* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i8
store i32* %0, i32** %3
store i64 8, i64* %4
store i64 0, i64* %5
%8 = load i64, i64* %4
%9 = call i64 @emalloc(i64 %8)
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %6
%11 = load i8*, i8** %6
%12 = call i32 @memset(i8* %11, i32 0, i32 8)
%13 = load i32*, i32** %3
%14 = call i64 @feof(i32* %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %1
%17 = load i8*, i8** %6
%18 = call i32 @free(i8* %17)
store i8* null, i8** %2
br label %56
19: ; preds = %1
br label %20
20: ; preds = %19, %53
%21 = load i32*, i32** %3
%22 = call signext i8 @fgetc(i32* %21)
store i8 %22, i8* %7
%23 = load i8, i8* %7
%24 = sext i8 %23 to i32
%25 = load i8, i8* @EOF
%26 = sext i8 %25 to i32
%27 = icmp eq i32 %24, %26
br i1 %27, label %28, label %29
28: ; preds = %20
br label %54
29: ; preds = %20
%30 = load i8, i8* %7
%31 = load i8*, i8** %6
%32 = load i64, i64* %5
%33 = add i64 %32, 1
store i64 %33, i64* %5
%34 = getelementptr inbounds i8, i8* %31, i64 %32
store i8 %30, i8* %34
%35 = load i8*, i8** %6
%36 = load i64, i64* %5
%37 = getelementptr inbounds i8, i8* %35, i64 %36
store i8 0, i8* %37
%38 = load i64, i64* %5
%39 = add i64 %38, 1
%40 = load i64, i64* %4
%41 = icmp uge i64 %39, %40
br i1 %41, label %42, label %48
42: ; preds = %29
%43 = load i64, i64* %4
%44 = shl i64 %43, 1
store i64 %44, i64* %4
%45 = load i8*, i8** %6
%46 = load i64, i64* %4
%47 = call i8* @erealloc(i8* %45, i64 %46)
store i8* %47, i8** %6
br label %48
48: ; preds = %42, %29
%49 = load i8, i8* %7
%50 = sext i8 %49 to i32
%51 = icmp eq i32 %50, 10
br i1 %51, label %52, label %53
52: ; preds = %48
br label %54
53: ; preds = %48
br label %20
54: ; preds = %52, %28
%55 = load i8*, i8** %6
store i8* %55, i8** %2
br label %56
56: ; preds = %54, %16
%57 = load i8*, i8** %2
ret i8* %57
}

LLVM IR (-O2):
@EOF = global i8 0
define i8* @efreadline(i32* nocapture %0) {
%2 = tail call i64 @emalloc(i64 8)
%3 = inttoptr i64 %2 to i8*
%4 = tail call i32 @memset(i8* %3, i32 0, i32 8)
%5 = tail call i64 @feof(i32* %0)
%6 = icmp eq i64 %5, 0
br i1 %6, label %9, label %7
7: ; preds = %1
%8 = tail call i32 @free(i8* %3)
br label %29
9: ; preds = %1, %25
%10 = phi i64 [ %26, %25 ], [ 8, %1 ]
%11 = phi i64 [ %17, %25 ], [ 0, %1 ]
%12 = phi i8* [ %27, %25 ], [ %3, %1 ]
%13 = tail call signext i8 @fgetc(i32* %0)
%14 = load i8, i8* @EOF
%15 = icmp eq i8 %13, %14
br i1 %15, label %29, label %16
16: ; preds = %9
%17 = add i64 %11, 1
%18 = getelementptr inbounds i8, i8* %12, i64 %11
store i8 %13, i8* %18
%19 = getelementptr inbounds i8, i8* %12, i64 %17
store i8 0, i8* %19
%20 = add i64 %11, 2
%21 = icmp ult i64 %20, %10
br i1 %21, label %25, label %22
22: ; preds = %16
%23 = shl i64 %10, 1
%24 = tail call i8* @erealloc(i8* %12, i64 %23)
br label %25
25: ; preds = %22, %16
%26 = phi i64 [ %23, %22 ], [ %10, %16 ]
%27 = phi i8* [ %24, %22 ], [ %12, %16 ]
%28 = icmp eq i8 %13, 10
br i1 %28, label %29, label %9
29: ; preds = %25, %9, %7
%30 = phi i8* [ null, %7 ], [ %27, %25 ], [ %12, %9 ]
ret i8* %30
}