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


x86 assembly (clang, -O0):
<fgetln>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,0x2ef0(%rip)
jne    119a <fgetln+0x5a>
movq   $0x400,0x2ee7(%rip)
mov    $0x400,%edi
call   1050 <malloc@plt>
mov    %rax,0x2ece(%rip)
cmpq   $0x0,0x2ec6(%rip)
jne    1195 <fgetln+0x55>
movq   $0x0,-0x8(%rbp)
jmp    1314 <fgetln+0x1d4>
jmp    119a <fgetln+0x5a>
mov    0x2ea7(%rip),%rax
mov    0x2ea8(%rip),%rcx
sub    $0x1,%rcx
movb   $0x1,(%rax,%rcx,1)
mov    0x2e91(%rip),%rdi
mov    0x2e92(%rip),%rsi
mov    -0x10(%rbp),%rdx
call   1040 <fgets@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11e3 <fgetln+0xa3>
movq   $0x0,-0x8(%rbp)
jmp    1314 <fgetln+0x1d4>
jmp    11e8 <fgetln+0xa8>
mov    0x2e59(%rip),%rax
mov    0x2e5a(%rip),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x29(%rbp)
jne    122b <fgetln+0xeb>
mov    0x2e35(%rip),%rax
mov    0x2e36(%rip),%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    123b <fgetln+0xfb>
jmp    12f3 <fgetln+0x1b3>
mov    0x2e06(%rip),%rdi
mov    0x2e07(%rip),%rsi
shl    $0x1,%rsi
call   1060 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    126e <fgetln+0x12e>
movq   $0x0,-0x8(%rbp)
jmp    1314 <fgetln+0x1d4>
mov    -0x28(%rbp),%rax
mov    %rax,0x2dcf(%rip)
mov    0x2dd0(%rip),%rax
shl    $0x1,%rax
mov    %rax,0x2dc5(%rip)
mov    0x2db6(%rip),%rax
mov    0x2db7(%rip),%rcx
sub    $0x1,%rcx
movb   $0x1,(%rax,%rcx,1)
mov    0x2da0(%rip),%rdi
mov    0x2da1(%rip),%rax
shr    $0x1,%rax
add    %rax,%rdi
add    $0xffffffffffffffff,%rdi
mov    0x2d8f(%rip),%rsi
shr    $0x1,%rsi
add    $0x1,%rsi
mov    -0x10(%rbp),%rdx
call   1040 <fgets@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    12ee <fgetln+0x1ae>
movq   $0x0,-0x8(%rbp)
jmp    1314 <fgetln+0x1d4>
jmp    11e8 <fgetln+0xa8>
mov    0x2d4e(%rip),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    0x2d38(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fgetln>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2ef0(%rip),%rdi
test   %rdi,%rdi
jne    1185 <fgetln+0x45>
movq   $0x400,0x2ee8(%rip)
mov    $0x400,%edi
call   1050 <malloc@plt>
mov    %rax,%rdi
mov    %rax,0x2ecc(%rip)
test   %rax,%rax
je     1214 <fgetln+0xd4>
mov    0x2ec4(%rip),%rsi
movb   $0x1,-0x1(%rsi,%rdi,1)
mov    %r15,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     1214 <fgetln+0xd4>
movabs $0x7fffffffffffffff,%rbp
ff ff 7f
nopl   0x0(%rax,%rax,1)
mov    0x2e91(%rip),%r12
mov    0x2e92(%rip),%rbx
cmpb   $0x0,-0x1(%rbx,%r12,1)
jne    1219 <fgetln+0xd9>
cmpb   $0xa,-0x2(%rbx,%r12,1)
je     1219 <fgetln+0xd9>
lea    (%rbx,%rbx,1),%r13
mov    %r12,%rdi
mov    %r13,%rsi
call   1060 <realloc@plt>
test   %rax,%rax
je     1214 <fgetln+0xd4>
mov    %rax,0x2e5f(%rip)
mov    %r13,0x2e60(%rip)
movb   $0x1,-0x1(%rax,%rbx,2)
and    %rbp,%rbx
lea    (%rax,%rbx,1),%rdi
add    $0xffffffffffffffff,%rdi
add    $0x1,%rbx
mov    %rbx,%rsi
mov    %r15,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
jne    11b0 <fgetln+0x70>
xor    %r12d,%r12d
jmp    1224 <fgetln+0xe4>
mov    %r12,%rdi
call   1030 <strlen@plt>
mov    %rax,(%r14)
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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