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


x86 assembly (clang, -O0):
<libedit_fgetln>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,0x2ee0(%rip)
jne    11be <libedit_fgetln+0x5e>
mov    0x2e5b(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x2ed1(%rip)
mov    0x2eca(%rip),%rdi
call   1070 <malloc@plt>
mov    %rax,0x2eb6(%rip)
cmp    $0x0,%rax
jne    11b9 <libedit_fgetln+0x59>
movq   $0x0,-0x8(%rbp)
jmp    1340 <libedit_fgetln+0x1e0>
jmp    11be <libedit_fgetln+0x5e>
mov    0x2e93(%rip),%rdi
mov    0x2e94(%rip),%rax
movslq %eax,%rsi
mov    -0x10(%rbp),%rdx
call   1060 <fgets@plt>
cmp    $0x0,%rax
jne    11ef <libedit_fgetln+0x8f>
movq   $0x0,-0x8(%rbp)
jmp    1340 <libedit_fgetln+0x1e0>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    0x2e57(%rip),%rdi
mov    -0x18(%rbp),%rax
add    (%rax),%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    131c <libedit_fgetln+0x1bc>
mov    0x2e39(%rip),%rax
mov    0x2db2(%rip),%rcx
add    (%rcx),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e1c(%rip),%rdi
mov    -0x28(%rbp),%rsi
call   1080 <realloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1290 <libedit_fgetln+0x130>
mov    0x2d7d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2df1(%rip),%rdi
call   1030 <free@plt>
mov    -0x34(%rbp),%ecx
mov    0x2d62(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,0x2dd5(%rip)
movq   $0x0,-0x8(%rbp)
jmp    1340 <libedit_fgetln+0x1e0>
mov    -0x30(%rbp),%rax
mov    %rax,0x2dbd(%rip)
mov    0x2db6(%rip),%rdi
add    0x2db7(%rip),%rdi
mov    0x2d30(%rip),%rax
mov    (%rax),%rsi
mov    -0x10(%rbp),%rdx
call   1060 <fgets@plt>
cmp    $0x0,%rax
jne    12fe <libedit_fgetln+0x19e>
mov    0x2d8b(%rip),%rax
mov    0x2d8c(%rip),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    0x2d79(%rip),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    0x2d63(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1340 <libedit_fgetln+0x1e0>
mov    0x2d5b(%rip),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,0x2d49(%rip)
jmp    11fa <libedit_fgetln+0x9a>
mov    -0x20(%rbp),%rcx
mov    0x2d31(%rip),%rax
sub    %rax,%rcx
add    $0x1,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    0x2d1c(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<libedit_fgetln>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2ee0(%rip),%rdi
test   %rdi,%rdi
je     1186 <libedit_fgetln+0x26>
mov    0x2edc(%rip),%rbx
jmp    11b2 <libedit_fgetln+0x52>
mov    0x2e53(%rip),%rax
mov    (%rax),%rbx
mov    %rbx,0x2ec9(%rip)
mov    %rbx,%rdi
call   1070 <malloc@plt>
mov    %rax,%rdi
mov    %rax,0x2eaf(%rip)
test   %rax,%rax
je     1295 <libedit_fgetln+0x135>
movslq %ebx,%rsi
mov    %r15,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     1295 <libedit_fgetln+0x135>
movq   $0x0,(%r14)
mov    0x2e84(%rip),%r12
mov    %r12,%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     11f5 <libedit_fgetln+0x95>
sub    %r12,%rax
add    $0x1,%rax
mov    %rax,(%r14)
jmp    1298 <libedit_fgetln+0x138>
mov    0x2e64(%rip),%r13
mov    0x2ddd(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x0(%rbp),%rbx
add    %r13,%rbx
mov    %r12,%rdi
mov    %rbx,%rsi
call   1080 <realloc@plt>
test   %rax,%rax
je     1277 <libedit_fgetln+0x117>
mov    %rax,0x2e2a(%rip)
add    %rax,%r13
mov    0x0(%rbp),%rsi
mov    %r13,%rdi
mov    %r15,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     12aa <libedit_fgetln+0x14a>
mov    0x2e14(%rip),%rdi
mov    %rdi,(%r14)
mov    %rbx,0x2e0a(%rip)
mov    0x2dfb(%rip),%r12
add    %r12,%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
mov    %rbx,%r13
test   %rax,%rax
je     1210 <libedit_fgetln+0xb0>
jmp    11e6 <libedit_fgetln+0x86>
mov    0x2d5a(%rip),%rbx
mov    (%rbx),%ebp
mov    %r12,%rdi
call   1030 <free@plt>
mov    %ebp,(%rbx)
movq   $0x0,0x2dc3(%rip)
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2da7(%rip),%r12
mov    0x2da8(%rip),%rax
movb   $0x0,(%r12,%rax,1)
mov    %r12,%rdi
call   1040 <strlen@plt>
jmp    11ed <libedit_fgetln+0x8d>

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

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