#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 *malloc(int);
char *realloc(char *, int);
char *getStr() {
  int len = 1;
  char inp;
  char *output = malloc(sizeof(char));
  while (1) {
    inp = getchar();
    if (inp == '\n') {
      *(output + len - 1) = '\0';
      return output;
    } else {
      *(output + len - 1) = inp;
      len++;
      output = realloc(output, len);
    }
  }
}


x86 assembly (clang, -O0):
<getStr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x1,-0x4(%rbp)
mov    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    $0x0,%al
call   1030 <getchar@plt>
mov    %al,-0x5(%rbp)
movsbl -0x5(%rbp),%eax
cmp    $0xa,%eax
jne    117b <getStr+0x4b>
mov    -0x10(%rbp),%rax
movslq -0x4(%rbp),%rcx
movb   $0x0,-0x1(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret
mov    -0x5(%rbp),%dl
mov    -0x10(%rbp),%rax
movslq -0x4(%rbp),%rcx
mov    %dl,-0x1(%rax,%rcx,1)
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%esi
call   1050 <realloc@plt>
mov    %rax,-0x10(%rbp)
jmp    114d <getStr+0x1d>

x86 assembly (clang, -O2):
<getStr>:
push   %r14
push   %rbx
push   %rax
mov    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1030 <getchar@plt>
mov    $0x1,%ecx
cmp    $0xa,%al
je     1183 <getStr+0x53>
mov    $0x1,%ebx
cs nopw 0x0(%rax,%rax,1)
mov    %al,-0x1(%r14,%rbx,1)
add    $0x1,%rbx
mov    %r14,%rdi
mov    %ebx,%esi
call   1050 <realloc@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1030 <getchar@plt>
cmp    $0xa,%al
jne    1160 <getStr+0x30>
mov    %ebx,%ecx
movb   $0x0,-0x1(%r14,%rcx,1)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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