// File: /workspace/github_c_makefile_star2_O1/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 $0x38, %rsp
movq %rdx, %r13
movq %rsi, %r12
movq %rdi, 0x10(%rsp)
xorl %r15d, %r15d
movl $0x12, %esi
movl $0x2c, %edx
xorl %eax, %eax
callq 0x55e0e
testq %r13, %r13
jne 0x4cd86
testq %r12, %r12
je 0x4cd03
movq %r12, %rdi
callq 0x41d0
movq %rax, %r14
addq $0x2, %rax
je 0x4cd10
movq %rax, %rcx
shrq $0x3e, %rcx
movq %rax, %r13
shrq %r13
xorl %r15d, %r15d
testq %rcx, %rcx
cmovneq %r15, %r13
addq %rax, %r13
movq %r13, %rdi
callq 0x4680
testq %rax, %rax
je 0x4d032
movq %rax, %rbp
movq %rax, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x43c0
movb $0x0, (%rbp,%r14)
movq %rbp, %r15
jmp 0x4cd24
callq 0x4050
movl $0xe, (%rax)
jmp 0x4cd1b
callq 0x4050
movl $0x4b, (%rax)
xorl %r14d, %r14d
xorl %r13d, %r13d
xorl %r15d, %r15d
movq %r14, %rax
subq $-0x7, %rax
jae 0x4cd5d
cmpq %rax, %r13
jae 0x4cd6d
movq %rax, %rcx
shrq $0x3e, %rcx
movq %rax, %rdx
shrq %rdx
xorl %esi, %esi
testq %rcx, %rcx
cmoveq %rdx, %rsi
addq %rax, %rsi
movq %r15, %rdi
callq 0x4840
movq %r15, %r13
testq %rax, %rax
jne 0x4cd70
jmp 0x4cd86
callq 0x4050
movl $0x4b, (%rax)
movq %r15, %r13
jmp 0x4cd86
movq %r15, %rax
movl $0x6172632e, (%rax,%r14) # imm = 0x6172632E
movw $0x69, 0x4(%rax,%r14)
movq %rax, %r15
movq %rax, %r13
leaq 0x497a6(%rip), %rsi     # 0x96533
movq %r13, %rdi
callq 0xa4a5
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0x4d018
movq %r15, %rdi
callq 0x45c0
movq 0x10(%rsp), %r14
movq (%r14), %rax
movq 0x28(%rax), %rcx
addq 0x8(%rax), %rcx
subq (%rax), %rcx
movq %rcx, 0x18(%rsp)
movq $-0x9, %rbx
movq $-0x9, 0x28(%rsp)
movq %r14, %rdi
callq 0x52e66
testq %rax, %rax
je 0x4cfc2
cmpl $0x0, 0x30(%r14)
jne 0x4d03d
movq %rax, %r12
movq (%r14), %rax
movq 0x28(%rax), %r15
addq 0x8(%rax), %r15
subq (%rax), %r15
movq %r14, %rdi
callq 0x4ee88
movq %rax, 0x50(%r12)
testq %rax, %rax
je 0x4d049
cmpl $0x1, 0x8(%rax)
jne 0x4d064
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
movq %r14, %rdi
movq %rax, %rsi
callq 0x387d9
movq %rax, 0x48(%r12)
testq %rax, %rax
je 0x4d051
movslq 0x4(%r12), %rcx
cmpq %rcx, %rbx
jne 0x4ce5b
movq 0x28(%rsp), %rax
cmpq %rax, 0x8(%r12)
jl 0x4cff4
movq %rcx, 0x30(%rsp)
movq 0x8(%r12), %rax
movq %rax, 0x28(%rsp)
cmpl $0x0, 0x30(%r12)
jle 0x4cf5b
xorl %r13d, %r13d
movq (%r14), %rax
movq 0x28(%rax), %rbp
addq 0x8(%rax), %rbp
subq (%rax), %rbp
movq 0x40(%r12), %rax
addq 0x18(%rsp), %rax
movq %rbp, %rcx
subq %rax, %rcx
movq 0x38(%r12), %rax
movslq (%rax,%r13,4), %r9
cmpq %r9, %rcx
jne 0x4cf00
movq %r14, %rdi
callq 0x5400a
testq %rax, %rax
je 0x4cf27
movq %r14, %rdi
movq %rax, %r14
movq (%rdi), %rax
movq 0x28(%rax), %rcx
addq 0x8(%rax), %rcx
addq (%rax), %rbp
subq %rbp, %rcx
movq 0x38(%r12), %rax
movslq (%rax,%r13,4), %r9
movq %rcx, (%rsp)
movq %r12, %rsi
movq %r14, %rdx
movq 0x20(%rsp), %rcx
movq 0x18(%rsp), %r8
callq 0x4c9c6
movl %eax, %ebp
movq %r14, %rdi
callq 0x52c5d
testl %ebp, %ebp
setns %r14b
cmovsl %ebp, %ebx
jmp 0x4cf2f
xorl %r14d, %r14d
movl $0x1, %edi
leaq 0x502a8(%rip), %rsi     # 0x9d1b7
leaq 0x502b6(%rip), %rdx     # 0x9d1cc
movl %r13d, %r8d
xorl %eax, %eax
callq 0x14378
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x4cf2f
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
xorl %r14d, %r14d
testb %r14b, %r14b
je 0x4cf4e
incq %r13
movslq 0x30(%r12), %rax
cmpq %rax, %r13
movq 0x10(%rsp), %r14
jl 0x4ce79
xorl %ebx, %ebx
jmp 0x4cf53
movq 0x10(%rsp), %r14
testl %ebx, %ebx
js 0x4cfe8
movq (%r14), %rax
movq 0x28(%rax), %r14
addq 0x8(%rax), %r14
subq (%rax), %r14
movslq (%r12), %rcx
leaq (%r15,%rcx), %r13
cmpq %r13, %r14
jne 0x4cf85
movq %r12, %rdi
callq 0x5295e
movq %r14, 0x18(%rsp)
jmp 0x4cfaa
movq %r14, %r9
subq %r15, %r9
movl $0x1, %edi
leaq 0x50173(%rip), %rsi     # 0x9d10a
leaq 0x501ae(%rip), %rdx     # 0x9d14c
movq 0x18(%rsp), %r8
xorl %eax, %eax
callq 0x14378
movq 0x30(%rsp), %rbx
cmpq %r13, %r14
movq 0x10(%rsp), %r14
je 0x4cdd3
jmp 0x4d051
movl 0x30(%r14), %ebx
movq 0x20(%rsp), %rdi
callq 0xd4a4
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
testl %ebx, %ebx
jne 0x4d051
sarl $0x1f, %eax
andl $-0x4, %eax
movl %eax, 0xc(%rsp)
jmp 0x4d051
movq 0x20(%rsp), %rdi
callq 0xd4a4
jmp 0x4d051
leaq 0x5010f(%rip), %rsi     # 0x9d10a
leaq 0x50119(%rip), %rdx     # 0x9d11b
movl $0x1, %edi
xorl %eax, %eax
callq 0x14378
movl $0xfffffffe, 0xc(%rsp)  # imm = 0xFFFFFFFE
jmp 0x4d051
movq %r13, %rdi
callq 0x43d0
movq %r15, %rdi
callq 0x45c0
movl $0xfffffffc, 0xc(%rsp)  # imm = 0xFFFFFFFC
jmp 0x4d051
xorl %r14d, %r14d
xorl %r13d, %r13d
jmp 0x4cd24
leaq 0x5002e(%rip), %rdi     # 0x9d072
callq 0x43d0
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
movl 0xc(%rsp), %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5001b(%rip), %rdi     # 0x9d086
leaq 0x5004a(%rip), %rsi     # 0x9d0bc
leaq 0x50055(%rip), %rcx     # 0x9d0ce
movl $0x32e, %edx            # imm = 0x32E
callq 0x42f0