// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x88, %rsp
movl %edi, 0x84(%rsp)
movq %rsi, 0x78(%rsp)
movq %rdx, 0x70(%rsp)
movq %rcx, 0x68(%rsp)
movl %r8d, 0x64(%rsp)
movl 0x84(%rsp), %eax
movl %eax, 0x17eef5(%rip)    # 0x607d04
movl $0x0, 0x58(%rsp)
movl $0x0, 0x60(%rsp)
movq $0x0, 0x48(%rsp)
movq 0x78(%rsp), %rdi
leaq 0xee79b(%rip), %rsi     # 0x5775cf
callq 0x32950
cmpl $0x0, %eax
je 0x488e56
movq 0x78(%rsp), %rdi
leaq 0x764f1(%rip), %rsi     # 0x4ff33b
callq 0x464500
movq %rax, 0x10(%rsp)
jmp 0x488e78
movq 0x1730e3(%rip), %rax    # 0x5fbf40
movq (%rax), %rdi
callq 0x2f230
movl %eax, %edi
leaq 0x764cd(%rip), %rsi     # 0x4ff33b
callq 0x464980
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
movq %rax, 0x38(%rsp)
cmpq $0x0, 0x38(%rsp)
jne 0x488eac
movq 0x173137(%rip), %rax    # 0x5fbfc8
movq (%rax), %rdi
movq 0x78(%rsp), %rdx
leaq 0xf2819(%rip), %rsi     # 0x57b6b9
movb $0x0, %al
callq 0x33960
jmp 0x489206
movq 0x38(%rsp), %rdi
callq 0x4691e0
movq %rax, 0x40(%rsp)
movq 0x68(%rsp), %rdi
shrq $0x5, %rdi
movl $0x8, %esi
callq 0x32f50
movq %rax, 0x28(%rsp)
movq 0x28(%rsp), %rax
movslq 0x58(%rsp), %rcx
cmpq $0x0, (%rax,%rcx,8)
jne 0x488f04
movl $0x1, %edi
movl $0x38, %esi
callq 0x32f50
movq %rax, %rdx
movq 0x28(%rsp), %rax
movslq 0x58(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movq 0x28(%rsp), %rax
movslq 0x58(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x30(%rsp)
movq 0x38(%rsp), %rdi
movq 0x30(%rsp), %rsi
callq 0x469710
movl %eax, 0x5c(%rsp)
cmpl $0x0, %eax
jge 0x488f31
jmp 0x488f95
movslq 0x5c(%rsp), %rax
addq 0x48(%rsp), %rax
movq %rax, 0x48(%rsp)
movl 0x58(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x58(%rsp)
movq 0x48(%rsp), %rax
cmpq 0x68(%rsp), %rax
jb 0x488f90
movl 0x60(%rsp), %edi
movl %edi, %eax
addl $0x1, %eax
movl %eax, 0x60(%rsp)
movl 0x58(%rsp), %esi
movq 0x28(%rsp), %rdx
movq 0x70(%rsp), %rcx
movq 0x40(%rsp), %r8
xorl %r9d, %r9d
callq 0x489210
movq $0x0, 0x48(%rsp)
movl $0x0, 0x58(%rsp)
jmp 0x488ed3
cmpl $-0x1, 0x5c(%rsp)
je 0x488fb4
movq 0x173025(%rip), %rax    # 0x5fbfc8
movq (%rax), %rdi
leaq 0xf2732(%rip), %rsi     # 0x57b6df
movb $0x0, %al
callq 0x33960
cmpl $0x0, 0x60(%rsp)
jne 0x488fe2
movl 0x58(%rsp), %esi
movq 0x28(%rsp), %rdx
movq 0x70(%rsp), %rcx
movq 0x40(%rsp), %r8
movl 0x64(%rsp), %r9d
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x489210
jmp 0x489183
movq 0x172fdf(%rip), %rax    # 0x5fbfc8
movq (%rax), %rdi
movl 0x60(%rsp), %edx
addl $0x1, %edx
leaq 0xf2717(%rip), %rsi     # 0x57b711
movb $0x0, %al
callq 0x33960
movl 0x60(%rsp), %edi
movl %edi, %eax
addl $0x1, %eax
movl %eax, 0x60(%rsp)
movl 0x58(%rsp), %esi
movq 0x28(%rsp), %rdx
movq 0x70(%rsp), %rcx
movq 0x40(%rsp), %r8
xorl %r9d, %r9d
callq 0x489210
movq 0x70(%rsp), %rdi
callq 0x30e70
movq %rax, %rdi
addq $0x14, %rdi
movl $0x1, %esi
callq 0x32f50
movq %rax, 0x18(%rsp)
cmpl $0x0, 0x64(%rsp)
je 0x489065
movq 0x18(%rsp), %rdi
leaq 0xee573(%rip), %rsi     # 0x5775cf
movb $0x0, %al
callq 0x31400
jmp 0x48907d
movq 0x18(%rsp), %rdi
movq 0x70(%rsp), %rdx
leaq 0xf26c5(%rip), %rsi     # 0x57b73b
movb $0x0, %al
callq 0x31400
movslq 0x60(%rsp), %rdi
movl $0x8, %esi
callq 0x32f50
movq %rax, 0x20(%rsp)
movl $0x0, 0x54(%rsp)
movl 0x54(%rsp), %eax
cmpl 0x60(%rsp), %eax
jge 0x489101
movq 0x70(%rsp), %rdi
callq 0x30e70
movq %rax, %rdi
addq $0x14, %rdi
movl $0x1, %esi
callq 0x32f50
movq %rax, %rdx
movq 0x20(%rsp), %rax
movslq 0x54(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movq 0x20(%rsp), %rax
movslq 0x54(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
movq 0x70(%rsp), %rdx
movl 0x54(%rsp), %ecx
leaq 0xf2655(%rip), %rsi     # 0x57b742
movb $0x0, %al
callq 0x31400
movl 0x54(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x54(%rsp)
jmp 0x489099
movl 0x84(%rsp), %edi
movq 0x18(%rsp), %rsi
movl 0x60(%rsp), %ecx
movq 0x20(%rsp), %r8
xorl %eax, %eax
movl %eax, %edx
xorl %r9d, %r9d
movq $0x0, (%rsp)
callq 0x4867c0
movq 0x18(%rsp), %rdi
callq 0x34078
movl $0x0, 0x54(%rsp)
movl 0x54(%rsp), %eax
cmpl 0x60(%rsp), %eax
jge 0x489179
movq 0x20(%rsp), %rax
movslq 0x54(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x303c0
movq 0x20(%rsp), %rax
movslq 0x54(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x34078
movl 0x54(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x54(%rsp)
jmp 0x48913c
movq 0x20(%rsp), %rdi
callq 0x34078
movl $0x0, 0x58(%rsp)
movslq 0x58(%rsp), %rax
movq 0x68(%rsp), %rcx
shrq $0x5, %rcx
cmpq %rcx, %rax
jae 0x4891e8
movq 0x28(%rsp), %rax
movslq 0x58(%rsp), %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0x4891d9
movq 0x28(%rsp), %rax
movslq 0x58(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x30(%rax), %rdi
callq 0x34078
movq 0x28(%rsp), %rax
movslq 0x58(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x34078
jmp 0x4891db
movl 0x58(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x58(%rsp)
jmp 0x48918b
movq 0x28(%rsp), %rdi
callq 0x34078
movq 0x40(%rsp), %rdi
callq 0x469100
movq 0x38(%rsp), %rdi
callq 0x4646c0
addq $0x88, %rsp
retq
nop