#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();
int /*<<< orphan*/ printf(char *);
scalar_t__ realloc(char *, int);
char *getastring(char *p) {
  int i = 0;
  printf("Enter name:");
  do {
    p = (char *)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):
<getastring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
lea    0xeb6(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%esi
call   1050 <realloc@plt>
mov    %rax,-0x8(%rbp)
mov    $0x0,%al
call   1040 <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    114f <getastring+0x1f>
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
lea    0xec2(%rip),%rdi
call   1030 <printf@plt>
mov    $0xffffffffffffffff,%rbx
nopw   0x0(%rax,%rax,1)
lea    0x2(%rbx),%esi
mov    %r14,%rdi
call   1050 <realloc@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1040 <getchar@plt>
mov    %al,0x1(%r14,%rbx,1)
add    $0x1,%rbx
cmp    $0xa,%al
jne    1150 <getastring+0x20>
mov    %ebx,%eax
movb   $0x0,(%r14,%rax,1)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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