// File: /workspace/github_cpp_makefile_star2_O2/alexandres[P]terashuf/terashuf.cc
// Function: fillBufAndMarkLines(_IO_FILE*, char*, unsigned long, std::vector<unsigned long, std::allocator<unsigned long>>&, char, float)
bool fillBufAndMarkLines(FILE *f, char *buf, const ll bufBytes, std::vector<ll> &shufIndexes, char sep, float memory)
{
    static ll bufPos = 0, lastLineEndPos = 0;
    static bool memoryOverheadDisplayed;

    // see if anything left from previous fillBuf
    if (bufPos)
    {
        ll bytesToCpy = bufBytes - lastLineEndPos;
        memmove(buf, buf + lastLineEndPos, bytesToCpy);
        bufPos = bytesToCpy;
    }

    bufPos += fread(buf + bufPos, sizeof(char), bufBytes - bufPos, f);
    // if got nothing in buffer, signal we're done
    if (!bufPos)
        return true;

    // handle missing sep on last line
    // buffer not full and previous position is not newline
    if (bufPos < bufBytes && buf[bufPos - 1] != sep)
        buf[bufPos++] = sep;

    // mark lines and store pos in shufIndexes
    ll lineStart = 0;
    shufIndexes.clear();
    lastLineEndPos = 0;
    for (ll i = 0; i < bufPos; i++)
    {
        if (buf[i] != sep)
            continue;
        shufIndexes.push_back(lineStart);
        lineStart = i + 1;
        lastLineEndPos = i + 1;
        if (!memoryOverheadDisplayed && shufIndexes.size() >= LINES_BEFORE_ESTIMATING_MEMORY_OVERHEAD)
        {
            memoryOverheadDisplayed = true;
            double averageBytesPerLine = (double)i / (double)shufIndexes.size();
            double memoryOverhead = sizeof(ll) / averageBytesPerLine + 1;
            fprintf(stderr, "mean line-length is %.2f, estimated memory usage is %.2f * %.2f GB = %.2f GB\nTip: If you would like use exactly %.2f GB of memory, use MEMORY=%.4f ./terashuf ...\n", averageBytesPerLine - 1, memoryOverhead, memory, memoryOverhead * memory, memory, memory / memoryOverhead);
        }
    }
    if (shufIndexes.size() == 0)
    {
        fprintf(
            stderr,
            "\nFATAL ERROR: line too long to fit in buffer (> %zu bytes):\n",
            bufBytes);
        fwrite(buf, sizeof(char), std::min(bufBytes, (ll)50), stderr);
        fprintf(stderr, "...\n");
        exit(1); // line was too long to fit in buf, can't be shuffled
    }
    // if buffer isn't full, signal we're done
    return bufPos < bufBytes;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movss %xmm0, (%rsp)
movl %r8d, %ebp
movq %rcx, %r15
movq %rdx, %rax
movq %rsi, %rbx
movq %rdi, %r12
cmpq $0x0, 0x3c84(%rip)      # 0x5140
je 0x14e8
movq 0x3c83(%rip), %rsi      # 0x5148
movq %rax, %r13
subq %rsi, %r13
addq %rbx, %rsi
movq %rbx, %rdi
movq %r13, %rdx
movq %rax, %r14
callq 0x11f0
movq %r14, %rax
movq %r13, 0x3c5a(%rip)      # 0x5140
jmp 0x14eb
xorl %r13d, %r13d
leaq (%rbx,%r13), %rdi
movq %rax, %rdx
subq %r13, %rdx
pushq $0x1
popq %rsi
movq %r12, %rcx
movq %rax, %r14
callq 0x11c0
addq 0x3c36(%rip), %rax      # 0x5140
movq %rax, 0x3c2f(%rip)      # 0x5140
je 0x167d
movq %r14, 0x8(%rsp)
cmpq %r14, %rax
jae 0x153a
cmpb %bpl, -0x1(%rbx,%rax)
je 0x153a
leaq 0x1(%rax), %rcx
movq %rcx, 0x3c0d(%rip)      # 0x5140
movb %bpl, (%rbx,%rax)
movq %rcx, %rax
andq $0x0, 0x10(%rsp)
movq (%r15), %rcx
cmpq %rcx, 0x8(%r15)
je 0x154d
movq %rcx, 0x8(%r15)
andq $0x0, 0x3bf3(%rip)      # 0x5148
cvtss2sd (%rsp), %xmm0
movsd %xmm0, (%rsp)
xorl %r13d, %r13d
leaq 0x10(%rsp), %r12
cmpq %rax, %r13
jae 0x166a
cmpb %bpl, (%rbx,%r13)
jne 0x1655
movq %r15, %rdi
movq %r12, %rsi
callq 0x1e94
leaq 0x1(%r13), %r14
movq %r14, 0x10(%rsp)
movq %r14, 0x3bb3(%rip)      # 0x5148
cmpb $0x0, 0x3bb4(%rip)      # 0x5150
jne 0x165b
movq 0x8(%r15), %rax
subq (%r15), %rax
sarq $0x3, %rax
cmpq $0xf4240, %rax          # imm = 0xF4240
jb 0x165b
movb $0x1, 0x3b90(%rip)      # 0x5150
movq %r13, %xmm1
movq 0x1a43(%rip), %xmm2     # 0x3010
punpckldq %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
movapd 0x1a47(%rip), %xmm3     # 0x3020
subpd %xmm3, %xmm1
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
movq %rax, %xmm1
punpckldq %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
subpd %xmm3, %xmm1
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
divsd %xmm2, %xmm0
movsd 0x1a32(%rip), %xmm1     # 0x3040
divsd %xmm0, %xmm1
addsd 0x1a2e(%rip), %xmm1     # 0x3048
movq 0x39b7(%rip), %rax      # 0x4fd8
movq (%rax), %rdi
addsd 0x1a24(%rip), %xmm0     # 0x3050
movapd %xmm1, %xmm3
movsd (%rsp), %xmm2
mulsd %xmm2, %xmm3
movapd %xmm2, %xmm5
divsd %xmm1, %xmm5
leaq 0x1a71(%rip), %rsi      # 0x30b9
movapd %xmm2, %xmm4
movb $0x6, %al
callq 0x11b0
jmp 0x165b
incq %r13
movq %r13, %r14
movq 0x3ade(%rip), %rax      # 0x5140
movq %r14, %r13
jmp 0x1567
movq 0x8(%r15), %rcx
cmpq (%r15), %rcx
je 0x168e
cmpq 0x8(%rsp), %rax
setb %al
jmp 0x167f
movb $0x1, %al
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x3943(%rip), %r15      # 0x4fd8
movq (%r15), %rdi
leaq 0x1abd(%rip), %rsi      # 0x315c
movq 0x8(%rsp), %r14
movq %r14, %rdx
xorl %eax, %eax
callq 0x11b0
cmpq $0x32, %r14
pushq $0x32
popq %rdx
cmovbq %r14, %rdx
movq (%r15), %rcx
pushq $0x1
popq %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1220
movq (%r15), %rcx
leaq 0x1a82(%rip), %rdi      # 0x3157
pushq $0x4
popq %rsi
movq %r14, %rdx
callq 0x1220
pushq $0x1
popq %rdi
callq 0x1150