// File: /workspace/github_c_makefile_star2_O2/icebert[P]pblat/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, spos, hpos;
    zfp *fp;
    kstring_t fn_idx_str = {0};

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

    if (!(fp = zfopen(fn_idx, "wz"))) {
        perror(fn_idx);
        free(fn_idx_str.s);
        return -1;
    }

    free(fn_idx_str.s);

    cpos = htell(fd->fp);
    while ((c = cram_read_container(fd))) {
        int j;

        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;

        // 2.0 format
        for (j = 0; j < c->num_landmarks; j++) {
            char buf[1024];
            cram_slice *s;
            int sz;

            spos = htell(fd->fp);
            assert(spos - cpos - c->offset == c->landmark[j]);

            if (!(s = cram_read_slice(fd))) {
		zfclose(fp);
		return -1;
	    }

            sz = (int)(htell(fd->fp) - spos);

	    if (s->hdr->ref_seq_id == -2) {
		cram_index_build_multiref(fd, c, s, fp,
					  cpos, c->landmark[j], sz);
	    } else {
		sprintf(buf, "%d\t%d\t%d\t%"PRId64"\t%d\t%d\n",
			s->hdr->ref_seq_id, s->hdr->ref_seq_start,
			s->hdr->ref_seq_span, (int64_t)cpos,
			c->landmark[j], sz);
		zfputs(buf, fp);
	    }

            cram_free_slice(s);
        }

        cpos = htell(fd->fp);
        assert(cpos == hpos + c->length);

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

    return (zfclose(fp) >= 0)? 0 : -1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x858, %rsp            # imm = 0x858
movq %rdi, 0x18(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
andq $0x0, 0x40(%rsp)
testq %rdx, %rdx
je 0x4c227
movq %rdx, %r12
xorl %ebx, %ebx
jmp 0x4c24d
leaq 0x30(%rsp), %rbx
movq %rsi, %rdi
movq %rbx, %rsi
callq 0x4c5a0
leaq 0x3b65f(%rip), %rdi     # 0x8789d
movq %rbx, %rsi
callq 0x4c5a0
movq 0x10(%rbx), %rbx
movq %rbx, %r12
leaq 0x3b66f(%rip), %rsi     # 0x878c3
movq %r12, %rdi
callq 0x5b3f3
movq %rax, (%rsp)
testq %rax, %rax
je 0x4c50d
movq %rbx, %rdi
callq 0x68a0
movq 0x18(%rsp), %r12
movq (%r12), %rax
movq 0x28(%rax), %rcx
addq 0x8(%rax), %rcx
subq (%rax), %rcx
movq %rcx, 0x8(%rsp)
movq %r12, %rdi
callq 0x50ca9
movl 0x30(%r12), %ebp
testq %rax, %rax
je 0x4c51f
testl %ebp, %ebp
jne 0x4c535
movq %rax, %rbx
movq (%r12), %rax
movq 0x28(%rax), %r14
addq 0x8(%rax), %r14
subq (%rax), %r14
movq %r12, %rdi
callq 0x4dbb3
movq %rax, 0x48(%rbx)
testq %rax, %rax
je 0x4c4f6
cmpl $0x1, 0x8(%rax)
jne 0x4c562
movq %r12, %rdi
movq %rax, %rsi
callq 0x66815
movq %rax, 0x40(%rbx)
testq %rax, %rax
je 0x4c4f6
movq %r14, 0x28(%rsp)
xorl %ebp, %ebp
movq %rbx, 0x10(%rsp)
movslq 0x28(%rbx), %rax
cmpq %rax, %rbp
jge 0x4c4b8
movq (%r12), %rax
movq 0x28(%rax), %r14
addq 0x8(%rax), %r14
subq (%rax), %r14
movq 0x38(%rbx), %rax
addq 0x8(%rsp), %rax
movq %r14, %rcx
subq %rax, %rcx
movq 0x30(%rbx), %rax
movslq (%rax,%rbp,4), %rax
cmpq %rax, %rcx
jne 0x4c543
movq %r12, %rdi
callq 0x52385
testq %rax, %rax
je 0x4c4ed
movq %rax, %r13
movq (%r12), %rax
movl 0x28(%rax), %r15d
addl 0x8(%rax), %r15d
addl (%rax), %r14d
subl %r14d, %r15d
movq (%r13), %rax
movl 0x4(%rax), %edx
cmpl $-0x2, %edx
jne 0x4c431
movq 0x10(%rsp), %rsi
movq 0x30(%rsi), %rax
movl (%rax,%rbp,4), %eax
movq %rax, 0x20(%rsp)
movq 0x18(%r12), %rcx
movq %r12, %rdi
movq %r13, %rdx
callq 0x683af
testl %eax, %eax
jne 0x4c475
movl $0x80000000, %r8d       # imm = 0x80000000
xorl %r14d, %r14d
xorl %r12d, %r12d
pushq $-0x2
popq %rdx
xorl %ecx, %ecx
movq (%r13), %rax
movslq 0x10(%rax), %rax
cmpq %rax, %r12
jge 0x4c48a
movq 0x28(%r13), %rax
movl 0x8(%rax,%r14), %esi
cmpl %edx, %esi
jne 0x4c3d5
movl 0x5c(%rax,%r14), %eax
cmpl %eax, %r8d
cmovlel %eax, %r8d
jmp 0x4c425
cmpl $-0x2, %edx
je 0x4c418
subl %ecx, %r8d
incl %r8d
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
leaq 0x3b599(%rip), %rsi     # 0x87988
movq 0x8(%rsp), %r9
xorl %eax, %eax
pushq %r15
pushq 0x28(%rsp)
callq 0x6170
popq %rcx
popq %rdx
movq %rbx, %rdi
movq (%rsp), %rsi
callq 0x5b339
movq 0x28(%r13), %rax
movl 0x8(%rax,%r14), %esi
movl 0x18(%rax,%r14), %ecx
movl $0x80000000, %r8d       # imm = 0x80000000
movl %esi, %edx
incq %r12
addq $0x70, %r14
jmp 0x4c3a9
movl 0x8(%rax), %ecx
movl 0xc(%rax), %r8d
movq 0x10(%rsp), %rax
movq 0x30(%rax), %rax
movl (%rax,%rbp,4), %r10d
leaq 0x450(%rsp), %rbx
movq %rbx, %rdi
leaq 0x3b531(%rip), %rsi     # 0x87988
movq 0x8(%rsp), %r9
xorl %eax, %eax
pushq %r15
pushq %r10
callq 0x6170
popq %rcx
popq %rdx
movq %rbx, %rdi
movq (%rsp), %rsi
callq 0x5b339
movq %r13, %rdi
callq 0x50b17
incq %rbp
movq 0x10(%rsp), %rbx
jmp 0x4c2fd
cmpl $-0x2, %edx
movq 0x18(%rsp), %r12
je 0x4c475
subl %ecx, %r8d
incl %r8d
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
leaq 0x3b4df(%rip), %rsi     # 0x87988
movq 0x8(%rsp), %r9
xorl %eax, %eax
pushq %r15
pushq 0x28(%rsp)
jmp 0x4c462
movq (%r12), %rax
movq 0x28(%rax), %rcx
addq 0x8(%rax), %rcx
subq (%rax), %rcx
movslq (%rbx), %rax
movq 0x28(%rsp), %rdx
addq %rax, %rdx
movq %rcx, 0x8(%rsp)
cmpq %rdx, %rcx
jne 0x4c581
movq %rbx, %rdi
callq 0x5093a
jmp 0x4c28a
movq (%rsp), %rdi
callq 0x5b4e7
pushq $-0x1
popq %rcx
movl %ecx, %eax
addq $0x858, %rsp            # imm = 0x858
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, %rdi
callq 0x6590
movq %rbx, %rdi
callq 0x68a0
jmp 0x4c4f6
movq (%rsp), %rdi
callq 0x5b4e7
sarl $0x1f, %eax
xorl %ecx, %ecx
negl %ebp
sbbl %ecx, %ecx
orl %eax, %ecx
jmp 0x4c4f9
leaq 0x3b38a(%rip), %rdi     # 0x878c6
callq 0x6590
jmp 0x4c4f6
leaq 0x3b414(%rip), %rdi     # 0x8795e
leaq 0x3b3bf(%rip), %rsi     # 0x87910
leaq 0x3b3ca(%rip), %rcx     # 0x87922
movl $0x224, %edx            # imm = 0x224
callq 0x6460
leaq 0x3b371(%rip), %rdi     # 0x878da
leaq 0x3b3a0(%rip), %rsi     # 0x87910
leaq 0x3b3ab(%rip), %rcx     # 0x87922
movl $0x217, %edx            # imm = 0x217
callq 0x6460
leaq 0x3b414(%rip), %rdi     # 0x8799c
leaq 0x3b381(%rip), %rsi     # 0x87910
leaq 0x3b38c(%rip), %rcx     # 0x87922
movl $0x23c, %edx            # imm = 0x23C
callq 0x6460