#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 errno;
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
char *realloc(char *, size_t);
char *strchr(char *, char);
char *fgetln(FILE *fp, size_t *len) {
  static char *buf = NULL;
  static size_t bufsiz = 0;
  char *ptr;
  if (buf == NULL) {
    bufsiz = 1024;
    if ((buf = malloc(bufsiz)) == NULL) {
      return NULL;
    }
  }
  if (fgets(buf, bufsiz, fp) == NULL) {
    return NULL;
  }
  *len = 0;
  while ((ptr = strchr(&buf[*len], '\n')) == NULL) {
    size_t nbufsiz = bufsiz + 1024;
    char *nbuf = realloc(buf, nbufsiz);
    if (nbuf == NULL) {
      int oerrno = errno;
      free(buf);
      errno = oerrno;
      buf = NULL;
      return NULL;
    } else {
      buf = nbuf;
    }
    *len = bufsiz;
    if (fgets(&buf[bufsiz], 1024, fp) == NULL) {
      return buf;
    }
    bufsiz = nbufsiz;
  }
  *len = (ptr - buf) + 1;
  return buf;
}


x86 assembly (clang, -O0):
<fgetln>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,0x2ee8(%rip)
jne    11a8 <fgetln+0x58>
movq   $0x400,0x2edf(%rip)
mov    0x2ed8(%rip),%rdi
call   1060 <malloc@plt>
mov    %rax,0x2ec4(%rip)
cmp    $0x0,%rax
jne    11a3 <fgetln+0x53>
movq   $0x0,-0x8(%rbp)
jmp    12f8 <fgetln+0x1a8>
jmp    11a8 <fgetln+0x58>
mov    0x2ea1(%rip),%rdi
mov    0x2ea2(%rip),%rax
mov    %eax,%esi
mov    -0x10(%rbp),%rdx
call   1050 <fgets@plt>
cmp    $0x0,%rax
jne    11d8 <fgetln+0x88>
movq   $0x0,-0x8(%rbp)
jmp    12f8 <fgetln+0x1a8>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    0x2e66(%rip),%rdi
mov    -0x18(%rbp),%rax
add    (%rax),%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    12d4 <fgetln+0x184>
mov    0x2e48(%rip),%rax
add    $0x400,%rax
mov    %rax,-0x28(%rbp)
mov    0x2e2f(%rip),%rdi
mov    -0x28(%rbp),%rsi
call   1070 <realloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1275 <fgetln+0x125>
mov    0x2da0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2e04(%rip),%rdi
call   1030 <free@plt>
mov    -0x34(%rbp),%ecx
mov    0x2d85(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,0x2de8(%rip)
movq   $0x0,-0x8(%rbp)
jmp    12f8 <fgetln+0x1a8>
mov    -0x30(%rbp),%rax
mov    %rax,0x2dd0(%rip)
mov    0x2dd1(%rip),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    0x2dbb(%rip),%rdi
add    0x2dbc(%rip),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x400,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
jne    12c4 <fgetln+0x174>
mov    0x2d95(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    12f8 <fgetln+0x1a8>
mov    -0x28(%rbp),%rax
mov    %rax,0x2d89(%rip)
jmp    11e3 <fgetln+0x93>
mov    -0x20(%rbp),%rcx
mov    0x2d71(%rip),%rax
sub    %rax,%rcx
add    $0x1,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    0x2d5c(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%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,%r15
mov    %rdi,%r14
mov    0x2ee8(%rip),%rdi
test   %rdi,%rdi
je     1175 <fgetln+0x25>
mov    0x2ee5(%rip),%esi
jmp    119e <fgetln+0x4e>
movq   $0x400,0x2ed8(%rip)
mov    $0x400,%edi
call   1060 <malloc@plt>
mov    %rax,0x2ebf(%rip)
test   %rax,%rax
je     11d7 <fgetln+0x87>
mov    %rax,%rdi
mov    $0x400,%esi
mov    %r14,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11d7 <fgetln+0x87>
movq   $0x0,(%r15)
mov    0x2e97(%rip),%r12
mov    %r12,%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     11ec <fgetln+0x9c>
sub    %r12,%rax
add    $0x1,%rax
mov    %rax,(%r15)
jmp    11da <fgetln+0x8a>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e65(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    0x400(%rbx),%r13
mov    %r12,%rdi
mov    %r13,%rsi
call   1070 <realloc@plt>
test   %rax,%rax
je     1264 <fgetln+0x114>
mov    %rax,0x2e32(%rip)
mov    %rbx,(%r15)
add    %rbx,%rax
mov    %rax,%rdi
mov    $0x400,%esi
mov    %r14,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     128a <fgetln+0x13a>
mov    %r13,0x2e18(%rip)
mov    0x2e09(%rip),%r12
mov    (%r15),%rdi
add    %r12,%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
mov    %r13,%rbx
test   %rax,%rax
je     1200 <fgetln+0xb0>
jmp    11cb <fgetln+0x7b>
mov    0x2d75(%rip),%rbx
mov    (%rbx),%ebp
mov    %r12,%rdi
call   1030 <free@plt>
mov    %ebp,(%rbx)
movq   $0x0,0x2dce(%rip)
xor    %r12d,%r12d
jmp    11da <fgetln+0x8a>
mov    0x2dbf(%rip),%r12
jmp    11da <fgetln+0x8a>

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

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