#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 *fgets(char *, size_t, int /*<<< orphan*/);
char *malloc(size_t);
char *realloc(char *, size_t);
int /*<<< orphan*/ stdin;
scalar_t__ strlen(char *);
char *readline() {
  size_t alloc_length = 1024;
  size_t data_length = 0;
  char *data = malloc(alloc_length);
  while (true) {
    char *cursor = data + data_length;
    char *line = fgets(cursor, alloc_length - data_length, stdin);
    if (!line) {
      break;
    }
    data_length += strlen(cursor);
    if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') {
      break;
    }
    size_t new_length = alloc_length << 1;
    data = realloc(data, new_length);
    if (!data) {
      break;
    }
    alloc_length = new_length;
  }
  if (data[data_length - 1] == '\n') {
    data[data_length - 1] = '\0';
  }
  data = realloc(data, data_length);
  return data;
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movq   $0x400,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
sub    -0x10(%rbp),%rsi
mov    0x2e64(%rip),%rax
mov    (%rax),%edx
call   1040 <fgets@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    119f <readline+0x5f>
jmp    121d <readline+0xdd>
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jb     11de <readline+0x9e>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11e3 <readline+0xa3>
jmp    121d <readline+0xdd>
mov    -0x8(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1060 <realloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1210 <readline+0xd0>
jmp    121d <readline+0xdd>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1165 <readline+0x25>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1246 <readline+0x106>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    $0x400,%r12d
mov    $0x400,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
xor    %r15d,%r15d
mov    0x2e82(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
lea    (%r14,%r15,1),%rbx
mov    %r12,%rsi
sub    %r15,%rsi
mov    0x0(%r13),%edx
mov    %rbx,%rdi
call   1040 <fgets@plt>
test   %rax,%rax
je     11c1 <readline+0x81>
mov    %rbx,%rdi
call   1030 <strlen@plt>
add    %rax,%r15
lea    -0x1(%r12),%rax
cmp    %rax,%r15
jb     11c1 <readline+0x81>
cmpb   $0xa,-0x1(%r15,%r14,1)
je     11c1 <readline+0x81>
add    %r12,%r12
mov    %r14,%rdi
mov    %r12,%rsi
call   1060 <realloc@plt>
mov    %rax,%r14
test   %rax,%rax
jne    1170 <readline+0x30>
xor    %r14d,%r14d
cmpb   $0xa,-0x1(%r15,%r14,1)
jne    11cf <readline+0x8f>
movb   $0x0,-0x1(%r14,%r15,1)
mov    %r14,%rdi
mov    %r15,%rsi
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1060 <realloc@plt>

LLVM IR (-O0):
@stdin = global i32 0
define i8* @readline() {
%1 = alloca i64
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
store i64 1024, i64* %1
store i64 0, i64* %2
%7 = load i64, i64* %1
%8 = call i8* @malloc(i64 %7)
store i8* %8, i8** %3
br label %9
9: ; preds = %0, %49
%10 = load i8*, i8** %3
%11 = load i64, i64* %2
%12 = getelementptr inbounds i8, i8* %10, i64 %11
store i8* %12, i8** %4
%13 = load i8*, i8** %4
%14 = load i64, i64* %1
%15 = load i64, i64* %2
%16 = sub i64 %14, %15
%17 = load i32, i32* @stdin
%18 = call i8* @fgets(i8* %13, i64 %16, i32 %17)
store i8* %18, i8** %5
%19 = load i8*, i8** %5
%20 = icmp ne i8* %19, null
br i1 %20, label %22, label %21
21: ; preds = %9
br label %51
22: ; preds = %9
%23 = load i8*, i8** %4
%24 = call i64 @strlen(i8* %23)
%25 = load i64, i64* %2
%26 = add i64 %25, %24
store i64 %26, i64* %2
%27 = load i64, i64* %2
%28 = load i64, i64* %1
%29 = sub i64 %28, 1
%30 = icmp ult i64 %27, %29
br i1 %30, label %39, label %31
31: ; preds = %22
%32 = load i8*, i8** %3
%33 = load i64, i64* %2
%34 = sub i64 %33, 1
%35 = getelementptr inbounds i8, i8* %32, i64 %34
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp eq i32 %37, 10
br i1 %38, label %39, label %40
39: ; preds = %31, %22
br label %51
40: ; preds = %31
%41 = load i64, i64* %1
%42 = shl i64 %41, 1
store i64 %42, i64* %6
%43 = load i8*, i8** %3
%44 = load i64, i64* %6
%45 = call i8* @realloc(i8* %43, i64 %44)
store i8* %45, i8** %3
%46 = load i8*, i8** %3
%47 = icmp ne i8* %46, null
br i1 %47, label %49, label %48
48: ; preds = %40
br label %51
49: ; preds = %40
%50 = load i64, i64* %6
store i64 %50, i64* %1
br label %9
51: ; preds = %48, %39, %21
%52 = load i8*, i8** %3
%53 = load i64, i64* %2
%54 = sub i64 %53, 1
%55 = getelementptr inbounds i8, i8* %52, i64 %54
%56 = load i8, i8* %55
%57 = sext i8 %56 to i32
%58 = icmp eq i32 %57, 10
br i1 %58, label %59, label %64
59: ; preds = %51
%60 = load i8*, i8** %3
%61 = load i64, i64* %2
%62 = sub i64 %61, 1
%63 = getelementptr inbounds i8, i8* %60, i64 %62
store i8 0, i8* %63
br label %64
64: ; preds = %59, %51
%65 = load i8*, i8** %3
%66 = load i64, i64* %2
%67 = call i8* @realloc(i8* %65, i64 %66)
store i8* %67, i8** %3
%68 = load i8*, i8** %3
ret i8* %68
}

LLVM IR (-O2):
@stdin = global i32 0
define noalias i8* @readline() {
%1 = tail call dereferenceable_or_null(1024) i8* @malloc(i64 1024)
br label %2
2: ; preds = %21, %0
%3 = phi i8* [ %1, %0 ], [ %23, %21 ]
%4 = phi i64 [ 0, %0 ], [ %13, %21 ]
%5 = phi i64 [ 1024, %0 ], [ %22, %21 ]
%6 = getelementptr inbounds i8, i8* %3, i64 %4
%7 = sub i64 %5, %4
%8 = load i32, i32* @stdin
%9 = tail call i8* @fgets(i8* %6, i64 %7, i32 %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %25, label %11
11: ; preds = %2
%12 = tail call i64 @strlen(i8* dereferenceable(1) %6)
%13 = add i64 %12, %4
%14 = add i64 %5, -1
%15 = icmp ult i64 %13, %14
br i1 %15, label %25, label %16
16: ; preds = %11
%17 = add i64 %13, -1
%18 = getelementptr inbounds i8, i8* %3, i64 %17
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 10
br i1 %20, label %25, label %21
21: ; preds = %16
%22 = shl i64 %5, 1
%23 = tail call i8* @realloc(i8* %3, i64 %22)
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %2
25: ; preds = %21, %2, %16, %11
%26 = phi i8* [ %3, %11 ], [ %3, %16 ], [ %3, %2 ], [ null, %21 ]
%27 = phi i64 [ %13, %11 ], [ %13, %16 ], [ %4, %2 ], [ %13, %21 ]
%28 = add i64 %27, -1
%29 = getelementptr inbounds i8, i8* %26, i64 %28
%30 = load i8, i8* %29
%31 = icmp eq i8 %30, 10
br i1 %31, label %32, label %33
32: ; preds = %25
store i8 0, i8* %29
br label %33
33: ; preds = %32, %25
%34 = tail call i8* @realloc(i8* %26, i64 %27)
ret i8* %34
}