// File: /workspace/github_cpp_makefile_star2_O2/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 $0x858, %rsp            # imm = 0x858
movq %rdi, 0x10(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
andq $0x0, 0x40(%rsp)
testq %rdx, %rdx
je 0x261c1
movq %rdx, %r12
xorl %r15d, %r15d
jmp 0x261e7
leaq 0x30(%rsp), %r14
movq %rsi, %rdi
movq %r14, %rsi
callq 0x26536
leaq 0x1f382(%rip), %rdi     # 0x4555a
movq %r14, %rsi
callq 0x26536
movq 0x10(%r14), %r15
movq %r15, %r12
leaq 0x1f392(%rip), %rsi     # 0x45580
movq %r12, %rdi
callq 0x355bf
movq %rax, (%rsp)
testq %rax, %rax
je 0x2649d
movq %r15, %rdi
callq 0x4600
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 0x2ad26
movl 0x30(%r15), %ebx
testq %rax, %rax
je 0x264b1
testl %ebx, %ebx
jne 0x264cb
movq %rax, %rbp
movq (%r15), %rax
movq 0x28(%rax), %rcx
addq 0x8(%rax), %rcx
subq (%rax), %rcx
movq %rcx, 0x28(%rsp)
movq %r15, %rdi
callq 0x27b4b
movq %rax, 0x48(%rbp)
testq %rax, %rax
je 0x26486
cmpl $0x1, 0x8(%rax)
jne 0x264f8
movq %r15, %rdi
movq %rax, %rsi
callq 0x16125
movq %rax, 0x40(%rbp)
testq %rax, %rax
je 0x26486
xorl %r14d, %r14d
movslq 0x28(%rbp), %rax
cmpq %rax, %r14
jge 0x26448
movq (%r15), %rax
movq 0x28(%rax), %rbx
addq 0x8(%rax), %rbx
subq (%rax), %rbx
movq 0x38(%rbp), %rax
addq 0x8(%rsp), %rax
movq %rbx, %rcx
subq %rax, %rcx
movq 0x30(%rbp), %rax
movslq (%rax,%r14,4), %rax
cmpq %rax, %rcx
jne 0x264d9
movq %r15, %rdi
callq 0x2c402
testq %rax, %rax
je 0x2647d
movq %rax, %r12
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 0x263c9
movq %r11, 0x20(%rsp)
movq 0x30(%rbp), %rax
movl (%rax,%r14,4), %eax
movq %rax, 0x18(%rsp)
movq 0x18(%r15), %rcx
movq %r15, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0x17cbf
testl %eax, %eax
jne 0x26408
movl $0x80000000, %r8d       # imm = 0x80000000
xorl %ebx, %ebx
xorl %r15d, %r15d
pushq $-0x2
popq %rdx
xorl %ecx, %ecx
movq (%r12), %rax
movslq 0x10(%rax), %rax
cmpq %rax, %r15
jge 0x26418
movq 0x28(%r12), %rax
movl 0x8(%rax,%rbx), %esi
cmpl %edx, %esi
jne 0x26366
movl 0x5c(%rax,%rbx), %eax
cmpl %eax, %r8d
cmovlel %eax, %r8d
jmp 0x263bd
cmpl $-0x2, %edx
je 0x263b1
subl %ecx, %r8d
incl %r8d
movq %rbp, %r13
leaq 0x50(%rsp), %rbp
movq %rbp, %rdi
leaq 0x1f2c2(%rip), %rsi     # 0x45645
movq 0x8(%rsp), %r9
xorl %eax, %eax
pushq 0x20(%rsp)
pushq 0x20(%rsp)
callq 0x4100
popq %rcx
popq %rdx
movq %rbp, %rdi
movq %r13, %rbp
movq (%rsp), %rsi
callq 0x35505
movq 0x28(%r12), %rax
movl 0x8(%rax,%rbx), %esi
movl 0x18(%rax,%rbx), %ecx
movl $0x80000000, %r8d       # imm = 0x80000000
movl %esi, %edx
incq %r15
addq $0x70, %rbx
jmp 0x2633b
movl 0x8(%rax), %ecx
movl 0xc(%rax), %r8d
movq 0x30(%rbp), %rax
movl (%rax,%r14,4), %r10d
leaq 0x450(%rsp), %rbx
movq %rbx, %rdi
leaq 0x1f25b(%rip), %rsi     # 0x45645
movq 0x8(%rsp), %r9
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x4100
popq %rcx
popq %rdx
movq %rbx, %rdi
movq (%rsp), %rsi
callq 0x35505
movq %r12, %rdi
callq 0x2ab94
incq %r14
jmp 0x26290
cmpl $-0x2, %edx
movq 0x10(%rsp), %r15
je 0x26408
subl %ecx, %r8d
incl %r8d
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
leaq 0x1f20e(%rip), %rsi     # 0x45645
movq 0x8(%rsp), %r9
xorl %eax, %eax
pushq 0x20(%rsp)
pushq 0x20(%rsp)
jmp 0x263f5
movq (%r15), %rax
movq 0x28(%rax), %rcx
addq 0x8(%rax), %rcx
subq (%rax), %rcx
movslq (%rbp), %rax
movq 0x28(%rsp), %rdx
addq %rax, %rdx
movq %rcx, 0x8(%rsp)
cmpq %rdx, %rcx
jne 0x26517
movq %rbp, %rdi
callq 0x2a9b7
jmp 0x26223
movq (%rsp), %rdi
callq 0x356c1
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 0x4440
movq %r15, %rdi
callq 0x4600
pushq $-0x4
jmp 0x26488
movq (%rsp), %rdi
callq 0x356c1
testl %ebx, %ebx
pushq $-0x1
popq %rcx
jne 0x26489
sarl $0x1f, %eax
andl $-0x4, %eax
movl %eax, %ecx
jmp 0x26489
leaq 0x1f0b1(%rip), %rdi     # 0x45583
callq 0x4440
jmp 0x26486
leaq 0x1f13b(%rip), %rdi     # 0x4561b
leaq 0x1f0e6(%rip), %rsi     # 0x455cd
leaq 0x1f0f1(%rip), %rcx     # 0x455df
movl $0x222, %edx            # imm = 0x222
callq 0x4360
leaq 0x1f098(%rip), %rdi     # 0x45597
leaq 0x1f0c7(%rip), %rsi     # 0x455cd
leaq 0x1f0d2(%rip), %rcx     # 0x455df
movl $0x215, %edx            # imm = 0x215
callq 0x4360
leaq 0x1f13b(%rip), %rdi     # 0x45659
leaq 0x1f0a8(%rip), %rsi     # 0x455cd
leaq 0x1f0b3(%rip), %rcx     # 0x455df
movl $0x23a, %edx            # imm = 0x23A
callq 0x4360