// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movl $0xfffffffd, -0x30(%rbp) # imm = 0xFFFFFFFD
cmpq $0x0, -0x28(%rbp)
je 0xc472d
movq -0x20(%rbp), %rax
movl 0x4(%rax), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movq -0x10(%rbp), %rax
cmpl $-0x2, 0x258(%rax)
jne 0xc4744
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0x7c(%rbp)
jmp 0xc4751
movq -0x10(%rbp), %rax
movl 0x258(%rax), %eax
movl %eax, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jne 0xc4787
movq -0x10(%rbp), %rax
movq 0x260(%rax), %rax
movq -0x20(%rbp), %rcx
movq 0x8(%rcx), %rcx
movq -0x20(%rbp), %rdx
addq 0x10(%rdx), %rcx
subq $0x1, %rcx
cmpq %rcx, %rax
jle 0xc47a8
movq -0x20(%rbp), %rax
cmpl $-0x2, 0x4(%rax)
je 0xc47a6
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0xc4bf0
movl %eax, -0x4(%rbp)
jmp 0xc4bd9
jmp 0xc47a8
movq -0x10(%rbp), %rdi
callq 0x149c0
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0xc47c8
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xc4bd9
movq -0x10(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x152b0
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x20(%rbp), %rax
cmpl $-0x2, 0x4(%rax)
jne 0xc48d5
movq -0x20(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x388(%rax), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0xc48d3
movq -0x48(%rbp), %rax
cmpl $0x3, (%rax)
jne 0xc48d3
movq -0x48(%rbp), %rax
cmpl $0x1, 0x60(%rax)
jne 0xc48d3
movslq -0x34(%rbp), %rax
movq -0x48(%rbp), %rcx
movq 0x68(%rcx), %rcx
cmpq (%rcx), %rax
jne 0xc48d3
movq -0x10(%rbp), %rax
cmpq $0x1, 0x260(%rax)
jg 0xc48d1
movq -0x10(%rbp), %rax
movabsq $0x7fffffff00000000, %rcx # imm = 0x7FFFFFFF00000000
cmpq %rcx, 0x268(%rax)
jb 0xc48d1
cmpq $0x0, -0x28(%rbp)
je 0xc487d
movl -0x34(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x14170
cmpl $0x0, %eax
setl %al
andb $0x1, %al
movzbl %al, %eax
orl -0x2c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movq -0x18(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x15900
orl -0x2c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rax
movl 0x30(%rax), %edx
callq 0x14a40
xorl %ecx, %ecx
subl -0x2c(%rbp), %ecx
orl %ecx, %eax
movl %eax, -0x4(%rbp)
jmp 0xc4bd9
jmp 0xc48d3
jmp 0xc48d5
movq -0x10(%rbp), %rax
movq 0x258(%rax), %rcx
movq %rcx, -0x60(%rbp)
movq 0x260(%rax), %rcx
movq %rcx, -0x58(%rbp)
movq 0x268(%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
movq %rcx, 0x260(%rax)
movq -0x10(%rbp), %rax
movabsq $0x7fffffffffffffff, %rcx # imm = 0x7FFFFFFFFFFFFFFF
movq %rcx, 0x268(%rax)
callq 0x14c40
movq %rax, -0x68(%rbp)
movq -0x20(%rbp), %rax
movl 0x5c(%rax), %ecx
movq -0x20(%rbp), %rdx
movb $0x1, %al
cmpl 0x58(%rdx), %ecx
movb %al, -0x7d(%rbp)
jl 0xc496a
movq -0x20(%rbp), %rax
movq 0x90(%rax), %rax
movl 0xe4(%rax), %eax
movq -0x20(%rbp), %rcx
movq 0x90(%rcx), %rcx
cmpl 0xe0(%rcx), %eax
setl %al
movb %al, -0x7d(%rbp)
movb -0x7d(%rbp), %al
testb $0x1, %al
jne 0xc4976
jmp 0xc4b63
movq -0x20(%rbp), %rax
cmpq $0x0, 0x90(%rax)
je 0xc49b9
movq -0x20(%rbp), %rax
movq 0x90(%rax), %rax
movl 0xe4(%rax), %eax
movq -0x20(%rbp), %rcx
movq 0x90(%rcx), %rcx
cmpl 0xe0(%rcx), %eax
jge 0xc49b9
movq -0x20(%rbp), %rax
movq 0x90(%rax), %rax
movq %rax, -0x70(%rbp)
jmp 0xc49e3
movq -0x20(%rbp), %rax
movl 0x5c(%rax), %eax
movq -0x20(%rbp), %rcx
cmpl 0x58(%rcx), %eax
jge 0xc49dc
movq -0x10(%rbp), %rdi
leaq -0x20(%rbp), %rsi
callq 0x16750
movq %rax, -0x70(%rbp)
jmp 0xc49e1
jmp 0xc4b63
jmp 0xc49e3
movq -0x70(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x90(%rax)
movq -0x20(%rbp), %rax
movq 0x90(%rax), %rax
movq 0x30(%rax), %rax
movq -0x20(%rbp), %rcx
movq 0x90(%rcx), %rcx
movslq 0xe4(%rcx), %rcx
imulq $0x90, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
cmpl $-0x3, -0x30(%rbp)
jne 0xc4a33
movq -0x78(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x30(%rbp)
jmp 0xc4a48
movl -0x30(%rbp), %eax
movq -0x78(%rbp), %rcx
cmpl 0x8(%rcx), %eax
je 0xc4a46
movl $0xfffffffe, -0x30(%rbp) # imm = 0xFFFFFFFE
jmp 0xc4a48
movl -0x60(%rbp), %eax
movq -0x78(%rbp), %rcx
cmpl 0x8(%rcx), %eax
je 0xc4abd
cmpl $-0x2, -0x60(%rbp)
jne 0xc4a85
movq -0x78(%rbp), %rax
cmpl $-0x1, 0x8(%rax)
jle 0xc4a83
movq -0x20(%rbp), %rax
movq 0x90(%rax), %rax
movl 0xe4(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0xe4(%rax)
jmp 0xc492d
jmp 0xc4abb
movl -0x60(%rbp), %eax
movq -0x78(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jg 0xc4a97
cmpl $-0x1, -0x60(%rbp)
jne 0xc4ab6
movq -0x20(%rbp), %rax
movq 0x90(%rax), %rax
movl 0xe4(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0xe4(%rax)
jmp 0xc492d
jmp 0xc4b63
jmp 0xc4abd
movq -0x78(%rbp), %rax
movq 0x78(%rax), %rax
cmpq -0x58(%rbp), %rax
jge 0xc4aea
movq -0x20(%rbp), %rax
movq 0x90(%rax), %rax
movl 0xe4(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0xe4(%rax)
jmp 0xc492d
movq -0x78(%rbp), %rax
movq 0x18(%rax), %rax
cmpq -0x50(%rbp), %rax
jle 0xc4afa
jmp 0xc4b63
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x10(%rbp), %rsi
movq -0x70(%rbp), %rdx
movq -0x78(%rbp), %rcx
movq -0x70(%rbp), %rax
movl 0xe4(%rax), %r8d
movl %r8d, %r9d
addl $0x1, %r9d
movl %r9d, 0xe4(%rax)
leaq -0x68(%rbp), %r9
callq 0x14190
cmpl $0x0, %eax
setl %al
andb $0x1, %al
movzbl %al, %eax
orl -0x2c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movq -0x18(%rbp), %rdi
movq -0x68(%rbp), %rsi
callq 0x15430
cmpl $0x0, %eax
jge 0xc4b5e
movl -0x2c(%rbp), %eax
orl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0xc4b63
jmp 0xc492d
movq -0x68(%rbp), %rdi
callq 0x14ec0
cmpq $0x0, -0x28(%rbp)
je 0xc4b7c
movl -0x30(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movq -0x10(%rbp), %rax
movq -0x60(%rbp), %rcx
movq %rcx, 0x258(%rax)
movq -0x58(%rbp), %rcx
movq %rcx, 0x260(%rax)
movq -0x50(%rbp), %rcx
movq %rcx, 0x268(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x38(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x40(%rax)
movq -0x18(%rbp), %rdi
callq 0x15550
orl -0x2c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movq -0x40(%rbp), %rdi
callq 0x151e0
xorl %eax, %eax
subl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)