// File: /workspace/github_c_makefile_star2_O1/xiongxu[P]bam2bedGraph2/samtools-0.1.19/misc/bamcheck.c
// Function: round_buffer_flush
void round_buffer_flush(stats_t *stats, int64_t pos)
{
    int ibuf,idp;

    if ( pos==stats->cov_rbuf.pos ) 
        return;

    int64_t new_pos = pos;
    if ( pos==-1 || pos - stats->cov_rbuf.pos >= stats->cov_rbuf.size )
    {
        // Flush the whole buffer, but in sequential order, 
        pos = stats->cov_rbuf.pos + stats->cov_rbuf.size - 1;
    }

    if ( pos < stats->cov_rbuf.pos ) 
        error("Expected coordinates in ascending order, got %ld after %ld\n", pos,stats->cov_rbuf.pos);

    int ifrom = stats->cov_rbuf.start;
    int ito = round_buffer_lidx2ridx(stats->cov_rbuf.start,stats->cov_rbuf.size,stats->cov_rbuf.pos,pos-1);
    if ( ifrom>ito )
    {
        for (ibuf=ifrom; ibuf<stats->cov_rbuf.size; ibuf++)
        {
            if ( !stats->cov_rbuf.buffer[ibuf] )
                continue;
            idp = coverage_idx(stats->cov_min,stats->cov_max,stats->ncov,stats->cov_step,stats->cov_rbuf.buffer[ibuf]);
            stats->cov[idp]++;
            stats->cov_rbuf.buffer[ibuf] = 0;
        }
        ifrom = 0;
    }
    for (ibuf=ifrom; ibuf<=ito; ibuf++)
    {
        if ( !stats->cov_rbuf.buffer[ibuf] )
            continue;
        idp = coverage_idx(stats->cov_min,stats->cov_max,stats->ncov,stats->cov_step,stats->cov_rbuf.buffer[ibuf]);
        stats->cov[idp]++;
        stats->cov_rbuf.buffer[ibuf] = 0;
    }
    stats->cov_rbuf.start = (new_pos==-1) ? 0 : round_buffer_lidx2ridx(stats->cov_rbuf.start,stats->cov_rbuf.size,stats->cov_rbuf.pos,pos);
    stats->cov_rbuf.pos   = new_pos;
}

// Assembly:
pushq %rax
movq 0x168(%rdi), %rdx
movq %rsi, %rax
subq %rdx, %rax
je 0x4647
cmpq $-0x1, %rsi
je 0x44e5
movslq 0x170(%rdi), %r8
movq %rsi, %rcx
cmpq %r8, %rax
jl 0x44f3
movslq 0x170(%rdi), %rax
leaq (%rdx,%rax), %rcx
decq %rcx
cmpq %rdx, %rcx
jl 0x4649
movl 0x174(%rdi), %r10d
movslq 0x170(%rdi), %r8
movq 0x168(%rdi), %rax
movslq %r10d, %r9
notq %rax
addq %rcx, %rax
cqto
idivq %r8
leaq (%rdx,%r9), %rax
cqto
idivq %r8
movq %rdx, %r8
cmpl %r8d, %r9d
jle 0x459b
cmpl 0x170(%rdi), %r10d
jge 0x4598
movq 0x178(%rdi), %r10
movl (%r10,%r9,4), %r11d
testl %r11d, %r11d
je 0x4589
xorl %eax, %eax
movl %r11d, %edx
subl 0x158(%rdi), %edx
jl 0x4574
cmpl 0x15c(%rdi), %r11d
jle 0x4569
movl 0x14c(%rdi), %eax
decl %eax
jmp 0x4574
movl %edx, %eax
cltd
idivl 0x160(%rdi)
incl %eax
movq 0x150(%rdi), %rdx
cltq
incq (%rdx,%rax,8)
movl $0x0, (%r10,%r9,4)
incq %r9
movslq 0x170(%rdi), %rax
cmpq %rax, %r9
jl 0x4540
xorl %r10d, %r10d
cmpl %r8d, %r10d
jg 0x4608
movslq %r10d, %r9
shlq $0x2, %r9
addq 0x178(%rdi), %r9
subl %r10d, %r8d
incl %r8d
xorl %r10d, %r10d
movl (%r9,%r10,4), %r11d
testl %r11d, %r11d
je 0x4600
xorl %eax, %eax
movl %r11d, %edx
subl 0x158(%rdi), %edx
jl 0x45eb
cmpl 0x15c(%rdi), %r11d
jle 0x45e0
movl 0x14c(%rdi), %eax
decl %eax
jmp 0x45eb
movl %edx, %eax
cltd
idivl 0x160(%rdi)
incl %eax
movq 0x150(%rdi), %rdx
cltq
incq (%rdx,%rax,8)
movl $0x0, (%r9,%r10,4)
incq %r10
cmpl %r10d, %r8d
jne 0x45b7
cmpq $-0x1, %rsi
je 0x4638
movslq 0x174(%rdi), %r8
movslq 0x170(%rdi), %r9
subq 0x168(%rdi), %rcx
movq %rcx, %rax
cqto
idivq %r9
addq %rdx, %r8
movq %r8, %rax
cqto
idivq %r9
jmp 0x463a
xorl %edx, %edx
movl %edx, 0x174(%rdi)
movq %rsi, 0x168(%rdi)
popq %rax
retq
leaq 0x14b48(%rip), %rdi     # 0x19198
movq %rcx, %rsi
xorl %eax, %eax
callq 0x465a