// File: /workspace/github_c_makefile_star2_O2/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 $0x58, %rsp
movq %rcx, %r12
movq %rdx, %r15
movq %rsi, %r13
movq %rdi, %rbx
movq %rdx, 0x10(%rsp)
movl 0x4(%rdx), %eax
testq %rcx, %rcx
je 0x75f65
movl %eax, (%r12)
movl 0x258(%rbx), %r14d
cmpl $-0x2, %r14d
pushq $-0x1
popq %rbp
cmovel %ebp, %r14d
cmpl %eax, %r14d
jne 0x7602c
movq 0x10(%r15), %rax
addq 0x8(%r15), %rax
cmpq %rax, 0x260(%rbx)
jl 0x76035
movq %rbx, %rdi
callq 0x14930
testq %rax, %rax
je 0x762db
movq %rax, %rdi
callq 0x15100
xorl %r13d, %r13d
cmpl 0x30(%r15), %r13d
jge 0x762ec
movq %rbx, %rdi
callq 0x14930
testq %rax, %rax
je 0x762db
movq %rax, %r12
movq %rbx, %rdi
movq %rax, %rsi
callq 0x16320
movq %rax, %r14
movq %r12, %rdi
callq 0x15100
testq %r14, %r14
je 0x762db
movl 0x28(%r14), %r12d
testl %r12d, %r12d
movl $0x0, %eax
cmovlel %eax, %r12d
subl $0x1, %r12d
jb 0x7601f
movq %rbx, %rdi
callq 0x14930
testq %rax, %rax
je 0x762d3
movq %rax, %rdi
callq 0x15100
jmp 0x75ffe
movq %r14, %rdi
callq 0x15920
incl %r13d
jmp 0x75fb1
cmpl $-0x2, %eax
jne 0x75f95
movq %rbx, %rdi
callq 0x14930
testq %rax, %rax
je 0x762db
movq %rbx, %rdi
movq %rax, 0x18(%rsp)
movq %rax, %rsi
callq 0x151d0
movq %rax, 0x48(%r15)
movq %r15, 0x38(%rbx)
cmpl $-0x2, 0x4(%r15)
jne 0x760e3
movq 0x388(%rax), %rax
testq %rax, %rax
je 0x760e3
cmpl $0x3, (%rax)
jne 0x760e3
cmpl $0x1, 0x60(%rax)
jne 0x760e3
movslq %r14d, %rcx
movq 0x68(%rax), %rax
cmpq %rcx, (%rax)
jne 0x760e3
cmpq $0x1, 0x260(%rbx)
jg 0x760e3
cmpl $0x7fffffff, 0x26c(%rbx) # imm = 0x7FFFFFFF
jb 0x760e3
testq %r12, %r12
je 0x760a7
movl %r14d, (%r12)
movq %r13, %rdi
movq %r15, %rsi
callq 0x14130
movl %eax, %r12d
shrl $0x1f, %r12d
movq %r13, %rdi
movq 0x18(%rsp), %rsi
callq 0x15810
movl %eax, %ebp
orl %r12d, %ebp
movl 0x30(%r15), %edx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x149b0
negl %ebp
orl %eax, %ebp
jmp 0x762db
movq %r12, 0x38(%rsp)
movq %r13, 0x20(%rsp)
leaq 0x38(%rbx), %rax
movq %rax, 0x30(%rsp)
movq 0x258(%rbx), %rbp
movq 0x260(%rbx), %r14
movq %rbp, %xmm0
movdqa %xmm0, 0x40(%rsp)
movq 0x268(%rbx), %rax
movq %rax, 0x28(%rsp)
movabsq $0x7fffffffffffffff, %rax # imm = 0x7FFFFFFFFFFFFFFF
leaq 0x1(%rax), %rcx
movq %rcx, 0x260(%rbx)
movq %rax, 0x268(%rbx)
callq 0x14ba0
leaq 0x8(%rsp), %rcx
movq %rax, (%rcx)
movl $0x0, 0x4(%rsp)
pushq $-0x3
popq %r13
pushq $-0x2
popq %r12
movl 0x5c(%r15), %ecx
movq 0x90(%r15), %rax
cmpl 0x58(%r15), %ecx
jge 0x76195
testq %rax, %rax
je 0x7617a
movl 0xe4(%rax), %r8d
cmpl 0xe0(%rax), %r8d
jl 0x761bb
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
callq 0x16620
movq 0x10(%rsp), %r15
movl 0xe4(%rax), %r8d
jmp 0x761bb
movl 0xe4(%rax), %ecx
cmpl 0xe0(%rax), %ecx
jge 0x76270
movl 0xe4(%rax), %r8d
cmpl 0xe0(%rax), %r8d
jge 0x76270
movq %rax, 0x90(%r15)
movq 0x30(%rax), %rcx
movslq %r8d, %rdx
imulq $0x90, %rdx, %rdx
movl 0x8(%rcx,%rdx), %esi
movl %r13d, %edi
cmpl %esi, %r13d
je 0x761df
movl %r12d, %edi
cmpl $-0x3, %r13d
movl %edi, %r13d
cmovel %esi, %r13d
cmpl %esi, %ebp
jne 0x7624d
addq %rdx, %rcx
cmpq %r14, 0x78(%rcx)
jl 0x76261
movq 0x28(%rsp), %rdx
cmpq %rdx, 0x18(%rcx)
jg 0x76270
movq 0x18(%rbx), %rdi
leal 0x1(%r8), %edx
movl %edx, 0xe4(%rax)
movq %rbx, %rsi
movq %rax, %rdx
leaq 0x8(%rsp), %r9
callq 0x14150
movl %eax, %r15d
movq 0x8(%rsp), %rsi
movq 0x20(%rsp), %rdi
callq 0x15340
testl %eax, %eax
js 0x762f0
shrl $0x1f, %r15d
orl %r15d, 0x4(%rsp)
movq 0x10(%rsp), %r15
jmp 0x76154
cmpl $-0x2, %ebp
jne 0x76258
testl %esi, %esi
jns 0x76261
jmp 0x761ee
cmpl $-0x1, %ebp
je 0x76261
cmpl %esi, %ebp
jle 0x76270
incl %r8d
movl %r8d, 0xe4(%rax)
jmp 0x76154
movl 0x4(%rsp), %r15d
movq 0x8(%rsp), %rdi
callq 0x14e00
movq 0x38(%rsp), %rax
testq %rax, %rax
je 0x7628c
movl %r13d, (%rax)
movaps 0x40(%rsp), %xmm0
movlps %xmm0, 0x258(%rbx)
movq %r14, 0x260(%rbx)
movq 0x28(%rsp), %rax
movq %rax, 0x268(%rbx)
xorps %xmm0, %xmm0
movq 0x30(%rsp), %rax
movups %xmm0, (%rax)
movq 0x20(%rsp), %rdi
callq 0x15460
movl %eax, %ebp
orl %r15d, %ebp
movq 0x18(%rsp), %rdi
callq 0x15100
negl %ebp
jmp 0x762db
movq %r14, %rdi
callq 0x15920
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebp, %ebp
jmp 0x762db
pushq $0x1
popq %r15
jmp 0x76275