#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 EOF;
int fgetc(int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, char *);
char *realloc(char *, int);
int /*<<< orphan*/ stdin;
int main() {
  char *d_s;
  int c;
  int sz = 0;
  d_s = malloc(sizeof(char));
  while ((c = fgetc(stdin)) != '\n' && c != EOF) {
    d_s = realloc(d_s, sizeof(char) * ++sz);
    *(d_s + (sz - 1)) = c;
  }
  *(d_s + sz) = '\0';
  printf("Dynamic string = [%s]\n", d_s);
  free(d_s);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x18(%rbp)
mov    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%edi
call   1050 <fgetc@plt>
mov    %eax,%ecx
mov    %ecx,-0x14(%rbp)
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x19(%rbp)
je     11a7 <main+0x57>
mov    -0x14(%rbp),%eax
mov    0x2e41(%rip),%rcx
cmp    (%rcx),%eax
setne  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    11b7 <main+0x67>
jmp    11ef <main+0x9f>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
cltq
shl    $0x0,%rax
mov    %eax,%esi
call   1070 <realloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    1174 <main+0x24>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rsi
lea    0xdfa(%rip),%rdi
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   %r13
push   %r12
push   %rbx
push   %rax
mov    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    0x2e79(%rip),%r15
mov    (%r15),%edi
call   1050 <fgetc@plt>
mov    %eax,%ebp
xor    %eax,%eax
cmp    $0xa,%ebp
je     11c1 <main+0x71>
mov    0x2e59(%rip),%r12
cmp    (%r12),%ebp
je     11c1 <main+0x71>
xor    %r13d,%r13d
mov    %r13,%rbx
add    $0x1,%r13
mov    %r14,%rdi
mov    %r13d,%esi
call   1070 <realloc@plt>
mov    %rax,%r14
mov    %bpl,(%rax,%rbx,1)
mov    (%r15),%edi
call   1050 <fgetc@plt>
cmp    $0xa,%eax
je     11be <main+0x6e>
mov    %eax,%ebp
cmp    (%r12),%eax
jne    1190 <main+0x40>
mov    %r13d,%eax
movb   $0x0,(%r14,%rax,1)
lea    0xe33(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@stdin = global i32 0
@EOF = global i32 0
@.str = [23 x i8] c"Dynamic string = [%s]\0A\00"
define i32 @main() {
%1 = tail call dereferenceable_or_null(1) i8* @malloc(i32 1)
%2 = load i32, i32* @stdin
%3 = tail call i32 @fgetc(i32 %2)
%4 = icmp ne i32 %3, 10
%5 = load i32, i32* @EOF
%6 = icmp ne i32 %3, %5
%7 = select i1 %4, i1 %6, i1 false
br i1 %7, label %8, label %25
8: ; preds = %0, %8
%9 = phi i64 [ %12, %8 ], [ 0, %0 ]
%10 = phi i32 [ %18, %8 ], [ %3, %0 ]
%11 = phi i8* [ %14, %8 ], [ %1, %0 ]
%12 = add nuw i64 %9, 1
%13 = trunc i64 %12 to i32
%14 = tail call i8* @realloc(i8* %11, i32 %13)
%15 = trunc i32 %10 to i8
%16 = getelementptr inbounds i8, i8* %14, i64 %9
store i8 %15, i8* %16
%17 = load i32, i32* @stdin
%18 = tail call i32 @fgetc(i32 %17)
%19 = icmp ne i32 %18, 10
%20 = load i32, i32* @EOF
%21 = icmp ne i32 %18, %20
%22 = select i1 %19, i1 %21, i1 false
br i1 %22, label %8, label %23
23: ; preds = %8
%24 = and i64 %12, 4294967295
br label %25
25: ; preds = %23, %0
%26 = phi i8* [ %1, %0 ], [ %14, %23 ]
%27 = phi i64 [ 0, %0 ], [ %24, %23 ]
%28 = getelementptr inbounds i8, i8* %26, i64 %27
store i8 0, i8* %28
%29 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %26)
%30 = tail call i32 @free(i8* %26)
ret i32 0
}