// File: karel-brinda[P]ococo/ext/htslib/hts.c
// Function: hts_idx_push
int hts_idx_push(hts_idx_t *idx, int tid, int beg, int end, uint64_t offset, int is_mapped)
{
    int bin;
    if (tid<0) beg = -1, end = 0;
    if (tid >= idx->m) { // enlarge the index
        int32_t oldm = idx->m;
        idx->m = idx->m? idx->m<<1 : 2;
        idx->bidx = (bidx_t**)realloc(idx->bidx, idx->m * sizeof(bidx_t*));
        idx->lidx = (lidx_t*) realloc(idx->lidx, idx->m * sizeof(lidx_t));
        memset(&idx->bidx[oldm], 0, (idx->m - oldm) * sizeof(bidx_t*));
        memset(&idx->lidx[oldm], 0, (idx->m - oldm) * sizeof(lidx_t));
    }
    if (idx->n < tid + 1) idx->n = tid + 1;
    if (idx->z.finished) return 0;
    if (idx->z.last_tid != tid || (idx->z.last_tid >= 0 && tid < 0)) { // change of chromosome
        if ( tid>=0 && idx->n_no_coor )
        {
            if (hts_verbose >= 1) fprintf(stderr,"[E::%s] NO_COOR reads not in a single block at the end %d %d\n", __func__, tid,idx->z.last_tid);
            return -1;
        }
        if (tid>=0 && idx->bidx[tid] != 0)
        {
            if (hts_verbose >= 1) fprintf(stderr, "[E::%s] chromosome blocks not continuous\n", __func__);
            return -1;
        }
        idx->z.last_tid = tid;
        idx->z.last_bin = 0xffffffffu;
    } else if (tid >= 0 && idx->z.last_coor > beg) { // test if positions are out of order
        if (hts_verbose >= 1) fprintf(stderr, "[E::%s] unsorted positions on sequence #%d: %d followed by %d\n", __func__, tid+1, idx->z.last_coor+1, beg+1);
        return -1;
    }
    if ( tid>=0 )
    {
        if (idx->bidx[tid] == 0) idx->bidx[tid] = kh_init(bin);
        if ( is_mapped)
            insert_to_l(&idx->lidx[tid], beg, end, idx->z.last_off, idx->min_shift); // last_off points to the start of the current record
    }
    else idx->n_no_coor++;
    bin = hts_reg2bin(beg, end, idx->min_shift, idx->n_lvls);
    if ((int)idx->z.last_bin != bin) { // then possibly write the binning index
        if (idx->z.save_bin != 0xffffffffu) // save_bin==0xffffffffu only happens to the first record
            insert_to_b(idx->bidx[idx->z.save_tid], idx->z.save_bin, idx->z.save_off, idx->z.last_off);
        if (idx->z.last_bin == 0xffffffffu && idx->z.save_bin != 0xffffffffu) { // change of chr; keep meta information
            idx->z.off_end = idx->z.last_off;
            insert_to_b(idx->bidx[idx->z.save_tid], META_BIN(idx), idx->z.off_beg, idx->z.off_end);
            insert_to_b(idx->bidx[idx->z.save_tid], META_BIN(idx), idx->z.n_mapped, idx->z.n_unmapped);
            idx->z.n_mapped = idx->z.n_unmapped = 0;
            idx->z.off_beg = idx->z.off_end;
        }
        idx->z.save_off = idx->z.last_off;
        idx->z.save_bin = idx->z.last_bin = bin;
        idx->z.save_tid = tid;
    }
    if (is_mapped) ++idx->z.n_mapped;
    else ++idx->z.n_unmapped;
    idx->z.last_off = offset;
    idx->z.last_coor = beg;
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq %r8, -0x28(%rbp)
movl %r9d, -0x2c(%rbp)
cmpl $0x0, -0x14(%rbp)
jge 0x16aa1
movl $0xffffffff, -0x18(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x1c(%rbp)
movl -0x14(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jl 0x16b85
movq -0x10(%rbp), %rax
movl 0x18(%rax), %eax
movl %eax, -0x34(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x18(%rax)
je 0x16ad3
movq -0x10(%rbp), %rax
movl 0x18(%rax), %eax
shll %eax
movl %eax, -0x38(%rbp)
jmp 0x16add
movl $0x2, %eax
movl %eax, -0x38(%rbp)
jmp 0x16add
movl -0x38(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
movq -0x10(%rbp), %rax
movslq 0x18(%rax), %rsi
shlq $0x3, %rsi
callq 0x4990
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rdi
movq -0x10(%rbp), %rax
movslq 0x18(%rax), %rsi
shlq $0x4, %rsi
callq 0x4990
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
movslq -0x34(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rdi
movq -0x10(%rbp), %rax
movl 0x18(%rax), %eax
subl -0x34(%rbp), %eax
movslq %eax, %rdx
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x42d0
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rdi
movslq -0x34(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rdi
movq -0x10(%rbp), %rax
movl 0x18(%rax), %eax
subl -0x34(%rbp), %eax
movslq %eax, %rdx
shlq $0x4, %rdx
xorl %esi, %esi
callq 0x42d0
movq -0x10(%rbp), %rax
movl 0x14(%rax), %eax
movl -0x14(%rbp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jge 0x16ba3
movl -0x14(%rbp), %ecx
addl $0x1, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x14(%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x54(%rax)
je 0x16bb9
movl $0x0, -0x4(%rbp)
jmp 0x16f0a
movq -0x10(%rbp), %rax
movl 0x4c(%rax), %eax
cmpl -0x14(%rbp), %eax
jne 0x16bdd
movq -0x10(%rbp), %rax
cmpl $0x0, 0x4c(%rax)
jl 0x16c91
cmpl $0x0, -0x14(%rbp)
jge 0x16c91
cmpl $0x0, -0x14(%rbp)
jl 0x16c2d
movq -0x10(%rbp), %rax
cmpq $0x0, 0x20(%rax)
je 0x16c2d
cmpl $0x1, 0x739eb(%rip)     # 0x8a5e0
jl 0x16c21
movq 0x733d2(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
movl -0x14(%rbp), %ecx
movq -0x10(%rbp), %rax
movl 0x4c(%rax), %r8d
leaq 0x64424(%rip), %rsi     # 0x7b037
leaq 0x6445b(%rip), %rdx     # 0x7b075
movb $0x0, %al
callq 0x4830
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x16f0a
cmpl $0x0, -0x14(%rbp)
jl 0x16c7a
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x14(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0x16c7a
cmpl $0x1, 0x73993(%rip)     # 0x8a5e0
jl 0x16c6e
movq 0x7337a(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
leaq 0x64422(%rip), %rsi     # 0x7b082
leaq 0x6440e(%rip), %rdx     # 0x7b075
movb $0x0, %al
callq 0x4830
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x16f0a
movl -0x14(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x4c(%rax)
movq -0x10(%rbp), %rax
movl $0xffffffff, 0x40(%rax) # imm = 0xFFFFFFFF
jmp 0x16cf3
cmpl $0x0, -0x14(%rbp)
jl 0x16cf1
movq -0x10(%rbp), %rax
movl 0x48(%rax), %eax
cmpl -0x18(%rbp), %eax
jle 0x16cf1
cmpl $0x1, 0x73936(%rip)     # 0x8a5e0
jl 0x16ce5
movq 0x7331d(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
movl -0x14(%rbp), %ecx
addl $0x1, %ecx
movq -0x10(%rbp), %rax
movl 0x48(%rax), %r8d
addl $0x1, %r8d
movl -0x18(%rbp), %r9d
addl $0x1, %r9d
leaq 0x643d5(%rip), %rsi     # 0x7b0ac
leaq 0x64397(%rip), %rdx     # 0x7b075
movb $0x0, %al
callq 0x4830
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x16f0a
jmp 0x16cf3
cmpl $0x0, -0x14(%rbp)
jl 0x16d5c
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x14(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
jne 0x16d24
callq 0x16f20
movq %rax, %rdx
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x14(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
cmpl $0x0, -0x2c(%rbp)
je 0x16d5a
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rdi
movslq -0x14(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rdi
movslq -0x18(%rbp), %rsi
movslq -0x1c(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rcx
movq -0x10(%rbp), %rax
movl 0x4(%rax), %r8d
callq 0x16f40
jmp 0x16d6c
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x20(%rax)
movslq -0x18(%rbp), %rdi
movslq -0x1c(%rbp), %rsi
movq -0x10(%rbp), %rax
movl 0x4(%rax), %edx
movq -0x10(%rbp), %rax
movl 0x8(%rax), %ecx
callq 0x17110
movl %eax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movl 0x40(%rax), %eax
cmpl -0x30(%rbp), %eax
je 0x16ebf
movq -0x10(%rbp), %rax
cmpl $-0x1, 0x44(%rax)
je 0x16dd4
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x10(%rbp), %rcx
movslq 0x50(%rcx), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x10(%rbp), %rax
movl 0x44(%rax), %esi
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rdx
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rcx
callq 0x16250
movq -0x10(%rbp), %rax
cmpl $-0x1, 0x40(%rax)
jne 0x16e94
movq -0x10(%rbp), %rax
cmpl $-0x1, 0x44(%rax)
je 0x16e94
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x70(%rax)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x10(%rbp), %rcx
movslq 0x50(%rcx), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x10(%rbp), %rax
movl 0xc(%rax), %esi
addl $0x1, %esi
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rdx
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rcx
callq 0x16250
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x10(%rbp), %rcx
movslq 0x50(%rcx), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x10(%rbp), %rax
movl 0xc(%rax), %esi
addl $0x1, %esi
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rdx
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rcx
callq 0x16250
movq -0x10(%rbp), %rax
movq $0x0, 0x80(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x78(%rax)
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x68(%rax)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x60(%rax)
movl -0x30(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x40(%rax)
movq -0x10(%rbp), %rax
movl %ecx, 0x44(%rax)
movl -0x14(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x50(%rax)
cmpl $0x0, -0x2c(%rbp)
je 0x16ed7
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x78(%rax)
jmp 0x16eed
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x80(%rax)
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x58(%rax)
movl -0x18(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x48(%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)