// File: /workspace/github_cpp_makefile_star2_O3/statgen[P]statgen/lib/samtools-0.1.7a-hybrid/bam_sort.c
// Function: bam_sort_core_ext
void bam_sort_core_ext(int is_by_qname, const char *fn, const char *prefix, size_t max_mem, int is_stdout)
{
	int n, ret, k, i;
	size_t mem;
	bam_header_t *header;
	bamFile fp;
	bam1_t *b, **buf;

	g_is_by_qname = is_by_qname;
	n = k = 0; mem = 0;
	fp = strcmp(fn, "-")? bam_open(fn, "r") : bam_dopen(fileno(stdin), "r");
	if (fp == 0) {
		fprintf(stderr, "[bam_sort_core] fail to open file %s\n", fn);
		return;
	}
	header = bam_header_read(fp);
	buf = (bam1_t**)calloc(max_mem / BAM_CORE_SIZE, sizeof(bam1_t*));
	// write sub files
	for (;;) {
		if (buf[k] == 0) buf[k] = (bam1_t*)calloc(1, sizeof(bam1_t));
		b = buf[k];
		if ((ret = bam_read1(fp, b)) < 0) break;
		mem += ret;
		++k;
		if (mem >= max_mem) {
			sort_blocks(n++, k, buf, prefix, header, is_stdout);
			mem = 0; k = 0;
		}
	}
	if (ret != -1)
		fprintf(stderr, "[bam_sort_core] truncated file. Continue anyway.\n");
	if (n == 0) sort_blocks(-1, k, buf, prefix, header, is_stdout);
	else { // then merge
		char **fns, *fnout;
		fprintf(stderr, "[bam_sort_core] merging from %d files...\n", n+1);
		sort_blocks(n++, k, buf, prefix, header, is_stdout);
		fnout = (char*)calloc(strlen(prefix) + 20, 1);
		if (is_stdout) sprintf(fnout, "-");
		else sprintf(fnout, "%s.bam", prefix);
		fns = (char**)calloc(n, sizeof(char*));
		for (i = 0; i < n; ++i) {
			fns[i] = (char*)calloc(strlen(prefix) + 20, 1);
			sprintf(fns[i], "%s.%.4d.bam", prefix, i);
		}
		bam_merge_core(is_by_qname, fnout, 0, n, fns, 0);
		free(fnout);
		for (i = 0; i < n; ++i) {
			unlink(fns[i]);
			free(fns[i]);
		}
		free(fns);
	}
	for (k = 0; k < max_mem / BAM_CORE_SIZE; ++k) {
		if (buf[k]) {
			free(buf[k]->data);
			free(buf[k]);
		}
	}
	free(buf);
	bam_header_destroy(header);
	bam_close(fp);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %r8d, 0x8(%rsp)
movq %rcx, 0x20(%rsp)
movq %rdx, %rbx
movq %rsi, %r12
movl %edi, %ebp
movl %edi, 0x1035d(%rip)     # 0x2f324
cmpb $0x2d, (%rsi)
jne 0x1efd8
cmpb $0x0, 0x1(%r12)
je 0x1f12f
leaq 0x80e8(%rip), %rsi      # 0x270c7
movq %r12, %rdi
callq 0x10048
movq %rax, %r14
testq %rax, %rax
je 0x1f151
movl %ebp, 0xc(%rsp)
movq %r14, %rdi
callq 0x12383
movq %rax, 0x10(%rsp)
movq 0x20(%rsp), %rdi
shrq $0x5, %rdi
movl $0x8, %esi
movq %rdi, 0x30(%rsp)
callq 0x3530
movq %rax, %r13
xorl %esi, %esi
xorl %r15d, %r15d
xorl %ebp, %ebp
xorl %r12d, %r12d
movq %r14, 0x18(%rsp)
testq %rsi, %rsi
jne 0x1f052
movslq %ebp, %r14
movl $0x1, %edi
movl $0x38, %esi
callq 0x3530
movq %rax, %rsi
movq %rax, (%r13,%r14,8)
movq 0x18(%rsp), %r14
movq %r14, %rdi
callq 0x126b5
testl %eax, %eax
js 0x1f0a2
movl %eax, %eax
addq %rax, %r15
incl %ebp
cmpq 0x20(%rsp), %r15
jb 0x1f098
leal 0x1(%r12), %r14d
movl %r12d, %edi
movl %ebp, %esi
movq %r13, %rdx
movq %rbx, %rcx
movq 0x10(%rsp), %r8
movl 0x8(%rsp), %r9d
callq 0x1f2d0
xorl %r15d, %r15d
xorl %ebp, %ebp
movl %r14d, %r12d
movq 0x18(%rsp), %r14
movslq %ebp, %rax
movq (%r13,%rax,8), %rsi
jmp 0x1f02e
cmpl $-0x1, %eax
jne 0x1f17a
testl %r12d, %r12d
jne 0x1f184
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movl %ebp, %esi
movq %r13, %rdx
movq %rbx, %rcx
movq 0x10(%rsp), %r8
movl 0x8(%rsp), %r9d
callq 0x1f2d0
cmpq $0x20, 0x20(%rsp)
movq 0x30(%rsp), %r15
jb 0x1f105
xorl %r14d, %r14d
movq (%r13,%r14,8), %rax
testq %rax, %rax
je 0x1f0fd
movq 0x30(%rax), %rdi
callq 0x32c0
movq (%r13,%r14,8), %rdi
callq 0x32c0
incq %r14
cmpq %r14, %r15
jne 0x1f0e0
movq %r13, %rdi
callq 0x32c0
movq 0x10(%rsp), %rdi
callq 0x122ff
movq 0x18(%rsp), %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x10d25
movq 0xee9a(%rip), %rax      # 0x2dfd0
movq (%rax), %rdi
callq 0x3030
leaq 0x7f82(%rip), %rsi      # 0x270c7
movl %eax, %edi
callq 0x101a3
jmp 0x1efe7
movq 0xee90(%rip), %rax      # 0x2dfe8
movq (%rax), %rdi
leaq 0x7ac2(%rip), %rsi      # 0x26c24
movq %r12, %rdx
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3630
callq 0x4fbc
jmp 0x1f0ab
movq 0xee5d(%rip), %rax      # 0x2dfe8
movq (%rax), %rdi
leal 0x1(%r12), %r15d
leaq 0x7ae2(%rip), %rsi      # 0x26c7c
movl %r15d, %edx
xorl %eax, %eax
callq 0x3630
movl %r12d, %edi
movl %ebp, %esi
movq %r13, %rdx
movq %rbx, %rcx
movq 0x10(%rsp), %r8
movl 0x8(%rsp), %ebp
movl %ebp, %r9d
callq 0x1f2d0
movq %rbx, %rdi
callq 0x32d0
leaq 0x14(%rax), %rdi
movl $0x1, %esi
callq 0x3530
testl %ebp, %ebp
movq %rax, 0x28(%rsp)
je 0x1f1e6
movw $0x2d, (%rax)
jmp 0x1f1fa
leaq 0x7ab9(%rip), %rsi      # 0x26ca6
movq %rax, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x3350
movslq %r15d, %rdi
movl $0x8, %esi
callq 0x3530
movq %rax, %r14
testl %r12d, %r12d
js 0x1f29f
movl %r15d, 0x8(%rsp)
movl %r15d, %r15d
xorl %ebp, %ebp
movq %rbx, %rdi
callq 0x32d0
leaq 0x14(%rax), %rdi
movl $0x1, %esi
callq 0x3530
movq %rax, (%r14,%rbp,8)
movq %rax, %rdi
leaq 0x7a6c(%rip), %rsi      # 0x26cad
movq %rbx, %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x3350
incq %rbp
cmpq %rbp, %r15
jne 0x1f21d
movl 0xc(%rsp), %edi
movq 0x28(%rsp), %rbx
movq %rbx, %rsi
xorl %edx, %edx
movl 0x8(%rsp), %ecx
movq %r14, %r8
xorl %r9d, %r9d
callq 0x1dd9f
movq %rbx, %rdi
callq 0x32c0
testl %r12d, %r12d
js 0x1f2c3
xorl %ebp, %ebp
movq (%r14,%rbp,8), %r12
movq %r12, %rdi
callq 0x31e0
movq %r12, %rdi
callq 0x32c0
incq %rbp
cmpq %rbp, %r15
jne 0x1f281
jmp 0x1f2c3
movl 0xc(%rsp), %edi
movq 0x28(%rsp), %rbx
movq %rbx, %rsi
xorl %edx, %edx
movl %r15d, %ecx
movq %r14, %r8
xorl %r9d, %r9d
callq 0x1dd9f
movq %rbx, %rdi
callq 0x32c0
movq %r14, %rdi
callq 0x32c0
jmp 0x1f0d0