#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 fgetc(int /*<<< orphan*/ *);
scalar_t__ realloc(char *, size_t);
char *get_line(FILE *file) {
  char *line = NULL;
  int c;
  size_t n = 0;
  while ((c = fgetc(file)) != EOF) {
    line = (char *)realloc(line, ++n * sizeof(char));
    if (c == '\n')
      break;
    line[n - 1] = (char)c;
  }
  if (line != NULL)
    line[n - 1] = '\0';
  return line;
}


x86 assembly (clang, -O0):
<get_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e99(%rip),%rcx
cmp    (%rcx),%eax
je     119c <get_line+0x7c>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0x1,%rsi
mov    %rsi,-0x20(%rbp)
shl    $0x0,%rsi
call   1040 <realloc@plt>
mov    %rax,-0x10(%rbp)
cmpl   $0xa,-0x14(%rbp)
jne    1183 <get_line+0x63>
jmp    119c <get_line+0x7c>
mov    -0x14(%rbp),%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    113c <get_line+0x1c>
cmpq   $0x0,-0x10(%rbp)
je     11b7 <get_line+0x97>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_line>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r14
call   1030 <fgetc@plt>
mov    0x2eb1(%rip),%r12
cmp    (%r12),%eax
jne    1142 <get_line+0x22>
xor    %r15d,%r15d
jmp    118c <get_line+0x6c>
mov    %eax,%ebp
mov    $0x1,%eax
xor    %r15d,%r15d
nopl   0x0(%rax)
mov    %rax,%rbx
mov    %r15,%rdi
mov    %rax,%rsi
call   1040 <realloc@plt>
mov    %rax,%r15
cmp    $0xa,%ebp
je     1181 <get_line+0x61>
mov    %bpl,-0x1(%r15,%rbx,1)
mov    %r14,%rdi
call   1030 <fgetc@plt>
mov    %eax,%ebp
lea    0x1(%rbx),%rax
cmp    (%r12),%ebp
jne    1150 <get_line+0x30>
jmp    1186 <get_line+0x66>
test   %r15,%r15
je     113d <get_line+0x1d>
movb   $0x0,-0x1(%r15,%rbx,1)
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EOF = global i32 0
define i8* @get_line(i32* %0) {
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i64
store i32* %0, i32** %2
store i8* null, i8** %3
store i64 0, i64* %5
br label %6
6: ; preds = %21, %1
%7 = load i32*, i32** %2
%8 = call i32 @fgetc(i32* %7)
store i32 %8, i32* %4
%9 = load i32, i32* @EOF
%10 = icmp ne i32 %8, %9
br i1 %10, label %11, label %28
11: ; preds = %6
%12 = load i8*, i8** %3
%13 = load i64, i64* %5
%14 = add i64 %13, 1
store i64 %14, i64* %5
%15 = mul i64 %14, 1
%16 = call i64 @realloc(i8* %12, i64 %15)
%17 = inttoptr i64 %16 to i8*
store i8* %17, i8** %3
%18 = load i32, i32* %4
%19 = icmp eq i32 %18, 10
br i1 %19, label %20, label %21
20: ; preds = %11
br label %28
21: ; preds = %11
%22 = load i32, i32* %4
%23 = trunc i32 %22 to i8
%24 = load i8*, i8** %3
%25 = load i64, i64* %5
%26 = sub i64 %25, 1
%27 = getelementptr inbounds i8, i8* %24, i64 %26
store i8 %23, i8* %27
br label %6
28: ; preds = %20, %6
%29 = load i8*, i8** %3
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %36
31: ; preds = %28
%32 = load i8*, i8** %3
%33 = load i64, i64* %5
%34 = sub i64 %33, 1
%35 = getelementptr inbounds i8, i8* %32, i64 %34
store i8 0, i8* %35
br label %36
36: ; preds = %31, %28
%37 = load i8*, i8** %3
ret i8* %37
}

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