// File: /workspace/github_c_makefile_star2_O2/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 $0x38, %rsp
movl %r8d, %r15d
movq %rcx, 0x8(%rsp)
movq %rdx, 0x28(%rsp)
movq %rsi, %rbx
movl %edi, %ebp
movl %edi, 0x170f12(%rip)    # 0x415384
cmpb $0x2d, (%rsi)
jne 0x2a4481
cmpb $0x0, 0x1(%rbx)
je 0x2a45c9
leaq 0x3e0c3(%rip), %rsi     # 0x2e254b
movq %rbx, %rdi
callq 0x292937
movq %rax, %r14
testq %rax, %rax
je 0x2a45eb
movl %r15d, 0x1c(%rsp)
movl %ebp, 0x18(%rsp)
movq %r14, %rdi
callq 0x294cb4
movq %rax, 0x10(%rsp)
movq 0x8(%rsp), %rdi
shrq $0x5, %rdi
pushq $0x8
popq %rsi
movq %rdi, 0x30(%rsp)
callq 0x31f40
movq %rax, %r12
xorl %esi, %esi
xorl %ebx, %ebx
xorl %r13d, %r13d
xorl %ebp, %ebp
movq %r14, %r15
testq %rsi, %rsi
jne 0x2a44f4
movslq %r13d, %r14
pushq $0x1
popq %rdi
pushq $0x38
popq %rsi
callq 0x31f40
movq %rax, %rsi
movq %rax, (%r12,%r14,8)
movq %r15, %r14
movq %r14, %rdi
callq 0x294ff7
testl %eax, %eax
js 0x2a4541
movl %eax, %eax
addq %rax, %rbx
incl %r13d
cmpq 0x8(%rsp), %rbx
jb 0x2a4538
leal 0x1(%rbp), %r14d
xorl %ebx, %ebx
movl %ebp, %edi
movl %r13d, %esi
movq %r12, %rdx
movq 0x28(%rsp), %rcx
movq 0x10(%rsp), %r8
xorl %r9d, %r9d
callq 0x2a474d
xorl %r13d, %r13d
movl %r14d, %ebp
movq %r15, %r14
movslq %r13d, %rax
movq (%r12,%rax,8), %rsi
jmp 0x2a44d7
cmpl $-0x1, %eax
jne 0x2a4614
testl %ebp, %ebp
movq 0x28(%rsp), %rbx
movl 0x1c(%rsp), %r15d
jne 0x2a4635
pushq $-0x1
popq %rdi
movl %r13d, %esi
movq %r12, %rdx
movq %rbx, %rcx
movq 0x10(%rsp), %r8
movl %r15d, %r9d
callq 0x2a474d
xorl %ebx, %ebx
movq 0x30(%rsp), %r15
cmpq %rbx, %r15
je 0x2a45a1
movq (%r12,%rbx,8), %rax
testq %rax, %rax
je 0x2a459c
movq 0x30(%rax), %rdi
callq 0x33058
movq (%r12,%rbx,8), %rdi
callq 0x33058
incq %rbx
jmp 0x2a457c
movq %r12, %rdi
callq 0x33058
movq 0x10(%rsp), %rdi
callq 0x294c39
movq %r14, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x292a35
movq 0x166940(%rip), %rax    # 0x40af10
movq (%rax), %rdi
callq 0x2e230
leaq 0x3df6c(%rip), %rsi     # 0x2e254b
movl %eax, %edi
callq 0x292c64
jmp 0x2a4490
movq 0x1669be(%rip), %rax    # 0x40afb0
movq (%rax), %rdi
leaq 0xb7ea6(%rip), %rsi     # 0x35c4a2
movq %rbx, %rdx
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x32960
movq 0x166995(%rip), %rax    # 0x40afb0
movq (%rax), %rcx
leaq 0xb7ea3(%rip), %rdi     # 0x35c4c8
pushq $0x31
popq %rsi
pushq $0x1
popq %rdx
callq 0x32760
jmp 0x2a454a
movq 0x166974(%rip), %rax    # 0x40afb0
movq (%rax), %rdi
leal 0x1(%rbp), %edx
leaq 0xb7eb1(%rip), %rsi     # 0x35c4fa
movl %edx, 0x8(%rsp)
xorl %eax, %eax
callq 0x32960
movl %ebp, %edi
movl %r13d, %esi
movq %r12, %rdx
movq %rbx, %rcx
movq 0x10(%rsp), %r8
xorl %r9d, %r9d
callq 0x2a474d
movq %rbx, %rdi
callq 0x2fe60
leaq 0x14(%rax), %rdi
pushq $0x1
popq %rsi
callq 0x31f40
testl %r15d, %r15d
movq %rax, 0x20(%rsp)
je 0x2a4691
movw $0x2d, (%rax)
jmp 0x2a46a5
leaq 0xb7e8c(%rip), %rsi     # 0x35c524
movq %rax, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x303f0
movslq 0x8(%rsp), %rdi
pushq $0x8
popq %rsi
callq 0x31f40
movq %rax, %r13
movslq %ebp, %r15
xorl %ebp, %ebp
cmpq %r15, %rbp
jg 0x2a46f3
movq %rbx, %rdi
callq 0x2fe60
leaq 0x14(%rax), %rdi
pushq $0x1
popq %rsi
callq 0x31f40
movq %rax, (%r13,%rbp,8)
movq %rax, %rdi
leaq 0xb7e49(%rip), %rsi     # 0x35c52b
movq %rbx, %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x303f0
incq %rbp
jmp 0x2a46ba
andq $0x0, (%rsp)
xorl %ebx, %ebx
movl 0x18(%rsp), %edi
movq 0x20(%rsp), %rsi
xorl %edx, %edx
movl 0x8(%rsp), %ecx
movq %r13, %rbp
movq %r13, %r8
xorl %r9d, %r9d
callq 0x2a3080
movq 0x20(%rsp), %rdi
callq 0x33058
cmpq %r15, %rbx
jg 0x2a4740
movq (%rbp,%rbx,8), %r13
movq %r13, %rdi
callq 0x2f3b0
movq %r13, %rdi
callq 0x33058
incq %rbx
jmp 0x2a4721
movq %rbp, %rdi
callq 0x33058
jmp 0x2a4575