// File: /workspace/github_c_makefile_star2_O1/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, (%rsp)
testq %rdi, %rdi
je 0x1dfb3
movq %rdi, %r15
cmpq $0x2, %rdi
jne 0x1db76
movq (%rsp), %rcx
movl (%rcx), %eax
cmpl %eax, 0x10(%rcx)
jae 0x1df70
movq (%rsp), %rcx
leaq 0x10(%rcx), %rax
movups (%rcx), %xmm0
movaps %xmm0, 0x20(%rsp)
movups (%rax), %xmm0
movups %xmm0, (%rcx)
movaps 0x20(%rsp), %xmm0
movups %xmm0, (%rax)
jmp 0x1dfb3
movl $0x2, %ecx
movl $0xf0, %edi
movl $0x2, %ebx
movl $0x1, %eax
shlq %cl, %rax
incq %rcx
addq $0xc0, %rdi
addl $0x2, %ebx
cmpq %r15, %rax
jb 0x1db85
callq 0x3240
shlq $0x4, %r15
movq (%rsp), %r13
leaq (%r15,%r13), %rcx
movq %rcx, 0x38(%rsp)
leaq (%r15,%r13), %rdi
addq $-0x10, %rdi
movq %rax, 0x40(%rsp)
movq %rax, 0x8(%rsp)
movl $0x10, %eax
subq %r13, %rax
movq %rax, 0x50(%rsp)
movq %r13, 0x48(%rsp)
cmpq %rdi, %r13
jae 0x1deb6
decl %ebx
je 0x1dea4
movl %ebx, 0x1c(%rsp)
movq %rdi, 0x10(%rsp)
movq %rdi, %r15
subq %r13, %r15
sarq $0x5, %r15
shlq $0x4, %r15
movl 0x10(%r13,%r15), %ebp
movq 0x18(%r13,%r15), %r14
movl (%r13), %r12d
cmpl %r12d, %ebp
jae 0x1dc70
movq 0x10(%rsp), %r12
movq %r12, %rcx
cmpl (%r12), %ebp
jb 0x1dcf9
leaq 0x10(%r13,%r15), %rbx
jne 0x1dc68
movq 0x8(%r12), %r15
movq 0x30(%r14), %rdi
movq 0x30(%r15), %rsi
callq 0x35a0
movq %r12, %rcx
testl %eax, %eax
js 0x1dcf9
jne 0x1dc68
movq 0x8(%r14), %rax
shrq $0x26, %rax
andl $0x3, %eax
movq 0x8(%r15), %rcx
shrq $0x26, %rcx
andl $0x3, %ecx
cmpl %ecx, %eax
cmovbq %r12, %rbx
movq %rbx, %rcx
jmp 0x1dcf9
movq 0x8(%r13), %rbx
jne 0x1dca7
movq 0x30(%r14), %rdi
movq 0x30(%rbx), %rsi
callq 0x35a0
testl %eax, %eax
js 0x1dc14
jne 0x1dca7
movq 0x8(%r14), %rax
shrq $0x26, %rax
andl $0x3, %eax
movq 0x8(%rbx), %rcx
shrq $0x26, %rcx
andl $0x3, %ecx
cmpl %ecx, %eax
jb 0x1dc14
movq %r13, %rcx
movq 0x10(%rsp), %rax
cmpl %r12d, (%rax)
movq %rax, %r12
jb 0x1dcf9
movq %r12, %rcx
jne 0x1dcf9
movq 0x8(%r12), %r14
movq 0x30(%r14), %rdi
movq 0x30(%rbx), %rsi
callq 0x35a0
movq %r13, %rcx
testl %eax, %eax
js 0x1dcf9
movq %r12, %rcx
jne 0x1dcf9
movq 0x8(%r14), %rax
shrq $0x26, %rax
andl $0x3, %eax
movq 0x8(%rbx), %rcx
shrq $0x26, %rcx
andl $0x3, %ecx
cmpl %ecx, %eax
movq %r12, %rcx
cmovbq %r13, %rcx
movl (%rcx), %r14d
movq 0x8(%rcx), %rbp
cmpq %r12, %rcx
je 0x1dd1f
movups (%rcx), %xmm0
movaps %xmm0, 0x20(%rsp)
movups (%r12), %xmm0
movups %xmm0, (%rcx)
movaps 0x20(%rsp), %xmm0
movups %xmm0, (%r12)
movq %r13, %r15
xorl %r13d, %r13d
cmpl %r14d, 0x10(%r15,%r13)
jb 0x1dd60
jne 0x1dd66
movq 0x18(%r15,%r13), %rbx
movq 0x30(%rbx), %rdi
movq 0x30(%rbp), %rsi
callq 0x35a0
testl %eax, %eax
js 0x1dd60
jne 0x1dd66
movq 0x8(%rbx), %rax
shrq $0x26, %rax
andl $0x3, %eax
movq 0x8(%rbp), %rcx
shrq $0x26, %rcx
andl $0x3, %ecx
cmpl %ecx, %eax
jae 0x1dd66
addq $0x10, %r13
jmp 0x1dd25
addq $-0x10, %r12
leaq (%r15,%r13), %rdx
addq $0x10, %rdx
cmpq %r12, %rdx
ja 0x1ddcd
cmpl (%r12), %r14d
jb 0x1ddc7
jne 0x1ddcd
movq 0x8(%r12), %rax
movq 0x30(%rbp), %rdi
movq %rax, 0x58(%rsp)
movq 0x30(%rax), %rsi
movq %rbp, %rbx
movq %rdx, %rbp
callq 0x35a0
movq %rbp, %rdx
movq %rbx, %rbp
testl %eax, %eax
js 0x1ddc7
jne 0x1ddcd
movq 0x8(%rbp), %rax
shrq $0x26, %rax
andl $0x3, %eax
movq 0x58(%rsp), %rcx
movq 0x8(%rcx), %rcx
shrq $0x26, %rcx
andl $0x3, %ecx
cmpl %ecx, %eax
jae 0x1ddcd
addq $-0x10, %r12
jmp 0x1dd72
movups (%rdx), %xmm0
movaps %xmm0, 0x20(%rsp)
cmpq %rdx, %r12
jbe 0x1ddf4
movups (%r12), %xmm0
movups %xmm0, (%rdx)
movaps 0x20(%rsp), %xmm0
movups %xmm0, (%r12)
movq %rdx, %r15
jmp 0x1dd22
leaq (%r15,%r13), %rax
movq 0x10(%rsp), %rdi
movups (%rdi), %xmm0
movups %xmm0, 0x10(%r15,%r13)
movaps 0x20(%rsp), %xmm0
movups %xmm0, (%rdi)
movq 0x50(%rsp), %rcx
addq %rax, %rcx
sarq $0x4, %rcx
movq %rdi, %rdx
subq %r15, %rdx
subq %r13, %rdx
addq $-0x10, %rdx
sarq $0x4, %rdx
cmpq %rdx, %rcx
movl 0x1c(%rsp), %ebx
jg 0x1de6f
cmpq $0x11, %rdx
jl 0x1de5a
leaq (%r15,%r13), %rdx
addq $0x20, %rdx
movq 0x8(%rsp), %rsi
movq %rdx, (%rsi)
movq %rdi, 0x8(%rsi)
movl %ebx, 0x10(%rsi)
addq $0x18, %rsi
movq %rsi, 0x8(%rsp)
cmpq $0x11, %rcx
movq 0x48(%rsp), %r13
cmovlq %r13, %rax
movq %rax, %rdi
jmp 0x1dbd9
cmpq $0x11, %rcx
jl 0x1de92
movq 0x8(%rsp), %rcx
movq 0x48(%rsp), %rsi
movq %rsi, (%rcx)
movq %rax, 0x8(%rcx)
movl %ebx, 0x10(%rcx)
addq $0x18, %rcx
movq %rcx, 0x8(%rsp)
cmpq $0x11, %rdx
leaq 0x20(%r15,%r13), %r13
cmovlq %rdi, %r13
jmp 0x1dbc7
subq %r13, %rdi
sarq $0x4, %rdi
incq %rdi
movq %r13, %rsi
callq 0x1d94c
movq 0x8(%rsp), %rax
cmpq 0x40(%rsp), %rax
je 0x1ded6
movq -0x18(%rax), %r13
movq -0x10(%rax), %rdi
movl -0x8(%rax), %ebx
addq $-0x18, %rax
jmp 0x1dbc2
movq 0x40(%rsp), %rdi
callq 0x3350
movq (%rsp), %rax
leaq 0x10(%rax), %rbx
cmpq 0x38(%rsp), %rbx
jae 0x1dfb3
cmpq (%rsp), %rbx
jbe 0x1df67
movq %rbx, %r14
movl -0x10(%r14), %eax
cmpl %eax, (%r14)
jb 0x1df3e
jne 0x1df67
movq 0x8(%r14), %r12
movq -0x8(%r14), %r15
movq 0x30(%r12), %rdi
movq 0x30(%r15), %rsi
callq 0x35a0
testl %eax, %eax
js 0x1df3e
jne 0x1df67
movq 0x8(%r12), %rax
shrq $0x26, %rax
andl $0x3, %eax
movq 0x8(%r15), %rcx
shrq $0x26, %rcx
andl $0x3, %ecx
cmpl %ecx, %eax
jae 0x1df67
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 (%rsp), %rax
ja 0x1defc
addq $0x10, %rbx
jmp 0x1dee8
jne 0x1dfb3
movq (%rsp), %rax
movq 0x18(%rax), %r14
movq 0x8(%rax), %rbx
movq 0x30(%r14), %rdi
movq 0x30(%rbx), %rsi
callq 0x35a0
testl %eax, %eax
js 0x1db53
jne 0x1dfb3
movq 0x8(%r14), %rax
shrq $0x26, %rax
andl $0x3, %eax
movq 0x8(%rbx), %rcx
shrq $0x26, %rcx
andl $0x3, %ecx
cmpl %ecx, %eax
jb 0x1db53
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq