#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 EOF;
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ delays();
char getchar();
char *malloc(int);
int /*<<< orphan*/ puts(char *);
char *realloc(char *, int);
void chars(void) {
  char *array;
  char ch;
  int len = 0;
  int size = 20;
  puts("Write anything you like and it will be automagically");
  puts("stored into a dinamically allocated array.");
  puts("Press '~' to stop.");
  array = malloc(size);
  assert(array != NULL);
  do {
    ch = getchar();
    if (ch == '~' || ch == EOF)
      ch = '\0';
    len += 1;
    if (len > size) {
      size += 20;
      array = realloc(array, size);
      assert(array != NULL);
    }
    array[len - 1] = ch;
  } while (ch != '\0');
  if (len < size) {
    array = realloc(array, len);
    assert(array != NULL);
  }
  delays();
  puts("So you wrote..:");
  delays();
  puts(array);
}


x86 assembly (clang, -O0):
<chars>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x10(%rbp)
movl   $0x14,-0x14(%rbp)
lea    0xe83(%rip),%rdi
call   1030 <puts@plt>
lea    0xeac(%rip),%rdi
call   1030 <puts@plt>
lea    0xecb(%rip),%rdi
call   1030 <puts@plt>
mov    -0x14(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    $0x0,%al
call   1040 <getchar@plt>
mov    %al,-0x9(%rbp)
movsbl -0x9(%rbp),%eax
cmp    $0x7e,%eax
je     11e5 <chars+0x85>
movsbl -0x9(%rbp),%eax
mov    0x2e0e(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    11e9 <chars+0x89>
movb   $0x0,-0x9(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    1229 <chars+0xc9>
mov    -0x14(%rbp),%eax
add    $0x14,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1070 <realloc@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x9(%rbp),%dl
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
movsbl -0x9(%rbp),%eax
cmp    $0x0,%eax
jne    11b8 <chars+0x58>
mov    -0x10(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1277 <chars+0x117>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1070 <realloc@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    $0x0,%al
call   1080 <delays@plt>
lea    0xdee(%rip),%rdi
call   1030 <puts@plt>
mov    $0x0,%al
call   1080 <delays@plt>
mov    -0x8(%rbp),%rdi
call   1030 <puts@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chars>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe8e(%rip),%rdi
call   1030 <puts@plt>
lea    0xeb7(%rip),%rdi
call   1030 <puts@plt>
lea    0xed6(%rip),%rdi
call   1030 <puts@plt>
mov    $0x14,%r13d
mov    $0x14,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1050 <assert@plt>
xor    %r15d,%r15d
mov    0x2e2e(%rip),%r12
xor    %ebx,%ebx
jmp    11cd <chars+0x6d>
xchg   %ax,%ax
mov    %bpl,(%r14,%rbx,1)
add    $0x1,%rbx
test   %bpl,%bpl
je     1211 <chars+0xb1>
xor    %eax,%eax
call   1040 <getchar@plt>
cmp    (%r12),%al
movzbl %al,%eax
mov    %eax,%ebp
cmove  %r15d,%ebp
cmp    $0x7e,%al
cmove  %r15d,%ebp
movslq %r13d,%rax
cmp    %rax,%rbx
jl     11c0 <chars+0x60>
add    $0x14,%r13d
mov    %r14,%rdi
mov    %r13d,%esi
call   1070 <realloc@plt>
mov    %rax,%r14
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1050 <assert@plt>
jmp    11c0 <chars+0x60>
cmp    %ebx,%r13d
jle    1231 <chars+0xd1>
mov    %r14,%rdi
mov    %ebx,%esi
call   1070 <realloc@plt>
mov    %rax,%r14
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1050 <assert@plt>
xor    %eax,%eax
call   1080 <delays@plt>
lea    0xe34(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1080 <delays@plt>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <puts@plt>

LLVM IR (-O0):
@.str = [53 x i8] c"Write anything you like and it will be automagically\00"
@.str.1 = [43 x i8] c"stored into a dinamically allocated array.\00"
@.str.2 = [19 x i8] c"Press '~' to stop.\00"
@EOF = global i8 0
@.str.3 = [16 x i8] c"So you wrote..:\00"
define void @chars() {
%1 = alloca i8*
%2 = alloca i8
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %3
store i32 20, i32* %4
%5 = call i32 @puts(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0))
%6 = call i32 @puts(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0))
%7 = call i32 @puts(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%8 = load i32, i32* %4
%9 = call i8* @malloc(i32 %8)
store i8* %9, i8** %1
%10 = load i8*, i8** %1
%11 = icmp ne i8* %10, null
%12 = zext i1 %11 to i32
%13 = call i32 @assert(i32 %12)
br label %14
14: ; preds = %49, %0
%15 = call signext i8 (...) @getchar()
store i8 %15, i8* %2
%16 = load i8, i8* %2
%17 = sext i8 %16 to i32
%18 = icmp eq i32 %17, 126
br i1 %18, label %25, label %19
19: ; preds = %14
%20 = load i8, i8* %2
%21 = sext i8 %20 to i32
%22 = load i8, i8* @EOF
%23 = sext i8 %22 to i32
%24 = icmp eq i32 %21, %23
br i1 %24, label %25, label %26
25: ; preds = %19, %14
store i8 0, i8* %2
br label %26
26: ; preds = %25, %19
%27 = load i32, i32* %3
%28 = add nsw i32 %27, 1
store i32 %28, i32* %3
%29 = load i32, i32* %3
%30 = load i32, i32* %4
%31 = icmp sgt i32 %29, %30
br i1 %31, label %32, label %42
32: ; preds = %26
%33 = load i32, i32* %4
%34 = add nsw i32 %33, 20
store i32 %34, i32* %4
%35 = load i8*, i8** %1
%36 = load i32, i32* %4
%37 = call i8* @realloc(i8* %35, i32 %36)
store i8* %37, i8** %1
%38 = load i8*, i8** %1
%39 = icmp ne i8* %38, null
%40 = zext i1 %39 to i32
%41 = call i32 @assert(i32 %40)
br label %42
42: ; preds = %32, %26
%43 = load i8, i8* %2
%44 = load i8*, i8** %1
%45 = load i32, i32* %3
%46 = sub nsw i32 %45, 1
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %44, i64 %47
store i8 %43, i8* %48
br label %49
49: ; preds = %42
%50 = load i8, i8* %2
%51 = sext i8 %50 to i32
%52 = icmp ne i32 %51, 0
br i1 %52, label %14, label %53
53: ; preds = %49
%54 = load i32, i32* %3
%55 = load i32, i32* %4
%56 = icmp slt i32 %54, %55
br i1 %56, label %57, label %65
57: ; preds = %53
%58 = load i8*, i8** %1
%59 = load i32, i32* %3
%60 = call i8* @realloc(i8* %58, i32 %59)
store i8* %60, i8** %1
%61 = load i8*, i8** %1
%62 = icmp ne i8* %61, null
%63 = zext i1 %62 to i32
%64 = call i32 @assert(i32 %63)
br label %65
65: ; preds = %57, %53
%66 = call i32 (...) @delays()
%67 = call i32 @puts(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%68 = call i32 (...) @delays()
%69 = load i8*, i8** %1
%70 = call i32 @puts(i8* %69)
ret void
}

LLVM IR (-O2):
@.str = [53 x i8] c"Write anything you like and it will be automagically\00"
@.str.1 = [43 x i8] c"stored into a dinamically allocated array.\00"
@.str.2 = [19 x i8] c"Press '~' to stop.\00"
@EOF = global i8 0
@.str.3 = [16 x i8] c"So you wrote..:\00"
define void @chars() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0))
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0))
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%4 = tail call dereferenceable_or_null(20) i8* @malloc(i32 20)
%5 = icmp ne i8* %4, null
%6 = zext i1 %5 to i32
%7 = tail call i32 @assert(i32 %6)
br label %8
8: ; preds = %27, %0
%9 = phi i64 [ %18, %27 ], [ 0, %0 ]
%10 = phi i8* [ %28, %27 ], [ %4, %0 ]
%11 = phi i32 [ %29, %27 ], [ 20, %0 ]
%12 = tail call signext i8 (...) @getchar()
%13 = icmp eq i8 %12, 126
%14 = load i8, i8* @EOF
%15 = icmp eq i8 %12, %14
%16 = select i1 %13, i1 true, i1 %15
%17 = select i1 %16, i8 0, i8 %12
%18 = add nuw i64 %9, 1
%19 = sext i32 %11 to i64
%20 = icmp slt i64 %9, %19
br i1 %20, label %27, label %21
21: ; preds = %8
%22 = add nsw i32 %11, 20
%23 = tail call i8* @realloc(i8* %10, i32 %22)
%24 = icmp ne i8* %23, null
%25 = zext i1 %24 to i32
%26 = tail call i32 @assert(i32 %25)
br label %27
27: ; preds = %21, %8
%28 = phi i8* [ %23, %21 ], [ %10, %8 ]
%29 = phi i32 [ %22, %21 ], [ %11, %8 ]
%30 = getelementptr inbounds i8, i8* %28, i64 %9
store i8 %17, i8* %30
%31 = icmp eq i8 %17, 0
br i1 %31, label %32, label %8
32: ; preds = %27
%33 = trunc i64 %18 to i32
%34 = icmp sgt i32 %29, %33
br i1 %34, label %35, label %40
35: ; preds = %32
%36 = tail call i8* @realloc(i8* %28, i32 %33)
%37 = icmp ne i8* %36, null
%38 = zext i1 %37 to i32
%39 = tail call i32 @assert(i32 %38)
br label %40
40: ; preds = %35, %32
%41 = phi i8* [ %36, %35 ], [ %28, %32 ]
%42 = tail call i32 (...) @delays()
%43 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%44 = tail call i32 (...) @delays()
%45 = tail call i32 @puts(i8* dereferenceable(1) %41)
ret void
}