// File: /workspace/github_cpp_makefile_star2_O3/karel-brinda[P]ococo/ext/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 -4;
    }

    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 : -4;
}

// 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 0x2f610
movq %rdx, %r13
xorl %r15d, %r15d
jmp 0x2f719
movq %rsi, %r12
movq %rsi, %rdi
callq 0x4230
movslq %eax, %r14
shlq $0x20, %rax
xorl %r13d, %r13d
movabsq $-0x200000000, %rcx     # imm = 0xFFFFFFFE00000000
movl $0x0, %r15d
cmpq %rcx, %rax
je 0x2f690
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, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
movq %rcx, %r13
shrq $0x20, %r13
orq %rcx, %r13
incq %r13
movq %r13, %rdi
callq 0x4700
movq %rax, %r15
testq %rax, %rax
je 0x2fa4a
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x4420
movb $0x0, (%r15,%r14)
leaq 0x7(%r14), %rax
cmpq %rax, %r13
jae 0x2f700
leaq 0x6(%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, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
movq %rcx, %rsi
shrq $0x20, %rsi
orq %rcx, %rsi
incq %rsi
movq %r15, %rdi
callq 0x48d0
movq %r15, %r13
testq %rax, %rax
jne 0x2f703
jmp 0x2f719
movq %r15, %rax
movl $0x6172632e, (%rax,%r14) # imm = 0x6172632E
movw $0x69, 0x4(%rax,%r14)
movq %rax, %r15
movq %rax, %r13
leaq 0x22f36(%rip), %rsi     # 0x52656
movq %r13, %rdi
callq 0x41ce3
movq %rax, (%rsp)
testq %rax, %rax
je 0x2fa58
movq %r15, %rdi
callq 0x4610
movq 0x10(%rsp), %r15
movq (%r15), %rax
movq 0x28(%rax), %r13
movq (%rax), %rbx
movq 0x8(%rax), %r14
movq %r15, %rdi
callq 0x3530f
testq %rax, %rax
je 0x2fa08
movq %rax, %r12
addq %r14, %r13
subq %rbx, %r13
movq %r13, 0x8(%rsp)
cmpl $0x0, 0x30(%r15)
jne 0x2fa6f
movq (%r15), %rax
movq 0x28(%rax), %rbx
addq 0x8(%rax), %rbx
subq (%rax), %rbx
movq %r15, %rdi
callq 0x31aab
movq %rax, 0x48(%r12)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x2fa36
cmpl $0x1, 0x8(%rax)
jne 0x2fa9c
movq %r15, %rdi
movq %rax, %rsi
callq 0x1c855
movq %rax, 0x40(%r12)
testq %rax, %rax
je 0x2fa36
movq %rbx, 0x28(%rsp)
cmpl $0x0, 0x28(%r12)
jle 0x2f9c4
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 0x2fa7d
movq %r15, %rdi
callq 0x36ba5
testq %rax, %rax
je 0x2fa28
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 0x2f969
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 0x1f5db
testl %eax, %eax
jne 0x2f9ab
movq (%r13), %rax
cmpl $0x0, 0x10(%rax)
jle 0x2f9ab
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 0x2f8a9
movl 0x5c(%rsi,%rbx), %edi
cmpl %edi, %r8d
cmovlel %edi, %r8d
jmp 0x2f906
cmpl $-0x2, %edx
je 0x2f8f8
addl %edi, %r8d
incl %r8d
movq %r12, %rbp
leaq 0x30(%rsp), %r12
movq %r12, %rdi
leaq 0x22e55(%rip), %rsi     # 0x5271b
movq 0x8(%rsp), %r9
xorl %eax, %eax
pushq %r10
pushq 0x20(%rsp)
callq 0x4110
addq $0x10, %rsp
movq %r12, %rdi
movq %rbp, %r12
movq (%rsp), %rsi
callq 0x41c27
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 0x2f896
cmpl $-0x2, %edx
je 0x2f95a
subl %ecx, %r8d
incl %r8d
leaq 0x30(%rsp), %rbx
movq %rbx, %rdi
leaq 0x22de3(%rip), %rsi     # 0x5271b
movq 0x8(%rsp), %r9
xorl %eax, %eax
pushq %r10
pushq 0x20(%rsp)
callq 0x4110
addq $0x10, %rsp
movq %rbx, %rdi
movq (%rsp), %rsi
callq 0x41c27
movq %r13, %rdi
callq 0x35120
movq 0x10(%rsp), %r15
jmp 0x2f9b3
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 0x22d90(%rip), %rsi     # 0x5271b
movq 0x8(%rsp), %r9
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x4110
addq $0x10, %rsp
movq %rbx, %rdi
movq (%rsp), %rsi
callq 0x41c27
movq %r13, %rdi
callq 0x35120
incq %r14
movslq 0x28(%r12), %rax
cmpq %rax, %r14
jl 0x2f7da
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 0x2fabb
movq %r12, %rdi
callq 0x34f02
movq %r15, %rdi
callq 0x3530f
movq %rax, %r12
testq %rax, %rax
jne 0x2f76f
movl 0x30(%r15), %ebx
movq (%rsp), %rdi
callq 0x41df6
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %ebx, %ebx
jne 0x2fa36
sarl $0x1f, %eax
andl $-0x4, %eax
movl %eax, %ebp
jmp 0x2fa36
movq (%rsp), %rdi
callq 0x41df6
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
xorl %r14d, %r14d
xorl %r13d, %r13d
xorl %r15d, %r15d
jmp 0x2f6a3
movq %r13, %rdi
callq 0x4440
movq %r15, %rdi
callq 0x4610
movl $0xfffffffc, %ebp       # imm = 0xFFFFFFFC
jmp 0x2fa36
leaq 0x22be3(%rip), %rdi     # 0x52659
callq 0x4440
jmp 0x2fa31
leaq 0x22c6d(%rip), %rdi     # 0x526f1
leaq 0x22c18(%rip), %rsi     # 0x526a3
leaq 0x22c23(%rip), %rcx     # 0x526b5
movl $0x222, %edx            # imm = 0x222
callq 0x4350
leaq 0x22bca(%rip), %rdi     # 0x5266d
leaq 0x22bf9(%rip), %rsi     # 0x526a3
leaq 0x22c04(%rip), %rcx     # 0x526b5
movl $0x215, %edx            # imm = 0x215
callq 0x4350
leaq 0x22c6d(%rip), %rdi     # 0x5272f
leaq 0x22bda(%rip), %rsi     # 0x526a3
leaq 0x22be5(%rip), %rcx     # 0x526b5
movl $0x23a, %edx            # imm = 0x23A
callq 0x4350
nop