// File: /workspace/github_cpp_makefile_star2_O1/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, 0x8(%rsp)
movl %r8d, %ebp
movq %rcx, %r15
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r12
cmpq $0x0, 0x3304(%rip)      # 0x5130
je 0x1e50
movq 0x3303(%rip), %rsi      # 0x5138
movq %rbx, %r13
subq %rsi, %r13
addq %r14, %rsi
movq %r14, %rdi
movq %r13, %rdx
callq 0x11d0
movq %r13, 0x32e0(%rip)      # 0x5130
movq 0x32d9(%rip), %rax      # 0x5130
leaq (%r14,%rax), %rdi
movq %rbx, %rdx
subq %rax, %rdx
movl $0x1, %esi
movq %r12, %rcx
callq 0x11a0
addq 0x32bb(%rip), %rax      # 0x5130
movq %rax, 0x32b4(%rip)      # 0x5130
je 0x1f72
cmpq %rbx, %rax
jae 0x1e9d
cmpb %bpl, -0x1(%r14,%rax)
je 0x1e9d
leaq 0x1(%rax), %rcx
movq %rcx, 0x3297(%rip)      # 0x5130
movb %bpl, (%r14,%rax)
movq $0x0, 0x10(%rsp)
movq (%r15), %rax
cmpq %rax, 0x8(%r15)
je 0x1eb3
movq %rax, 0x8(%r15)
movq $0x0, 0x327a(%rip) # 0x5138
movq 0x326b(%rip), %rax      # 0x5130
testq %rax, %rax
je 0x1f61
xorps %xmm0, %xmm0
cvtss2sd 0x8(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %r12d, %r12d
leaq 0x10(%rsp), %r13
cmpb %bpl, (%r14,%r12)
jne 0x1f40
movq 0x8(%r15), %rsi
cmpq 0x10(%r15), %rsi
je 0x1f07
movq 0x10(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x8(%r15)
jmp 0x1f12
movq %r15, %rdi
movq %r13, %rdx
callq 0x2512
leaq 0x1(%r12), %rax
movq %rax, 0x10(%rsp)
movq %rax, 0x3215(%rip)      # 0x5138
cmpb $0x0, 0x3216(%rip)      # 0x5140
jne 0x1f40
movq 0x8(%r15), %rsi
subq (%r15), %rsi
sarq $0x3, %rsi
cmpq $0xf4240, %rsi          # imm = 0xF4240
jae 0x1f51
incq %r12
movq 0x31e6(%rip), %rax      # 0x5130
cmpq %rax, %r12
jb 0x1ee5
jmp 0x1f61
movq %r12, %rdi
movsd 0x8(%rsp), %xmm0
callq 0x1a95
jmp 0x1f40
movq 0x8(%r15), %rcx
cmpq (%r15), %rcx
je 0x1f83
cmpq %rbx, %rax
setb %al
jmp 0x1f74
movb $0x1, %al
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1b34