#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 *, size_t, int /*<<< orphan*/ *);
scalar_t__ malloc(size_t);
scalar_t__ realloc(char *, size_t);
size_t strlen(char *);
char *readline(FILE *fp, size_t size) {
  char *buff = (char *)malloc(size * sizeof(char));
  size_t times = 1;
  if (fgets(buff, size, fp) == NULL)
    return NULL;
  while ((strlen(buff) == size - 1) && buff[size - 2] != '\n') {
    buff = (char *)realloc(buff, ++times * size * sizeof(char));
    if (fgets(buff + (times - 1) * size - 1, size, fp) == NULL)
      break;
  }
  return buff;
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
shl    $0x0,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
movq   $0x1,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1040 <fgets@plt>
cmp    $0x0,%rax
jne    1191 <readline+0x51>
movq   $0x0,-0x8(%rbp)
jmp    1244 <readline+0x104>
jmp    1196 <readline+0x56>
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rdx
sub    $0x1,%rdx
xor    %eax,%eax
cmp    %rdx,%rcx
mov    %al,-0x29(%rbp)
jne    11d1 <readline+0x91>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x2,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
setne  %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    11e1 <readline+0xa1>
jmp    123c <readline+0xfc>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
add    $0x1,%rsi
mov    %rsi,-0x28(%rbp)
imul   -0x18(%rbp),%rsi
shl    $0x0,%rsi
call   1060 <realloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rax
sub    $0x1,%rax
imul   -0x18(%rbp),%rax
add    %rax,%rdi
add    $0xffffffffffffffff,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1040 <fgets@plt>
cmp    $0x0,%rax
jne    1237 <readline+0xf7>
jmp    123c <readline+0xfc>
jmp    1196 <readline+0x56>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsi,%rdi
call   1050 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     11bf <readline+0x7f>
lea    -0x1(%r14),%r13
lea    (%r14,%r14,1),%rbx
mov    %r13,%rbp
nopw   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1030 <strlen@plt>
cmp    %r13,%rax
jne    11c2 <readline+0x82>
cmpb   $0xa,-0x2(%r12,%r14,1)
je     11c2 <readline+0x82>
mov    %r12,%rdi
mov    %rbx,%rsi
call   1060 <realloc@plt>
mov    %rax,%r12
lea    (%rax,%rbp,1),%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <fgets@plt>
add    %r14,%rbp
add    %r14,%rbx
test   %rax,%rax
jne    1180 <readline+0x40>
jmp    11c2 <readline+0x82>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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