#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);
    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),%esi
add    $0x1,%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 = %18, %1
%6 = load i8*, i8** %2
%7 = load i32, i32* %3
%8 = add nsw i32 %7, 1
%9 = call i64 @realloc(i8* %6, i32 %8)
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %2
%11 = call signext i8 (...) @getchar()
%12 = load i8*, i8** %2
%13 = load i32, i32* %3
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %12, i64 %14
store i8 %11, i8* %15
%16 = load i32, i32* %3
%17 = add nsw i32 %16, 1
store i32 %17, i32* %3
br label %18
18: ; preds = %5
%19 = load i8*, i8** %2
%20 = load i32, i32* %3
%21 = sub nsw i32 %20, 1
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %19, i64 %22
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp ne i32 %25, 10
br i1 %26, label %5, label %27
27: ; preds = %18
%28 = load i8*, i8** %2
%29 = load i32, i32* %3
%30 = sub nsw i32 %29, 1
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %28, i64 %31
store i8 0, i8* %32
%33 = load i8*, i8** %2
ret i8* %33
}

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
}