// File: /workspace/github_c_makefile_star2_O1/samtools[P]htslib/cram/cram_external.c
// Function: cram_filter_container
int cram_filter_container(cram_fd *in, cram_fd *out, cram_container *c,
                          int *ref_id) {
    int err = 0, fixed_ref = -3;

    if (ref_id)
        *ref_id = c->ref_seq_id;

    int rid = in->range.refid == -2 ? -1 : in->range.refid;
    if (rid != c->ref_seq_id ||
        in->range.start > c->ref_seq_start + c->ref_seq_span-1)
        // Except for multi-ref cases
        if (c->ref_seq_id != -2)
            return cram_skip_container(in, c);

    // Container compression header
    cram_block *blk = cram_read_block(in);
    if (!blk)
        return -1;
    c->comp_hdr = cram_decode_compression_header(in, blk);
    in->ctr = c;

    // If it's multi-ref but a constant ref-id, then we can still do
    // basic level chromosome filtering.  Similarly multi-ref where we're
    // _already_ in ref "*" (unmapped) means we can just copy the container
    // as there are no positions to filter on and "*" sorts to the end.
    // TODO: how to tell "already in" though?
    if (c->ref_seq_id == -2) {
        cram_codec *cd = c->comp_hdr->codecs[DS_RI];
        if (cd && cd->codec == E_HUFFMAN && cd->u.huffman.ncodes == 1 &&
            // this check should be always true anyway
            rid == cd->u.huffman.codes[0].symbol)
            // We're in multi-ref mode, but actually the entire container
            // matches.  So if we're in whole-chromosome mode we can just
            // copy.
            if (in->range.start <= 1 &&
                in->range.end >= (INT64_MAX&(0xffffffffULL<<32))) {
                if (ref_id)
                    *ref_id = rid;
                err |= cram_write_container(out, c) < 0;
                err |= cram_write_block(out, blk);
                return cram_copy_slice(in, out, c->num_landmarks) | -err;
            }
    }

    // A simple read-write loop with region filtering automatically due to
    // an earlier CRAM_OPT_RANGE request.
    //
    // We can hit EOF when reaching the end of the range, but we still need
    // to manually check we don't attempt to read beyond this single container.

    cram_range rng_copy = in->range;
    in->range.start = INT64_MIN;
    in->range.end = INT64_MAX;

    bam1_t *b = bam_init1();
    while ((c->curr_slice < c->max_slice ||
            c->slice->curr_rec < c->slice->max_rec)) {
        cram_slice *s;
        if (c->slice && c->slice->curr_rec < c->slice->max_rec)
            s = c->slice;
        else if (c->curr_slice < c->max_slice)
            s = cram_next_slice(in, &c);
        else
            break; // end of container
        c->slice = s;

        // This is more efficient if we check as a cram record instead of a
        // bam record as we don't have to parse CIGAR end.
        cram_record *cr = &c->slice->crecs[c->slice->curr_rec];
        if (fixed_ref == -3)
            fixed_ref = cr->ref_id;
        else if (fixed_ref != cr->ref_id)
            fixed_ref = -2;

        if (rng_copy.refid != cr->ref_id) {
            if (rng_copy.refid == -2) {
                if (cr->ref_id > -1) {
                    // Want unmapped, but have mapped
                    c->slice->curr_rec++;
                    continue;
                }
            } else {
                if (rng_copy.refid > cr->ref_id || rng_copy.refid == -1) {
                    // multi-ref and not at the correct ref yet
                    c->slice->curr_rec++;
                    continue;
                } else {
                    // multi-ref and beyond the desired ref
                    break;
                }
            }
        }

        // Correct ref, but check the desired region
        if (cr->aend < rng_copy.start) {
            c->slice->curr_rec++;
            continue;
        }
        if (cr->apos > rng_copy.end)
            break;

        // Broadly rquivalent to cram_get_bam_seq, but starting from 'cr'
        err |= cram_to_bam(in->header, in, s, cr, s->curr_rec++, &b) < 0;

        if (cram_put_bam_seq(out, b) < 0) {
            err |= 1;
            break;
        }
    }
    bam_destroy1(b);

    if (ref_id)
        *ref_id = fixed_ref;

    in->range = rng_copy;

    // Avoids double frees as we stole the container from our other
    // file descriptor.
    in->ctr    = NULL;
    in->ctr_mt = NULL;

    err |= cram_flush(out);
    cram_free_block(blk);

    return -err;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdx, %r12
movq %rsi, %r14
movq %rdi, %rbx
movq %rdx, (%rsp)
testq %rcx, %rcx
je 0x93886
movl 0x4(%r12), %eax
movl %eax, (%rcx)
movl 0x258(%rbx), %eax
cmpl $-0x2, %eax
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmovnel %eax, %ebp
movl 0x4(%r12), %eax
cmpl %eax, %ebp
jne 0x9398f
cmpl $-0x2, %eax
je 0x93998
movq 0x10(%r12), %rax
addq 0x8(%r12), %rax
cmpq %rax, 0x260(%rbx)
jl 0x93998
movq %rbx, %rdi
callq 0x14900
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x93c69
movq %rax, %rdi
callq 0x15090
cmpl $0x0, 0x30(%r12)
jle 0x93988
xorl %ebp, %ebp
movq %rbx, %rdi
callq 0x14900
testq %rax, %rax
je 0x93a60
movq %rax, %r15
movq %rbx, %rdi
movq %rax, %rsi
callq 0x16270
movq %rax, %r14
movq %r15, %rdi
callq 0x15090
testq %r14, %r14
je 0x93a60
movl 0x28(%r14), %r15d
testl %r15d, %r15d
setg %r13b
jle 0x9396a
movq %rbx, %rdi
callq 0x14900
testq %rax, %rax
je 0x9396a
movl $0x1, %ecx
movl %ecx, %r13d
movq %rax, %rdi
callq 0x15090
cmpl %r13d, %r15d
je 0x93963
movq %rbx, %rdi
callq 0x14900
leal 0x1(%r13), %ecx
testq %rax, %rax
jne 0x93942
cmpl %r15d, %r13d
setl %r13b
movq %r14, %rdi
callq 0x158a0
testb %r13b, %r13b
jne 0x93a60
incl %ebp
cmpl 0x30(%r12), %ebp
jl 0x938f0
xorl %ebp, %ebp
jmp 0x93c69
cmpl $-0x2, %eax
jne 0x938c4
movq %rcx, 0x20(%rsp)
movq %rbx, %rdi
callq 0x14900
testq %rax, %rax
je 0x93a60
movq %rax, %r15
movq %rbx, %rdi
movq %rax, %rsi
callq 0x15160
movq (%rsp), %rsi
movq %rax, 0x48(%rsi)
movq %rsi, 0x38(%rbx)
xorl %r12d, %r12d
cmpl $-0x2, 0x4(%rsi)
jne 0x93a74
movq 0x388(%rax), %rcx
xorl %r12d, %r12d
movb $0x1, %al
testq %rcx, %rcx
je 0x93a6a
cmpl $0x3, (%rcx)
jne 0x93a6a
cmpl $0x1, 0x60(%rcx)
jne 0x93a6a
movslq %ebp, %rdx
movq 0x68(%rcx), %rcx
cmpq %rdx, (%rcx)
jne 0x93a6a
cmpq $0x1, 0x260(%rbx)
jg 0x93a6a
cmpl $0x7fffffff, 0x26c(%rbx) # imm = 0x7FFFFFFF
jb 0x93a6a
movq 0x20(%rsp), %rax
testq %rax, %rax
je 0x93a23
movl %ebp, (%rax)
movq %r14, %rdi
callq 0x14120
movl %eax, %r12d
shrl $0x1f, %r12d
movq %r14, %rdi
movq %r15, %rsi
callq 0x15790
orl %r12d, %eax
movl %eax, %r12d
movq (%rsp), %rax
movl 0x30(%rax), %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x14970
movl %r12d, %ecx
negl %ecx
orl %eax, %ecx
xorl %eax, %eax
jmp 0x93a6a
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x93c69
testb %al, %al
movl %ecx, %ebp
je 0x93c69
movq %r15, 0x40(%rsp)
movq %r14, 0x28(%rsp)
leaq 0x38(%rbx), %rax
movq %rax, 0x38(%rsp)
movl 0x258(%rbx), %r14d
movl 0x25c(%rbx), %eax
movl %eax, 0x14(%rsp)
movq 0x260(%rbx), %r13
movq 0x268(%rbx), %rax
movq %rax, 0x30(%rsp)
movabsq $0x7fffffffffffffff, %rax # imm = 0x7FFFFFFFFFFFFFFF
leaq 0x1(%rax), %rcx
movq %rcx, 0x260(%rbx)
movq %rax, 0x268(%rbx)
callq 0x14b60
leaq 0x8(%rsp), %rcx
movq %rax, (%rcx)
movl $0xfffffffd, %ebp       # imm = 0xFFFFFFFD
movq %r13, 0x18(%rsp)
movq (%rsp), %rax
movl 0x58(%rax), %ecx
movl 0x5c(%rax), %edx
cmpl %ecx, %edx
jl 0x93b05
movq 0x90(%rax), %rsi
movl 0xe4(%rsi), %edi
cmpl 0xe0(%rsi), %edi
jge 0x93c09
movq 0x90(%rax), %rax
testq %rax, %rax
je 0x93b1f
movl 0xe4(%rax), %esi
cmpl 0xe0(%rax), %esi
jl 0x93b35
movb $0x1, %r15b
cmpl %ecx, %edx
jge 0x93c00
movq %rbx, %rdi
movq %rsp, %rsi
callq 0x16570
movq (%rsp), %rcx
movq %rax, 0x90(%rcx)
movq 0x30(%rax), %rcx
movslq 0xe4(%rax), %r8
leaq (%r8,%r8,8), %rdx
shlq $0x4, %rdx
movl 0x8(%rcx,%rdx), %esi
cmpl %esi, %ebp
movl $0xfffffffe, %edi       # imm = 0xFFFFFFFE
cmovel %ebp, %edi
cmpl $-0x3, %ebp
movl %edi, %ebp
cmovel %esi, %ebp
cmpl %esi, %r14d
jne 0x93bcb
addq %rdx, %rcx
cmpq %r13, 0x78(%rcx)
jl 0x93be5
movb $0x1, %r15b
movq 0x30(%rsp), %rdx
cmpq %rdx, 0x18(%rcx)
jg 0x93c00
movl %r12d, %r13d
movq 0x18(%rbx), %rdi
leal 0x1(%r8), %edx
movl %edx, 0xe4(%rax)
movq %rbx, %rsi
movq %rax, %rdx
leaq 0x8(%rsp), %r9
callq 0x14140
movl %eax, %r12d
movq 0x8(%rsp), %rsi
movq 0x28(%rsp), %rdi
callq 0x152d0
testl %eax, %eax
js 0x93bf4
shrl $0x1f, %r12d
orl %r12d, %r13d
movl %r13d, %r12d
xorl %r15d, %r15d
jmp 0x93bfb
cmpl $-0x2, %r14d
jne 0x93bd7
testl %esi, %esi
jns 0x93be5
jmp 0x93b6e
cmpl $-0x1, %r14d
je 0x93be5
movb $0x1, %r15b
cmpl %esi, %r14d
jle 0x93c00
leal 0x1(%r8), %ecx
movl %ecx, 0xe4(%rax)
xorl %r15d, %r15d
jmp 0x93c00
movl %r13d, %r12d
orl $0x1, %r12d
movq 0x18(%rsp), %r13
testb %r15b, %r15b
je 0x93ade
movq 0x8(%rsp), %rdi
callq 0x14db0
movq 0x20(%rsp), %rax
testq %rax, %rax
je 0x93c1f
movl %ebp, (%rax)
movl %r14d, 0x258(%rbx)
movl 0x14(%rsp), %eax
movl %eax, 0x25c(%rbx)
movq %r13, 0x260(%rbx)
movq 0x30(%rsp), %rax
movq %rax, 0x268(%rbx)
xorps %xmm0, %xmm0
movq 0x38(%rsp), %rax
movups %xmm0, (%rax)
movq 0x28(%rsp), %rdi
callq 0x153f0
movl %eax, %ebp
orl %r12d, %ebp
movq 0x40(%rsp), %rdi
callq 0x15090
negl %ebp
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq