// File: /workspace/github_c_makefile_star2_O0/lh3[P]bgt/hts.c
// Function: hts_idx_save
void hts_idx_save(const hts_idx_t *idx, const char *fn, int fmt)
{
	char *fnidx;
	fnidx = (char*)calloc(1, strlen(fn) + 5);
	strcpy(fnidx, fn);
	if (fmt == HTS_FMT_CSI) {
		BGZF *fp;
		uint32_t x[3];
		int is_be, i;
		is_be = ed_is_big();
		fp = bgzf_open(strcat(fnidx, ".csi"), "w");
		bgzf_write(fp, "CSI\1", 4);
		x[0] = idx->min_shift; x[1] = idx->n_lvls; x[2] = idx->l_meta;
		if (is_be) {
			for (i = 0; i < 3; ++i)
				bgzf_write(fp, ed_swap_4p(&x[i]), 4);
		} else bgzf_write(fp, &x, 12);
		if (idx->l_meta) bgzf_write(fp, idx->meta, idx->l_meta);
		hts_idx_save_core(idx, fp, HTS_FMT_CSI);
		if (idx->rec_shift > 0) { // FIXME: NOT WORKING ON BIG ENDIAN!!!
			bgzf_write(fp, "RNI\1", 4);
			bgzf_write(fp, &idx->n_rec, 8);
			bgzf_write(fp, &idx->rec_shift, 4);
			bgzf_write(fp, &idx->ridx.n, 4);
			bgzf_write(fp, idx->ridx.offset, idx->ridx.n * 8);
		}
		bgzf_close(fp);
	} else if (fmt == HTS_FMT_TBI) {
		BGZF *fp;
		fp = bgzf_open(strcat(fnidx, ".tbi"), "w");
		bgzf_write(fp, "TBI\1", 4);
		hts_idx_save_core(idx, fp, HTS_FMT_TBI);
		bgzf_close(fp);
	} else if (fmt == HTS_FMT_BAI) {
		FILE *fp;
		fp = fopen(strcat(fnidx, ".bai"), "w");
		fwrite("BAI\1", 1, 4, fp);
		hts_idx_save_core(idx, fp, HTS_FMT_BAI);
		fclose(fp);
	} else abort();
	free(fnidx);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq -0x10(%rbp), %rdi
callq 0x2170
movq %rax, %rsi
addq $0x5, %rsi
movl $0x1, %edi
callq 0x2230
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x2090
cmpl $0x0, -0x14(%rbp)
jne 0xa8a5
callq 0xc9b0
movl %eax, -0x38(%rbp)
movq -0x20(%rbp), %rdi
leaq 0x1d8b8(%rip), %rsi     # 0x27ff3
callq 0x2400
movq %rax, %rdi
leaq 0x1cdba(%rip), %rsi     # 0x27504
callq 0x47c0
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
leaq 0x1d89a(%rip), %rsi     # 0x27ff8
movl $0x4, %edx
callq 0x5a10
movq -0x8(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x34(%rbp)
movq -0x8(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x38(%rbp)
je 0xa7d3
movl $0x0, -0x3c(%rbp)
cmpl $0x3, -0x3c(%rbp)
jge 0xa7d1
movq -0x28(%rbp), %rax
movq %rax, -0x58(%rbp)
movslq -0x3c(%rbp), %rax
leaq -0x34(%rbp), %rdi
shlq $0x2, %rax
addq %rax, %rdi
callq 0xa980
movq -0x58(%rbp), %rdi
movq %rax, %rsi
movl $0x4, %edx
callq 0x5a10
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0xa793
jmp 0xa7e5
movq -0x28(%rbp), %rdi
leaq -0x34(%rbp), %rsi
movl $0xc, %edx
callq 0x5a10
movq -0x8(%rbp), %rax
cmpl $0x0, 0x14(%rax)
je 0xa809
movq -0x28(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rsi
movq -0x8(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, %edx
callq 0x5a10
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
xorl %edx, %edx
callq 0xa9b0
movq -0x8(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jle 0xa897
movq -0x28(%rbp), %rdi
leaq 0x1d7d0(%rip), %rsi     # 0x27ffd
movl $0x4, %edx
callq 0x5a10
movq -0x28(%rbp), %rdi
movq -0x8(%rbp), %rsi
addq $0x28, %rsi
movl $0x8, %edx
callq 0x5a10
movq -0x28(%rbp), %rdi
movq -0x8(%rbp), %rsi
addq $0x10, %rsi
movl $0x4, %edx
callq 0x5a10
movq -0x28(%rbp), %rdi
movq -0x8(%rbp), %rsi
addq $0x48, %rsi
movl $0x4, %edx
callq 0x5a10
movq -0x28(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rsi
movq -0x8(%rbp), %rax
movl 0x48(%rax), %eax
shll $0x3, %eax
movslq %eax, %rdx
callq 0x5a10
movq -0x28(%rbp), %rdi
callq 0x5c50
jmp 0xa969
cmpl $0x2, -0x14(%rbp)
jne 0xa900
movq -0x20(%rbp), %rdi
leaq 0x1d74c(%rip), %rsi     # 0x28002
callq 0x2400
movq %rax, %rdi
leaq 0x1cc3f(%rip), %rsi     # 0x27504
callq 0x47c0
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
leaq 0x1d72e(%rip), %rsi     # 0x28007
movl $0x4, %edx
callq 0x5a10
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rsi
movl $0x2, %edx
callq 0xa9b0
movq -0x48(%rbp), %rdi
callq 0x5c50
jmp 0xa967
cmpl $0x1, -0x14(%rbp)
jne 0xa960
movq -0x20(%rbp), %rdi
leaq 0x1d6fb(%rip), %rsi     # 0x2800c
callq 0x2400
movq %rax, %rdi
leaq 0x1cbe4(%rip), %rsi     # 0x27504
callq 0x23c0
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rcx
leaq 0x1d6dd(%rip), %rdi     # 0x28011
movl $0x1, %esi
movl $0x4, %edx
callq 0x2420
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rsi
movl $0x1, %edx
callq 0xa9b0
movq -0x50(%rbp), %rdi
callq 0x2160
jmp 0xa965
callq 0x2060
jmp 0xa967
jmp 0xa969
movq -0x20(%rbp), %rdi
callq 0x2040
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax,%rax)