#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 */
int /*<<< orphan*/ free(char *);
char getchar();
char *malloc(int);
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ realloc(char *, int);
int main() {
  char *string = malloc(sizeof(char));
  char scan;
  int n = 0;
  for (n = 0; (scan = getchar()) != '\n'; n++) {
    realloc(string, sizeof(char) * (n + 2));
    *(string + n) = scan;
    printf("\n%d_%c\n", n, *(string + n));
  }
  *(string + n) = '\0';
  printf("\n%d_%s\n", n, string);
  free(string);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x18(%rbp)
mov    $0x0,%al
call   1050 <getchar@plt>
mov    %al,-0x11(%rbp)
movsbl %al,%eax
cmp    $0xa,%eax
je     11e1 <main+0x91>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%eax
add    $0x2,%eax
cltq
shl    $0x0,%rax
mov    %eax,%esi
call   1070 <realloc@plt>
mov    -0x11(%rbp),%dl
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%esi
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%edx
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    117b <main+0x2b>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%esi
mov    -0x10(%rbp),%rdx
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <getchar@plt>
cmp    $0xa,%al
je     11af <main+0x5f>
lea    0xe88(%rip),%r15
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
movsbl %al,%ebp
lea    0x2(%rbx),%esi
mov    %r14,%rdi
call   1070 <realloc@plt>
mov    %bpl,(%r14,%rbx,1)
mov    %r15,%rdi
mov    %ebx,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%rbx
xor    %eax,%eax
call   1050 <getchar@plt>
cmp    $0xa,%al
jne    1180 <main+0x30>
mov    %ebx,%eax
movb   $0x0,(%r14,%rax,1)
lea    0xe4b(%rip),%rdi
mov    %ebx,%esi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [8 x i8] c"\0A%d_%c\0A\00"
@.str.1 = [8 x i8] c"\0A%d_%s\0A\00"
define i32 @main() {
%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 %19, label %4
4: ; preds = %0, %4
%5 = phi i64 [ %14, %4 ], [ 0, %0 ]
%6 = phi i8 [ %15, %4 ], [ %2, %0 ]
%7 = sext i8 %6 to i32
%8 = trunc i64 %5 to i32
%9 = add i32 %8, 2
%10 = tail call i32 @realloc(i8* %1, i32 %9)
%11 = getelementptr inbounds i8, i8* %1, i64 %5
store i8 %6, i8* %11
%12 = trunc i64 %5 to i32
%13 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %12, i32 %7)
%14 = add nuw i64 %5, 1
%15 = tail call signext i8 (...) @getchar()
%16 = icmp eq i8 %15, 10
br i1 %16, label %17, label %4
17: ; preds = %4
%18 = trunc i64 %14 to i32
br label %19
19: ; preds = %17, %0
%20 = phi i32 [ 0, %0 ], [ %18, %17 ]
%21 = zext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %1, i64 %21
store i8 0, i8* %22
%23 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %20, i8* %1)
%24 = tail call i32 @free(i8* %1)
ret i32 0
}