// File: /workspace/github_cpp_makefile_star2_O3/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, %r14
movq %rdi, %r12
cmpq $0x0, 0x335e(%rip)      # 0x5130
je 0x1dfe
movq 0x335d(%rip), %rsi      # 0x5138
movq %rax, %r13
subq %rsi, %r13
addq %r14, %rsi
movq %r14, %rdi
movq %r13, %rdx
movq %rax, %rbx
callq 0x11d0
movq %rbx, %rax
movq %r13, 0x3334(%rip)      # 0x5130
jmp 0x1e01
xorl %r13d, %r13d
leaq (%r14,%r13), %rdi
movq %rax, %rdx
subq %r13, %rdx
movl $0x1, %esi
movq %r12, %rcx
movq %rax, %rbx
callq 0x11a0
addq 0x330e(%rip), %rax      # 0x5130
movq %rax, 0x3307(%rip)      # 0x5130
je 0x1f1a
movq %rbx, 0x10(%rsp)
cmpq %rbx, %rax
jae 0x1e4f
cmpb %bpl, -0x1(%r14,%rax)
je 0x1e4f
leaq 0x1(%rax), %rcx
movq %rcx, 0x32e5(%rip)      # 0x5130
movb %bpl, (%r14,%rax)
movq $0x0, 0x8(%rsp)
movq (%r15), %rax
cmpq %rax, 0x8(%r15)
je 0x1e65
movq %rax, 0x8(%r15)
movq $0x0, 0x32c8(%rip) # 0x5138
xorps %xmm0, %xmm0
cvtss2sd (%rsp), %xmm0
movsd %xmm0, (%rsp)
xorl %r13d, %r13d
leaq 0x8(%rsp), %r12
cmpb %bpl, (%r14,%r13)
jne 0x1ea7
movq 0x8(%r15), %rsi
cmpq 0x10(%r15), %rsi
je 0x1eaf
movq 0x8(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x8(%r15)
jmp 0x1eba
incq %r13
movq %r13, %rbx
jmp 0x1ee7
movq %r15, %rdi
movq %r12, %rdx
callq 0x2574
leaq 0x1(%r13), %rbx
movq %rbx, 0x8(%rsp)
movq %rbx, 0x326e(%rip)      # 0x5138
cmpb $0x0, 0x326f(%rip)      # 0x5140
jne 0x1ee7
movq 0x8(%r15), %rsi
subq (%r15), %rsi
sarq $0x3, %rsi
cmpq $0xf4240, %rsi          # imm = 0xF4240
jae 0x1ef8
movq 0x3242(%rip), %rax      # 0x5130
movq %rbx, %r13
cmpq %rax, %rbx
jb 0x1e85
jmp 0x1f07
movq %r13, %rdi
movsd (%rsp), %xmm0
callq 0x1a30
jmp 0x1ee7
movq 0x8(%r15), %rcx
cmpq (%r15), %rcx
je 0x1f2b
cmpq 0x10(%rsp), %rax
setb %al
jmp 0x1f1c
movb $0x1, %al
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x1acf