// File: /workspace/github_c_makefile_star2_O3/xiongxu[P]bam2bedGraph2/samtools-0.1.19/bamshuf.c
// Function: ks_introsort_bamshuf
static void bamshuf(const char *fn, int n_files, const char *pre, int clevel, int is_stdout)
{
	BGZF *fp, *fpw, **fpt;
	char **fnt, modew[8];
	bam1_t *b;
	int i, l;
	bam_hdr_t *h;
	int64_t *cnt;

	// split
	fp = strcmp(fn, "-")? bgzf_open(fn, "r") : bgzf_dopen(fileno(stdin), "r");
	assert(fp);
	h = bam_hdr_read(fp);
	fnt = (char**)calloc(n_files, sizeof(void*));
	fpt = (BGZF**)calloc(n_files, sizeof(void*));
	cnt = (int64_t*)calloc(n_files, 8);
	l = strlen(pre);
	for (i = 0; i < n_files; ++i) {
		fnt[i] = (char*)calloc(l + 10, 1);
		sprintf(fnt[i], "%s.%.4d.bam", pre, i);
		fpt[i] = bgzf_open(fnt[i], "w1");
		bam_hdr_write(fpt[i], h);
	}
	b = bam_init1();
	while (bam_read1(fp, b) >= 0) {
		uint32_t x;
		x = hash_X31_Wang(bam_get_qname(b)) % n_files;
		bam_write1(fpt[x], b);
		++cnt[x];
	}
	bam_destroy1(b);
	for (i = 0; i < n_files; ++i) bgzf_close(fpt[i]);
	free(fpt);
	bgzf_close(fp);
	// merge
	sprintf(modew, "w%d", (clevel >= 0 && clevel <= 9)? clevel : DEF_CLEVEL);
	if (!is_stdout) { // output to a file
		char *fnw = (char*)calloc(l + 5, 1);
		sprintf(fnw, "%s.bam", pre);
		fpw = bgzf_open(fnw, modew);
		free(fnw);
	} else fpw = bgzf_dopen(fileno(stdout), modew); // output to stdout
	bam_hdr_write(fpw, h);
	bam_hdr_destroy(h);
	for (i = 0; i < n_files; ++i) {
		int64_t j, c = cnt[i];
		elem_t *a;
		fp = bgzf_open(fnt[i], "r");
		bam_hdr_destroy(bam_hdr_read(fp));
		a = (elem_t*)calloc(c, sizeof(elem_t));
		for (j = 0; j < c; ++j) {
			a[j].b = bam_init1();
			assert(bam_read1(fp, a[j].b) >= 0);
			a[j].key = hash_X31_Wang(bam_get_qname(a[j].b));
		}
		bgzf_close(fp);
		unlink(fnt[i]);
		free(fnt[i]);
		ks_introsort(bamshuf, c, a);
		for (j = 0; j < c; ++j) {
			bam_write1(fpw, a[j].b);
			bam_destroy1(a[j].b);
		}
		free(a);
	}
	bgzf_close(fpw);
	free(fnt); free(cnt);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rsi, 0x10(%rsp)
testq %rdi, %rdi
je 0x1e528
movq %rdi, %r15
cmpq $0x2, %rdi
jne 0x1e0b9
movq 0x10(%rsp), %rcx
movl (%rcx), %eax
cmpl %eax, 0x10(%rcx)
jae 0x1e4e4
movq 0x10(%rsp), %rcx
leaq 0x10(%rcx), %rax
movups (%rcx), %xmm0
movaps %xmm0, 0x30(%rsp)
movups (%rax), %xmm0
movups %xmm0, (%rcx)
movaps 0x30(%rsp), %xmm0
movups %xmm0, (%rax)
jmp 0x1e528
movl $0x2, %ecx
movl $0x2, %ebx
movl $0xf0, %edi
movl $0x1, %eax
shlq %cl, %rax
incq %rcx
addl $0x2, %ebx
addq $0xc0, %rdi
cmpq %r15, %rax
jb 0x1e0c8
callq 0x3240
shlq $0x4, %r15
movq 0x10(%rsp), %rdx
leaq (%rdx,%r15), %rcx
movq %rcx, 0x48(%rsp)
leaq (%rdx,%r15), %rdi
addq $-0x10, %rdi
movq %rax, 0x50(%rsp)
movq %rax, 0x18(%rsp)
movq %rdx, %rsi
movl $0x10, %eax
subq %rsi, %rax
movq %rax, 0x58(%rsp)
movq %rsi, (%rsp)
cmpq %rdi, %rsi
jae 0x1e3ea
decl %ebx
je 0x1e3db
movl %ebx, 0x24(%rsp)
movq %rdi, 0x8(%rsp)
movq %rdi, %r15
subq %rsi, %r15
sarq $0x5, %r15
shlq $0x4, %r15
movl 0x10(%rsi,%r15), %ebx
movq 0x18(%rsi,%r15), %r12
movl (%rsi), %ebp
cmpl %ebp, %ebx
jae 0x1e172
movq 0x8(%rsp), %rcx
movl (%rcx), %eax
movq 0x8(%rcx), %r13
cmpl %eax, %ebx
jae 0x1e1e2
movl %eax, %ebx
movq 0x8(%rsp), %r12
jmp 0x1e236
movq (%rsp), %rax
movq 0x8(%rax), %r14
jne 0x1e1ab
movq 0x30(%r12), %rdi
movq 0x30(%r14), %rsi
callq 0x35a0
testl %eax, %eax
js 0x1e157
jne 0x1e1ab
movq 0x8(%r12), %rax
shrq $0x26, %rax
andl $0x3, %eax
movq 0x8(%r14), %rcx
shrq $0x26, %rcx
andl $0x3, %ecx
cmpl %ecx, %eax
jb 0x1e157
movq 0x8(%rsp), %r12
movl (%r12), %ebx
movq (%rsp), %r15
cmpl %ebp, %ebx
jb 0x1e20e
movq 0x8(%r12), %r13
jne 0x1e236
movq 0x30(%r13), %rdi
movq 0x30(%r14), %rsi
callq 0x35a0
movq (%rsp), %r15
testl %eax, %eax
js 0x1e20e
je 0x1e3b2
movl %ebp, %ebx
jmp 0x1e236
movq (%rsp), %rax
leaq 0x10(%rax,%r15), %r15
jne 0x1e209
movq 0x30(%r12), %rdi
movq 0x30(%r13), %rsi
callq 0x35a0
testl %eax, %eax
js 0x1e168
je 0x1e389
movq 0x8(%rsp), %r12
movl (%r15), %ebx
movq 0x8(%r15), %r13
cmpq %r12, %r15
je 0x1e236
movups (%r15), %xmm0
movaps %xmm0, 0x30(%rsp)
movups (%r12), %xmm0
movups %xmm0, (%r15)
movaps 0x30(%rsp), %xmm0
movups %xmm0, (%r12)
movq (%rsp), %r15
movq %r13, 0x28(%rsp)
xorl %ebp, %ebp
cmpl %ebx, 0x10(%r15,%rbp)
jb 0x1e27c
jne 0x1e282
movq 0x18(%r15,%rbp), %r14
movq 0x30(%r14), %rdi
movq 0x30(%r13), %rsi
callq 0x35a0
testl %eax, %eax
js 0x1e27c
jne 0x1e282
movq 0x8(%r14), %rax
shrq $0x26, %rax
andl $0x3, %eax
movq 0x8(%r13), %rcx
shrq $0x26, %rcx
andl $0x3, %ecx
cmpl %ecx, %eax
jae 0x1e282
addq $0x10, %rbp
jmp 0x1e241
addq $-0x10, %r12
leaq (%r15,%rbp), %r14
addq $0x10, %r14
cmpq %r12, %r14
ja 0x1e2dd
cmpl (%r12), %ebx
jb 0x1e2d7
jne 0x1e2dd
movq 0x8(%r12), %r13
movq 0x28(%rsp), %rax
movq 0x30(%rax), %rdi
movq 0x30(%r13), %rsi
callq 0x35a0
testl %eax, %eax
js 0x1e2d7
jne 0x1e2dd
movq 0x28(%rsp), %rax
movq 0x8(%rax), %rax
shrq $0x26, %rax
andl $0x3, %eax
movq 0x8(%r13), %rcx
shrq $0x26, %rcx
andl $0x3, %ecx
cmpl %ecx, %eax
jae 0x1e2dd
addq $-0x10, %r12
jmp 0x1e28e
movups (%r14), %xmm0
movaps %xmm0, 0x30(%rsp)
cmpq %r14, %r12
jbe 0x1e30b
movups (%r12), %xmm0
movups %xmm0, (%r14)
movaps 0x30(%rsp), %xmm0
movups %xmm0, (%r12)
movq %r14, %r15
movq 0x28(%rsp), %r13
jmp 0x1e23f
leaq (%r15,%rbp), %rax
movq 0x8(%rsp), %rdi
movups (%rdi), %xmm0
movups %xmm0, 0x10(%r15,%rbp)
movaps 0x30(%rsp), %xmm0
movups %xmm0, (%rdi)
movq 0x58(%rsp), %rcx
addq %rax, %rcx
sarq $0x4, %rcx
movq %rdi, %rdx
subq %r15, %rdx
subq %rbp, %rdx
addq $-0x10, %rdx
sarq $0x4, %rdx
cmpq %rdx, %rcx
jg 0x1e40f
cmpq $0x11, %rdx
movl 0x24(%rsp), %ebx
jl 0x1e375
leaq (%r15,%rbp), %rdx
addq $0x20, %rdx
movq 0x18(%rsp), %rsi
movq %rdx, (%rsi)
movq %rdi, 0x8(%rsi)
movl %ebx, 0x10(%rsi)
addq $0x18, %rsi
movq %rsi, 0x18(%rsp)
cmpq $0x11, %rcx
movq (%rsp), %rsi
cmovlq %rsi, %rax
movq %rax, %rdi
jmp 0x1e11f
movq 0x8(%r12), %rax
shrq $0x26, %rax
andl $0x3, %eax
movq 0x8(%r13), %rcx
shrq $0x26, %rcx
andl $0x3, %ecx
cmpl %ecx, %eax
movq 0x8(%rsp), %r12
jae 0x1e20e
jmp 0x1e236
movq 0x8(%r13), %rax
shrq $0x26, %rax
andl $0x3, %eax
movq 0x8(%r14), %rcx
shrq $0x26, %rcx
andl $0x3, %ecx
movq (%rsp), %r15
movl %ebp, %ebx
cmpl %ecx, %eax
jb 0x1e20e
jmp 0x1e236
subq %rsi, %rdi
sarq $0x4, %rdi
incq %rdi
callq 0x1de8c
movq 0x18(%rsp), %rax
cmpq 0x50(%rsp), %rax
je 0x1e447
movq -0x18(%rax), %rsi
movq -0x10(%rax), %rdi
movl -0x8(%rax), %ebx
addq $-0x18, %rax
movq %rax, 0x18(%rsp)
jmp 0x1e10e
cmpq $0x11, %rcx
movl 0x24(%rsp), %ebx
jl 0x1e435
movq 0x18(%rsp), %rcx
movq (%rsp), %rsi
movq %rsi, (%rcx)
movq %rax, 0x8(%rcx)
movl %ebx, 0x10(%rcx)
addq $0x18, %rcx
movq %rcx, 0x18(%rsp)
cmpq $0x11, %rdx
leaq 0x20(%r15,%rbp), %rsi
cmovlq %rdi, %rsi
jmp 0x1e10e
movq 0x50(%rsp), %rdi
callq 0x3350
movq 0x10(%rsp), %rax
leaq 0x10(%rax), %rbx
cmpq 0x48(%rsp), %rbx
jae 0x1e528
cmpq 0x10(%rsp), %rbx
jbe 0x1e4db
movq %rbx, %r14
movl -0x10(%r14), %eax
cmpl %eax, (%r14)
jb 0x1e4b1
jne 0x1e4db
movq 0x8(%r14), %r12
movq -0x8(%r14), %r15
movq 0x30(%r12), %rdi
movq 0x30(%r15), %rsi
callq 0x35a0
testl %eax, %eax
js 0x1e4b1
jne 0x1e4db
movq 0x8(%r12), %rax
shrq $0x26, %rax
andl $0x3, %eax
movq 0x8(%r15), %rcx
shrq $0x26, %rcx
andl $0x3, %ecx
cmpl %ecx, %eax
jae 0x1e4db
movups (%r14), %xmm0
movaps %xmm0, 0x60(%rsp)
movups -0x10(%r14), %xmm0
movups %xmm0, (%r14)
movaps 0x60(%rsp), %xmm0
movups %xmm0, -0x10(%r14)
leaq -0x10(%r14), %rax
movq %rax, %r14
cmpq 0x10(%rsp), %rax
ja 0x1e46f
addq $0x10, %rbx
jmp 0x1e45a
jne 0x1e528
movq 0x10(%rsp), %rax
movq 0x18(%rax), %r14
movq 0x8(%rax), %rbx
movq 0x30(%r14), %rdi
movq 0x30(%rbx), %rsi
callq 0x35a0
testl %eax, %eax
js 0x1e095
jne 0x1e528
movq 0x8(%r14), %rax
shrq $0x26, %rax
andl $0x3, %eax
movq 0x8(%rbx), %rcx
shrq $0x26, %rcx
andl $0x3, %ecx
cmpl %ecx, %eax
jb 0x1e095
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq