#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*/ *);
char *malloc(size_t);
char *realloc(char *, int);
size_t strlen(char *);
char *fgetln(FILE *stream, size_t *len) {
  static char *buffer = NULL;
  static size_t buflen = 0;
  char *newbuf;
  if (buflen == 0) {
    buflen = 512;
    if ((buffer = malloc(buflen + 1)) == NULL)
      return NULL;
  }
  if (fgets(buffer, buflen + 1, stream) == NULL)
    return NULL;
  *len = strlen(buffer);
  while (*len == buflen && buffer[*len - 1] != '\n') {
    if ((newbuf = realloc(buffer, 2 * buflen + 1)) == NULL)
      return NULL;
    buffer = newbuf;
    if (fgets(buffer + buflen, buflen + 1, stream) == NULL)
      return NULL;
    *len += strlen(buffer + buflen);
    buflen *= 2;
  }
  return buffer;
}


x86 assembly (clang, -O0):
<fgetln>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,0x2ef8(%rip)
jne    119c <fgetln+0x5c>
movq   $0x200,0x2ee7(%rip)
mov    0x2ee0(%rip),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,0x2ec8(%rip)
cmp    $0x0,%rax
jne    1197 <fgetln+0x57>
movq   $0x0,-0x8(%rbp)
jmp    12ea <fgetln+0x1aa>
jmp    119c <fgetln+0x5c>
mov    0x2ea5(%rip),%rdi
mov    0x2ea6(%rip),%rsi
add    $0x1,%rsi
mov    -0x10(%rbp),%rdx
call   1040 <fgets@plt>
cmp    $0x0,%rax
jne    11ce <fgetln+0x8e>
movq   $0x0,-0x8(%rbp)
jmp    12ea <fgetln+0x1aa>
mov    0x2e73(%rip),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
xor    %eax,%eax
cmp    0x2e5c(%rip),%rcx
mov    %al,-0x21(%rbp)
jne    121c <fgetln+0xdc>
mov    0x2e44(%rip),%rax
mov    -0x18(%rbp),%rcx
mov    (%rcx),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    122c <fgetln+0xec>
jmp    12df <fgetln+0x19f>
mov    0x2e15(%rip),%rdi
mov    0x2e16(%rip),%rax
shl    $0x1,%rax
add    $0x1,%rax
mov    %eax,%esi
call   1060 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1264 <fgetln+0x124>
movq   $0x0,-0x8(%rbp)
jmp    12ea <fgetln+0x1aa>
mov    -0x20(%rbp),%rax
mov    %rax,0x2dd9(%rip)
mov    0x2dd2(%rip),%rdi
add    0x2dd3(%rip),%rdi
mov    0x2dcc(%rip),%rsi
add    $0x1,%rsi
mov    -0x10(%rbp),%rdx
call   1040 <fgets@plt>
cmp    $0x0,%rax
jne    12a8 <fgetln+0x168>
movq   $0x0,-0x8(%rbp)
jmp    12ea <fgetln+0x1aa>
mov    0x2d99(%rip),%rdi
add    0x2d9a(%rip),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
add    (%rax),%rcx
mov    %rcx,(%rax)
mov    0x2d81(%rip),%rax
shl    $0x1,%rax
mov    %rax,0x2d76(%rip)
jmp    11e4 <fgetln+0xa4>
mov    0x2d62(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fgetln>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
mov    0x2efb(%rip),%rbx
test   %rbx,%rbx
je     1167 <fgetln+0x27>
mov    0x2ee7(%rip),%rdi
add    $0x1,%rbx
jmp    1194 <fgetln+0x54>
movq   $0x200,0x2ede(%rip)
mov    $0x201,%ebx
mov    $0x201,%edi
call   1050 <malloc@plt>
mov    %rax,%rdi
mov    %rax,0x2ebd(%rip)
test   %rax,%rax
je     123e <fgetln+0xfe>
mov    %rbx,%rsi
mov    %r14,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     123e <fgetln+0xfe>
mov    0x2e99(%rip),%r12
mov    %r12,%rdi
call   1030 <strlen@plt>
mov    %rax,(%r15)
cmp    0x2e8f(%rip),%rax
jne    1241 <fgetln+0x101>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmpb   $0xa,-0x1(%rax,%r12,1)
je     1241 <fgetln+0x101>
lea    (%rax,%rax,1),%esi
add    $0x1,%esi
mov    %r12,%rdi
call   1060 <realloc@plt>
test   %rax,%rax
je     123e <fgetln+0xfe>
mov    %rax,0x2e56(%rip)
mov    0x2e57(%rip),%rsi
add    %rsi,%rax
add    $0x1,%rsi
mov    %rax,%rdi
mov    %r14,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     123e <fgetln+0xfe>
mov    0x2e31(%rip),%r12
mov    0x2e32(%rip),%rbx
lea    (%r12,%rbx,1),%rdi
call   1030 <strlen@plt>
add    (%r15),%rax
mov    %rax,(%r15)
add    %rbx,%rbx
mov    %rbx,0x2e19(%rip)
cmp    %rbx,%rax
je     11d0 <fgetln+0x90>
jmp    1241 <fgetln+0x101>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@fgetln.buffer = internal unnamed_addr global i8* null
@fgetln.buflen = internal unnamed_addr global i64 0
define i8* @fgetln(i32* nocapture %0, i64* nocapture %1) {
%3 = load i64, i64* @fgetln.buflen
%4 = icmp eq i64 %3, 0
br i1 %4, label %8, label %5
5: ; preds = %2
%6 = load i8*, i8** @fgetln.buffer
%7 = add i64 %3, 1
br label %11
8: ; preds = %2
store i64 512, i64* @fgetln.buflen
%9 = tail call dereferenceable_or_null(513) i8* @malloc(i64 513)
store i8* %9, i8** @fgetln.buffer
%10 = icmp eq i8* %9, null
br i1 %10, label %49, label %11
11: ; preds = %5, %8
%12 = phi i64 [ %7, %5 ], [ 513, %8 ]
%13 = phi i8* [ %6, %5 ], [ %9, %8 ]
%14 = tail call i32* @fgets(i8* %13, i64 %12, i32* %0)
%15 = icmp eq i32* %14, null
br i1 %15, label %49, label %16
16: ; preds = %11
%17 = load i8*, i8** @fgetln.buffer
%18 = tail call i64 @strlen(i8* dereferenceable(1) %17)
store i64 %18, i64* %1
%19 = load i64, i64* @fgetln.buflen
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %49
21: ; preds = %16, %40
%22 = phi i8* [ %41, %40 ], [ %17, %16 ]
%23 = phi i64 [ %46, %40 ], [ %18, %16 ]
%24 = add i64 %23, -1
%25 = getelementptr inbounds i8, i8* %22, i64 %24
%26 = load i8, i8* %25
%27 = icmp eq i8 %26, 10
br i1 %27, label %49, label %28
28: ; preds = %21
%29 = trunc i64 %23 to i32
%30 = shl i32 %29, 1
%31 = or i32 %30, 1
%32 = tail call i8* @realloc(i8* %22, i32 %31)
%33 = icmp eq i8* %32, null
br i1 %33, label %49, label %34
34: ; preds = %28
store i8* %32, i8** @fgetln.buffer
%35 = load i64, i64* @fgetln.buflen
%36 = getelementptr inbounds i8, i8* %32, i64 %35
%37 = add i64 %35, 1
%38 = tail call i32* @fgets(i8* %36, i64 %37, i32* %0)
%39 = icmp eq i32* %38, null
br i1 %39, label %49, label %40
40: ; preds = %34
%41 = load i8*, i8** @fgetln.buffer
%42 = load i64, i64* @fgetln.buflen
%43 = getelementptr inbounds i8, i8* %41, i64 %42
%44 = tail call i64 @strlen(i8* dereferenceable(1) %43)
%45 = load i64, i64* %1
%46 = add i64 %45, %44
store i64 %46, i64* %1
%47 = shl i64 %42, 1
store i64 %47, i64* @fgetln.buflen
%48 = icmp eq i64 %46, %47
br i1 %48, label %21, label %49
49: ; preds = %34, %28, %40, %21, %16, %11, %8
%50 = phi i8* [ null, %8 ], [ null, %11 ], [ %17, %16 ], [ null, %34 ], [ null, %28 ], [ %22, %21 ], [ %41, %40 ]
ret i8* %50
}