#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);
char *realloc(char *, int);
char *readString() {
  char *str = malloc(sizeof(char) * 32), *err;
  int pos;
  for (pos = 0; str != NULL && (str[pos] = getchar()) != '\n'; pos++) {
    if (pos % 32 == 32 - 1) {
      if ((err = realloc(str, sizeof(char) * (32 + pos + 1))) == NULL)
        free(str);
      str = err;
    }
  }
  if (str != NULL)
    str[pos] = '\0';
  return str;
}


x86 assembly (clang, -O0):
<readString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x20,%edi
call   1050 <malloc@plt>
mov    %rax,-0x8(%rbp)
movl   $0x0,-0x14(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x8(%rbp)
mov    %al,-0x15(%rbp)
je     118b <readString+0x4b>
mov    $0x0,%al
call   1040 <getchar@plt>
mov    -0x8(%rbp),%rcx
movslq -0x14(%rbp),%rdx
mov    %al,(%rcx,%rdx,1)
movsbl %al,%eax
cmp    $0xa,%eax
setne  %al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
test   $0x1,%al
jne    119b <readString+0x5b>
jmp    11fb <readString+0xbb>
mov    -0x14(%rbp),%eax
mov    $0x20,%ecx
cltd
idiv   %ecx
cmp    $0x1f,%edx
jne    11e8 <readString+0xa8>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%eax
add    $0x20,%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%esi
call   1060 <realloc@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11e0 <readString+0xa0>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11ed <readString+0xad>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    115d <readString+0x1d>
cmpq   $0x0,-0x8(%rbp)
je     1212 <readString+0xd2>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readString>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    $0x20,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11b4 <readString+0x74>
mov    %rax,%r14
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <getchar@plt>
mov    %al,(%r14)
cmp    $0xa,%al
je     11be <readString+0x7e>
xor    %r12d,%r12d
jmp    118e <readString+0x4e>
nop
mov    %r14,%r15
lea    0x1(%r12),%rbx
xor    %eax,%eax
call   1040 <getchar@plt>
mov    %al,0x1(%r15,%r12,1)
mov    %r15,%r14
mov    %rbx,%r12
cmp    $0xa,%al
je     11b9 <readString+0x79>
mov    %r12d,%eax
not    %eax
test   $0x1f,%al
jne    1170 <readString+0x30>
lea    0x21(%r12),%esi
mov    %r14,%rdi
call   1060 <realloc@plt>
mov    %rax,%r15
test   %rax,%rax
jne    1173 <readString+0x33>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
jmp    11c3 <readString+0x83>
mov    %ebx,%ebx
mov    %r15,%r14
movb   $0x0,(%r14,%rbx,1)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @readString() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i32
%4 = call i8* @malloc(i32 32)
store i8* %4, i8** %1
store i32 0, i32* %3
br label %5
5: ; preds = %38, %0
%6 = load i8*, i8** %1
%7 = icmp ne i8* %6, null
br i1 %7, label %8, label %16
8: ; preds = %5
%9 = call signext i8 (...) @getchar()
%10 = load i8*, i8** %1
%11 = load i32, i32* %3
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %10, i64 %12
store i8 %9, i8* %13
%14 = sext i8 %9 to i32
%15 = icmp ne i32 %14, 10
br label %16
16: ; preds = %8, %5
%17 = phi i1 [ false, %5 ], [ %15, %8 ]
br i1 %17, label %18, label %41
18: ; preds = %16
%19 = load i32, i32* %3
%20 = srem i32 %19, 32
%21 = icmp eq i32 %20, 31
br i1 %21, label %22, label %37
22: ; preds = %18
%23 = load i8*, i8** %1
%24 = load i32, i32* %3
%25 = add nsw i32 32, %24
%26 = add nsw i32 %25, 1
%27 = sext i32 %26 to i64
%28 = mul i64 1, %27
%29 = trunc i64 %28 to i32
%30 = call i8* @realloc(i8* %23, i32 %29)
store i8* %30, i8** %2
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %35
32: ; preds = %22
%33 = load i8*, i8** %1
%34 = call i32 @free(i8* %33)
br label %35
35: ; preds = %32, %22
%36 = load i8*, i8** %2
store i8* %36, i8** %1
br label %37
37: ; preds = %35, %18
br label %38
38: ; preds = %37
%39 = load i32, i32* %3
%40 = add nsw i32 %39, 1
store i32 %40, i32* %3
br label %5
41: ; preds = %16
%42 = load i8*, i8** %1
%43 = icmp ne i8* %42, null
br i1 %43, label %44, label %49
44: ; preds = %41
%45 = load i8*, i8** %1
%46 = load i32, i32* %3
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %45, i64 %47
store i8 0, i8* %48
br label %49
49: ; preds = %44, %41
%50 = load i8*, i8** %1
ret i8* %50
}

LLVM IR (-O2):
define i8* @readString() {
%1 = tail call dereferenceable_or_null(32) i8* @malloc(i32 32)
%2 = icmp eq i8* %1, null
br i1 %2, label %30, label %3
3: ; preds = %0
%4 = tail call signext i8 (...) @getchar()
store i8 %4, i8* %1
%5 = icmp eq i8 %4, 10
br i1 %5, label %24, label %6
6: ; preds = %3, %16
%7 = phi i8* [ %17, %16 ], [ %1, %3 ]
%8 = phi i64 [ %18, %16 ], [ 0, %3 ]
%9 = and i64 %8, 31
%10 = icmp eq i64 %9, 31
br i1 %10, label %11, label %16
11: ; preds = %6
%12 = trunc i64 %8 to i32
%13 = add i32 %12, 33
%14 = tail call i8* @realloc(i8* %7, i32 %13)
%15 = icmp eq i8* %14, null
br i1 %15, label %28, label %16
16: ; preds = %11, %6
%17 = phi i8* [ %7, %6 ], [ %14, %11 ]
%18 = add nuw i64 %8, 1
%19 = tail call signext i8 (...) @getchar()
%20 = getelementptr inbounds i8, i8* %17, i64 %18
store i8 %19, i8* %20
%21 = icmp eq i8 %19, 10
br i1 %21, label %22, label %6
22: ; preds = %16
%23 = and i64 %18, 4294967295
br label %24
24: ; preds = %22, %3
%25 = phi i64 [ %23, %22 ], [ 0, %3 ]
%26 = phi i8* [ %17, %22 ], [ %1, %3 ]
%27 = getelementptr inbounds i8, i8* %26, i64 %25
store i8 0, i8* %27
br label %30
28: ; preds = %11
%29 = tail call i32 @free(i8* %7)
br label %30
30: ; preds = %28, %0, %24
%31 = phi i8* [ %26, %24 ], [ null, %0 ], [ null, %28 ]
ret i8* %31
}