// File: /workspace/github_cpp_makefile_star2_O1/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, (%rsp)
testq %rdi, %rdi
je 0x1ed35
movq %rdi, %r15
cmpq $0x2, %rdi
jne 0x1ed44
movq (%rsp), %rax
movq (%rax), %rbx
movq 0x8(%rax), %r14
cmpl $0x0, 0x11636(%rip)     # 0x30324
je 0x1ed03
movq 0x30(%r14), %rdi
movq 0x30(%rbx), %rsi
callq 0x1f89e
testl %eax, %eax
js 0x1ed23
jne 0x1ed35
movl (%r14), %eax
shlq $0x20, %rax
movslq 0x4(%r14), %rcx
orq %rax, %rcx
movl (%rbx), %eax
shlq $0x20, %rax
movslq 0x4(%rbx), %rdx
orq %rax, %rdx
cmpq %rdx, %rcx
jae 0x1ed35
movq (%rsp), %rdx
movq (%rdx), %rax
movq 0x8(%rdx), %rcx
movq %rcx, (%rdx)
movq %rax, 0x8(%rdx)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x2, %ecx
movl $0xf0, %edi
movl $0x2, %ebp
movl $0x1, %eax
shlq %cl, %rax
incq %rcx
addq $0xc0, %rdi
addl $0x2, %ebp
cmpq %r15, %rax
jb 0x1ed53
callq 0x31d0
movq (%rsp), %r12
leaq (%r12,%r15,8), %rcx
movq %rcx, 0x18(%rsp)
leaq (%r12,%r15,8), %r13
addq $-0x8, %r13
movq %rax, 0x20(%rsp)
movq %rax, 0x8(%rsp)
leaq 0x8(%r12), %rax
movq %rax, 0x30(%rsp)
movl $0x8, %eax
subq %r12, %rax
movq %rax, 0x38(%rsp)
movq %r12, 0x28(%rsp)
cmpq %r13, %r12
jae 0x1f05b
decl %ebp
je 0x1f046
movl %ebp, 0x14(%rsp)
movq %r13, %rbx
subq %r12, %rbx
sarq $0x4, %rbx
movq 0x8(%r12,%rbx,8), %r15
movq (%r12), %r14
cmpl $0x0, 0x11548(%rip)     # 0x30324
je 0x1edf1
movq 0x30(%r15), %rdi
movq 0x30(%r14), %rsi
callq 0x1f89e
testl %eax, %eax
js 0x1ee12
jne 0x1ee69
movl (%r15), %eax
shlq $0x20, %rax
movslq 0x4(%r15), %rcx
orq %rax, %rcx
movl (%r14), %eax
shlq $0x20, %rax
movslq 0x4(%r14), %rdx
orq %rax, %rdx
cmpq %rdx, %rcx
jae 0x1ee69
movq 0x30(%rsp), %rax
leaq (%rax,%rbx,8), %rbx
movq (%rbx), %r15
movq (%r13), %r14
cmpl $0x0, 0x114fb(%rip)     # 0x30324
je 0x1ee41
movq 0x30(%r15), %rdi
movq 0x30(%r14), %rsi
callq 0x1f89e
movq %r13, %rcx
testl %eax, %eax
js 0x1eeb8
jne 0x1ee64
movl (%r15), %eax
shlq $0x20, %rax
movslq 0x4(%r15), %rcx
orq %rax, %rcx
movl (%r14), %eax
shlq $0x20, %rax
movslq 0x4(%r14), %rdx
orq %rax, %rdx
cmpq %rdx, %rcx
cmovbq %r13, %rbx
movq %rbx, %rcx
jmp 0x1eeb8
movq (%r13), %r14
movq (%r12), %rbx
cmpl $0x0, 0x114ac(%rip)     # 0x30324
je 0x1ee93
movq 0x30(%r14), %rdi
movq 0x30(%rbx), %rsi
callq 0x1f89e
movq %r12, %rcx
testl %eax, %eax
js 0x1eeb8
movq %r13, %rcx
jne 0x1eeb8
movl (%r14), %eax
shlq $0x20, %rax
movslq 0x4(%r14), %rcx
orq %rax, %rcx
movl (%rbx), %eax
shlq $0x20, %rax
movslq 0x4(%rbx), %rdx
orq %rax, %rdx
cmpq %rdx, %rcx
movq %r13, %rcx
cmovbq %r12, %rcx
movq (%rcx), %r14
cmpq %r13, %rcx
je 0x1eecb
movq (%r13), %rax
movq %rax, (%rcx)
movq %r14, (%r13)
movq %r12, %r15
movq %r13, 0x40(%rsp)
movq %r13, %r12
xorl %ebx, %ebx
movq 0x8(%r15,%rbx), %r13
cmpl $0x0, 0x11440(%rip)     # 0x30324
je 0x1eef9
movq 0x30(%r13), %rdi
movq 0x30(%r14), %rsi
callq 0x1f89e
testl %eax, %eax
js 0x1ef1b
jne 0x1ef21
movl (%r13), %eax
shlq $0x20, %rax
movslq 0x4(%r13), %rcx
orq %rax, %rcx
movl (%r14), %eax
shlq $0x20, %rax
movslq 0x4(%r14), %rdx
orq %rax, %rdx
cmpq %rdx, %rcx
jae 0x1ef21
addq $0x8, %rbx
jmp 0x1eed8
addq $-0x8, %r12
leaq (%r15,%rbx), %r13
addq $0x8, %r13
cmpq %r12, %r13
ja 0x1ef79
movq (%r12), %rbp
cmpl $0x0, 0x113e7(%rip)     # 0x30324
je 0x1ef52
movq 0x30(%r14), %rdi
movq 0x30(%rbp), %rsi
callq 0x1f89e
testl %eax, %eax
js 0x1ef73
jne 0x1ef79
movl (%r14), %eax
shlq $0x20, %rax
movslq 0x4(%r14), %rcx
orq %rax, %rcx
movl (%rbp), %eax
shlq $0x20, %rax
movslq 0x4(%rbp), %rdx
orq %rax, %rdx
cmpq %rdx, %rcx
jae 0x1ef79
addq $-0x8, %r12
jmp 0x1ef2d
movq (%r13), %rcx
cmpq %r13, %r12
jbe 0x1ef96
movq (%r12), %rax
movq %rax, (%r13)
movq %rcx, (%r12)
movq %r13, %r15
jmp 0x1eed6
movq 0x40(%rsp), %r13
movq (%r13), %rdx
leaq (%r15,%rbx), %rax
movq %rdx, 0x8(%r15,%rbx)
movq %rcx, (%r13)
movq 0x38(%rsp), %rcx
addq %rax, %rcx
sarq $0x3, %rcx
movq %r13, %rdx
subq %r15, %rdx
subq %rbx, %rdx
addq $-0x8, %rdx
sarq $0x3, %rdx
cmpq %rdx, %rcx
jg 0x1f00d
cmpq $0x11, %rdx
movl 0x14(%rsp), %ebp
movq 0x28(%rsp), %r12
jl 0x1effd
leaq (%r15,%rbx), %rdx
addq $0x10, %rdx
movq 0x8(%rsp), %rsi
movq %rdx, (%rsi)
movq %r13, 0x8(%rsi)
movl %ebp, 0x10(%rsi)
addq $0x18, %rsi
movq %rsi, 0x8(%rsp)
cmpq $0x11, %rcx
cmovlq %r12, %rax
movq %rax, %r13
jmp 0x1edad
cmpq $0x11, %rcx
movl 0x14(%rsp), %ebp
movq 0x28(%rsp), %rsi
jl 0x1f034
movq 0x8(%rsp), %rcx
movq %rsi, (%rcx)
movq %rax, 0x8(%rcx)
movl %ebp, 0x10(%rcx)
addq $0x18, %rcx
movq %rcx, 0x8(%rsp)
cmpq $0x11, %rdx
leaq 0x10(%r15,%rbx), %r12
cmovlq %r13, %r12
jmp 0x1ed91
subq %r12, %r13
sarq $0x3, %r13
incq %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x1eb0d
movq 0x8(%rsp), %rax
cmpq 0x20(%rsp), %rax
je 0x1f07b
movq -0x18(%rax), %r12
movq -0x10(%rax), %r13
movl -0x8(%rax), %ebp
addq $-0x18, %rax
jmp 0x1ed8c
movq 0x20(%rsp), %rdi
callq 0x32c0
movq (%rsp), %rax
leaq 0x8(%rax), %rbx
cmpq 0x18(%rsp), %rbx
jae 0x1ed35
cmpq (%rsp), %rbx
jbe 0x1f103
movq %rbx, %r14
movq -0x8(%r14), %r15
movq (%r14), %r12
cmpl $0x0, 0x11275(%rip)     # 0x30324
je 0x1f0c5
movq 0x30(%r12), %rdi
movq 0x30(%r15), %rsi
callq 0x1f89e
testl %eax, %eax
js 0x1f0e8
jne 0x1f103
movl (%r12), %eax
shlq $0x20, %rax
movslq 0x4(%r12), %rcx
orq %rax, %rcx
movl (%r15), %eax
shlq $0x20, %rax
movslq 0x4(%r15), %rdx
orq %rax, %rdx
cmpq %rdx, %rcx
jae 0x1f103
movq -0x8(%r14), %rax
movq (%r14), %rcx
movq %rax, (%r14)
movq %rcx, -0x8(%r14)
leaq -0x8(%r14), %rax
movq %rax, %r14
cmpq (%rsp), %rax
ja 0x1f0a1
addq $0x8, %rbx
jmp 0x1f08d