// File: /workspace/github_c_makefile_star2_O3/samtools[P]htslib/cram/cram_index.c
// Function: cram_index_build
int cram_index_build(cram_fd *fd, const char *fn_base, const char *fn_idx) {
    cram_container *c;
    off_t cpos, hpos;
    BGZF *fp;
    kstring_t fn_idx_str = {0};
    int64_t last_ref = -9, last_start = -9;

    // Useful for cram_index_build_multiref
    cram_set_option(fd, CRAM_OPT_REQUIRED_FIELDS, SAM_RNAME | SAM_POS | SAM_CIGAR);

    if (! fn_idx) {
        kputs(fn_base, &fn_idx_str);
        kputs(".crai", &fn_idx_str);
        fn_idx = fn_idx_str.s;
    }

    if (!(fp = bgzf_open(fn_idx, "wg"))) {
        perror(fn_idx);
        free(fn_idx_str.s);
        return -4;
    }

    free(fn_idx_str.s);

    cpos = htell(fd->fp);
    while ((c = cram_read_container(fd))) {
        if (fd->err) {
            perror("Cram container read");
            return -1;
        }

        hpos = htell(fd->fp);

        if (!(c->comp_hdr_block = cram_read_block(fd)))
            return -1;
        assert(c->comp_hdr_block->content_type == COMPRESSION_HEADER);

        c->comp_hdr = cram_decode_compression_header(fd, c->comp_hdr_block);
        if (!c->comp_hdr)
            return -1;

        if (c->ref_seq_id == last_ref && c->ref_seq_start < last_start) {
            hts_log_error("CRAM file is not sorted by chromosome / position");
            return -2;
        }
        last_ref = c->ref_seq_id;
        last_start = c->ref_seq_start;

        if (cram_index_container(fd, c, fp, cpos) < 0) {
            bgzf_close(fp);
            return -1;
        }

        off_t next_cpos = htell(fd->fp);
        if (next_cpos != hpos + c->length) {
            hts_log_error("Length %"PRId32" in container header at offset %lld does not match block lengths (%lld)",
                          c->length, (long long) cpos, (long long) next_cpos - hpos);
            return -1;
        }
        cpos = next_cpos;

        cram_free_container(c);
    }
    if (fd->err) {
        bgzf_close(fp);
        return -1;
    }

    return (bgzf_close(fp) >= 0)? 0 : -4;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, %r13
movq %rsi, %r12
movq %rdi, %rbx
xorl %r15d, %r15d
movl $0x12, %esi
movl $0x2c, %edx
xorl %eax, %eax
callq 0x5348c
testq %r13, %r13
movq %rbx, 0x8(%rsp)
jne 0x4a560
testq %r12, %r12
je 0x4a50d
movq %r12, %rdi
callq 0x41d0
movq %rax, %r14
addq $0x2, %rax
je 0x4a514
movabsq $0x3ffffffffffffff8, %rbx # imm = 0x3FFFFFFFFFFFFFF8
leaq 0x7(%rbx), %rcx
movq %rax, %r13
shrq %r13
xorl %ebp, %ebp
cmpq %rcx, %rax
cmovaq %rbp, %r13
addq %rax, %r13
movq %r13, %rdi
callq 0x4680
testq %rax, %rax
je 0x4a7b0
movq %rax, %r15
movq %rbx, %rbp
movq %rax, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x43c0
movb $0x0, (%r15,%r14)
movq %r14, %rsi
subq $-0x7, %rsi
movq 0x8(%rsp), %rbx
jae 0x4a7c2
cmpq %rsi, %r13
jae 0x4a7f7
movq %rsi, %rcx
shrq %rcx
xorl %eax, %eax
cmpq %rbp, %r14
cmovbeq %rcx, %rax
movq %r14, %rbp
jmp 0x4a52f
movl $0xe, %ebp
jmp 0x4a519
movl $0x4b, %ebp
callq 0x4050
movl %ebp, (%rax)
movl $0x3, %eax
movl $0x7, %esi
xorl %r15d, %r15d
xorl %ebp, %ebp
addq %rax, %rsi
movq %r15, %rdi
callq 0x4840
movq %rbp, %r14
movq %r15, %r13
testq %rax, %rax
movq 0x8(%rsp), %rbx
je 0x4a560
movl $0x6172632e, (%rax,%r14) # imm = 0x6172632E
movw $0x69, 0x4(%rax,%r14)
movq %rax, %r15
movq %rax, %r13
leaq 0x48fcc(%rip), %rsi     # 0x93533
movq %r13, %rdi
callq 0xa550
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x4a798
movq %r15, %rdi
callq 0x45c0
movq (%rbx), %rax
movq 0x28(%rax), %r13
movq (%rax), %r14
movq 0x8(%rax), %r15
movq %rbx, %rdi
callq 0x5032e
testq %rax, %rax
movq %rbx, %rdi
je 0x4a737
movq %rax, %r12
addq %r15, %r13
subq %r14, %r13
movq $-0x9, %r14
movq $-0x9, %rbp
cmpl $0x0, 0x30(%rdi)
jne 0x4a827
movq %r13, %rbx
movq (%rdi), %rax
movq 0x28(%rax), %r15
addq 0x8(%rax), %r15
subq (%rax), %r15
callq 0x4c6b7
movq %rax, 0x50(%r12)
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x4a786
cmpl $0x1, 0x8(%rax)
jne 0x4a838
movq 0x8(%rsp), %rdi
movq %rax, %rsi
callq 0x373d4
movq %rax, 0x48(%r12)
testq %rax, %rax
je 0x4a786
movslq 0x4(%r12), %rax
movq 0x8(%r12), %rcx
cmpq %rax, %rbp
jne 0x4a62f
cmpq %r14, %rcx
jl 0x4a7d5
movq %rcx, 0x18(%rsp)
movq %rax, 0x20(%rsp)
cmpl $0x0, 0x30(%r12)
jle 0x4a6e9
xorl %r13d, %r13d
movq 0x8(%rsp), %rbp
movq (%rbp), %rax
movq 0x28(%rax), %r14
addq 0x8(%rax), %r14
subq (%rax), %r14
movq 0x40(%r12), %rax
addq %rbx, %rax
movq %r14, %rcx
subq %rax, %rcx
movq 0x38(%r12), %rax
movslq (%rax,%r13,4), %r9
cmpq %r9, %rcx
jne 0x4a759
movq %rbp, %rdi
callq 0x514d9
testq %rax, %rax
je 0x4a776
movq %rbp, %rdi
movq %rax, %rbp
movq (%rdi), %rax
movq 0x28(%rax), %rcx
addq 0x8(%rax), %rcx
addq (%rax), %r14
subq %r14, %rcx
movq 0x38(%r12), %rax
movslq (%rax,%r13,4), %r9
movq %rcx, (%rsp)
movq %r12, %rsi
movq %rbp, %rdx
movq 0x10(%rsp), %rcx
movq %rbx, %r8
callq 0x4a1b0
movl %eax, %r14d
movq %rbp, %rdi
callq 0x50127
testl %r14d, %r14d
js 0x4a776
incq %r13
movslq 0x30(%r12), %rax
cmpq %rax, %r13
jl 0x4a648
movq 0x8(%rsp), %rbp
movq (%rbp), %rax
movq 0x28(%rax), %r13
addq 0x8(%rax), %r13
subq (%rax), %r13
movslq (%r12), %rcx
leaq (%r15,%rcx), %rax
cmpq %rax, %r13
jne 0x4a7ff
movq %r12, %rdi
callq 0x4fe2e
movq %rbp, %rdi
callq 0x5032e
movq %rax, %r12
movq 0x18(%rsp), %r14
movq %rbp, %rdi
movq 0x20(%rsp), %rbp
testq %rax, %rax
jne 0x4a5be
movl 0x30(%rdi), %ebx
movq 0x10(%rsp), %rdi
callq 0xd3cd
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
testl %ebx, %ebx
jne 0x4a786
sarl $0x1f, %eax
andl $-0x4, %eax
movl %eax, %r13d
jmp 0x4a786
leaq 0x4fab7(%rip), %rsi     # 0x9a217
leaq 0x4fac5(%rip), %rdx     # 0x9a22c
movl $0x1, %edi
movl %r13d, %r8d
xorl %eax, %eax
callq 0x13de5
movq 0x10(%rsp), %rdi
callq 0xd3cd
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movl %r13d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r13, %rdi
callq 0x43d0
movq %r15, %rdi
callq 0x45c0
movl $0xfffffffc, %r13d      # imm = 0xFFFFFFFC
jmp 0x4a786
movl $0x3, %eax
movl $0x7, %esi
xorl %r15d, %r15d
jmp 0x4a52f
callq 0x4050
movl $0x4b, (%rax)
movq %r15, %r13
jmp 0x4a560
leaq 0x4f98e(%rip), %rsi     # 0x9a16a
leaq 0x4f998(%rip), %rdx     # 0x9a17b
movl $0x1, %edi
xorl %eax, %eax
callq 0x13de5
movl $0xfffffffe, %r13d      # imm = 0xFFFFFFFE
jmp 0x4a786
movq %r15, %rax
jmp 0x4a54a
subq %r15, %r13
leaq 0x4f961(%rip), %rsi     # 0x9a16a
leaq 0x4f99c(%rip), %rdx     # 0x9a1ac
movl $0x1, %edi
movq %rbx, %r8
movq %r13, %r9
xorl %eax, %eax
callq 0x13de5
jmp 0x4a780
leaq 0x4f8a4(%rip), %rdi     # 0x9a0d2
callq 0x43d0
jmp 0x4a780
leaq 0x4f8a7(%rip), %rdi     # 0x9a0e6
leaq 0x4f8d6(%rip), %rsi     # 0x9a11c
leaq 0x4f8e1(%rip), %rcx     # 0x9a12e
movl $0x32e, %edx            # imm = 0x32E
callq 0x42f0