// File: /workspace/github_c_makefile_star2_O1/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 $0x438, %rsp            # imm = 0x438
movq %rdi, 0x10(%rsp)
testq %rdx, %rdx
je 0x53bf6
movq %rdx, %r13
xorl %r15d, %r15d
jmp 0x53ccf
movq %rsi, %r12
movq %rsi, %rdi
callq 0x62d0
movslq %eax, %r14
leaq 0x1(%r14), %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %r13
shrq $0x10, %r13
orq %rax, %r13
incq %r13
movq %r13, %rdi
callq 0x69d0
testq %rax, %rax
je 0x53c61
movq %rax, %r15
movq %rax, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x6570
movb $0x0, (%r15,%r14)
jmp 0x53c67
xorl %r14d, %r14d
xorl %r15d, %r15d
leaq 0x6(%r14), %rax
cmpq %r13, %rax
jae 0x53c75
movq %r15, %rax
jmp 0x53cb9
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rsi
shrq $0x10, %rsi
orq %rax, %rsi
incq %rsi
movq %r15, %rdi
callq 0x6c80
movq %r15, %r13
testq %rax, %rax
je 0x53ccf
movl $0x6172632e, (%rax,%r14) # imm = 0x6172632E
movw $0x69, 0x4(%rax,%r14)
movq %rax, %r15
movq %rax, %r13
leaq 0x44bcb(%rip), %rsi     # 0x988a1
movq %r13, %rdi
callq 0x660aa
movq %rax, (%rsp)
testq %rax, %rax
je 0x53fe7
movq %r15, %rdi
callq 0x68a0
movq 0x10(%rsp), %r15
movq (%r15), %rax
movq 0x28(%rax), %rcx
addq 0x8(%rax), %rcx
subq (%rax), %rcx
movq %rcx, 0x8(%rsp)
movq %r15, %rdi
callq 0x597db
testq %rax, %rax
je 0x53fbf
cmpl $0x0, 0x30(%r15)
jne 0x53fa2
movq %rax, %r13
movq (%r15), %rax
movq 0x28(%rax), %rbx
addq 0x8(%rax), %rbx
subq (%rax), %rbx
movq %r15, %rdi
callq 0x55f49
movq %rax, 0x48(%r13)
testq %rax, %rax
je 0x53fae
cmpl $0x1, 0x8(%rax)
jne 0x54018
movq %r15, %rdi
movq %rax, %rsi
callq 0x7508d
movq %rax, 0x40(%r13)
testq %rax, %rax
je 0x53fae
movq %rbx, 0x28(%rsp)
cmpl $0x0, 0x28(%r13)
jle 0x53f6e
xorl %r14d, %r14d
movq (%r15), %rax
movq 0x28(%rax), %rbx
addq 0x8(%rax), %rbx
subq (%rax), %rbx
movq 0x38(%r13), %rax
addq 0x8(%rsp), %rax
movq %rbx, %rcx
subq %rax, %rcx
movq 0x30(%r13), %rax
movslq (%rax,%r14,4), %rax
cmpq %rax, %rcx
jne 0x53ff9
movq %r15, %rdi
callq 0x5b0aa
movq %rax, %r12
testq %rax, %rax
je 0x53ec8
movq (%r15), %rax
movl 0x28(%rax), %r11d
addl 0x8(%rax), %r11d
addl (%rax), %ebx
subl %ebx, %r11d
movq (%r12), %rax
movl 0x4(%rax), %edx
cmpl $-0x2, %edx
jne 0x53ed3
movq %r11, 0x20(%rsp)
movq 0x30(%r13), %rax
movl (%rax,%r14,4), %eax
movq %rax, 0x18(%rsp)
movq 0x18(%r15), %rcx
movq %r15, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x77ea5
testl %eax, %eax
jne 0x53f11
movq (%r12), %rax
cmpl $0x0, 0x10(%rax)
jle 0x53f34
movl $0xfffffffe, %edx       # imm = 0xFFFFFFFE
movl $0x80000000, %eax       # imm = 0x80000000
xorl %r15d, %r15d
xorl %ebx, %ebx
xorl %esi, %esi
xorl %ecx, %ecx
movq 0x28(%r12), %rdi
cmpl %edx, 0x8(%rdi,%r15)
jne 0x53e49
movl 0x5c(%rdi,%r15), %esi
cmpl %esi, %eax
cmovlel %esi, %eax
jmp 0x53e9c
cmpl $-0x2, %edx
je 0x53e88
leal (%rsi,%rax), %r8d
incl %r8d
leaq 0x30(%rsp), %rbp
movq %rbp, %rdi
leaq 0x44b02(%rip), %rsi     # 0x98966
movq 0x8(%rsp), %r9
xorl %eax, %eax
pushq 0x20(%rsp)
pushq 0x20(%rsp)
callq 0x6170
addq $0x10, %rsp
movq %rbp, %rdi
movq (%rsp), %rsi
callq 0x65ff3
movq 0x28(%r12), %rax
movl 0x8(%rax,%r15), %edx
movl 0x18(%rax,%r15), %ecx
movl $0x80000000, %eax       # imm = 0x80000000
incq %rbx
movl %ecx, %esi
negl %esi
movq (%r12), %rdi
movslq 0x10(%rdi), %rdi
addq $0x70, %r15
cmpq %rdi, %rbx
jl 0x53e31
movl $0x1, %r8d
subl %ecx, %r8d
movq 0x10(%rsp), %r15
jmp 0x53f46
movq (%rsp), %rdi
callq 0x661cd
jmp 0x53f19
movl 0x8(%rax), %ecx
movl 0xc(%rax), %r8d
movq 0x30(%r13), %rax
movl (%rax,%r14,4), %r10d
leaq 0x30(%rsp), %rbx
movq %rbx, %rdi
leaq 0x44a75(%rip), %rsi     # 0x98966
movq 0x8(%rsp), %r9
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x6170
addq $0x10, %rsp
movq %rbx, %rdi
movq (%rsp), %rsi
callq 0x65ff3
movq %r12, %rdi
callq 0x595ed
testq %r12, %r12
je 0x53fae
incq %r14
movslq 0x28(%r13), %rax
cmpq %rax, %r14
jl 0x53d82
jmp 0x53f6e
movl $0x1, %r8d
movl $0x80000000, %eax       # imm = 0x80000000
xorl %ecx, %ecx
movl $0xfffffffe, %edx       # imm = 0xFFFFFFFE
cmpl $-0x2, %edx
je 0x53f11
addl %eax, %r8d
leaq 0x30(%rsp), %rbx
movq %rbx, %rdi
leaq 0x44a09(%rip), %rsi     # 0x98966
movq 0x8(%rsp), %r9
xorl %eax, %eax
pushq 0x20(%rsp)
pushq 0x20(%rsp)
jmp 0x53efc
movq (%r15), %rax
movq 0x28(%rax), %rcx
addq 0x8(%rax), %rcx
subq (%rax), %rcx
movslq (%r13), %rax
movq 0x28(%rsp), %rdx
addq %rax, %rdx
movq %rcx, 0x8(%rsp)
cmpq %rdx, %rcx
jne 0x54037
movq %r13, %rdi
callq 0x593d4
movb $0x1, %al
jmp 0x53fb0
leaq 0x448fb(%rip), %rdi     # 0x988a4
callq 0x6590
xorl %eax, %eax
testb %al, %al
jne 0x53d0b
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x53fd5
xorl %ebx, %ebx
cmpl 0x30(%r15), %ebx
sbbl %ebx, %ebx
movq (%rsp), %rdi
callq 0x661cd
sarl $0x1f, %eax
orl %ebx, %eax
addq $0x438, %rsp            # imm = 0x438
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r13, %rdi
callq 0x6590
movq %r15, %rdi
callq 0x68a0
jmp 0x53fb8
leaq 0x4493c(%rip), %rdi     # 0x9893c
leaq 0x448e7(%rip), %rsi     # 0x988ee
leaq 0x448f2(%rip), %rcx     # 0x98900
movl $0x224, %edx            # imm = 0x224
callq 0x6460
leaq 0x44899(%rip), %rdi     # 0x988b8
leaq 0x448c8(%rip), %rsi     # 0x988ee
leaq 0x448d3(%rip), %rcx     # 0x98900
movl $0x217, %edx            # imm = 0x217
callq 0x6460
leaq 0x4493c(%rip), %rdi     # 0x9897a
leaq 0x448a9(%rip), %rsi     # 0x988ee
leaq 0x448b4(%rip), %rcx     # 0x98900
movl $0x23c, %edx            # imm = 0x23C
callq 0x6460
nop