#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;
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
char fgetc(int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ print_error(char *);
char *realloc(char *, int);
char *read_line(FILE *stream) {
  long size = 1;
  char c, *line;
  if ((line = malloc(sizeof(char))) == NULL)
    print_error("Out of memory");
  while (!feof(stream) && (c = fgetc(stream)) != '\n' && c != EOF) {
    line[size - 1] = c;
    if ((line = realloc(line, sizeof(char) * ++size)) == NULL)
      print_error("Out of memory");
    ;
  }
  line[size - 1] = '\0';
  return line;
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x1,-0x10(%rbp)
mov    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1188 <read_line+0x38>
lea    0xe7d(%rip),%rdi
call   1030 <print_error@plt>
jmp    118d <read_line+0x3d>
mov    -0x8(%rbp),%rdi
call   1050 <feof@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x21(%rbp)
jne    11d9 <read_line+0x89>
mov    -0x8(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %al,-0x11(%rbp)
movsbl %al,%ecx
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x21(%rbp)
je     11d9 <read_line+0x89>
movsbl -0x11(%rbp),%eax
mov    0x2e1a(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    11e9 <read_line+0x99>
jmp    1235 <read_line+0xe5>
mov    -0x11(%rbp),%dl
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    $0x1,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
shl    $0x0,%rax
mov    %eax,%esi
call   1070 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1230 <read_line+0xe0>
lea    0xdd5(%rip),%rdi
call   1030 <print_error@plt>
jmp    118d <read_line+0x3d>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r14
mov    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,%r12
test   %rax,%rax
jne    117a <read_line+0x2a>
lea    0xe8b(%rip),%rdi
call   1030 <print_error@plt>
mov    %r14,%rdi
call   1050 <feof@plt>
mov    $0x1,%ebx
test   %eax,%eax
je     119e <read_line+0x4e>
movb   $0x0,-0x1(%rbx,%r12,1)
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    $0x1,%ebx
mov    0x2e3e(%rip),%r13
lea    0xe4f(%rip),%r15
jmp    11d0 <read_line+0x80>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r14,%rdi
call   1050 <feof@plt>
add    $0x1,%rbx
test   %eax,%eax
jne    118b <read_line+0x3b>
mov    %r14,%rdi
call   1040 <fgetc@plt>
cmp    $0xa,%al
je     118b <read_line+0x3b>
cmp    0x0(%r13),%al
je     118b <read_line+0x3b>
mov    %al,-0x1(%r12,%rbx,1)
lea    0x1(%rbx),%esi
mov    %r12,%rdi
call   1070 <realloc@plt>
mov    %rax,%r12
test   %rax,%rax
jne    11c0 <read_line+0x70>
mov    %r15,%rdi
call   1030 <print_error@plt>
jmp    11c0 <read_line+0x70>

LLVM IR (-O0):
@.str = [14 x i8] c"Out of memory\00"
@EOF = global i8 0
define i8* @read_line(i32* %0) {
%2 = alloca i32*
%3 = alloca i64
%4 = alloca i8
%5 = alloca i8*
store i32* %0, i32** %2
store i64 1, i64* %3
%6 = call i8* @malloc(i32 1)
store i8* %6, i8** %5
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i32 @print_error(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %1
br label %11
11: ; preds = %43, %10
%12 = load i32*, i32** %2
%13 = call i32 @feof(i32* %12)
%14 = icmp ne i32 %13, 0
br i1 %14, label %26, label %15
15: ; preds = %11
%16 = load i32*, i32** %2
%17 = call signext i8 @fgetc(i32* %16)
store i8 %17, i8* %4
%18 = sext i8 %17 to i32
%19 = icmp ne i32 %18, 10
br i1 %19, label %20, label %26
20: ; preds = %15
%21 = load i8, i8* %4
%22 = sext i8 %21 to i32
%23 = load i8, i8* @EOF
%24 = sext i8 %23 to i32
%25 = icmp ne i32 %22, %24
br label %26
26: ; preds = %20, %15, %11
%27 = phi i1 [ false, %15 ], [ false, %11 ], [ %25, %20 ]
br i1 %27, label %28, label %44
28: ; preds = %26
%29 = load i8, i8* %4
%30 = load i8*, i8** %5
%31 = load i64, i64* %3
%32 = sub nsw i64 %31, 1
%33 = getelementptr inbounds i8, i8* %30, i64 %32
store i8 %29, i8* %33
%34 = load i8*, i8** %5
%35 = load i64, i64* %3
%36 = add nsw i64 %35, 1
store i64 %36, i64* %3
%37 = mul i64 1, %36
%38 = trunc i64 %37 to i32
%39 = call i8* @realloc(i8* %34, i32 %38)
store i8* %39, i8** %5
%40 = icmp eq i8* %39, null
br i1 %40, label %41, label %43
41: ; preds = %28
%42 = call i32 @print_error(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %43
43: ; preds = %41, %28
br label %11
44: ; preds = %26
%45 = load i8*, i8** %5
%46 = load i64, i64* %3
%47 = sub nsw i64 %46, 1
%48 = getelementptr inbounds i8, i8* %45, i64 %47
store i8 0, i8* %48
%49 = load i8*, i8** %5
ret i8* %49
}

LLVM IR (-O2):
@.str = [14 x i8] c"Out of memory\00"
@EOF = global i8 0
define i8* @read_line(i32* nocapture %0) {
%2 = tail call dereferenceable_or_null(1) i8* @malloc(i32 1)
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 @print_error(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %6
6: ; preds = %4, %1
%7 = tail call i32 @feof(i32* %0)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %29
9: ; preds = %6, %26
%10 = phi i8* [ %22, %26 ], [ %2, %6 ]
%11 = phi i64 [ %20, %26 ], [ 1, %6 ]
%12 = tail call signext i8 @fgetc(i32* %0)
%13 = icmp eq i8 %12, 10
%14 = load i8, i8* @EOF
%15 = icmp eq i8 %12, %14
%16 = select i1 %13, i1 true, i1 %15
br i1 %16, label %29, label %17
17: ; preds = %9
%18 = add nsw i64 %11, -1
%19 = getelementptr inbounds i8, i8* %10, i64 %18
store i8 %12, i8* %19
%20 = add nuw nsw i64 %11, 1
%21 = trunc i64 %20 to i32
%22 = tail call i8* @realloc(i8* %10, i32 %21)
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %26
24: ; preds = %17
%25 = tail call i32 @print_error(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %26
26: ; preds = %24, %17
%27 = tail call i32 @feof(i32* %0)
%28 = icmp eq i32 %27, 0
br i1 %28, label %9, label %29
29: ; preds = %9, %26, %6
%30 = phi i64 [ 1, %6 ], [ %20, %26 ], [ %11, %9 ]
%31 = phi i8* [ %2, %6 ], [ %22, %26 ], [ %10, %9 ]
%32 = add nsw i64 %30, -1
%33 = getelementptr inbounds i8, i8* %31, i64 %32
store i8 0, i8* %33
ret i8* %31
}