// File: /workspace/github_c_makefile_star2_O2/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 $0x48, %rsp
movq %rdx, %r12
movq %rsi, %r15
movq %rdi, %r14
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
andq $0x0, 0x40(%rsp)
xorl %ebx, %ebx
pushq $0x12
popq %rsi
pushq $0x2c
popq %rdx
xorl %eax, %eax
callq 0x451f7
testq %r12, %r12
jne 0x3dc74
leaq 0x30(%rsp), %rbx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x3dedf
leaq 0x3acd9(%rip), %rdi     # 0x7893e
movq %rbx, %rsi
callq 0x3dedf
movq 0x10(%rbx), %rbx
movq %rbx, %r12
leaq 0x378b8(%rip), %rsi     # 0x75533
movq %r12, %rdi
callq 0x9596
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x3de3c
movq %rbx, %rdi
callq 0x45c0
movq (%r14), %rax
movq 0x28(%rax), %rcx
addq 0x8(%rax), %rcx
subq (%rax), %rcx
movq %rcx, 0x10(%rsp)
pushq $-0x9
popq %r15
movq %r15, %r12
movq %r14, %rdi
callq 0x427b3
movl 0x30(%r14), %ebx
testq %rax, %rax
je 0x3de50
testl %ebx, %ebx
jne 0x3deaf
movq %rax, %r13
movq (%r14), %rax
movq 0x28(%rax), %rbx
addq 0x8(%rax), %rbx
subq (%rax), %rbx
movq %r14, %rdi
callq 0x3f467
movq %rax, 0x50(%r13)
testq %rax, %rax
je 0x3de28
cmpl $0x1, 0x8(%rax)
jne 0x3dec0
movq %r14, %rdi
movq %rax, %rsi
callq 0x2cffe
movq %rax, 0x48(%r13)
testq %rax, %rax
je 0x3de28
movslq 0x4(%r13), %rax
movq 0x8(%r13), %rcx
cmpq %rax, %r15
jne 0x3dd2e
cmpq %r12, %rcx
jl 0x3de6b
movq %rcx, 0x20(%rsp)
movq %rax, 0x28(%rsp)
xorl %ebp, %ebp
movq (%r14), %rax
movq 0x28(%rax), %r12
addq 0x8(%rax), %r12
movslq 0x30(%r13), %rcx
subq (%rax), %r12
cmpq %rcx, %rbp
jge 0x3ddd6
movq 0x40(%r13), %rax
addq 0x10(%rsp), %rax
movq %r12, %rcx
subq %rax, %rcx
movq 0x38(%r13), %rax
movslq (%rax,%rbp,4), %r9
cmpq %r9, %rcx
jne 0x3de03
movq %r14, %rdi
callq 0x43759
testq %rax, %rax
je 0x3de1e
movq %rax, %r15
movq (%r14), %rax
movq 0x28(%rax), %rcx
addq 0x8(%rax), %rcx
addq (%rax), %r12
subq %r12, %rcx
movq 0x38(%r13), %rax
movslq (%rax,%rbp,4), %r9
movq %rcx, (%rsp)
movq %r14, %rdi
movq %r13, %rsi
movq %r15, %rdx
movq 0x18(%rsp), %rcx
movq 0x10(%rsp), %r8
callq 0x3d9b4
movl %eax, %r12d
movq %r15, %rdi
callq 0x42611
testl %r12d, %r12d
js 0x3de1e
incq %rbp
jmp 0x3dd3a
movslq (%r13), %rcx
leaq (%rbx,%rcx), %rax
cmpq %rax, %r12
jne 0x3de87
movq %r13, %rdi
callq 0x4237d
movq %r12, 0x10(%rsp)
movq 0x28(%rsp), %r15
movq 0x20(%rsp), %r12
jmp 0x3dcb3
leaq 0x3e385(%rip), %rsi     # 0x7c18f
leaq 0x3e393(%rip), %rdx     # 0x7c1a4
pushq $0x1
popq %rdi
movl %ebp, %r8d
xorl %eax, %eax
callq 0x110c6
movq 0x18(%rsp), %rdi
callq 0xbc96
pushq $-0x1
popq %rcx
movl %ecx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, %rdi
callq 0x43d0
movq %rbx, %rdi
callq 0x45c0
pushq $-0x4
jmp 0x3de2a
movq 0x18(%rsp), %rdi
callq 0xbc96
testl %ebx, %ebx
pushq $-0x1
popq %rcx
jne 0x3de2b
sarl $0x1f, %eax
andl $-0x4, %eax
movl %eax, %ecx
jmp 0x3de2b
leaq 0x3e270(%rip), %rsi     # 0x7c0e2
leaq 0x3e27a(%rip), %rdx     # 0x7c0f3
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x110c6
pushq $-0x2
jmp 0x3de2a
subq %rbx, %r12
leaq 0x3e251(%rip), %rsi     # 0x7c0e2
leaq 0x3e28c(%rip), %rdx     # 0x7c124
pushq $0x1
popq %rdi
movq 0x10(%rsp), %r8
movq %r12, %r9
xorl %eax, %eax
callq 0x110c6
jmp 0x3de28
leaq 0x3e194(%rip), %rdi     # 0x7c04a
callq 0x43d0
jmp 0x3de28
leaq 0x3e197(%rip), %rdi     # 0x7c05e
leaq 0x3e1c6(%rip), %rsi     # 0x7c094
leaq 0x3e1d1(%rip), %rcx     # 0x7c0a6
movl $0x32e, %edx            # imm = 0x32E
callq 0x42f0