// File: /workspace/github_c_makefile_star2_O1/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, %r14
movq %rdx, 0x30(%rsp)
movq %rsi, %r15
movl %edi, %ebp
movl %edi, 0x1960bc(%rip)    # 0x49a364
leaq 0xba1b3(%rip), %rsi     # 0x3be462
movq %r15, %rdi
callq 0x308e0
testl %eax, %eax
je 0x3042cc
leaq 0x44329(%rip), %rsi     # 0x3485eb
movq %r15, %rdi
callq 0x2eee0b
jmp 0x3042e9
movq 0x18bc75(%rip), %rax    # 0x48ff48
movq (%rax), %rdi
callq 0x2d230
leaq 0x44309(%rip), %rsi     # 0x3485eb
movl %eax, %edi
callq 0x2ef17d
movq %rax, %rbx
testq %rax, %rax
je 0x304430
movl %r12d, 0x14(%rsp)
movl %ebp, 0x24(%rsp)
movq %rbx, %rdi
callq 0x2f18d8
movq %rax, 0x18(%rsp)
movq %r14, %r12
shrq $0x5, %r12
movl $0x8, %esi
movq %r12, %rdi
callq 0x30ed0
movq %rax, %r13
movq $0x0, 0x8(%rsp)
movl $0x1, %eax
movq %rax, 0x28(%rsp)
xorl %ebp, %ebp
xorl %r15d, %r15d
cmpq $0x0, (%r13,%rbp,8)
jne 0x304356
movl $0x1, %edi
movl $0x38, %esi
callq 0x30ed0
movq %rax, (%r13,%rbp,8)
movq (%r13,%rbp,8), %rsi
movq %rbx, %rdi
callq 0x2f1c0d
testl %eax, %eax
js 0x3043a3
movl %eax, %eax
addq %rax, %r15
incq %rbp
cmpq %r14, %r15
jb 0x30433a
movq 0x8(%rsp), %rdi
leal 0x1(%rdi), %r15d
movl %ebp, %esi
movq %r13, %rdx
movq 0x30(%rsp), %rcx
movq 0x18(%rsp), %r8
xorl %r9d, %r9d
callq 0x3045a2
incq 0x28(%rsp)
movl %r15d, %eax
movq %rax, 0x8(%rsp)
jmp 0x304335
cmpl $-0x1, %eax
jne 0x304459
movq 0x8(%rsp), %rax
testl %eax, %eax
movq 0x30(%rsp), %r15
jne 0x304463
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movl %ebp, %esi
movq %r13, %rdx
movq %r15, %rcx
movq 0x18(%rsp), %r8
movl 0x14(%rsp), %r9d
callq 0x3045a2
cmpq $0x20, %r14
jb 0x304408
xorl %r14d, %r14d
movq (%r13,%r14,8), %rax
testq %rax, %rax
je 0x304400
movq 0x30(%rax), %rdi
callq 0x32018
movq (%r13,%r14,8), %rdi
callq 0x32018
incq %r14
cmpq %r14, %r12
jne 0x3043e3
movq %r13, %rdi
callq 0x32018
movq 0x18(%rsp), %rdi
callq 0x2f1856
movq %rbx, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2eef2d
movq 0x18bb89(%rip), %rax    # 0x48ffc0
movq (%rax), %rdi
leaq 0xbe491(%rip), %rsi     # 0x3c28d2
movq %r15, %rdx
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x31920
callq 0xab2b1
jmp 0x3043ac
movq %rax, %rcx
movq %rax, 0x8(%rsp)
movq 0x18bb4e(%rip), %rax    # 0x48ffc0
movq (%rax), %rdi
leal 0x1(%rcx), %edx
leaq 0xbe4ab(%rip), %rsi     # 0x3c292a
movl %edx, 0x10(%rsp)
xorl %eax, %eax
callq 0x31920
movq 0x8(%rsp), %rdi
movl %ebp, %esi
movq %r13, %rdx
movq %r15, %rcx
movq 0x18(%rsp), %r8
xorl %r9d, %r9d
callq 0x3045a2
movq %r15, %rdi
callq 0x2ee50
leaq 0x14(%rax), %rdi
movl $0x1, %esi
callq 0x30ed0
cmpl $0x0, 0x14(%rsp)
movq %r12, 0x40(%rsp)
movq %rax, 0x38(%rsp)
je 0x3044d2
movw $0x2d, (%rax)
jmp 0x3044e6
leaq 0xbe47b(%rip), %rsi     # 0x3c2954
movq %rax, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x2f3c0
movl 0x10(%rsp), %edi
movl $0x8, %esi
callq 0x30ed0
movq %rax, 0x8(%rsp)
xorl %ebp, %ebp
movq 0x28(%rsp), %r12
movq %r15, %rdi
callq 0x2ee50
leaq 0x14(%rax), %rdi
movl $0x1, %esi
callq 0x30ed0
movq 0x8(%rsp), %rcx
movq %rax, (%rcx,%rbp,8)
movq %rax, %rdi
leaq 0xbe432(%rip), %rsi     # 0x3c295b
movq %r15, %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x2f3c0
incq %rbp
cmpq %rbp, %r12
jne 0x304500
movq $0x0, (%rsp)
movl 0x24(%rsp), %edi
movq 0x38(%rsp), %r15
movq %r15, %rsi
xorl %edx, %edx
movl 0x10(%rsp), %ecx
movq 0x8(%rsp), %rbp
movq %rbp, %r8
xorl %r9d, %r9d
callq 0x3029f5
movq %r15, %rdi
callq 0x32018
movq %rbp, %r15
movq (%r15), %rbp
movq %rbp, %rdi
callq 0x2e3a0
movq %rbp, %rdi
callq 0x32018
addq $0x8, %r15
decq %r12
jne 0x304572
movq 0x8(%rsp), %rdi
callq 0x32018
movq 0x40(%rsp), %r12
jmp 0x3043da