// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x490, %rsp            # imm = 0x490
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
leaq -0x60(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x63a0
cmpq $0x0, -0x20(%rbp)
jne 0x7aff3
movq -0x18(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x7b330
leaq 0x677db(%rip), %rdi     # 0xe27bd
leaq -0x60(%rbp), %rsi
callq 0x7b330
movq -0x50(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
leaq 0x677e5(%rip), %rsi     # 0xe27e3
callq 0x99e70
movq %rax, -0x48(%rbp)
cmpq $0x0, %rax
jne 0x7b02b
movq -0x20(%rbp), %rdi
callq 0x6590
movq -0x50(%rbp), %rdi
callq 0x6880
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x7b31d
movq -0x50(%rbp), %rdi
callq 0x6880
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x7b370
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rdi
callq 0x841e0
movq %rax, -0x28(%rbp)
cmpq $0x0, %rax
je 0x7b2e6
movq -0x10(%rbp), %rax
cmpl $0x0, 0x30(%rax)
je 0x7b07d
leaq 0x6777a(%rip), %rdi     # 0xe27e6
callq 0x6590
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x7b31d
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x7b370
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rdi
callq 0x7e250
movq -0x28(%rbp), %rcx
movq %rax, 0x48(%rcx)
cmpq $0x0, %rax
jne 0x7b0b0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x7b31d
movq -0x28(%rbp), %rax
movq 0x48(%rax), %rax
cmpl $0x1, 0x8(%rax)
jne 0x7b0c0
jmp 0x7b0df
leaq 0x67733(%rip), %rdi     # 0xe27fa
leaq 0x67762(%rip), %rsi     # 0xe2830
movl $0x217, %edx            # imm = 0x217
leaq 0x67768(%rip), %rcx     # 0xe2842
callq 0x6450
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x48(%rax), %rsi
callq 0xaf200
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x28(%rbp), %rax
cmpq $0x0, 0x40(%rax)
jne 0x7b112
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x7b31d
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
movq -0x28(%rbp), %rcx
cmpl 0x28(%rcx), %eax
jge 0x7b290
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x7b370
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
subq -0x30(%rbp), %rax
movq -0x28(%rbp), %rcx
subq 0x38(%rcx), %rax
movq -0x28(%rbp), %rcx
movq 0x30(%rcx), %rcx
movslq -0x64(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
cmpq %rcx, %rax
jne 0x7b160
jmp 0x7b17f
leaq 0x67717(%rip), %rdi     # 0xe287e
leaq 0x676c2(%rip), %rsi     # 0xe2830
movl $0x224, %edx            # imm = 0x224
leaq 0x676c8(%rip), %rcx     # 0xe2842
callq 0x6450
movq -0x10(%rbp), %rdi
callq 0x87eb0
movq %rax, -0x478(%rbp)
cmpq $0x0, %rax
jne 0x7b1aa
movq -0x48(%rbp), %rdi
callq 0x9a050
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x7b31d
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x7b370
subq -0x38(%rbp), %rax
movl %eax, -0x47c(%rbp)
movq -0x478(%rbp), %rax
movq (%rax), %rax
cmpl $-0x2, 0x4(%rax)
jne 0x7b207
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x478(%rbp), %rdx
movq -0x48(%rbp), %rcx
movq -0x30(%rbp), %r8
movq -0x28(%rbp), %rax
movq 0x30(%rax), %rax
movslq -0x64(%rbp), %r9
movl (%rax,%r9,4), %r9d
movl -0x47c(%rbp), %eax
movl %eax, (%rsp)
callq 0x7b3a0
jmp 0x7b276
leaq -0x470(%rbp), %rdi
movq -0x478(%rbp), %rax
movq (%rax), %rax
movl 0x4(%rax), %edx
movq -0x478(%rbp), %rax
movq (%rax), %rax
movl 0x8(%rax), %ecx
movq -0x478(%rbp), %rax
movq (%rax), %rax
movl 0xc(%rax), %r8d
movq -0x30(%rbp), %r9
movq -0x28(%rbp), %rax
movq 0x30(%rax), %rax
movslq -0x64(%rbp), %rsi
movl (%rax,%rsi,4), %r10d
movl -0x47c(%rbp), %eax
leaq 0x67651(%rip), %rsi     # 0xe28a8
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
movb $0x0, %al
callq 0x6160
leaq -0x470(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x99d00
movq -0x478(%rbp), %rdi
callq 0x83eb0
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x7b119
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x7b370
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq -0x40(%rbp), %rcx
movq -0x28(%rbp), %rdx
movslq (%rdx), %rdx
addq %rdx, %rcx
cmpq %rcx, %rax
jne 0x7b2b9
jmp 0x7b2d8
leaq 0x675fc(%rip), %rdi     # 0xe28bc
leaq 0x67569(%rip), %rsi     # 0xe2830
movl $0x23c, %edx            # imm = 0x23C
leaq 0x6756f(%rip), %rcx     # 0xe2842
callq 0x6450
movq -0x28(%rbp), %rdi
callq 0x83b70
jmp 0x7b044
movq -0x10(%rbp), %rax
cmpl $0x0, 0x30(%rax)
je 0x7b302
movq -0x48(%rbp), %rdi
callq 0x9a050
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x7b31d
movq -0x48(%rbp), %rdi
callq 0x9a050
movl %eax, %edx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
xorl %ecx, %ecx
cmpl $0x0, %edx
cmovgel %ecx, %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x490, %rsp            # imm = 0x490
popq %rbp
retq
nopl (%rax)