// File: /workspace/github_cpp_makefile_star2_O3/statgen[P]statgen/lib/samtools-0.1.7a-hybrid/bam_sort.c
// Function: ks_introsort_sort
static void sort_blocks(int n, int k, bam1_p *buf, const char *prefix, const bam_header_t *h, int is_stdout)
{
	char *name;
	int i;
	bamFile fp;
	ks_mergesort(sort, k, buf, 0);
	name = (char*)calloc(strlen(prefix) + 20, 1);
	if (n >= 0) sprintf(name, "%s.%.4d.bam", prefix, n);
	else sprintf(name, "%s.bam", prefix);
	fp = is_stdout? bam_dopen(fileno(stdout), "w") : bam_open(name, "w");
	if (fp == 0) {
		fprintf(stderr, "[sort_blocks] fail to create file %s.\n", name);
		free(name);
		// FIXME: possible memory leak
		return;
	}
	free(name);
	bam_header_write(fp, h);
	for (i = 0; i < k; ++i)
		bam_write1_core(fp, &buf[i]->core, buf[i]->data_len, buf[i]->data);
	bam_close(fp);
}

// Assembly:
testq %rdi, %rdi
je 0x1ebeb
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %r14
movq %rdi, %r15
cmpq $0x2, %rdi
jne 0x1ebec
movq (%r14), %rsi
movq 0x8(%r14), %rdi
callq 0x1e81a
testl %eax, %eax
je 0x1ebdd
movdqu (%r14), %xmm0
pshufd $0x4e, %xmm0, %xmm0     # xmm0 = xmm0[2,3,0,1]
movdqu %xmm0, (%r14)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x2, %ecx
movl $0x2, %ebp
movl $0xf0, %edi
movl $0x1, %eax
shlq %cl, %rax
incq %rcx
addl $0x2, %ebp
addq $0xc0, %rdi
cmpq %r15, %rax
jb 0x1ebfb
callq 0x31d0
movq %rax, %rcx
leaq (%r14,%r15,8), %rax
movq %rax, 0x28(%rsp)
leaq (%r14,%r15,8), %r12
addq $-0x8, %r12
movq %rcx, 0x18(%rsp)
movq %rcx, 0x8(%rsp)
movq %r14, 0x30(%rsp)
leaq 0x8(%r14), %rax
movq %rax, 0x38(%rsp)
movq %r14, %rax
negq %rax
movq %rax, 0x40(%rsp)
movq %r14, 0x20(%rsp)
cmpq %r12, %r14
jae 0x1eda0
decl %ebp
je 0x1ed8b
movq %r12, %rbx
subq %r14, %rbx
sarq $0x4, %rbx
movq 0x8(%r14,%rbx,8), %rdi
movq (%r14), %rsi
callq 0x1e81a
testl %eax, %eax
je 0x1ec9f
movq 0x38(%rsp), %rax
leaq (%rax,%rbx,8), %rbx
movq (%rbx), %rdi
movq (%r12), %rsi
callq 0x1e81a
testl %eax, %eax
cmovneq %r12, %rbx
jmp 0x1ecb4
movq (%r12), %rdi
movq (%r14), %rsi
callq 0x1e81a
testl %eax, %eax
movq %r14, %rbx
cmoveq %r12, %rbx
movl %ebp, 0x14(%rsp)
movq (%rbx), %r13
cmpq %r12, %rbx
je 0x1eccb
movq (%r12), %rax
movq %rax, (%rbx)
movq %r13, (%r12)
movq %r14, %rbx
movq %r12, %rbp
movq %r12, %r15
subq %rbx, %r15
movq 0x40(%rsp), %rax
leaq (%rax,%rbx), %r14
movq 0x8(%rbx), %rdi
addq $0x8, %rbx
movq %r13, %rsi
callq 0x1e81a
addq $-0x8, %r15
addq $0x8, %r14
testl %eax, %eax
jne 0x1ece0
addq $-0x8, %rbp
movq %rbp, %rcx
movq %rcx, %rbp
cmpq %rcx, %rbx
ja 0x1ed1f
movq (%rbp), %rsi
movq %r13, %rdi
callq 0x1e81a
leaq -0x8(%rbp), %rcx
testl %eax, %eax
jne 0x1ed03
movq (%rbx), %rax
cmpq %rbx, %rbp
jbe 0x1ed34
movq (%rbp), %rcx
movq %rcx, (%rbx)
movq %rax, (%rbp)
jmp 0x1ecd1
movq (%r12), %rcx
movq %rcx, (%rbx)
movq %rax, (%r12)
sarq $0x3, %r14
sarq $0x3, %r15
leaq -0x8(%rbx), %rax
cmpq %r15, %r14
jg 0x1edc5
cmpq $0x11, %r15
movl 0x14(%rsp), %ebp
jl 0x1ed76
addq $0x8, %rbx
movq 0x8(%rsp), %rcx
movq %rbx, (%rcx)
movq %r12, 0x8(%rcx)
movl %ebp, 0x10(%rcx)
addq $0x18, %rcx
movq %rcx, 0x8(%rsp)
cmpq $0x11, %r14
movq 0x20(%rsp), %r14
cmovlq %r14, %rax
movq %rax, %r12
jmp 0x1ec56
subq %r14, %r12
sarq $0x3, %r12
incq %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x1ea62
movq 0x8(%rsp), %rax
cmpq 0x18(%rsp), %rax
je 0x1ee00
movq -0x18(%rax), %r14
movq -0x10(%rax), %r12
movl -0x8(%rax), %ebp
addq $-0x18, %rax
movq %rax, 0x8(%rsp)
jmp 0x1ec3d
cmpq $0x11, %r14
movl 0x14(%rsp), %ebp
jl 0x1edec
movq 0x8(%rsp), %rcx
movq 0x20(%rsp), %rdx
movq %rdx, (%rcx)
movq %rax, 0x8(%rcx)
movl %ebp, 0x10(%rcx)
addq $0x18, %rcx
movq %rcx, 0x8(%rsp)
addq $0x8, %rbx
cmpq $0x11, %r15
cmovlq %r12, %rbx
movq %rbx, %r14
jmp 0x1ec3d
movq 0x18(%rsp), %rdi
callq 0x32c0
movq 0x30(%rsp), %r15
leaq 0x8(%r15), %rbx
movq 0x28(%rsp), %r12
cmpq %r12, %rbx
jae 0x1ebdd
cmpq %r15, %rbx
jbe 0x1ee53
movq (%rbx), %rdi
movq %rbx, %r14
movq -0x8(%r14), %rsi
callq 0x1e81a
testl %eax, %eax
je 0x1ee53
movq -0x8(%r14), %rax
movq (%r14), %rdi
movq %rax, (%r14)
movq %rdi, -0x8(%r14)
leaq -0x8(%r14), %rax
movq %rax, %r14
cmpq %r15, %rax
ja 0x1ee2c
addq $0x8, %rbx
jmp 0x1ee18