// File: /workspace/github_c_makefile_star2_O3/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 %rbx
movq 0x168(%rdi), %rax
movq %rsi, %rdx
subq %rax, %rdx
je 0x4910
cmpq $-0x1, %rsi
je 0x47ba
movl 0x170(%rdi), %r10d
movslq %r10d, %r8
movq %rsi, %rcx
cmpq %r8, %rdx
jge 0x47c4
jmp 0x47cb
movl 0x170(%rdi), %r10d
movslq %r10d, %r8
leaq (%rax,%r8), %rcx
decq %rcx
cmpq %rax, %rcx
jl 0x4912
movl 0x174(%rdi), %r11d
movslq %r11d, %r9
notq %rax
addq %rcx, %rax
movslq %r10d, %r8
cqto
idivq %r8
leaq (%rdx,%r9), %rax
cqto
idivq %r8
movq %rdx, %r8
cmpl %r8d, %r9d
jle 0x4864
cmpl %r10d, %r11d
jge 0x4861
movq 0x178(%rdi), %r11
movl (%r11,%r9,4), %ebx
testl %ebx, %ebx
je 0x4856
xorl %eax, %eax
movl %ebx, %edx
subl 0x158(%rdi), %edx
jl 0x483a
cmpl 0x15c(%rdi), %ebx
jle 0x482f
movl 0x14c(%rdi), %eax
decl %eax
jmp 0x483a
movl %edx, %eax
cltd
idivl 0x160(%rdi)
incl %eax
movq 0x150(%rdi), %rdx
cltq
incq (%rdx,%rax,8)
movl $0x0, (%r11,%r9,4)
movl 0x170(%rdi), %r10d
incq %r9
movslq %r10d, %rax
cmpq %rax, %r9
jl 0x4809
xorl %r11d, %r11d
cmpl %r8d, %r11d
jg 0x48d1
movslq %r11d, %r9
shlq $0x2, %r9
addq 0x178(%rdi), %r9
subl %r11d, %r8d
incl %r8d
xorl %r10d, %r10d
movl (%r9,%r10,4), %r11d
testl %r11d, %r11d
je 0x48c9
xorl %eax, %eax
movl %r11d, %edx
subl 0x158(%rdi), %edx
jl 0x48b4
cmpl 0x15c(%rdi), %r11d
jle 0x48a9
movl 0x14c(%rdi), %eax
decl %eax
jmp 0x48b4
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 0x4880
cmpq $-0x1, %rsi
je 0x4901
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 0x4903
xorl %edx, %edx
movl %edx, 0x174(%rdi)
movq %rsi, 0x168(%rdi)
popq %rbx
retq
leaq 0x1592f(%rip), %rdi     # 0x1a248
movq %rcx, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x4926