#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 */

/* Variables and functions */
char getchar();
scalar_t__ malloc(int);
scalar_t__ realloc(char *, int);
char *getLine() {
  char c, *line = (char *)malloc(sizeof(char));
  int i = 0;
  c = getchar();
  while (c != '\n') {
    *(line + i++) = c;
    c = getchar();
    line = (char *)realloc(line, (i + 1) * sizeof(char));
  }
  *(line + i) = '\0';
  return line;
}


x86 assembly (clang, -O0):
<getLine>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    $0x0,%al
call   1030 <getchar@plt>
mov    %al,-0x1(%rbp)
movsbl -0x1(%rbp),%eax
cmp    $0xa,%eax
je     11a6 <getLine+0x76>
mov    -0x1(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x14(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    $0x0,%al
call   1030 <getchar@plt>
mov    %al,-0x1(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%esi
call   1050 <realloc@plt>
mov    %rax,-0x10(%rbp)
jmp    1157 <getLine+0x27>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getLine>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,%r14
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <getchar@plt>
cmp    $0xa,%al
je     118d <getLine+0x5d>
mov    %eax,%ebp
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
mov    %r15,%rbx
add    $0x1,%r15
mov    %bpl,(%r14,%rbx,1)
xor    %eax,%eax
call   1030 <getchar@plt>
mov    %eax,%ebp
add    $0x2,%ebx
mov    %r14,%rdi
mov    %ebx,%esi
call   1050 <realloc@plt>
mov    %rax,%r14
cmp    $0xa,%bpl
jne    1160 <getLine+0x30>
mov    %r15d,%ebx
movb   $0x0,(%r14,%rbx,1)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @getLine() {
%1 = alloca i8
%2 = alloca i8*
%3 = alloca i32
%4 = call i64 @malloc(i32 1)
%5 = inttoptr i64 %4 to i8*
store i8* %5, i8** %2
store i32 0, i32* %3
%6 = call signext i8 (...) @getchar()
store i8 %6, i8* %1
br label %7
7: ; preds = %11, %0
%8 = load i8, i8* %1
%9 = sext i8 %8 to i32
%10 = icmp ne i32 %9, 10
br i1 %10, label %11, label %27
11: ; preds = %7
%12 = load i8, i8* %1
%13 = load i8*, i8** %2
%14 = load i32, i32* %3
%15 = add nsw i32 %14, 1
store i32 %15, i32* %3
%16 = sext i32 %14 to i64
%17 = getelementptr inbounds i8, i8* %13, i64 %16
store i8 %12, i8* %17
%18 = call signext i8 (...) @getchar()
store i8 %18, i8* %1
%19 = load i8*, i8** %2
%20 = load i32, i32* %3
%21 = add nsw i32 %20, 1
%22 = sext i32 %21 to i64
%23 = mul i64 %22, 1
%24 = trunc i64 %23 to i32
%25 = call i64 @realloc(i8* %19, i32 %24)
%26 = inttoptr i64 %25 to i8*
store i8* %26, i8** %2
br label %7
27: ; preds = %7
%28 = load i8*, i8** %2
%29 = load i32, i32* %3
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
store i8 0, i8* %31
%32 = load i8*, i8** %2
ret i8* %32
}

LLVM IR (-O2):
define i8* @getLine() {
%1 = tail call i64 @malloc(i32 1)
%2 = tail call signext i8 (...) @getchar()
%3 = icmp eq i8 %2, 10
br i1 %3, label %18, label %4
4: ; preds = %0, %4
%5 = phi i64 [ %9, %4 ], [ 0, %0 ]
%6 = phi i64 [ %14, %4 ], [ %1, %0 ]
%7 = phi i8 [ %11, %4 ], [ %2, %0 ]
%8 = inttoptr i64 %6 to i8*
%9 = add nuw i64 %5, 1
%10 = getelementptr inbounds i8, i8* %8, i64 %5
store i8 %7, i8* %10
%11 = tail call signext i8 (...) @getchar()
%12 = trunc i64 %5 to i32
%13 = add i32 %12, 2
%14 = tail call i64 @realloc(i8* %8, i32 %13)
%15 = icmp eq i8 %11, 10
br i1 %15, label %16, label %4
16: ; preds = %4
%17 = and i64 %9, 4294967295
br label %18
18: ; preds = %16, %0
%19 = phi i64 [ %1, %0 ], [ %14, %16 ]
%20 = phi i64 [ 0, %0 ], [ %17, %16 ]
%21 = inttoptr i64 %19 to i8*
%22 = getelementptr inbounds i8, i8* %21, i64 %20
store i8 0, i8* %22
ret i8* %21
}