// File: /workspace/github_c_makefile_star2_O1/genometools[P]genometools/src/external/samtools-0.1.18/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, mode[3];
	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);
		strcpy(mode, "w1");
	} else {
		sprintf(name, "%s.bam", prefix);
		strcpy(mode, "w");
	}
	fp = is_stdout? bam_dopen(fileno(stdout), mode) : bam_open(name, mode);
	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, 0x10(%rsp)
testq %rdi, %rdi
je 0x303be2
movq %rdi, %r12
cmpq $0x2, %rdi
jne 0x303bf1
movq 0x10(%rsp), %rax
movq (%rax), %rbx
movq 0x8(%rax), %r14
cmpl $0x0, 0x1967cf(%rip)    # 0x49a364
je 0x303baa
movq 0x30(%r14), %rdi
movq 0x30(%rbx), %rsi
callq 0x30478d
testl %eax, %eax
js 0x303bd0
jne 0x303be2
movl (%r14), %eax
shlq $0x20, %rax
movslq 0x4(%r14), %rcx
incq %rcx
orq %rax, %rcx
movl (%rbx), %eax
shlq $0x20, %rax
movslq 0x4(%rbx), %rdx
incq %rdx
orq %rax, %rdx
cmpq %rdx, %rcx
jae 0x303be2
movq 0x10(%rsp), %rax
movdqu (%rax), %xmm0
pshufd $0x4e, %xmm0, %xmm0     # xmm0 = xmm0[2,3,0,1]
movdqu %xmm0, (%rax)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x2, %ecx
movl $0xf0, %edi
movl $0x2, %ebx
movl $0x1, %eax
shlq %cl, %rax
incq %rcx
addq $0xc0, %rdi
addl $0x2, %ebx
cmpq %r12, %rax
jb 0x303c00
callq 0x32008
movq 0x10(%rsp), %rdx
leaq (%rdx,%r12,8), %rcx
movq %rcx, 0x28(%rsp)
leaq (%rdx,%r12,8), %r13
addq $-0x8, %r13
movq %rax, 0x18(%rsp)
movq %rax, (%rsp)
movq %rdx, %r12
leaq 0x8(%r12), %rax
movq %rax, 0x30(%rsp)
movl $0x8, %eax
subq %r12, %rax
movq %rax, 0x38(%rsp)
movq %r12, 0x20(%rsp)
cmpq %r13, %r12
jae 0x303eed
decl %ebx
je 0x303ed8
movl %ebx, 0xc(%rsp)
movq %r13, %rbx
subq %r12, %rbx
sarq $0x4, %rbx
movq 0x8(%r12,%rbx,8), %r15
movq (%r12), %r14
cmpl $0x0, 0x1966d8(%rip)    # 0x49a364
je 0x303ca5
movq 0x30(%r15), %rdi
movq 0x30(%r14), %rsi
callq 0x30478d
testl %eax, %eax
js 0x303ccc
jne 0x303d2d
movl (%r15), %eax
shlq $0x20, %rax
movslq 0x4(%r15), %rcx
incq %rcx
orq %rax, %rcx
movl (%r14), %eax
shlq $0x20, %rax
movslq 0x4(%r14), %rdx
incq %rdx
orq %rax, %rdx
cmpq %rdx, %rcx
jae 0x303d2d
movq 0x30(%rsp), %rax
leaq (%rax,%rbx,8), %rbx
movq (%rbx), %r15
movq (%r13), %r14
cmpl $0x0, 0x196681(%rip)    # 0x49a364
je 0x303cff
movq 0x30(%r15), %rdi
movq 0x30(%r14), %rsi
callq 0x30478d
movq %r13, %rcx
testl %eax, %eax
js 0x303d82
jne 0x303d28
movl (%r15), %eax
shlq $0x20, %rax
movslq 0x4(%r15), %rcx
incq %rcx
orq %rax, %rcx
movl (%r14), %eax
shlq $0x20, %rax
movslq 0x4(%r14), %rdx
incq %rdx
orq %rax, %rdx
cmpq %rdx, %rcx
cmovbq %r13, %rbx
movq %rbx, %rcx
jmp 0x303d82
movq (%r13), %r14
movq (%r12), %rbx
cmpl $0x0, 0x196628(%rip)    # 0x49a364
je 0x303d57
movq 0x30(%r14), %rdi
movq 0x30(%rbx), %rsi
callq 0x30478d
movq %r12, %rcx
testl %eax, %eax
js 0x303d82
movq %r13, %rcx
jne 0x303d82
movl (%r14), %eax
shlq $0x20, %rax
movslq 0x4(%r14), %rcx
incq %rcx
orq %rax, %rcx
movl (%rbx), %eax
shlq $0x20, %rax
movslq 0x4(%rbx), %rdx
incq %rdx
orq %rax, %rdx
cmpq %rdx, %rcx
movq %r13, %rcx
cmovbq %r12, %rcx
movq (%rcx), %r15
cmpq %r13, %rcx
je 0x303d95
movq (%r13), %rax
movq %rax, (%rcx)
movq %r15, (%r13)
movq %r13, 0x40(%rsp)
xorl %r14d, %r14d
movq 0x8(%r12,%r14), %rbx
cmpl $0x0, 0x1965bb(%rip)    # 0x49a364
je 0x303dbe
movq 0x30(%rbx), %rdi
movq 0x30(%r15), %rsi
callq 0x30478d
testl %eax, %eax
js 0x303de4
jne 0x303dea
movl (%rbx), %eax
shlq $0x20, %rax
movslq 0x4(%rbx), %rcx
incq %rcx
orq %rax, %rcx
movl (%r15), %eax
shlq $0x20, %rax
movslq 0x4(%r15), %rdx
incq %rdx
orq %rax, %rdx
cmpq %rdx, %rcx
jae 0x303dea
addq $0x8, %r14
jmp 0x303d9d
addq $-0x8, %r13
leaq (%r12,%r14), %rbx
addq $0x8, %rbx
cmpq %r13, %rbx
ja 0x303e48
movq (%r13), %rbp
cmpl $0x0, 0x19655e(%rip)    # 0x49a364
je 0x303e1b
movq 0x30(%r15), %rdi
movq 0x30(%rbp), %rsi
callq 0x30478d
testl %eax, %eax
js 0x303e42
jne 0x303e48
movl (%r15), %eax
shlq $0x20, %rax
movslq 0x4(%r15), %rcx
incq %rcx
orq %rax, %rcx
movl (%rbp), %eax
shlq $0x20, %rax
movslq 0x4(%rbp), %rdx
incq %rdx
orq %rax, %rdx
cmpq %rdx, %rcx
jae 0x303e48
addq $-0x8, %r13
jmp 0x303df6
movq (%rbx), %rcx
cmpq %rbx, %r13
jbe 0x303e63
movq (%r13), %rax
movq %rax, (%rbx)
movq %rcx, (%r13)
movq %rbx, %r12
jmp 0x303d9a
movq 0x40(%rsp), %r13
movq (%r13), %rdx
leaq (%r12,%r14), %rax
movq %rdx, 0x8(%r12,%r14)
movq %rcx, (%r13)
movq 0x38(%rsp), %rcx
addq %rax, %rcx
sarq $0x3, %rcx
movq %r13, %rdx
subq %r12, %rdx
subq %r14, %rdx
addq $-0x8, %rdx
sarq $0x3, %rdx
cmpq %rdx, %rcx
jg 0x303f10
cmpq $0x11, %rdx
movl 0xc(%rsp), %ebx
jl 0x303ec3
leaq (%r12,%r14), %rdx
addq $0x10, %rdx
movq (%rsp), %rsi
movq %rdx, (%rsi)
movq %r13, 0x8(%rsi)
movl %ebx, 0x10(%rsi)
addq $0x18, %rsi
movq %rsi, (%rsp)
cmpq $0x11, %rcx
movq 0x20(%rsp), %r12
cmovlq %r12, %rax
movq %rax, %r13
jmp 0x303c5d
subq %r12, %r13
sarq $0x3, %r13
incq %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x303a14
movq (%rsp), %rax
cmpq 0x18(%rsp), %rax
je 0x303f47
movq -0x18(%rax), %r12
movq -0x10(%rax), %r13
movl -0x8(%rax), %ebx
addq $-0x18, %rax
movq %rax, (%rsp)
jmp 0x303c41
cmpq $0x11, %rcx
movl 0xc(%rsp), %ebx
jl 0x303f35
movq (%rsp), %rcx
movq 0x20(%rsp), %rsi
movq %rsi, (%rcx)
movq %rax, 0x8(%rcx)
movl %ebx, 0x10(%rcx)
addq $0x18, %rcx
movq %rcx, (%rsp)
cmpq $0x11, %rdx
leaq 0x10(%r12,%r14), %r12
cmovlq %r13, %r12
jmp 0x303c41
movq 0x18(%rsp), %rdi
callq 0x32018
movq 0x10(%rsp), %rdi
movq 0x28(%rsp), %rsi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x303973