// File: /workspace/github_c_makefile_star2_O3/genometools[P]genometools/src/external/samtools-0.1.18/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, 0);
			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, 0);
		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, 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, %r12d
movq %rcx, 0x30(%rsp)
movq %rdx, %rbx
movq %rsi, %r15
movl %edi, %ebp
movl %edi, 0x194c6e(%rip)    # 0x490364
cmpb $0x2d, (%rsi)
jne 0x2fb706
cmpb $0x0, 0x1(%r15)
je 0x2fb862
leaq 0x43f2e(%rip), %rsi     # 0x33f63b
movq %r15, %rdi
callq 0x2e5723
movq %rax, %r14
testq %rax, %rax
je 0x2fb884
movl %r12d, 0x18(%rsp)
movl %ebp, 0x14(%rsp)
movq %r14, %rdi
callq 0x2e811e
movq %rax, 0x20(%rsp)
movq 0x30(%rsp), %rdi
shrq $0x5, %rdi
movl $0x8, %esi
movq %rdi, 0x40(%rsp)
callq 0x30ed0
movq %rax, %r13
xorl %esi, %esi
xorl %ebp, %ebp
xorl %r12d, %r12d
xorl %r15d, %r15d
movq %r14, 0x28(%rsp)
testq %rsi, %rsi
jne 0x2fb785
movslq %r12d, %r14
movl $0x1, %edi
movl $0x38, %esi
callq 0x30ed0
movq %rax, %rsi
movq %rax, (%r13,%r14,8)
movq 0x28(%rsp), %r14
movq %r14, %rdi
callq 0x2e8452
testl %eax, %eax
js 0x2fb7d4
movl %eax, %eax
addq %rax, %rbp
incl %r12d
cmpq 0x30(%rsp), %rbp
jb 0x2fb7ca
leal 0x1(%r15), %r14d
xorl %ebp, %ebp
movl %r15d, %edi
movl %r12d, %esi
movq %r13, %rdx
movq %rbx, %rcx
movq 0x20(%rsp), %r8
xorl %r9d, %r9d
callq 0x2fba11
xorl %r12d, %r12d
movl %r14d, %r15d
movq 0x28(%rsp), %r14
movslq %r12d, %rax
movq (%r13,%rax,8), %rsi
jmp 0x2fb761
cmpl $-0x1, %eax
jne 0x2fb8ad
testl %r15d, %r15d
jne 0x2fb8b7
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movl %r12d, %esi
movq %r13, %rdx
movq %rbx, %rcx
movq 0x20(%rsp), %r8
movl 0x18(%rsp), %r9d
callq 0x2fba11
cmpq $0x20, 0x30(%rsp)
movq 0x40(%rsp), %r15
jb 0x2fb838
xorl %r14d, %r14d
movq (%r13,%r14,8), %rax
testq %rax, %rax
je 0x2fb830
movq 0x30(%rax), %rdi
callq 0x32018
movq (%r13,%r14,8), %rdi
callq 0x32018
incq %r14
cmpq %r14, %r15
jne 0x2fb813
movq %r13, %rdi
callq 0x32018
movq 0x20(%rsp), %rdi
callq 0x2e809a
movq 0x28(%rsp), %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2e582b
movq 0x18a6df(%rip), %rax    # 0x485f48
movq (%rax), %rdi
callq 0x2d230
leaq 0x43dc3(%rip), %rsi     # 0x33f63b
movl %eax, %edi
callq 0x2e5a78
jmp 0x2fb715
movq 0x18a735(%rip), %rax    # 0x485fc0
movq (%rax), %rdi
leaq 0xbd94d(%rip), %rsi     # 0x3b91e2
movq %r15, %rdx
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x31920
callq 0xa9529
jmp 0x2fb7dd
movq 0x18a702(%rip), %rax    # 0x485fc0
movq (%rax), %rdi
leal 0x1(%r15), %ebp
leaq 0xbd96e(%rip), %rsi     # 0x3b923a
movl %ebp, %edx
xorl %eax, %eax
callq 0x31920
movl %r15d, %edi
movl %r12d, %esi
movq %r13, %rdx
movq %rbx, %rcx
movq 0x20(%rsp), %r8
xorl %r9d, %r9d
callq 0x2fba11
movq %rbx, %rdi
callq 0x2ee50
leaq 0x14(%rax), %rdi
movl $0x1, %esi
callq 0x30ed0
movq %rax, %r12
cmpl $0x0, 0x18(%rsp)
je 0x2fb917
movw $0x2d, (%r12)
jmp 0x2fb92b
leaq 0xbd946(%rip), %rsi     # 0x3b9264
movq %r12, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2f3c0
movslq %ebp, %rdi
movl $0x8, %esi
callq 0x30ed0
movq %rax, %r14
testl %r15d, %r15d
js 0x2fb9de
movq %r12, 0x18(%rsp)
movl %ebp, 0x3c(%rsp)
movl %ebp, %ebp
xorl %r12d, %r12d
movq %rbx, %rdi
callq 0x2ee50
leaq 0x14(%rax), %rdi
movl $0x1, %esi
callq 0x30ed0
movq %rax, (%r14,%r12,8)
movq %rax, %rdi
leaq 0xbd8f5(%rip), %rsi     # 0x3b926b
movq %rbx, %rdx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x2f3c0
incq %r12
cmpq %r12, %rbp
jne 0x2fb952
movq $0x0, (%rsp)
movl 0x14(%rsp), %edi
movq 0x18(%rsp), %rbx
movq %rbx, %rsi
xorl %edx, %edx
movl 0x3c(%rsp), %ecx
movq %r14, %r8
xorl %r9d, %r9d
callq 0x2fa237
movq %rbx, %rdi
callq 0x32018
testl %r15d, %r15d
js 0x2fba04
xorl %r12d, %r12d
movq (%r14,%r12,8), %r15
movq %r15, %rdi
callq 0x2e3a0
movq %r15, %rdi
callq 0x32018
incq %r12
cmpq %r12, %rbp
jne 0x2fb9c0
jmp 0x2fba04
movq $0x0, (%rsp)
movl 0x14(%rsp), %edi
movq %r12, %rsi
xorl %edx, %edx
movl %ebp, %ecx
movq %r14, %r8
xorl %r9d, %r9d
callq 0x2fa237
movq %r12, %rdi
callq 0x32018
movq %r14, %rdi
callq 0x32018
jmp 0x2fb803