// File: /workspace/github_cpp_makefile_star2_O1/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 $0x438, %rsp            # imm = 0x438
movq %rdi, 0x10(%rsp)
testq %rdx, %rdx
je 0x30292
movq %rdx, %r13
xorl %r15d, %r15d
jmp 0x3039b
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 0x30312
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 0x306ad
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x4420
movb $0x0, (%r15,%r14)
leaq 0x7(%r14), %rax
cmpq %rax, %r13
jae 0x30382
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 0x30385
jmp 0x3039b
movq %r15, %rax
movl $0x6172632e, (%rax,%r14) # imm = 0x6172632E
movw $0x69, 0x4(%rax,%r14)
movq %rax, %r15
movq %rax, %r13
leaq 0x24154(%rip), %rsi     # 0x544f6
movq %r13, %rdi
callq 0x42d26
movq %rax, (%rsp)
testq %rax, %rax
je 0x306bb
movq %r15, %rdi
callq 0x4610
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 0x360e5
testq %rax, %rax
je 0x3068b
cmpl $0x0, 0x30(%r15)
jne 0x3066e
movq %rax, %r13
movq (%r15), %rax
movq 0x28(%rax), %rbx
addq 0x8(%rax), %rbx
subq (%rax), %rbx
movq %r15, %rdi
callq 0x32631
movq %rax, 0x48(%r13)
testq %rax, %rax
je 0x3067a
cmpl $0x1, 0x8(%rax)
jne 0x30701
movq %r15, %rdi
movq %rax, %rsi
callq 0x1caed
movq %rax, 0x40(%r13)
testq %rax, %rax
je 0x3067a
movq %rbx, 0x28(%rsp)
cmpl $0x0, 0x28(%r13)
jle 0x3063a
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 0x306e2
movq %r15, %rdi
callq 0x379b4
movq %rax, %r12
testq %rax, %rax
je 0x30594
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 0x3059f
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 0x1f905
testl %eax, %eax
jne 0x305dd
movq (%r12), %rax
cmpl $0x0, 0x10(%rax)
jle 0x30600
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 0x30515
movl 0x5c(%rdi,%r15), %esi
cmpl %esi, %eax
cmovlel %esi, %eax
jmp 0x30568
cmpl $-0x2, %edx
je 0x30554
leal (%rsi,%rax), %r8d
incl %r8d
leaq 0x30(%rsp), %rbp
movq %rbp, %rdi
leaq 0x2408b(%rip), %rsi     # 0x545bb
movq 0x8(%rsp), %r9
xorl %eax, %eax
pushq 0x20(%rsp)
pushq 0x20(%rsp)
callq 0x4110
addq $0x10, %rsp
movq %rbp, %rdi
movq (%rsp), %rsi
callq 0x42c6f
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 0x304fd
movl $0x1, %r8d
subl %ecx, %r8d
movq 0x10(%rsp), %r15
jmp 0x30612
movq (%rsp), %rdi
callq 0x42e55
jmp 0x305e5
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 0x23ffe(%rip), %rsi     # 0x545bb
movq 0x8(%rsp), %r9
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x4110
addq $0x10, %rsp
movq %rbx, %rdi
movq (%rsp), %rsi
callq 0x42c6f
movq %r12, %rdi
callq 0x35ef7
testq %r12, %r12
je 0x3067a
incq %r14
movslq 0x28(%r13), %rax
cmpq %rax, %r14
jl 0x3044e
jmp 0x3063a
movl $0x1, %r8d
movl $0x80000000, %eax       # imm = 0x80000000
xorl %ecx, %ecx
movl $0xfffffffe, %edx       # imm = 0xFFFFFFFE
cmpl $-0x2, %edx
je 0x305dd
addl %eax, %r8d
leaq 0x30(%rsp), %rbx
movq %rbx, %rdi
leaq 0x23f92(%rip), %rsi     # 0x545bb
movq 0x8(%rsp), %r9
xorl %eax, %eax
pushq 0x20(%rsp)
pushq 0x20(%rsp)
jmp 0x305c8
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 0x30720
movq %r13, %rdi
callq 0x35cde
movb $0x1, %al
jmp 0x3067c
leaq 0x23e84(%rip), %rdi     # 0x544f9
callq 0x4440
xorl %eax, %eax
testb %al, %al
jne 0x303d7
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x306d0
movl 0x30(%r15), %ebx
movq (%rsp), %rdi
callq 0x42e55
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testl %ebx, %ebx
jne 0x306d0
sarl $0x1f, %ecx
andl $-0x4, %ecx
movl %ecx, %eax
jmp 0x306d0
xorl %r14d, %r14d
xorl %r13d, %r13d
xorl %r15d, %r15d
jmp 0x30325
movq %r13, %rdi
callq 0x4440
movq %r15, %rdi
callq 0x4610
movl $0xfffffffc, %eax       # imm = 0xFFFFFFFC
addq $0x438, %rsp            # imm = 0x438
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x23ea8(%rip), %rdi     # 0x54591
leaq 0x23e53(%rip), %rsi     # 0x54543
leaq 0x23e5e(%rip), %rcx     # 0x54555
movl $0x222, %edx            # imm = 0x222
callq 0x4350
leaq 0x23e05(%rip), %rdi     # 0x5450d
leaq 0x23e34(%rip), %rsi     # 0x54543
leaq 0x23e3f(%rip), %rcx     # 0x54555
movl $0x215, %edx            # imm = 0x215
callq 0x4350
leaq 0x23ea8(%rip), %rdi     # 0x545cf
leaq 0x23e15(%rip), %rsi     # 0x54543
leaq 0x23e20(%rip), %rcx     # 0x54555
movl $0x23a, %edx            # imm = 0x23A
callq 0x4350
nop