#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 INT_MAX;
int /*<<< orphan*/ *fgets(char *, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ memory_error();
int /*<<< orphan*/ stdin;
size_t strlen(char *);
char *xmalloc(size_t);
char *xrealloc(char *, size_t);
char *readline(void) {
  size_t buf_len = 16;
  char *buf = xmalloc(buf_len * sizeof(char));
  if (fgets(buf, buf_len, stdin) == NULL) {
    free(buf);
    return NULL;
  }
  do {
    size_t l = strlen(buf);
    if ((l > 0) && (buf[l - 1] == '\n')) {
      l--;
      buf[l] = 0;
      return buf;
    }
    if (buf_len >= (INT_MAX / 2))
      memory_error();
    buf_len *= 2;
    buf = xrealloc(buf, buf_len * sizeof(char));
    if (fgets(buf + l, buf_len - l, stdin) == NULL)
      return buf;
  } while (1);
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movq   $0x10,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
shl    $0x0,%rdi
call   1060 <xmalloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2e58(%rip),%rax
mov    (%rax),%edx
call   1070 <fgets@plt>
cmp    $0x0,%rax
jne    11b7 <readline+0x57>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12ae <readline+0x14e>
jmp    11bc <readline+0x5c>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jbe    1212 <readline+0xb2>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1212 <readline+0xb2>
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12ae <readline+0x14e>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
cmp    %rcx,%rax
jb     1244 <readline+0xe4>
mov    $0x0,%al
call   1080 <memory_error@plt>
mov    -0x10(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
shl    $0x0,%rsi
call   1050 <xrealloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
add    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
sub    -0x20(%rbp),%rsi
mov    0x2d6c(%rip),%rax
mov    (%rax),%edx
call   1070 <fgets@plt>
cmp    $0x0,%rax
jne    129a <readline+0x13a>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12ae <readline+0x14e>
jmp    129f <readline+0x13f>
mov    $0x1,%al
test   $0x1,%al
jne    11bc <readline+0x5c>
jmp    12ae <readline+0x14e>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    $0x10,%ebx
mov    $0x10,%edi
call   1060 <xmalloc@plt>
mov    %rax,%r14
mov    0x2e66(%rip),%r12
mov    (%r12),%edx
mov    $0x10,%esi
mov    %rax,%rdi
call   1070 <fgets@plt>
test   %rax,%rax
je     120f <readline+0xaf>
mov    0x2e41(%rip),%r13
jmp    11d9 <readline+0x79>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    %rbx,%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1050 <xrealloc@plt>
mov    %rax,%r14
lea    (%rax,%r15,1),%rdi
mov    %rbx,%rsi
sub    %r15,%rsi
mov    (%r12),%edx
call   1070 <fgets@plt>
test   %rax,%rax
je     1222 <readline+0xc2>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rax,%r15
test   %rax,%rax
je     11f1 <readline+0x91>
cmpb   $0xa,-0x1(%r15,%r14,1)
je     121c <readline+0xbc>
mov    0x0(%r13),%eax
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %eax,%ecx
sar    %ecx
movslq %ecx,%rax
cmp    %rax,%rbx
jb     11b0 <readline+0x50>
xor    %eax,%eax
call   1080 <memory_error@plt>
jmp    11b0 <readline+0x50>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
jmp    1222 <readline+0xc2>
movb   $0x0,-0x1(%r14,%r15,1)
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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