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


x86 assembly (clang, -O0):
<getstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%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    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1133 <getstr+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):
<getstr>:
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 <getstr+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* @getstr(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %3
br label %4
4: ; preds = %19, %1
%5 = load i8*, i8** %2
%6 = load i32, i32* %3
%7 = add nsw i32 %6, 1
%8 = sext i32 %7 to i64
%9 = mul i64 %8, 1
%10 = trunc i64 %9 to i32
%11 = call i8* @realloc(i8* %5, i32 %10)
store i8* %11, i8** %2
%12 = call signext i8 (...) @getchar()
%13 = load i8*, i8** %2
%14 = load i32, i32* %3
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %13, i64 %15
store i8 %12, i8* %16
%17 = load i32, i32* %3
%18 = add nsw i32 %17, 1
store i32 %18, i32* %3
br label %19
19: ; preds = %4
%20 = load i8*, i8** %2
%21 = load i32, i32* %3
%22 = sub nsw i32 %21, 1
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %20, i64 %23
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = icmp ne i32 %26, 10
br i1 %27, label %4, label %28
28: ; preds = %19
%29 = load i8*, i8** %2
%30 = load i32, i32* %3
%31 = sub nsw i32 %30, 1
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %29, i64 %32
store i8 0, i8* %33
%34 = load i8*, i8** %2
ret i8* %34
}

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