#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__ realloc(char *, int);
char *getastring(char *p) {
  int i = 0;
  do {
    p = (char *)realloc(p, i + 1);
    p[i] = getchar();
  } while (p[i++] != 10);
  p[i - 1] = 0;
  return p;
}


x86 assembly (clang, -O0):
<getastring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
add    $0x1,%esi
call   1040 <realloc@plt>
mov    %rax,-0x8(%rbp)
mov    $0x0,%al
call   1030 <getchar@plt>
mov    %al,%dl
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0xc(%rbp)
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1133 <getastring+0x13>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getastring>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    $0xffffffffffffffff,%rbx
xchg   %ax,%ax
lea    0x2(%rbx),%esi
mov    %r14,%rdi
call   1040 <realloc@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1030 <getchar@plt>
mov    %al,0x1(%r14,%rbx,1)
add    $0x1,%rbx
cmp    $0xa,%al
jne    1130 <getastring+0x10>
mov    %ebx,%eax
movb   $0x0,(%r14,%rax,1)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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