#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 */
scalar_t__ feof(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
char *malloc(int);
char *realloc(char *, unsigned int);
char *readline(FILE *dict) {
  unsigned i = 0;
  int ch = 0;
  char *line = NULL;
  line = malloc(2);
  while (!feof(dict)) {
    ch = fgetc(dict);
    if (ch == '\n') {
      line = realloc(line, i + 1);
      line[i] = '\0';
      break;
    }
    if (feof(dict))
      break;
    line = realloc(line, i + 2);
    line[i] = ch;
    line[++i] = '\0';
  }
  return line;
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    $0x2,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <feof@plt>
cmp    $0x0,%rax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    118f <readline+0x4f>
jmp    121a <readline+0xda>
mov    -0x8(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xa,-0x10(%rbp)
jne    11c8 <readline+0x88>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%esi
add    $0x1,%esi
call   1060 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    -0xc(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
jmp    121a <readline+0xda>
mov    -0x8(%rbp),%rdi
call   1040 <feof@plt>
cmp    $0x0,%rax
je     11e0 <readline+0xa0>
jmp    121a <readline+0xda>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%esi
add    $0x2,%esi
call   1060 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%eax
mov    %al,%dl
mov    -0x18(%rbp),%rax
mov    -0xc(%rbp),%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    -0xc(%rbp),%ecx
add    $0x1,%ecx
mov    %ecx,-0xc(%rbp)
mov    %ecx,%ecx
movb   $0x0,(%rax,%rcx,1)
jmp    1170 <readline+0x30>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    $0x2,%edi
call   1050 <malloc@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1040 <feof@plt>
test   %rax,%rax
je     1172 <readline+0x32>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r14,%rdi
call   1030 <fgetc@plt>
cmp    $0xa,%eax
je     11c7 <readline+0x87>
mov    %eax,%ebp
mov    %r14,%rdi
call   1040 <feof@plt>
test   %rax,%rax
jne    1164 <readline+0x24>
lea    0x2(%rbx),%esi
mov    %r15,%rdi
call   1060 <realloc@plt>
mov    %rax,%r15
mov    %ebx,%eax
mov    %bpl,(%r15,%rax,1)
add    $0x1,%ebx
movb   $0x0,(%r15,%rbx,1)
mov    %r14,%rdi
call   1040 <feof@plt>
test   %rax,%rax
je     1180 <readline+0x40>
jmp    1164 <readline+0x24>
lea    0x1(%rbx),%esi
mov    %r15,%rdi
call   1060 <realloc@plt>
mov    %rax,%r15
mov    %ebx,%eax
movb   $0x0,(%r15,%rax,1)
jmp    1164 <readline+0x24>

LLVM IR (-O0):
define i8* @readline(i32* %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
store i32* %0, i32** %2
store i32 0, i32* %3
store i32 0, i32* %4
store i8* null, i8** %5
%6 = call i8* @malloc(i32 2)
store i8* %6, i8** %5
br label %7
7: ; preds = %31, %1
%8 = load i32*, i32** %2
%9 = call i64 @feof(i32* %8)
%10 = icmp ne i64 %9, 0
%11 = xor i1 %10, true
br i1 %11, label %12, label %47
12: ; preds = %7
%13 = load i32*, i32** %2
%14 = call i32 @fgetc(i32* %13)
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = icmp eq i32 %15, 10
br i1 %16, label %17, label %26
17: ; preds = %12
%18 = load i8*, i8** %5
%19 = load i32, i32* %3
%20 = add i32 %19, 1
%21 = call i8* @realloc(i8* %18, i32 %20)
store i8* %21, i8** %5
%22 = load i8*, i8** %5
%23 = load i32, i32* %3
%24 = zext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
store i8 0, i8* %25
br label %47
26: ; preds = %12
%27 = load i32*, i32** %2
%28 = call i64 @feof(i32* %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %26
br label %47
31: ; preds = %26
%32 = load i8*, i8** %5
%33 = load i32, i32* %3
%34 = add i32 %33, 2
%35 = call i8* @realloc(i8* %32, i32 %34)
store i8* %35, i8** %5
%36 = load i32, i32* %4
%37 = trunc i32 %36 to i8
%38 = load i8*, i8** %5
%39 = load i32, i32* %3
%40 = zext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %38, i64 %40
store i8 %37, i8* %41
%42 = load i8*, i8** %5
%43 = load i32, i32* %3
%44 = add i32 %43, 1
store i32 %44, i32* %3
%45 = zext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %42, i64 %45
store i8 0, i8* %46
br label %7
47: ; preds = %30, %17, %7
%48 = load i8*, i8** %5
ret i8* %48
}

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