#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 */
typedef size_t ssize_t;
typedef int /*<<< orphan*/ buff;

/* Variables and functions */
size_t read(int /*<<< orphan*/, char *, int);
char *realloc(char *, size_t);
char *readline() {
  char *line = 0;
  static char buff[100];
  static ssize_t buff_length;
  static ssize_t old_index = sizeof(buff);
  size_t i = 0;
  size_t length = 0;
  while (1) {
    length += buff_length;
    line = realloc(line, length);
    for (; old_index < buff_length; ++old_index) {
      line[i++] = buff[old_index];
      if (buff[old_index] == '\n') {
        line[i - 1] = '\0';
        ++old_index;
        return (line);
      }
    }
    old_index = 0;
    buff_length = read(0, buff, sizeof(buff));
    if (buff_length <= 0) {
      return (NULL);
    }
  }
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2f71(%rip),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <realloc@plt>
mov    %rax,-0x10(%rbp)
mov    0x2ec9(%rip),%rax
cmp    0x2f4a(%rip),%rax
jae    1201 <readline+0xe1>
mov    0x2eb5(%rip),%rcx
lea    0x2ece(%rip),%rax
mov    (%rax,%rcx,1),%dl
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x18(%rbp)
mov    %dl,(%rax,%rcx,1)
mov    0x2e8e(%rip),%rcx
lea    0x2ea7(%rip),%rax
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11e5 <readline+0xc5>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    0x2e63(%rip),%rax
add    $0x1,%rax
mov    %rax,0x2e58(%rip)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1246 <readline+0x126>
jmp    11ea <readline+0xca>
mov    0x2e3f(%rip),%rax
add    $0x1,%rax
mov    %rax,0x2e34(%rip)
jmp    1160 <readline+0x40>
movq   $0x0,0x2e24(%rip)
xor    %edi,%edi
lea    0x2e3b(%rip),%rsi
mov    $0x64,%edx
call   1030 <read@plt>
mov    %rax,0x2e92(%rip)
cmpq   $0x0,0x2e8a(%rip)
ja     1241 <readline+0x121>
movq   $0x0,-0x8(%rbp)
jmp    1246 <readline+0x126>
jmp    1140 <readline+0x20>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    0x2f88(%rip),%rbx
xor    %r14d,%r14d
lea    0x2f16(%rip),%r12
xor    %r13d,%r13d
xor    %r15d,%r15d
jmp    1179 <readline+0x59>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movq   $0x0,0x2ed5(%rip)
xor    %edi,%edi
mov    %r12,%rsi
mov    $0x64,%edx
call   1030 <read@plt>
mov    %rax,%rbx
mov    %rax,0x2f44(%rip)
test   %rax,%rax
je     11ee <readline+0xce>
add    %rbx,%r15
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <realloc@plt>
mov    %rax,%r14
mov    0x2e9f(%rip),%rax
cmp    %rbx,%rax
jae    1150 <readline+0x30>
lea    (%rbx,%r13,1),%r8
sub    %rax,%r8
mov    %r14,%rdx
add    %r13,%rdx
sub    %rax,%rbx
xor    %esi,%esi
nopl   0x0(%rax,%rax,1)
lea    (%rax,%rsi,1),%rdi
movzbl (%r12,%rdi,1),%ecx
mov    %cl,(%rdx,%rsi,1)
cmp    $0xa,%cl
je     11cf <readline+0xaf>
add    $0x1,%rsi
cmp    %rsi,%rbx
jne    11b0 <readline+0x90>
mov    %r8,%r13
jmp    1150 <readline+0x30>
movb   $0x0,(%rsi,%rdx,1)
add    %rsi,%rax
add    $0x1,%rax
mov    %rax,0x2e4f(%rip)
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
xor    %r14d,%r14d
jmp    11e1 <readline+0xc1>

LLVM IR (-O0):
@readline.buff = internal global [100 x i8] zeroinitializer
@readline.buff_length = internal global i64 0
@readline.old_index = internal global i64 100
define i8* @readline() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i64
store i8* null, i8** %2
store i64 0, i64* %3
store i64 0, i64* %4
br label %5
5: ; preds = %0, %46
%6 = load i64, i64* @readline.buff_length
%7 = load i64, i64* %4
%8 = add i64 %7, %6
store i64 %8, i64* %4
%9 = load i8*, i8** %2
%10 = load i64, i64* %4
%11 = call i8* @realloc(i8* %9, i64 %10)
store i8* %11, i8** %2
br label %12
12: ; preds = %38, %5
%13 = load i64, i64* @readline.old_index
%14 = load i64, i64* @readline.buff_length
%15 = icmp ult i64 %13, %14
br i1 %15, label %16, label %41
16: ; preds = %12
%17 = load i64, i64* @readline.old_index
%18 = getelementptr inbounds [100 x i8], [100 x i8]* @readline.buff, i64 0, i64 %17
%19 = load i8, i8* %18
%20 = load i8*, i8** %2
%21 = load i64, i64* %3
%22 = add i64 %21, 1
store i64 %22, i64* %3
%23 = getelementptr inbounds i8, i8* %20, i64 %21
store i8 %19, i8* %23
%24 = load i64, i64* @readline.old_index
%25 = getelementptr inbounds [100 x i8], [100 x i8]* @readline.buff, i64 0, i64 %24
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %27, 10
br i1 %28, label %29, label %37
29: ; preds = %16
%30 = load i8*, i8** %2
%31 = load i64, i64* %3
%32 = sub i64 %31, 1
%33 = getelementptr inbounds i8, i8* %30, i64 %32
store i8 0, i8* %33
%34 = load i64, i64* @readline.old_index
%35 = add i64 %34, 1
store i64 %35, i64* @readline.old_index
%36 = load i8*, i8** %2
store i8* %36, i8** %1
br label %47
37: ; preds = %16
br label %38
38: ; preds = %37
%39 = load i64, i64* @readline.old_index
%40 = add i64 %39, 1
store i64 %40, i64* @readline.old_index
br label %12
41: ; preds = %12
store i64 0, i64* @readline.old_index
%42 = call i64 @read(i32 0, i8* getelementptr inbounds ([100 x i8], [100 x i8]* @readline.buff, i64 0, i64 0), i32 100)
store i64 %42, i64* @readline.buff_length
%43 = load i64, i64* @readline.buff_length
%44 = icmp ule i64 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %41
store i8* null, i8** %1
br label %47
46: ; preds = %41
br label %5
47: ; preds = %45, %29
%48 = load i8*, i8** %1
ret i8* %48
}

LLVM IR (-O2):
@readline.buff = internal global [100 x i8] zeroinitializer
@readline.buff_length = internal unnamed_addr global i64 0
@readline.old_index = internal unnamed_addr global i64 100
define noalias i8* @readline() {
%1 = load i64, i64* @readline.buff_length
br label %2
2: ; preds = %28, %0
%3 = phi i64 [ %1, %0 ], [ %30, %28 ]
%4 = phi i8* [ null, %0 ], [ %8, %28 ]
%5 = phi i64 [ 0, %0 ], [ %29, %28 ]
%6 = phi i64 [ 0, %0 ], [ %7, %28 ]
%7 = add i64 %3, %6
%8 = tail call i8* @realloc(i8* %4, i64 %7)
%9 = load i64, i64* @readline.old_index
%10 = icmp ult i64 %9, %3
br i1 %10, label %11, label %28
11: ; preds = %2
%12 = add i64 %5, %3
%13 = sub i64 %12, %9
br label %14
14: ; preds = %11, %24
%15 = phi i64 [ %25, %24 ], [ %5, %11 ]
%16 = phi i64 [ %26, %24 ], [ %9, %11 ]
%17 = getelementptr inbounds [100 x i8], [100 x i8]* @readline.buff, i64 0, i64 %16
%18 = load i8, i8* %17
%19 = getelementptr inbounds i8, i8* %8, i64 %15
store i8 %18, i8* %19
%20 = icmp eq i8 %18, 10
br i1 %20, label %21, label %24
21: ; preds = %14
%22 = getelementptr inbounds i8, i8* %8, i64 %15
store i8 0, i8* %22
%23 = add nuw i64 %16, 1
store i64 %23, i64* @readline.old_index
br label %32
24: ; preds = %14
%25 = add i64 %15, 1
%26 = add nuw i64 %16, 1
%27 = icmp eq i64 %25, %13
br i1 %27, label %28, label %14
28: ; preds = %24, %2
%29 = phi i64 [ %5, %2 ], [ %13, %24 ]
store i64 0, i64* @readline.old_index
%30 = tail call i64 @read(i32 0, i8* getelementptr inbounds ([100 x i8], [100 x i8]* @readline.buff, i64 0, i64 0), i32 100)
store i64 %30, i64* @readline.buff_length
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %2
32: ; preds = %28, %21
%33 = phi i8* [ %8, %21 ], [ null, %28 ]
ret i8* %33
}