// File: /workspace/github_c_makefile_star2_O3/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 $0x838, %rsp            # imm = 0x838
movq %rdi, 0x10(%rsp)
testq %rdx, %rdx
je 0x53ef8
movq %rdx, %r13
xorl %r15d, %r15d
jmp 0x53fce
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
movq %rax, %r15
testq %rax, %rax
je 0x53f63
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x6570
movb $0x0, (%r15,%r14)
jmp 0x53f66
xorl %r14d, %r14d
leaq 0x6(%r14), %rax
cmpq %r13, %rax
jae 0x53f74
movq %r15, %rax
jmp 0x53fb8
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 0x53fce
movl $0x6172632e, (%rax,%r14) # imm = 0x6172632E
movw $0x69, 0x4(%rax,%r14)
movq %rax, %r15
movq %rax, %r13
leaq 0x449c0(%rip), %rsi     # 0x98995
movq %r13, %rdi
callq 0x6608b
movq %rax, (%rsp)
testq %rax, %rax
je 0x542f9
movq %r15, %rdi
callq 0x68a0
movq 0x10(%rsp), %r15
movq (%r15), %rax
movq 0x28(%rax), %r13
movq (%rax), %rbx
movq 0x8(%rax), %r14
movq %r15, %rdi
callq 0x59a12
testq %rax, %rax
je 0x542bd
movq %rax, %r12
addq %r14, %r13
subq %rbx, %r13
movq %r13, 0x8(%rsp)
cmpl $0x0, 0x30(%r15)
jne 0x5430b
movq (%r15), %rax
movq 0x28(%rax), %rbx
addq 0x8(%rax), %rbx
subq (%rax), %rbx
movq %r15, %rdi
callq 0x56347
movq %rax, 0x48(%r12)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x542e5
cmpl $0x1, 0x8(%rax)
jne 0x54338
movq %r15, %rdi
movq %rax, %rsi
callq 0x74d91
movq %rax, 0x40(%r12)
testq %rax, %rax
je 0x542e5
movq %rbx, 0x28(%rsp)
cmpl $0x0, 0x28(%r12)
jle 0x54279
xorl %r14d, %r14d
movq (%r15), %rax
movq 0x28(%rax), %rbx
addq 0x8(%rax), %rbx
subq (%rax), %rbx
movq 0x38(%r12), %rax
addq 0x8(%rsp), %rax
movq %rbx, %rcx
subq %rax, %rcx
movq 0x30(%r12), %rax
movslq (%rax,%r14,4), %rax
cmpq %rax, %rcx
jne 0x54319
movq %r15, %rdi
callq 0x5b2a8
testq %rax, %rax
je 0x542d7
movq %rax, %r13
movq (%r15), %rax
movl 0x28(%rax), %r11d
addl 0x8(%rax), %r11d
addl (%rax), %ebx
subl %ebx, %r11d
movq (%r13), %rax
movl 0x4(%rax), %edx
cmpl $-0x2, %edx
jne 0x5421e
movq %r11, 0x20(%rsp)
movq 0x30(%r12), %rax
movl (%rax,%r14,4), %eax
movq %rax, 0x18(%rsp)
movq 0x18(%r15), %rcx
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x77b17
testl %eax, %eax
jne 0x54260
movq (%r13), %rax
cmpl $0x0, 0x10(%rax)
jle 0x54260
movq 0x28(%r13), %rsi
movl $0xfffffffe, %edx       # imm = 0xFFFFFFFE
movl $0x80000000, %r8d       # imm = 0x80000000
xorl %ebx, %ebx
xorl %r15d, %r15d
xorl %edi, %edi
xorl %ecx, %ecx
movq 0x20(%rsp), %r10
cmpl %edx, 0x8(%rsi,%rbx)
jne 0x5415e
movl 0x5c(%rsi,%rbx), %edi
cmpl %edi, %r8d
cmovlel %edi, %r8d
jmp 0x541bb
cmpl $-0x2, %edx
je 0x541ad
addl %edi, %r8d
incl %r8d
movq %r12, %rbp
leaq 0x30(%rsp), %r12
movq %r12, %rdi
leaq 0x448df(%rip), %rsi     # 0x98a5a
movq 0x8(%rsp), %r9
xorl %eax, %eax
pushq %r10
pushq 0x20(%rsp)
callq 0x6170
addq $0x10, %rsp
movq %r12, %rdi
movq %rbp, %r12
movq (%rsp), %rsi
callq 0x65fcf
movq 0x20(%rsp), %r10
movq (%r13), %rax
movq 0x28(%r13), %rsi
movl 0x8(%rsi,%rbx), %edx
movl 0x18(%rsi,%rbx), %ecx
movl $0x80000000, %r8d       # imm = 0x80000000
movl %ecx, %edi
negl %edi
incq %r15
movslq 0x10(%rax), %r9
addq $0x70, %rbx
cmpq %r9, %r15
jl 0x5414b
cmpl $-0x2, %edx
je 0x5420f
subl %ecx, %r8d
incl %r8d
leaq 0x30(%rsp), %rbx
movq %rbx, %rdi
leaq 0x4486d(%rip), %rsi     # 0x98a5a
movq 0x8(%rsp), %r9
xorl %eax, %eax
pushq %r10
pushq 0x20(%rsp)
callq 0x6170
addq $0x10, %rsp
movq %rbx, %rdi
movq (%rsp), %rsi
callq 0x65fcf
movq %r13, %rdi
callq 0x59823
movq 0x10(%rsp), %r15
jmp 0x54268
movl 0x8(%rax), %ecx
movl 0xc(%rax), %r8d
movq 0x30(%r12), %rax
movl (%rax,%r14,4), %r10d
leaq 0x430(%rsp), %rbx
movq %rbx, %rdi
leaq 0x4481a(%rip), %rsi     # 0x98a5a
movq 0x8(%rsp), %r9
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x6170
addq $0x10, %rsp
movq %rbx, %rdi
movq (%rsp), %rsi
callq 0x65fcf
movq %r13, %rdi
callq 0x59823
incq %r14
movslq 0x28(%r12), %rax
cmpq %rax, %r14
jl 0x5408f
movq (%r15), %rax
movq 0x28(%rax), %rcx
addq 0x8(%rax), %rcx
subq (%rax), %rcx
movslq (%r12), %rax
movq 0x28(%rsp), %rdx
addq %rax, %rdx
movq %rcx, 0x8(%rsp)
cmpq %rdx, %rcx
jne 0x54357
movq %r12, %rdi
callq 0x59605
movq %r15, %rdi
callq 0x59a12
movq %rax, %r12
testq %rax, %rax
jne 0x54024
xorl %ebx, %ebx
cmpl 0x30(%r15), %ebx
sbbl %ebx, %ebx
movq (%rsp), %rdi
callq 0x6618a
movl %eax, %ebp
sarl $0x1f, %ebp
orl %ebx, %ebp
jmp 0x542e5
movq (%rsp), %rdi
callq 0x6618a
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x838, %rsp            # imm = 0x838
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r13, %rdi
callq 0x6590
movq %r15, %rdi
callq 0x68a0
jmp 0x542e0
leaq 0x44686(%rip), %rdi     # 0x98998
callq 0x6590
jmp 0x542e0
leaq 0x44710(%rip), %rdi     # 0x98a30
leaq 0x446bb(%rip), %rsi     # 0x989e2
leaq 0x446c6(%rip), %rcx     # 0x989f4
movl $0x224, %edx            # imm = 0x224
callq 0x6460
leaq 0x4466d(%rip), %rdi     # 0x989ac
leaq 0x4469c(%rip), %rsi     # 0x989e2
leaq 0x446a7(%rip), %rcx     # 0x989f4
movl $0x217, %edx            # imm = 0x217
callq 0x6460
leaq 0x44710(%rip), %rdi     # 0x98a6e
leaq 0x4467d(%rip), %rsi     # 0x989e2
leaq 0x44688(%rip), %rcx     # 0x989f4
movl $0x23c, %edx            # imm = 0x23C
callq 0x6460
nop