#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 *ReadString(void) {
  char *String = (char *)malloc(sizeof(char));
  char *OP = String;
  int num = 1;
  while ((String[num - 1] = getchar()) != '\n') {
    String = (char *)realloc(String, (++num) * sizeof(char));
  }
  return OP;
}


x86 assembly (clang, -O0):
<ReadString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
mov    $0x0,%al
call   1030 <getchar@plt>
mov    -0x8(%rbp),%rcx
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
movslq %edx,%rdx
mov    %al,(%rcx,%rdx,1)
movsbl %al,%eax
cmp    $0xa,%eax
je     119b <ReadString+0x6b>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
cltq
shl    $0x0,%rax
mov    %eax,%esi
call   1050 <realloc@plt>
mov    %rax,-0x8(%rbp)
jmp    1155 <ReadString+0x25>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ReadString>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1030 <getchar@plt>
mov    %al,(%r14)
cmp    $0xa,%al
je     1185 <ReadString+0x55>
mov    $0x1,%r12d
mov    %r14,%rbx
nopl   0x0(%rax)
lea    0x1(%r12),%r15
mov    %rbx,%rdi
mov    %r15d,%esi
call   1050 <realloc@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <getchar@plt>
mov    %al,(%rbx,%r12,1)
mov    %r15,%r12
cmp    $0xa,%al
jne    1160 <ReadString+0x30>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @ReadString() {
%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** %1
%6 = load i8*, i8** %1
store i8* %6, i8** %2
store i32 1, i32* %3
br label %7
7: ; preds = %16, %0
%8 = call signext i8 (...) @getchar()
%9 = load i8*, i8** %1
%10 = load i32, i32* %3
%11 = sub nsw i32 %10, 1
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %9, i64 %12
store i8 %8, i8* %13
%14 = sext i8 %8 to i32
%15 = icmp ne i32 %14, 10
br i1 %15, label %16, label %25
16: ; preds = %7
%17 = load i8*, i8** %1
%18 = load i32, i32* %3
%19 = add nsw i32 %18, 1
store i32 %19, i32* %3
%20 = sext i32 %19 to i64
%21 = mul i64 %20, 1
%22 = trunc i64 %21 to i32
%23 = call i64 @realloc(i8* %17, i32 %22)
%24 = inttoptr i64 %23 to i8*
store i8* %24, i8** %1
br label %7
25: ; preds = %7
%26 = load i8*, i8** %2
ret i8* %26
}

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