// File: /workspace/github_c_makefile_star2_O3/xiongxu[P]bam2bedGraph2/samtools-0.1.19/bam_sort.c
// Function: bam_merge_core2
int bam_merge_core2(int by_qname, const char *out, const char *headers, int n, char * const *fn, int flag, const char *reg, int n_threads, int level)
{
	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, mode[8];
	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) level = 0;
	else if (flag & MERGE_LEVEL1) level = 1;
	strcpy(mode, "w");
	if (level >= 0) sprintf(mode + 1, "%d", level < 9? level : 9);
	if ((fpout = strcmp(out, "-")? bam_open(out, "w") : bam_dopen(fileno(stdout), "w")) == 0) {
		fprintf(stderr, "[%s] fail to create the output file.\n", __func__);
		return -1;
	}
	bam_header_write(fpout, hout);
	bam_header_destroy(hout);
	if (!(flag & MERGE_UNCOMP)) bgzf_mt(fpout, n_threads, 256);

	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, 0x14(%rsp)
movq %r8, %rbp
movq %rsi, 0x88(%rsp)
movl %edi, %ebx
testq %rdx, %rdx
movl %ecx, 0x4(%rsp)
movq %rdx, 0x68(%rsp)
je 0x33f92
movq %rdx, %r15
movq %rdx, %rdi
callq 0x27d11
testq %rax, %rax
je 0x3430e
movq %rax, %r14
movq %rax, %rdi
callq 0x268b9
movq %rax, %r15
movq %r14, %rdi
callq 0x27db4
movl 0x4(%rsp), %ecx
jmp 0x33f95
xorl %r15d, %r15d
movl %ebx, 0x1d3d5(%rip)     # 0x51370
movslq %ecx, %rbx
movl $0x8, %esi
movq %rbx, %rdi
callq 0x3610
movq %rax, %r14
movl $0x20, %esi
movq %rbx, %rdi
callq 0x3610
movq %rax, 0x18(%rsp)
movl $0x8, %esi
movq %rbx, %rdi
callq 0x3610
movq %rax, %r13
testb $0x1, 0x14(%rsp)
movq %r14, 0x30(%rsp)
movq %rax, 0x20(%rsp)
movq %rbp, 0x28(%rsp)
movq %rbx, 0x80(%rsp)
movq %r15, 0x38(%rsp)
jne 0x3400a
movq $0x0, 0x48(%rsp)
movq $0x0, 0x40(%rsp)
jmp 0x340fc
movl $0x8, %esi
movq %rbx, %rdi
callq 0x3610
movq %rax, 0x48(%rsp)
movl $0x4, %esi
movq %rbx, %rdi
callq 0x3610
movq %rax, 0x40(%rsp)
testl %ebx, %ebx
je 0x3425d
movl 0x4(%rsp), %ebx
xorl %r15d, %r15d
leaq 0xe061(%rip), %r12      # 0x420a5
movq (%rbp,%r15,8), %r13
movq %r13, %rdi
callq 0x3360
movq %rax, %rbp
cmpl $0x5, %ebp
jl 0x3407a
movl %ebp, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
leaq (%rax,%r13), %rdi
addq $-0x4, %rdi
movq %r12, %rsi
callq 0x35a0
leal -0x4(%rbp), %ecx
testl %eax, %eax
cmovnel %ebp, %ecx
movl %ecx, %ebp
movl %ebp, %eax
movl %ebp, %r14d
sarl $0x1f, %r14d
andl %ebp, %r14d
leaq (%rax,%r13), %rcx
decq %rcx
movl %ebp, %eax
testl %eax, %eax
jle 0x340a3
decl %eax
cmpb $0x2f, (%rcx)
leaq -0x1(%rcx), %rcx
jne 0x3408f
incl %eax
movl %eax, %r14d
subl %r14d, %ebp
leal 0x1(%rbp), %eax
movslq %eax, %rdi
movl $0x1, %esi
callq 0x3610
movq 0x48(%rsp), %rcx
movq %rax, (%rcx,%r15,8)
movq 0x40(%rsp), %rcx
movl %ebp, (%rcx,%r15,4)
movslq %r14d, %rcx
addq %rcx, %r13
movslq %ebp, %rdx
movq %rax, %rdi
movq %r13, %rsi
callq 0x3670
incq %r15
cmpq %rbx, %r15
movq 0x28(%rsp), %rbp
jne 0x34044
movq 0x30(%rsp), %r14
movq 0x20(%rsp), %r13
movq 0x38(%rsp), %r15
movl 0x4(%rsp), %eax
testl %eax, %eax
sete 0x3(%rsp)
je 0x34212
movl %eax, %eax
movq %rax, 0x90(%rsp)
xorl %r12d, %r12d
movq $0x0, 0x8(%rsp)
movq (%rbp,%r12,8), %rdi
leaq 0x11c20(%rip), %rsi     # 0x45d4f
callq 0x20308
movq %rax, (%r14,%r12,8)
testq %rax, %rax
je 0x3431b
movq %rax, %rdi
callq 0x235fb
movq %rax, %rcx
testq %r12, %r12
je 0x341fa
movq %r12, 0x70(%rsp)
movq 0x8(%rsp), %rax
movl (%rax), %eax
movq %rcx, 0x78(%rsp)
movl (%rcx), %ecx
cmpl %eax, %ecx
movl %eax, 0x54(%rsp)
movl %ecx, 0x50(%rsp)
cmovll %ecx, %eax
testl %eax, %eax
jle 0x341b0
movq 0x8(%rsp), %rcx
movq 0x8(%rcx), %r14
movq 0x78(%rsp), %rcx
movq 0x8(%rcx), %r13
movl %eax, %ebp
xorl %r15d, %r15d
movq (%r14,%r15,8), %r12
movq (%r13,%r15,8), %rbx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x35a0
testl %eax, %eax
jne 0x34227
incq %r15
cmpq %r15, %rbp
jne 0x34190
movl 0x54(%rsp), %ecx
movl 0x50(%rsp), %edx
cmpl %ecx, %edx
movq 0x78(%rsp), %rdi
jle 0x341da
movq 0x8(%rsp), %rax
movl %edx, (%rax)
movl %ecx, (%rdi)
movups 0x8(%rdi), %xmm0
movups 0x8(%rax), %xmm1
movups %xmm0, 0x8(%rax)
movups %xmm1, 0x8(%rdi)
callq 0x23577
movq 0x30(%rsp), %r14
movq 0x20(%rsp), %r13
movq 0x28(%rsp), %rbp
movq 0x38(%rsp), %r15
movq 0x70(%rsp), %r12
jmp 0x341ff
movq %rcx, 0x8(%rsp)
incq %r12
cmpq 0x90(%rsp), %r12
jne 0x34123
jmp 0x3426b
movq $0x0, 0x8(%rsp)
movb $0x1, 0x3(%rsp)
movq 0x18(%rsp), %r14
jmp 0x34274
movq 0x1bdba(%rip), %rax     # 0x4ffe8
movq (%rax), %rdi
movq 0x28(%rsp), %rax
movq 0x70(%rsp), %rcx
movq (%rax,%rcx,8), %r8
leaq 0x10eb4(%rip), %rsi     # 0x450fa
movq %r12, %rdx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x3750
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x34790
movb $0x1, 0x3(%rsp)
movq $0x0, 0x8(%rsp)
movq 0x18(%rsp), %r14
movl 0x4(%rsp), %eax
movq 0xd0(%rsp), %r12
testq %r15, %r15
je 0x343dc
movl (%r15), %r14d
testl %r14d, %r14d
jle 0x3439e
movq 0x8(%rsp), %rax
cmpl %r14d, (%rax)
jne 0x34367
movq 0x1bd42(%rip), %r15     # 0x4ffe8
leaq 0x10ef1(%rip), %rbp     # 0x4519e
xorl %r13d, %r13d
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rax
movq (%rax,%r13,8), %rdi
movq 0x38(%rsp), %rax
movq 0x8(%rax), %rax
movq (%rax,%r13,8), %rbx
movq %rbx, %rsi
callq 0x35a0
testl %eax, %eax
jne 0x342e6
incq %r13
movslq %r14d, %rax
cmpq %rax, %r13
jl 0x342b0
jmp 0x3439e
movq (%r15), %rdi
movq %rbp, %rsi
movq %rbx, %rdx
movq 0x68(%rsp), %rcx
xorl %eax, %eax
callq 0x3750
testq %r12, %r12
je 0x34253
movq 0x8(%rsp), %rax
movl (%rax), %r14d
jmp 0x342d6
movq %r15, %rdi
callq 0x5803
jmp 0x34253
movq 0x1bcc6(%rip), %rax     # 0x4ffe8
movq (%rax), %rdi
movq (%rbp,%r12,8), %rdx
leaq 0x10da2(%rip), %rsi     # 0x450d3
xorl %eax, %eax
callq 0x3750
testq %r12, %r12
je 0x34350
xorl %ebx, %ebx
movq (%r14,%rbx,8), %rdi
callq 0x21311
incq %rbx
cmpq %rbx, %r12
jne 0x3433f
movq %r14, %rdi
callq 0x3350
movq 0x18(%rsp), %rdi
callq 0x3350
jmp 0x34253
movq 0x1bc7a(%rip), %rax     # 0x4ffe8
movq (%rax), %rdi
leaq 0x10dce(%rip), %rsi     # 0x45146
movq 0x68(%rsp), %rdx
xorl %eax, %eax
callq 0x3750
testq %r12, %r12
je 0x34253
movq 0x8(%rsp), %rax
movl (%rax), %r14d
testl %r14d, %r14d
jg 0x3429f
movq 0x8(%rsp), %rdx
movl 0x30(%rdx), %eax
movq 0x38(%rsp), %rdi
movl 0x30(%rdi), %ecx
movl %ecx, 0x30(%rdx)
movl %eax, 0x30(%rdi)
movq 0x38(%rdx), %rax
movq 0x38(%rdi), %rcx
movq %rcx, 0x38(%rdx)
movq %rax, 0x38(%rdi)
callq 0x23577
movq 0x18(%rsp), %r14
movl 0x4(%rsp), %eax
movq 0x20(%rsp), %r13
movq 0x28(%rsp), %rbp
testq %r12, %r12
je 0x34453
leaq 0x60(%rsp), %rdx
leaq 0x5c(%rsp), %rcx
leaq 0x58(%rsp), %r8
movq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0x22e08
testl %eax, %eax
js 0x347ce
cmpl $0x0, 0x4(%rsp)
jle 0x3444a
movl 0x4(%rsp), %r14d
xorl %r15d, %r15d
movq (%rbp,%r15,8), %rdi
callq 0x2a134
movq %rax, %rbx
movl 0x60(%rsp), %esi
movl 0x5c(%rsp), %edx
movl 0x58(%rsp), %ecx
movq %rax, %rdi
callq 0x2a484
movq %rax, (%r13,%r15,8)
movq %rbx, %rdi
callq 0x298a9
incq %r15
cmpq %r15, %r14
jne 0x34414
movq 0x18(%rsp), %r14
movl 0x4(%rsp), %eax
xorl %ebx, %ebx
testl %eax, %eax
jle 0x344e8
movl %eax, %r13d
addq $0x18, %r14
xorl %ebp, %ebp
xorl %r15d, %r15d
movl %ebp, -0x18(%r14)
movl $0x1, %edi
movl $0x38, %esi
callq 0x3610
movq %rax, %r12
movq %rax, (%r14)
movq 0x30(%rsp), %rax
movq (%rax,%rbp,8), %rdi
movq 0x20(%rsp), %rax
movq (%rax,%rbp,8), %rsi
movq %r12, %rdx
callq 0x2acb6
testl %eax, %eax
js 0x344cf
movl (%r12), %eax
movl 0x4(%r12), %ecx
shlq $0x20, %rax
leal 0x2(,%rcx,2), %ecx
orq %rax, %rcx
movq 0x8(%r12), %rax
shrq $0x24, %rax
andl $0x1, %eax
orq %rcx, %rax
movq %r15, -0x8(%r14)
incq %r15
jmp 0x344d6
movq $-0x1, %rax
movq %rax, -0x10(%r14)
incq %rbp
addq $0x20, %r14
cmpq %rbp, %r13
jne 0x34469
jmp 0x344eb
xorl %r15d, %r15d
movl 0x14(%rsp), %ecx
testb $0x4, %cl
movl $0x1, %eax
cmovel 0xe0(%rsp), %eax
testb $0x2, %cl
cmovel %eax, %ebx
movw $0x77, 0x60(%rsp)
testl %ebx, %ebx
js 0x3452e
leaq 0x61(%rsp), %rdi
cmpl $0x9, %ebx
movl $0x9, %edx
cmovbl %ebx, %edx
leaq 0xfe4d(%rip), %rsi      # 0x44374
xorl %eax, %eax
callq 0x3400
movq 0x88(%rsp), %rdi
cmpb $0x2d, (%rdi)
movq 0x18(%rsp), %r14
movq 0x80(%rsp), %r13
jne 0x34552
cmpb $0x0, 0x1(%rdi)
je 0x347a2
leaq 0xad01(%rip), %rsi      # 0x3f25a
callq 0x20308
movq %rax, %r12
testq %rax, %rax
je 0x347c4
movq %r12, %rdi
movq 0x8(%rsp), %rbx
movq %rbx, %rsi
callq 0x237d0
movq %rbx, %rdi
callq 0x23577
testb $0x2, 0x14(%rsp)
jne 0x3459d
movl 0xd8(%rsp), %esi
movq %r12, %rdi
movl $0x100, %edx            # imm = 0x100
callq 0x20d7d
cmpl $0x2, 0x4(%rsp)
jb 0x345c1
movq %r13, %rbx
shrq %rbx
decq %rbx
movq %rbx, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x32c55
addq $-0x1, %rbx
jb 0x345ad
cmpq $-0x1, 0x8(%r14)
je 0x346f9
leaq 0xd7be(%rip), %r13      # 0x41d91
movq 0x18(%r14), %rbp
testb $0x1, 0x14(%rsp)
je 0x3461f
movq %rbp, %rdi
movq %r13, %rsi
callq 0x22374
testq %rax, %rax
je 0x345f9
movq %rbp, %rdi
movq %rax, %rsi
callq 0x224e7
movslq (%r14), %rax
movq 0x40(%rsp), %rcx
movl (%rcx,%rax,4), %ecx
incl %ecx
movq 0x48(%rsp), %rdx
movq (%rdx,%rax,8), %r8
movq %rbp, %rdi
movq %r13, %rsi
movl $0x5a, %edx
callq 0x222b8
movl 0x24(%rbp), %edx
movq 0x30(%rbp), %rcx
movq %r12, %rdi
movq %rbp, %rsi
callq 0x24229
movq %r14, %rbx
movslq (%r14), %r14
movq 0x30(%rsp), %rax
movq (%rax,%r14,8), %rdi
movq 0x20(%rsp), %rax
movq (%rax,%r14,8), %rsi
movq %rbp, %rdx
callq 0x2acb6
testl %eax, %eax
js 0x34687
movl (%rbp), %eax
movl 0x4(%rbp), %ecx
shlq $0x20, %rax
leal 0x2(,%rcx,2), %ecx
orq %rax, %rcx
movq 0x8(%rbp), %rax
shrq $0x24, %rax
andl $0x1, %eax
orq %rcx, %rax
movq %rax, 0x8(%rbx)
movq %r15, 0x10(%rbx)
incq %r15
movq %rbx, %r14
jmp 0x346b2
cmpl $-0x1, %eax
jne 0x346d1
movq 0x18(%rsp), %r14
movq $-0x1, 0x8(%r14)
movq 0x30(%rbp), %rdi
callq 0x3350
movq %rbp, %rdi
callq 0x3350
movq $0x0, 0x18(%r14)
xorl %edi, %edi
movq 0x80(%rsp), %rsi
movq %r14, %rdx
callq 0x32c55
cmpq $-0x1, 0x8(%r14)
jne 0x345d3
jmp 0x346f9
movq 0x1b910(%rip), %rax     # 0x4ffe8
movq (%rax), %rdi
movq 0x28(%rsp), %rax
movq (%rax,%r14,8), %rdx
leaq 0x10b6c(%rip), %rsi     # 0x45257
xorl %eax, %eax
callq 0x3750
movq 0x18(%rsp), %r14
jmp 0x346b2
testb $0x1, 0x14(%rsp)
movq 0x30(%rsp), %r15
movq 0x20(%rsp), %r13
je 0x34741
cmpb $0x0, 0x3(%rsp)
movq 0x48(%rsp), %rbp
jne 0x3472f
movl 0x4(%rsp), %ebx
xorl %r14d, %r14d
movq (%rbp,%r14,8), %rdi
callq 0x3350
incq %r14
cmpq %r14, %rbx
jne 0x3471d
movq %rbp, %rdi
callq 0x3350
movq 0x40(%rsp), %rdi
callq 0x3350
cmpb $0x0, 0x3(%rsp)
movl 0x4(%rsp), %eax
jne 0x3476c
movl %eax, %ebx
xorl %r14d, %r14d
movq (%r13,%r14,8), %rdi
callq 0x2ac9a
movq (%r15,%r14,8), %rdi
callq 0x21311
incq %r14
cmpq %r14, %rbx
jne 0x34751
movq %r12, %rdi
callq 0x21311
movq %r15, %rdi
callq 0x3350
movq 0x18(%rsp), %rdi
callq 0x3350
movq %r13, %rdi
callq 0x3350
xorl %eax, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1b84f(%rip), %rax     # 0x4fff8
movq (%rax), %rdi
callq 0x3030
leaq 0xaaa2(%rip), %rsi      # 0x3f25a
movl %eax, %edi
callq 0x20487
jmp 0x3455e
callq 0x5832
jmp 0x34253
movq 0x1b813(%rip), %rax     # 0x4ffe8
movq (%rax), %rdi
leaq 0x10a06(%rip), %rsi     # 0x451e5
leaq 0x10a3b(%rip), %rdx     # 0x45221
xorl %eax, %eax
callq 0x3750
jmp 0x34253