// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
leaq -0x58(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x4250
movq $-0x9, -0x60(%rbp)
movq $-0x9, -0x68(%rbp)
movq -0x10(%rbp), %rdi
movl $0x12, %esi
movl $0x2c, %edx
movb $0x0, %al
callq 0x79f60
cmpq $0x0, -0x20(%rbp)
jne 0x6bd85
movq -0x18(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x6bfe0
leaq 0x84e11(%rip), %rdi     # 0xf0b85
leaq -0x58(%rbp), %rsi
callq 0x6bfe0
movq -0x48(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
leaq 0x819cc(%rip), %rsi     # 0xed75c
callq 0xc090
movq %rax, -0x40(%rbp)
cmpq $0x0, %rax
jne 0x6bdbd
movq -0x20(%rbp), %rdi
callq 0x43c0
movq -0x48(%rbp), %rdi
callq 0x45a0
movl $0xfffffffc, -0x4(%rbp) # imm = 0xFFFFFFFC
jmp 0x6bfd6
movq -0x48(%rbp), %rdi
callq 0x45a0
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x6c040
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rdi
callq 0x75260
movq %rax, -0x28(%rbp)
cmpq $0x0, %rax
je 0x6bf9f
movq -0x10(%rbp), %rax
cmpl $0x0, 0x30(%rax)
je 0x6be0f
leaq 0x88b84(%rip), %rdi     # 0xf4982
callq 0x43c0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6bfd6
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x6c040
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rdi
callq 0x6eec0
movq -0x28(%rbp), %rcx
movq %rax, 0x50(%rcx)
cmpq $0x0, %rax
jne 0x6be42
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6bfd6
movq -0x28(%rbp), %rax
movq 0x50(%rax), %rax
cmpl $0x1, 0x8(%rax)
jne 0x6be52
jmp 0x6be71
leaq 0x88b3d(%rip), %rdi     # 0xf4996
leaq 0x88b6c(%rip), %rsi     # 0xf49cc
movl $0x32e, %edx            # imm = 0x32E
leaq 0x88b72(%rip), %rcx     # 0xf49de
callq 0x42e0
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x50(%rax), %rsi
callq 0x4f0d0
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x28(%rbp), %rax
cmpq $0x0, 0x48(%rax)
jne 0x6bea4
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6bfd6
movq -0x28(%rbp), %rax
movslq 0x4(%rax), %rax
cmpq -0x60(%rbp), %rax
jne 0x6bee6
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
cmpq -0x68(%rbp), %rax
jge 0x6bee6
movl $0x1, %edi
leaq 0x88b4e(%rip), %rsi     # 0xf4a1a
leaq 0x88b58(%rip), %rdx     # 0xf4a2b
movb $0x0, %al
callq 0x1b030
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x6bfd6
movq -0x28(%rbp), %rax
movslq 0x4(%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x68(%rbp)
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x30(%rbp), %rcx
callq 0x6c070
cmpl $0x0, %eax
jge 0x6bf2d
movq -0x40(%rbp), %rdi
callq 0x103d0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6bfd6
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x6c040
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq -0x38(%rbp), %rcx
movq -0x28(%rbp), %rdx
movslq (%rdx), %rdx
addq %rdx, %rcx
cmpq %rcx, %rax
je 0x6bf89
movq -0x28(%rbp), %rax
movl (%rax), %ecx
movq -0x30(%rbp), %r8
movq -0x70(%rbp), %r9
subq -0x38(%rbp), %r9
movl $0x1, %edi
leaq 0x88aa8(%rip), %rsi     # 0xf4a1a
leaq 0x88ae3(%rip), %rdx     # 0xf4a5c
movb $0x0, %al
callq 0x1b030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6bfd6
movq -0x70(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x28(%rbp), %rdi
callq 0x74a70
jmp 0x6bdd6
movq -0x10(%rbp), %rax
cmpl $0x0, 0x30(%rax)
je 0x6bfbb
movq -0x40(%rbp), %rdi
callq 0x103d0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6bfd6
movq -0x40(%rbp), %rdi
callq 0x103d0
movl %eax, %edx
movl $0xfffffffc, %eax       # imm = 0xFFFFFFFC
xorl %ecx, %ecx
cmpl $0x0, %edx
cmovgel %ecx, %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nop