// File: /workspace/github_c_makefile_star2_O2/genometools[P]genometools/src/external/samtools-0.1.18/bam_sort.c
// Function: bam_merge_core
int bam_merge_core(int by_qname, const char *out, const char *headers, int n, char * const *fn,
					int flag, const char *reg)
{
	bamFile fpout, *fp;
	heap1_t *heap;
	bam_header_t *hout = 0;
	bam_header_t *hheaders = NULL;
	int i, j, *RG_len = 0;
	uint64_t idx = 0;
	char **RG = 0;
	bam_iter_t *iter = 0;

	if (headers) {
		tamFile fpheaders = sam_open(headers);
		if (fpheaders == 0) {
			const char *message = strerror(errno);
			fprintf(stderr, "[bam_merge_core] cannot open '%s': %s\n", headers, message);
			return -1;
		}
		hheaders = sam_header_read(fpheaders);
		sam_close(fpheaders);
	}

	g_is_by_qname = by_qname;
	fp = (bamFile*)calloc(n, sizeof(bamFile));
	heap = (heap1_t*)calloc(n, sizeof(heap1_t));
	iter = (bam_iter_t*)calloc(n, sizeof(bam_iter_t));
	// prepare RG tag
	if (flag & MERGE_RG) {
		RG = (char**)calloc(n, sizeof(void*));
		RG_len = (int*)calloc(n, sizeof(int));
		for (i = 0; i != n; ++i) {
			int l = strlen(fn[i]);
			const char *s = fn[i];
			if (l > 4 && strcmp(s + l - 4, ".bam") == 0) l -= 4;
			for (j = l - 1; j >= 0; --j) if (s[j] == '/') break;
			++j; l -= j;
			RG[i] = calloc(l + 1, 1);
			RG_len[i] = l;
			strncpy(RG[i], s + j, l);
		}
	}
	// read the first
	for (i = 0; i != n; ++i) {
		bam_header_t *hin;
		fp[i] = bam_open(fn[i], "r");
		if (fp[i] == 0) {
			int j;
			fprintf(stderr, "[bam_merge_core] fail to open file %s\n", fn[i]);
			for (j = 0; j < i; ++j) bam_close(fp[j]);
			free(fp); free(heap);
			// FIXME: possible memory leak
			return -1;
		}
		hin = bam_header_read(fp[i]);
		if (i == 0) { // the first BAM
			hout = hin;
		} else { // validate multiple baf
			int min_n_targets = hout->n_targets;
			if (hin->n_targets < min_n_targets) min_n_targets = hin->n_targets;

			for (j = 0; j < min_n_targets; ++j)
				if (strcmp(hout->target_name[j], hin->target_name[j]) != 0) {
					fprintf(stderr, "[bam_merge_core] different target sequence name: '%s' != '%s' in file '%s'\n",
							hout->target_name[j], hin->target_name[j], fn[i]);
					return -1;
				}

			// If this input file has additional target reference sequences,
			// add them to the headers to be output
			if (hin->n_targets > hout->n_targets) {
				swap_header_targets(hout, hin);
				// FIXME Possibly we should also create @SQ text headers
				// for the newly added reference sequences
			}

			bam_header_destroy(hin);
		}
	}

	if (hheaders) {
		// If the text headers to be swapped in include any @SQ headers,
		// check that they are consistent with the existing binary list
		// of reference information.
		if (hheaders->n_targets > 0) {
			if (hout->n_targets != hheaders->n_targets) {
				fprintf(stderr, "[bam_merge_core] number of @SQ headers in '%s' differs from number of target sequences\n", headers);
				if (!reg) return -1;
			}
			for (j = 0; j < hout->n_targets; ++j)
				if (strcmp(hout->target_name[j], hheaders->target_name[j]) != 0) {
					fprintf(stderr, "[bam_merge_core] @SQ header '%s' in '%s' differs from target sequence\n", hheaders->target_name[j], headers);
					if (!reg) return -1;
				}
		}

		swap_header_text(hout, hheaders);
		bam_header_destroy(hheaders);
	}

	if (reg) {
		int tid, beg, end;
		if (bam_parse_region(hout, reg, &tid, &beg, &end) < 0) {
			fprintf(stderr, "[%s] Malformated region string or undefined reference name\n", __func__);
			return -1;
		}
		for (i = 0; i < n; ++i) {
			bam_index_t *idx;
			idx = bam_index_load(fn[i]);
			iter[i] = bam_iter_query(idx, tid, beg, end);
			bam_index_destroy(idx);
		}
	}

	for (i = 0; i < n; ++i) {
		heap1_t *h = heap + i;
		h->i = i;
		h->b = (bam1_t*)calloc(1, sizeof(bam1_t));
		if (bam_iter_read(fp[i], iter[i], h->b) >= 0) {
			h->pos = ((uint64_t)h->b->core.tid<<32) | (uint32_t)((int32_t)h->b->core.pos+1)<<1 | bam1_strand(h->b);
			h->idx = idx++;
		}
		else h->pos = HEAP_EMPTY;
	}
	if (flag & MERGE_UNCOMP) fpout = strcmp(out, "-")? bam_open(out, "wu") : bam_dopen(fileno(stdout), "wu");
	else if (flag & MERGE_LEVEL1) fpout = strcmp(out, "-")? bam_open(out, "w1") : bam_dopen(fileno(stdout), "w1");
	else fpout = strcmp(out, "-")? bam_open(out, "w") : bam_dopen(fileno(stdout), "w");
	if (fpout == 0) {
		fprintf(stderr, "[%s] fail to create the output file.\n", __func__);
		return -1;
	}
	bam_header_write(fpout, hout);
	bam_header_destroy(hout);

	ks_heapmake(heap, n, heap);
	while (heap->pos != HEAP_EMPTY) {
		bam1_t *b = heap->b;
		if (flag & MERGE_RG) {
			uint8_t *rg = bam_aux_get(b, "RG");
			if (rg) bam_aux_del(b, rg);
			bam_aux_append(b, "RG", 'Z', RG_len[heap->i] + 1, (uint8_t*)RG[heap->i]);
		}
		bam_write1_core(fpout, &b->core, b->data_len, b->data);
		if ((j = bam_iter_read(fp[heap->i], iter[heap->i], b)) >= 0) {
			heap->pos = ((uint64_t)b->core.tid<<32) | (uint32_t)((int)b->core.pos+1)<<1 | bam1_strand(b);
			heap->idx = idx++;
		} else if (j == -1) {
			heap->pos = HEAP_EMPTY;
			free(heap->b->data); free(heap->b);
			heap->b = 0;
		} else fprintf(stderr, "[bam_merge_core] '%s' is truncated. Continue anyway.\n", fn[heap->i]);
		ks_heapadjust(heap, 0, n, heap);
	}

	if (flag & MERGE_RG) {
		for (i = 0; i != n; ++i) free(RG[i]);
		free(RG); free(RG_len);
	}
	for (i = 0; i != n; ++i) {
		bam_iter_destroy(iter[i]);
		bam_close(fp[i]);
	}
	bam_close(fpout);
	free(fp); free(heap); free(iter);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movl %r9d, %ebp
movq %r8, 0x18(%rsp)
movl %ecx, %r15d
movq %rsi, 0x78(%rsp)
movl %edi, %ebx
pushq $-0x1
popq %r12
testq %rdx, %rdx
movq %rdx, 0x50(%rsp)
je 0x2a30dc
movq %rdx, %rdi
callq 0x297c3a
testq %rax, %rax
je 0x2a37bc
movq %rax, %r14
movq %rax, %rdi
callq 0x296ae6
movq %rax, 0x40(%rsp)
movq %r14, %rdi
callq 0x297cbb
jmp 0x2a30e5
movq $0x0, 0x40(%rsp)
movl %ebx, 0x172299(%rip)    # 0x415384
movslq %r15d, %rbx
pushq $0x8
popq %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x31f40
movq %rax, 0x8(%rsp)
pushq $0x20
popq %rsi
movq %rbx, %rdi
callq 0x31f40
movl %r15d, %r12d
movq %rax, %r15
movq %rbx, %rdi
movq %r14, %rsi
callq 0x31f40
movq %rax, 0x30(%rsp)
movl %r12d, 0x3c(%rsp)
movl %r12d, %r12d
testb $0x1, %bpl
movq %r15, 0x28(%rsp)
movl %ebp, 0x14(%rsp)
movq %rbx, 0x80(%rsp)
jne 0x2a328c
movq $0x0, 0x20(%rsp)
movq $0x0, 0x48(%rsp)
movq %r12, %rbx
xorl %r14d, %r14d
xorl %r13d, %r13d
movq %rbx, 0x58(%rsp)
cmpq %rbx, %r14
je 0x2a3374
movq 0x18(%rsp), %rax
movq (%rax,%r14,8), %rdi
leaq 0x3f3cc(%rip), %rsi     # 0x2e254b
callq 0x292937
movq 0x8(%rsp), %rcx
movq %rax, (%rcx,%r14,8)
testq %rax, %rax
je 0x2a37ea
movq %rax, %rdi
callq 0x294cb4
movq %rax, %r12
testq %r14, %r14
je 0x2a3244
movq %r14, 0x88(%rsp)
movq %r13, %rbp
movl (%r13), %eax
movl (%r12), %ecx
cmpl %eax, %ecx
movl %eax, 0x68(%rsp)
movl %eax, %ebx
movl %ecx, 0x64(%rsp)
cmovll %ecx, %ebx
testl %ebx, %ebx
movl $0x0, %eax
cmovlel %eax, %ebx
xorl %r13d, %r13d
cmpq %r13, %rbx
je 0x2a3203
movq 0x8(%rbp), %rax
movq (%rax,%r13,8), %r15
movq 0x8(%r12), %rax
movq (%rax,%r13,8), %r14
movq %r15, %rdi
movq %r14, %rsi
callq 0x31930
incq %r13
testl %eax, %eax
je 0x2a31d9
jmp 0x2a3254
movl 0x68(%rsp), %ecx
movl 0x64(%rsp), %edx
cmpl %ecx, %edx
jle 0x2a322a
movl %edx, (%rbp)
movl %ecx, (%r12)
movups 0x8(%r12), %xmm0
movups 0x8(%rbp), %xmm1
movups %xmm0, 0x8(%rbp)
movups %xmm1, 0x8(%r12)
movq %r12, %rdi
callq 0x294c39
movq 0x58(%rsp), %rbx
movq %rbp, %r13
movq 0x88(%rsp), %r14
jmp 0x2a3247
movq %r12, %r13
incq %r14
movq 0x28(%rsp), %r15
jmp 0x2a3166
movq 0x167d55(%rip), %rax    # 0x40afb0
movq (%rax), %rdi
movq 0x18(%rsp), %rax
movq 0x88(%rsp), %rcx
movq (%rax,%rcx,8), %r8
leaq 0xb8db1(%rip), %rsi     # 0x35c027
movq %r15, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x32960
pushq $-0x1
popq %r12
jmp 0x2a378c
pushq $0x8
popq %rsi
movq %rbx, %rdi
callq 0x31f40
movq %rax, 0x20(%rsp)
pushq $0x4
popq %rsi
movq %rbx, %rdi
callq 0x31f40
movq %rax, 0x48(%rsp)
xorl %r14d, %r14d
movq %r12, %rbx
cmpq %rbx, %r14
je 0x2a315b
movq %rbx, %rbp
movq 0x18(%rsp), %rax
movq (%rax,%r14,8), %r13
movq %r13, %rdi
callq 0x2fe60
movq %rax, %r12
cmpl $0x5, %r12d
jl 0x2a3302
movl %r12d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
leaq (%rax,%r13), %rdi
addq $-0x4, %rdi
leaq 0xb9243(%rip), %rsi     # 0x35c532
callq 0x31930
leal -0x4(%r12), %ecx
testl %eax, %eax
cmovnel %r12d, %ecx
movl %ecx, %r12d
movl %r12d, %eax
movl %r12d, %ebx
sarl $0x1f, %ebx
andl %r12d, %ebx
leaq (%rax,%r13), %rcx
decq %rcx
movl %r12d, %eax
testl %eax, %eax
jle 0x2a332b
decl %eax
cmpb $0x2f, (%rcx)
leaq -0x1(%rcx), %rcx
jne 0x2a3318
incl %eax
movl %eax, %ebx
subl %ebx, %r12d
leal 0x1(%r12), %eax
movslq %eax, %rdi
pushq $0x1
popq %rsi
callq 0x31f40
movq 0x20(%rsp), %rcx
movq %rax, (%rcx,%r14,8)
movq 0x48(%rsp), %rcx
movl %r12d, (%rcx,%r14,4)
movslq %ebx, %rcx
addq %rcx, %r13
movslq %r12d, %rdx
movq %rax, %rdi
movq %r13, %rsi
callq 0x32180
incq %r14
movq 0x28(%rsp), %r15
movq %rbp, %rbx
jmp 0x2a32b2
movq 0x40(%rsp), %rax
testq %rax, %rax
je 0x2a3421
movl (%rax), %eax
testl %eax, %eax
jle 0x2a33f7
cmpl %eax, (%r13)
jne 0x2a38b1
movq 0x167c17(%rip), %rbx    # 0x40afb0
leaq 0xb8d2b(%rip), %r12     # 0x35c0cb
xorl %r15d, %r15d
movslq (%r13), %rax
cmpq %rax, %r15
jge 0x2a33f7
movq 0x8(%r13), %rax
movq (%rax,%r15,8), %rdi
movq 0x40(%rsp), %rax
movq 0x8(%rax), %rax
movq (%rax,%r15,8), %r14
movq %r14, %rsi
callq 0x31930
testl %eax, %eax
jne 0x2a33d2
incq %r15
jmp 0x2a33a3
movq (%rbx), %rdi
movq %r12, %rsi
movq %r14, %rdx
movq 0x50(%rsp), %rcx
xorl %eax, %eax
callq 0x32960
cmpq $0x0, 0xd0(%rsp)
jne 0x2a33cd
jmp 0x2a3283
movslq 0x30(%r13), %rax
movq 0x40(%rsp), %rdi
movq 0x30(%rdi), %rcx
movq %rcx, 0x30(%r13)
movq %rax, 0x30(%rdi)
movq 0x40(%r13), %rax
movq 0x40(%rdi), %rcx
movq %rcx, 0x40(%r13)
movq %rax, 0x40(%rdi)
callq 0x294c39
movq 0xd0(%rsp), %rsi
testq %rsi, %rsi
movq %r13, 0x90(%rsp)
je 0x2a34a4
leaq 0x74(%rsp), %rdx
leaq 0x70(%rsp), %rcx
leaq 0x6c(%rsp), %r8
movq %r13, %rdi
callq 0x294836
testl %eax, %eax
movl 0x3c(%rsp), %eax
js 0x2a388d
xorl %ebx, %ebx
testl %eax, %eax
movl %eax, %r12d
cmovlel %ebx, %r12d
cmpq %rbx, %r12
je 0x2a34b1
movq 0x18(%rsp), %rax
movq (%rax,%rbx,8), %rdi
callq 0x299c2c
movq %rax, %r14
movl 0x74(%rsp), %esi
movl 0x70(%rsp), %edx
movl 0x6c(%rsp), %ecx
movq %rax, %rdi
callq 0x29a06a
movq 0x30(%rsp), %rcx
movq %rax, (%rcx,%rbx,8)
movq %r14, %rdi
callq 0x299403
incq %rbx
jmp 0x2a3464
xorl %r12d, %r12d
movl 0x3c(%rsp), %eax
testl %eax, %eax
cmovgl %eax, %r12d
movq 0x28(%rsp), %rbx
addq $0x18, %rbx
xorl %ebp, %ebp
pushq $0x1
popq %r13
xorl %r14d, %r14d
cmpq %rbp, %r12
je 0x2a3536
movl %ebp, -0x18(%rbx)
movq %r13, %rdi
pushq $0x38
popq %rsi
callq 0x31f40
movq %rax, %r15
movq %rax, (%rbx)
movq 0x8(%rsp), %rax
movq (%rax,%rbp,8), %rdi
movq 0x30(%rsp), %rax
movq (%rax,%rbp,8), %rsi
movq %r15, %rdx
callq 0x29a431
testl %eax, %eax
js 0x2a3526
movl (%r15), %eax
movl 0x4(%r15), %ecx
shlq $0x20, %rax
leal 0x2(,%rcx,2), %ecx
orq %rax, %rcx
movq 0x8(%r15), %rax
shrq $0x24, %rax
andl $0x1, %eax
orq %rcx, %rax
movq %r14, -0x8(%rbx)
incq %r14
jmp 0x2a3529
pushq $-0x1
popq %rax
movq %rax, -0x10(%rbx)
incq %rbp
addq $0x20, %rbx
jmp 0x2a34c3
movl 0x14(%rsp), %ecx
testb $0x2, %cl
jne 0x2a356c
movq 0x78(%rsp), %rdi
movb (%rdi), %al
testb $0x4, %cl
movq 0x28(%rsp), %r12
movq 0x58(%rsp), %r15
jne 0x2a3593
cmpb $0x2d, %al
jne 0x2a3563
cmpb $0x0, 0x1(%rdi)
je 0x2a3853
leaq 0x76881(%rip), %rsi     # 0x319deb
jmp 0x2a35a8
movq 0x78(%rsp), %rdi
cmpb $0x2d, (%rdi)
movq 0x28(%rsp), %r12
movq 0x58(%rsp), %r15
jne 0x2a358a
cmpb $0x0, 0x1(%rdi)
je 0x2a37a1
leaq 0xb8bcc(%rip), %rsi     # 0x35c15d
jmp 0x2a35a8
cmpb $0x2d, %al
jne 0x2a35a1
cmpb $0x0, 0x1(%rdi)
je 0x2a386b
leaq 0xb8bb8(%rip), %rsi     # 0x35c160
callq 0x292937
movq %rax, %rbp
testq %rax, %rax
je 0x2a3840
movq %rbp, %rdi
movq 0x90(%rsp), %rbx
movq %rbx, %rsi
callq 0x294e9a
movq %rbx, %rdi
callq 0x294c39
movq 0x80(%rsp), %rdi
movq %r12, %rsi
callq 0x2a25d6
cmpq $-0x1, 0x8(%r12)
je 0x2a3708
movq 0x18(%r12), %r13
testb $0x1, 0x14(%rsp)
je 0x2a3644
movq %r13, %rdi
leaq 0xb4952(%rip), %rsi     # 0x357f58
callq 0x293fec
testq %rax, %rax
je 0x2a361b
movq %r13, %rdi
movq %rax, %rsi
callq 0x29410b
movslq (%r12), %rax
movq 0x48(%rsp), %rcx
movl (%rcx,%rax,4), %ecx
incl %ecx
movq 0x20(%rsp), %rdx
movq (%rdx,%rax,8), %r8
movq %r13, %rdi
leaq 0xb491c(%rip), %rsi     # 0x357f58
pushq $0x5a
popq %rdx
callq 0x293f30
movl 0x24(%r13), %edx
movq 0x30(%r13), %rcx
movq %rbp, %rdi
movq %r13, %rsi
callq 0x29532d
movslq (%r12), %rbx
movq 0x8(%rsp), %rax
movq (%rax,%rbx,8), %rdi
movq 0x30(%rsp), %rax
movq (%rax,%rbx,8), %rsi
movq %r13, %rdx
callq 0x29a431
testl %eax, %eax
js 0x2a36ac
movl (%r13), %eax
movl 0x4(%r13), %ecx
shlq $0x20, %rax
leal 0x2(,%rcx,2), %ecx
orq %rax, %rcx
movq 0x8(%r13), %rax
shrq $0x24, %rax
andl $0x1, %eax
orq %rcx, %rax
movq %rax, 0x8(%r12)
movq %r14, 0x10(%r12)
incq %r14
jmp 0x2a36ce
cmpl $-0x1, %eax
jne 0x2a36e5
orq $-0x1, 0x8(%r12)
movq 0x30(%r13), %rdi
callq 0x33058
movq %r13, %rdi
callq 0x33058
andq $0x0, 0x18(%r12)
xorl %edi, %edi
movq 0x80(%rsp), %rsi
movq %r12, %rdx
callq 0x2a24c5
jmp 0x2a35e4
movq 0x1678c4(%rip), %rax    # 0x40afb0
movq (%rax), %rdi
movq 0x18(%rsp), %rax
movq (%rax,%rbx,8), %rdx
leaq 0xb8a8a(%rip), %rsi     # 0x35c189
xorl %eax, %eax
callq 0x32960
jmp 0x2a36ce
testb $0x1, 0x14(%rsp)
je 0x2a373d
xorl %ebx, %ebx
cmpq %rbx, %r15
je 0x2a3729
movq 0x20(%rsp), %rax
movq (%rax,%rbx,8), %rdi
callq 0x33058
incq %rbx
jmp 0x2a3711
movq 0x20(%rsp), %rdi
callq 0x33058
movq 0x48(%rsp), %rdi
callq 0x33058
xorl %ebx, %ebx
cmpq %rbx, %r15
je 0x2a3765
movq 0x30(%rsp), %rax
movq (%rax,%rbx,8), %rdi
callq 0x29a415
movq 0x8(%rsp), %rax
movq (%rax,%rbx,8), %rdi
callq 0x292a35
incq %rbx
jmp 0x2a373f
movq %rbp, %rdi
callq 0x292a35
movq 0x8(%rsp), %rdi
callq 0x33058
movq %r12, %rdi
callq 0x33058
movq 0x30(%rsp), %rdi
callq 0x33058
xorl %r12d, %r12d
movl %r12d, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x167840(%rip), %rax    # 0x40afe8
movq (%rax), %rdi
callq 0x2e230
leaq 0xb89a6(%rip), %rsi     # 0x35c15d
jmp 0x2a3881
callq 0x31620
movl (%rax), %edi
callq 0x309d0
movq 0x1677e1(%rip), %rcx    # 0x40afb0
movq (%rcx), %rdi
leaq 0xb8800(%rip), %rsi     # 0x35bfd9
movq 0x50(%rsp), %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x32960
jmp 0x2a378c
movq 0x1677bf(%rip), %rax    # 0x40afb0
movq (%rax), %rdi
movq 0x18(%rsp), %rax
movq (%rax,%r14,8), %rdx
leaq 0xb87fc(%rip), %rsi     # 0x35c000
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x32960
pushq $-0x1
popq %r12
cmpq %rbx, %r14
je 0x2a3829
movq 0x8(%rsp), %rax
movq (%rax,%rbx,8), %rdi
callq 0x292a35
incq %rbx
jmp 0x2a3811
movq 0x8(%rsp), %rdi
callq 0x33058
movq %r15, %rdi
callq 0x33058
jmp 0x2a378c
movq 0x167769(%rip), %rax    # 0x40afb0
movq (%rax), %rdi
leaq 0xb8912(%rip), %rsi     # 0x35c163
jmp 0x2a389e
movq 0x16778e(%rip), %rax    # 0x40afe8
movq (%rax), %rdi
callq 0x2e230
leaq 0x76582(%rip), %rsi     # 0x319deb
jmp 0x2a3881
movq 0x167776(%rip), %rax    # 0x40afe8
movq (%rax), %rdi
callq 0x2e230
leaq 0xb88df(%rip), %rsi     # 0x35c160
movl %eax, %edi
callq 0x292c64
jmp 0x2a35ad
movq 0x16771c(%rip), %rax    # 0x40afb0
movq (%rax), %rdi
leaq 0xb8874(%rip), %rsi     # 0x35c112
leaq 0xb88a9(%rip), %rdx     # 0x35c14e
xorl %eax, %eax
callq 0x32960
jmp 0x2a3283
movq 0x1676f8(%rip), %rax    # 0x40afb0
movq (%rax), %rdi
leaq 0xb87b1(%rip), %rsi     # 0x35c073
movq 0x50(%rsp), %rdx
xorl %eax, %eax
callq 0x32960
cmpq $0x0, 0xd0(%rsp)
jne 0x2a3392
jmp 0x2a3283