// File: /workspace/github_c_makefile_star2_O3/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, %r14
movq %rsi, %r15
movq %rdi, %rbx
movq %rdx, 0x18(%rsp)
movl 0x4(%rdx), %eax
testq %rcx, %rcx
je 0x90a0c
movl %eax, (%r12)
movl 0x258(%rbx), %ecx
cmpl $-0x2, %ecx
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmovnel %ecx, %ebp
cmpl %eax, %ebp
jne 0x90ad1
movq 0x10(%r14), %rax
addq 0x8(%r14), %rax
cmpq %rax, 0x260(%rbx)
jl 0x90ada
movq %rbx, %rdi
callq 0x14900
testq %rax, %rax
je 0x90d79
movq %rax, %rdi
callq 0x15090
cmpl $0x0, 0x30(%r14)
jle 0x90d8f
xorl %r13d, %r13d
movq %rbx, %rdi
callq 0x14900
testq %rax, %rax
je 0x90d79
movq %rax, %r12
movq %rbx, %rdi
movq %rax, %rsi
callq 0x16260
movq %rax, %r15
movq %r12, %rdi
callq 0x15090
testq %r15, %r15
je 0x90d79
movl 0x28(%r15), %ebp
testl %ebp, %ebp
jle 0x90ab9
movq %rbx, %rdi
callq 0x14900
testq %rax, %rax
je 0x90d71
movq %rax, %rdi
callq 0x15090
decl %ebp
jne 0x90a9c
movq %r15, %rdi
callq 0x158a0
incl %r13d
xorl %ebp, %ebp
cmpl 0x30(%r14), %r13d
jl 0x90a61
jmp 0x90d7e
cmpl $-0x2, %eax
jne 0x90a3a
movq %rbx, %rdi
callq 0x14900
testq %rax, %rax
je 0x90d79
movq %rax, %r13
movq %rbx, %rdi
movq %rax, %rsi
callq 0x15160
movq %rax, 0x48(%r14)
movq %r14, 0x38(%rbx)
cmpl $-0x2, 0x4(%r14)
jne 0x90b84
movq 0x388(%rax), %rax
testq %rax, %rax
je 0x90b84
cmpl $0x3, (%rax)
jne 0x90b84
cmpl $0x1, 0x60(%rax)
jne 0x90b84
movslq %ebp, %rcx
movq 0x68(%rax), %rax
cmpq %rcx, (%rax)
jne 0x90b84
cmpq $0x1, 0x260(%rbx)
jg 0x90b84
cmpl $0x7fffffff, 0x26c(%rbx) # imm = 0x7FFFFFFF
jb 0x90b84
testq %r12, %r12
je 0x90b4a
movl %ebp, (%r12)
movq %r15, %rdi
movq %r14, %rsi
callq 0x14120
movl %eax, %r12d
shrl $0x1f, %r12d
movq %r15, %rdi
movq %r13, %rsi
callq 0x15790
movl %eax, %ebp
orl %r12d, %ebp
movl 0x30(%r14), %edx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x14970
negl %ebp
orl %eax, %ebp
jmp 0x90d7e
movq %r13, 0x38(%rsp)
movq %r15, 0x20(%rsp)
leaq 0x38(%rbx), %rax
movq %rax, 0x30(%rsp)
movq 0x258(%rbx), %rbp
movq 0x260(%rbx), %r15
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 0x14b60
leaq 0x10(%rsp), %rcx
movq %rax, (%rcx)
movl $0x0, 0xc(%rsp)
movl $0xfffffffd, %r13d      # imm = 0xFFFFFFFD
movl 0x5c(%r14), %ecx
movq 0x90(%r14), %rax
cmpl 0x58(%r14), %ecx
jge 0x90c34
testq %rax, %rax
je 0x90c19
movl 0xe4(%rax), %r8d
cmpl 0xe0(%rax), %r8d
jl 0x90c5a
movq %rbx, %rdi
leaq 0x18(%rsp), %rsi
callq 0x16560
movq 0x18(%rsp), %r14
movl 0xe4(%rax), %r8d
jmp 0x90c5a
movl 0xe4(%rax), %ecx
cmpl 0xe0(%rax), %ecx
jge 0x90d12
movl 0xe4(%rax), %r8d
cmpl 0xe0(%rax), %r8d
jge 0x90d12
movq %rax, 0x90(%r14)
movq 0x30(%rax), %rcx
movslq %r8d, %rdx
leaq (%rdx,%rdx,8), %rdx
shlq $0x4, %rdx
movl 0x8(%rcx,%rdx), %esi
movl %r13d, %edi
cmpl %esi, %r13d
je 0x90c81
movl $0xfffffffe, %edi       # imm = 0xFFFFFFFE
cmpl $-0x3, %r13d
movl %edi, %r13d
cmovel %esi, %r13d
cmpl %esi, %ebp
jne 0x90cef
addq %rdx, %rcx
cmpq %r15, 0x78(%rcx)
jl 0x90d03
movq 0x28(%rsp), %rdx
cmpq %rdx, 0x18(%rcx)
jg 0x90d12
movq 0x18(%rbx), %rdi
leal 0x1(%r8), %edx
movl %edx, 0xe4(%rax)
movq %rbx, %rsi
movq %rax, %rdx
leaq 0x10(%rsp), %r9
callq 0x14140
movl %eax, %r14d
movq 0x10(%rsp), %rsi
movq 0x20(%rsp), %rdi
callq 0x152d0
testl %eax, %eax
js 0x90d93
shrl $0x1f, %r14d
orl %r14d, 0xc(%rsp)
movq 0x18(%rsp), %r14
jmp 0x90bf3
cmpl $-0x2, %ebp
jne 0x90cfa
testl %esi, %esi
jns 0x90d03
jmp 0x90c90
cmpl $-0x1, %ebp
je 0x90d03
cmpl %esi, %ebp
jle 0x90d12
incl %r8d
movl %r8d, 0xe4(%rax)
jmp 0x90bf3
movl 0xc(%rsp), %r14d
movq 0x10(%rsp), %rdi
callq 0x14db0
testq %r12, %r12
je 0x90d2a
movl %r13d, (%r12)
movaps 0x40(%rsp), %xmm0
movlps %xmm0, 0x258(%rbx)
movq %r15, 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 0x153f0
movl %eax, %ebp
orl %r14d, %ebp
movq 0x38(%rsp), %rdi
callq 0x15090
negl %ebp
jmp 0x90d7e
movq %r15, %rdi
callq 0x158a0
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebp, %ebp
jmp 0x90d7e
movl $0x1, %r14d
jmp 0x90d17