// File: /workspace/github_cpp_makefile_star2_O1/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 $0x48, %rsp
movl %r8d, 0x10(%rsp)
movq %rcx, %r14
movq %rdx, %r12
movq %rsi, %r15
movl %edi, %ebp
movl %edi, 0x10f5a(%rip)     # 0x30324
leaq 0x59e5(%rip), %rsi      # 0x24db6
movq %r15, %rdi
callq 0x34e0
testl %eax, %eax
je 0x1f3ee
leaq 0x8be3(%rip), %rsi      # 0x27fc7
movq %r15, %rdi
callq 0xfa04
jmp 0x1f40b
movq 0xfbdb(%rip), %rax      # 0x2efd0
movq (%rax), %rdi
callq 0x3030
leaq 0x8bc3(%rip), %rsi      # 0x27fc7
movl %eax, %edi
callq 0xfb6d
movq %rax, %rbx
testq %rax, %rax
je 0x1f552
movl %ebp, 0x2c(%rsp)
movq %r12, 0x20(%rsp)
movq %rbx, %rdi
callq 0x11d21
movq %rax, 0x18(%rsp)
movq %r14, %r12
shrq $0x5, %r12
movl $0x8, %esi
movq %r12, %rdi
callq 0x3530
movq %rax, %r13
movq $0x0, 0x8(%rsp)
movl $0x1, %eax
movq %rax, 0x30(%rsp)
xorl %ebp, %ebp
xorl %r15d, %r15d
cmpq $0x0, (%r13,%rbp,8)
jne 0x1f478
movl $0x1, %edi
movl $0x38, %esi
callq 0x3530
movq %rax, (%r13,%rbp,8)
movq (%r13,%rbp,8), %rsi
movq %rbx, %rdi
callq 0x1204b
testl %eax, %eax
js 0x1f4c7
movl %eax, %eax
addq %rax, %r15
incq %rbp
cmpq %r14, %r15
jb 0x1f45c
movq 0x8(%rsp), %rdi
leal 0x1(%rdi), %r15d
movl %ebp, %esi
movq %r13, %rdx
movq 0x20(%rsp), %rcx
movq 0x18(%rsp), %r8
movl 0x10(%rsp), %r9d
callq 0x1f6bb
incq 0x30(%rsp)
movl %r15d, %eax
movq %rax, 0x8(%rsp)
jmp 0x1f457
cmpl $-0x1, %eax
jne 0x1f57b
movq 0x8(%rsp), %r15
testl %r15d, %r15d
jne 0x1f585
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movl %ebp, %esi
movq %r13, %rdx
movq 0x20(%rsp), %rcx
movq 0x18(%rsp), %r8
movl 0x10(%rsp), %r9d
callq 0x1f6bb
cmpq $0x20, %r14
jb 0x1f52a
xorl %r14d, %r14d
movq (%r13,%r14,8), %rax
testq %rax, %rax
je 0x1f522
movq 0x30(%rax), %rdi
callq 0x32c0
movq (%r13,%r14,8), %rdi
callq 0x32c0
incq %r14
cmpq %r14, %r12
jne 0x1f505
movq %r13, %rdi
callq 0x32c0
movq 0x18(%rsp), %rdi
callq 0x11c9f
movq %rbx, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1050e
movq 0xfa8f(%rip), %rax      # 0x2efe8
movq (%rax), %rdi
leaq 0x85c1(%rip), %rsi      # 0x27b24
movq %r15, %rdx
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3630
callq 0x4d15
jmp 0x1f4d0
movq 0xfa5c(%rip), %rax      # 0x2efe8
movq (%rax), %rdi
leal 0x1(%r15), %edx
leaq 0x85e2(%rip), %rsi      # 0x27b7c
movl %edx, 0x14(%rsp)
xorl %eax, %eax
callq 0x3630
movl %r15d, %edi
movl %ebp, %esi
movq %r13, %rdx
movq 0x20(%rsp), %rbp
movq %rbp, %rcx
movq 0x18(%rsp), %r8
movl 0x10(%rsp), %r15d
movl %r15d, %r9d
callq 0x1f6bb
movq %rbp, %rdi
callq 0x32d0
leaq 0x14(%rax), %rdi
movl $0x1, %esi
callq 0x3530
testl %r15d, %r15d
movq %r12, 0x40(%rsp)
movq %rax, 0x38(%rsp)
je 0x1f5f3
movw $0x2d, (%rax)
jmp 0x1f607
leaq 0x85ac(%rip), %rsi      # 0x27ba6
movq %rax, %rdi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x3350
movl 0x14(%rsp), %edi
movl $0x8, %esi
callq 0x3530
movq %rax, 0x8(%rsp)
xorl %r15d, %r15d
movq 0x30(%rsp), %r12
movq %rbp, %rdi
callq 0x32d0
leaq 0x14(%rax), %rdi
movl $0x1, %esi
callq 0x3530
movq 0x8(%rsp), %rcx
movq %rax, (%rcx,%r15,8)
movq %rax, %rdi
leaq 0x8562(%rip), %rsi      # 0x27bad
movq %rbp, %rdx
movl %r15d, %ecx
xorl %eax, %eax
callq 0x3350
incq %r15
cmpq %r15, %r12
jne 0x1f622
movl 0x2c(%rsp), %edi
movq 0x38(%rsp), %r15
movq %r15, %rsi
xorl %edx, %edx
movl 0x14(%rsp), %ecx
movq 0x8(%rsp), %rbp
movq %rbp, %r8
xorl %r9d, %r9d
callq 0x1de28
movq %r15, %rdi
callq 0x32c0
movq (%rbp), %r15
movq %r15, %rdi
callq 0x31e0
movq %r15, %rdi
callq 0x32c0
addq $0x8, %rbp
decq %r12
jne 0x1f68a
movq 0x8(%rsp), %rdi
callq 0x32c0
movq 0x40(%rsp), %r12
jmp 0x1f4fc