#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 int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
scalar_t__ realloc(char *, int);
scalar_t__ strlen(char *);
int /*<<< orphan*/ *strrchr(char *, char);
char *readline(FILE *input, char *line) {
  int len;
  int max_line_len = 4096;
  if (fgets(line, max_line_len, input) == NULL)
    return NULL;
  while (strrchr(line, '\n') == NULL) {
    max_line_len *= 2;
    line = (char *)realloc(line, max_line_len);
    len = (int)strlen(line);
    if (fgets(line + len, max_line_len - len, input) == NULL)
      break;
  }
  return line;
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x1000,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    -0x10(%rbp),%rdx
call   1050 <fgets@plt>
cmp    $0x0,%rax
jne    117e <readline+0x3e>
movq   $0x0,-0x8(%rbp)
jmp    11f6 <readline+0xb6>
jmp    1183 <readline+0x43>
mov    -0x18(%rbp),%rdi
mov    $0xa,%esi
call   1040 <strrchr@plt>
cmp    $0x0,%rax
jne    11ee <readline+0xae>
mov    -0x20(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1060 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
mov    -0x20(%rbp),%esi
sub    -0x1c(%rbp),%esi
mov    -0x10(%rbp),%rdx
call   1050 <fgets@plt>
cmp    $0x0,%rax
jne    11e9 <readline+0xa9>
jmp    11ee <readline+0xae>
jmp    1183 <readline+0x43>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
mov    $0x1000,%ebp
mov    %rsi,%rdi
mov    $0x1000,%esi
mov    %r14,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11b2 <readline+0x72>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
mov    $0xa,%esi
call   1040 <strrchr@plt>
test   %rax,%rax
jne    11b4 <readline+0x74>
add    %ebp,%ebp
mov    %rbx,%rdi
mov    %ebp,%esi
call   1060 <realloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1030 <strlen@plt>
cltq
lea    (%rbx,%rax,1),%rdi
mov    %ebp,%esi
sub    %eax,%esi
mov    %r14,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
jne    1170 <readline+0x30>
jmp    11b4 <readline+0x74>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i8* @readline(i32* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i32* %0, i32** %4
store i8* %1, i8** %5
store i32 4096, i32* %7
%8 = load i8*, i8** %5
%9 = load i32, i32* %7
%10 = load i32*, i32** %4
%11 = call i32* @fgets(i8* %8, i32 %9, i32* %10)
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %14
13: ; preds = %2
store i8* null, i8** %3
br label %43
14: ; preds = %2
br label %15
15: ; preds = %40, %14
%16 = load i8*, i8** %5
%17 = call i32* @strrchr(i8* %16, i8 signext 10)
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %41
19: ; preds = %15
%20 = load i32, i32* %7
%21 = mul nsw i32 %20, 2
store i32 %21, i32* %7
%22 = load i8*, i8** %5
%23 = load i32, i32* %7
%24 = call i64 @realloc(i8* %22, i32 %23)
%25 = inttoptr i64 %24 to i8*
store i8* %25, i8** %5
%26 = load i8*, i8** %5
%27 = call i64 @strlen(i8* %26)
%28 = trunc i64 %27 to i32
store i32 %28, i32* %6
%29 = load i8*, i8** %5
%30 = load i32, i32* %6
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
%33 = load i32, i32* %7
%34 = load i32, i32* %6
%35 = sub nsw i32 %33, %34
%36 = load i32*, i32** %4
%37 = call i32* @fgets(i8* %32, i32 %35, i32* %36)
%38 = icmp eq i32* %37, null
br i1 %38, label %39, label %40
39: ; preds = %19
br label %41
40: ; preds = %19
br label %15
41: ; preds = %39, %15
%42 = load i8*, i8** %5
store i8* %42, i8** %3
br label %43
43: ; preds = %41, %13
%44 = load i8*, i8** %3
ret i8* %44
}

LLVM IR (-O2):
define i8* @readline(i32* nocapture %0, i8* %1) {
%3 = tail call i32* @fgets(i8* %1, i32 4096, i32* %0)
%4 = icmp eq i32* %3, null
br i1 %4, label %24, label %5
5: ; preds = %2, %10
%6 = phi i8* [ %13, %10 ], [ %1, %2 ]
%7 = phi i32 [ %11, %10 ], [ 4096, %2 ]
%8 = tail call i32* @strrchr(i8* %6, i8 signext 10)
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %24
10: ; preds = %5
%11 = shl nsw i32 %7, 1
%12 = tail call i64 @realloc(i8* %6, i32 %11)
%13 = inttoptr i64 %12 to i8*
%14 = tail call i64 @strlen(i8* dereferenceable(1) %13)
%15 = trunc i64 %14 to i32
%16 = shl i64 %14, 32
%17 = ashr exact i64 %16, 32
%18 = getelementptr inbounds i8, i8* %13, i64 %17
%19 = sub nsw i32 %11, %15
%20 = tail call i32* @fgets(i8* %18, i32 %19, i32* %0)
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %5
22: ; preds = %10
%23 = inttoptr i64 %12 to i8*
br label %24
24: ; preds = %5, %22, %2
%25 = phi i8* [ null, %2 ], [ %23, %22 ], [ %6, %5 ]
ret i8* %25
}