// File: /workspace/github_c_makefile_star2_O1/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, 0x4(%rsp)
movq %r8, 0x40(%rsp)
movl %ecx, %ebp
movq %rdx, %r15
movq %rsi, 0x88(%rsp)
movl %edi, %ebx
testq %rdx, %rdx
je 0x3369e
movq %r15, %rdi
callq 0x27492
movq %rax, %r14
testq %rax, %rax
je 0x33e78
movq %r14, %rdi
callq 0x26091
movq %rax, 0x18(%rsp)
movq %r14, %rdi
callq 0x2753d
testq %r14, %r14
jne 0x336a7
jmp 0x33e93
movq $0x0, 0x18(%rsp)
movq %r15, 0x60(%rsp)
movl %ebx, 0x1ccbe(%rip)     # 0x50370
movslq %ebp, %rbx
movl $0x8, %esi
movq %rbx, %rdi
callq 0x3610
movq %rax, %r14
movl $0x20, %esi
movq %rbx, %rdi
callq 0x3610
movq %rax, %r15
movl $0x8, %esi
movq %rbx, %rdi
callq 0x3610
movq %rax, 0x38(%rsp)
testb $0x1, 0x4(%rsp)
movl %ebp, (%rsp)
movq %r14, 0x10(%rsp)
movq %r15, 0x8(%rsp)
movq %rbx, 0x80(%rsp)
jne 0x3371a
movq $0x0, 0x30(%rsp)
movq $0x0, 0x28(%rsp)
jmp 0x3380f
movl $0x8, %esi
movq %rbx, %rdi
callq 0x3610
movq %rax, 0x30(%rsp)
movl $0x4, %esi
movq %rbx, %rdi
callq 0x3610
movq %rax, 0x28(%rsp)
testl %ebx, %ebx
je 0x3380f
movl %ebp, %r14d
xorl %r15d, %r15d
leaq 0xd902(%rip), %rbx      # 0x41055
movq 0x40(%rsp), %rax
movq (%rax,%r15,8), %r13
movq %r13, %rdi
callq 0x3360
movq %rax, %r12
cmpl $0x5, %r12d
jl 0x33793
movl %r12d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
leaq (%rax,%r13), %rdi
addq $-0x4, %rdi
movq %rbx, %rsi
callq 0x35a0
leal -0x4(%r12), %ecx
testl %eax, %eax
cmovnel %r12d, %ecx
movl %ecx, %r12d
movl %r12d, %ebp
movl %r12d, %eax
sarl $0x1f, %eax
andl %r12d, %eax
incl %r12d
movq %rbp, %rcx
testl %ecx, %ecx
jle 0x337ba
decl %r12d
cmpb $0x2f, -0x1(%r13,%rcx)
leaq -0x1(%rcx), %rcx
jne 0x337a5
jmp 0x337bd
movl %eax, %r12d
subl %r12d, %ebp
leal 0x1(%rbp), %eax
movslq %eax, %rdi
movl $0x1, %esi
callq 0x3610
movq 0x30(%rsp), %rcx
movq %rax, (%rcx,%r15,8)
movq 0x28(%rsp), %rcx
movl %ebp, (%rcx,%r15,4)
movslq %r12d, %rcx
addq %rcx, %r13
movslq %ebp, %rdx
movq %rax, %rdi
movq %r13, %rsi
callq 0x3670
incq %r15
cmpq %r14, %r15
jne 0x33753
movl (%rsp), %ebp
movq 0x10(%rsp), %r14
movq 0x8(%rsp), %r15
testl %ebp, %ebp
je 0x339bd
movl %ebp, %r13d
xorl %ebx, %ebx
xorl %r12d, %r12d
movq %r13, 0x68(%rsp)
movq 0x40(%rsp), %rax
leaq (%rax,%rbx,8), %rax
movq %rax, 0x70(%rsp)
movq (%rax), %rdi
leaq 0x11433(%rip), %rsi     # 0x44c6f
callq 0x1fdb8
movq %rax, (%r14,%rbx,8)
testq %rax, %rax
je 0x339a6
movq %rax, %rdi
callq 0x22efa
movq %rax, %rdi
testq %rbx, %rbx
je 0x33927
movq %rbx, 0x78(%rsp)
movl (%r12), %eax
movl (%rdi), %ecx
cmpl %eax, %ecx
movl %eax, %ebx
cmovll %ecx, %ebx
testl %ebx, %ebx
setle %bpl
jle 0x33957
movl %ecx, 0x48(%rsp)
movl %eax, 0x4c(%rsp)
movq %r12, 0x20(%rsp)
movq 0x8(%r12), %r13
movq %rdi, 0x90(%rsp)
movq 0x8(%rdi), %r12
movq (%r13), %r14
movq (%r12), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x35a0
testl %eax, %eax
jne 0x338e6
movl %ebx, %ebp
movl $0x1, %ebx
cmpq %rbx, %rbp
je 0x3392f
movq (%r13,%rbx,8), %r14
movq (%r12,%rbx,8), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x35a0
incq %rbx
testl %eax, %eax
je 0x338bc
decq %rbx
cmpq %rbp, %rbx
setae %bpl
movq 0x1b6fb(%rip), %rax     # 0x4efe8
movq (%rax), %rdi
movq 0x70(%rsp), %rax
movq (%rax), %r8
leaq 0x1071b(%rip), %rsi     # 0x4401a
movq %r14, %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0x3750
movq 0x10(%rsp), %r14
movq 0x8(%rsp), %r15
movq 0x20(%rsp), %r12
movq 0x68(%rsp), %r13
movq 0x78(%rsp), %rbx
jmp 0x3398f
movb $0x1, %bpl
movq %rdi, %r12
jmp 0x3398f
setbe %bpl
movq 0x10(%rsp), %r14
movq 0x8(%rsp), %r15
movq 0x20(%rsp), %r12
movq 0x68(%rsp), %r13
movq 0x90(%rsp), %rdi
movl 0x4c(%rsp), %eax
movl 0x48(%rsp), %ecx
cmpl %eax, %ecx
movq 0x78(%rsp), %rbx
jle 0x3398a
movl %ecx, (%r12)
movl %eax, (%rdi)
movq 0x8(%r12), %rax
movq 0x8(%rdi), %rcx
movq %rcx, 0x8(%r12)
movq %rax, 0x8(%rdi)
movq 0x10(%r12), %rax
movq 0x10(%rdi), %rcx
movq %rcx, 0x10(%r12)
movq %rax, 0x10(%rdi)
callq 0x22e78
testb %bpl, %bpl
je 0x33e93
incq %rbx
cmpq %r13, %rbx
jne 0x33824
jmp 0x339c0
movq 0x70(%rsp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
movq %r15, %rcx
callq 0x5549
xorl %ebp, %ebp
jmp 0x3398f
xorl %r12d, %r12d
movq 0xd0(%rsp), %rbx
movq 0x18(%rsp), %r15
testq %r15, %r15
movl (%rsp), %ebp
je 0x33a81
movl (%r15), %eax
testl %eax, %eax
jle 0x33a52
cmpl %eax, (%r12)
jne 0x33eb4
cmpl $0x0, (%r12)
jle 0x33a52
movq 0x1b5f0(%rip), %rbp     # 0x4efe8
leaq 0x106bf(%rip), %r14     # 0x440be
xorl %r13d, %r13d
movq 0x8(%r12), %rax
movq (%rax,%r13,8), %rdi
movq 0x8(%r15), %rax
movq (%rax,%r13,8), %r15
movq %r15, %rsi
callq 0x35a0
testl %eax, %eax
jne 0x33a32
incq %r13
movslq (%r12), %rax
cmpq %rax, %r13
movq 0x18(%rsp), %r15
jl 0x33a02
jmp 0x33a52
movq (%rbp), %rdi
movq %r14, %rsi
movq %r15, %rdx
movq 0x60(%rsp), %rcx
xorl %eax, %eax
callq 0x3750
testq %rbx, %rbx
jne 0x33a1f
jmp 0x33e93
movl 0x30(%r12), %eax
movl 0x30(%r15), %ecx
movl %ecx, 0x30(%r12)
movl %eax, 0x30(%r15)
movq 0x38(%r12), %rax
movq 0x38(%r15), %rcx
movq %rcx, 0x38(%r12)
movq %rax, 0x38(%r15)
movq %r15, %rdi
callq 0x22e78
movl (%rsp), %ebp
testq %rbx, %rbx
movq %r12, 0x20(%rsp)
je 0x33b05
leaq 0x58(%rsp), %rdx
leaq 0x54(%rsp), %rcx
leaq 0x50(%rsp), %r8
movq %r12, %rdi
movq %rbx, %rsi
callq 0x22482
movl %eax, %ebx
testl %eax, %eax
js 0x33eaa
testl %ebp, %ebp
movq 0x38(%rsp), %r13
movq 0x40(%rsp), %rbp
jle 0x33afa
movl (%rsp), %r15d
xorl %r12d, %r12d
movq (%rbp,%r12,8), %rdi
callq 0x29a74
movq %rax, %r14
movl 0x58(%rsp), %esi
movl 0x54(%rsp), %edx
movl 0x50(%rsp), %ecx
movq %rax, %rdi
callq 0x29dc7
movq %rax, (%r13,%r12,8)
movq %r14, %rdi
callq 0x291f9
incq %r12
cmpq %r12, %r15
jne 0x33ac4
testl %ebx, %ebx
movl (%rsp), %ebp
js 0x33e93
xorl %r14d, %r14d
testl %ebp, %ebp
jle 0x33bab
movl %ebp, %r13d
xorl %ebp, %ebp
movq 0x8(%rsp), %r12
xorl %r15d, %r15d
movl %ebp, (%r12)
movl $0x1, %edi
movl $0x38, %esi
callq 0x3610
movq %rax, %rbx
movq %rax, 0x18(%r12)
movq 0x10(%rsp), %rax
movq (%rax,%rbp,8), %rdi
movq 0x38(%rsp), %rax
movq (%rax,%rbp,8), %rsi
movq %rbx, %rdx
callq 0x2a30d
testl %eax, %eax
js 0x33b89
movl (%rbx), %eax
movl 0x4(%rbx), %ecx
shlq $0x20, %rax
leal 0x2(,%rcx,2), %ecx
orq %rax, %rcx
movq 0x8(%rbx), %rax
shrq $0x24, %rax
andl $0x1, %eax
orq %rcx, %rax
movq %rax, 0x8(%r12)
movq %r15, %rax
incq %r15
movl $0x10, %ecx
jmp 0x33b95
movq $-0x1, %rax
movl $0x8, %ecx
movq %rax, (%r12,%rcx)
incq %rbp
addq $0x20, %r12
cmpq %rbp, %r13
jne 0x33b1d
jmp 0x33bae
xorl %r15d, %r15d
movl 0x4(%rsp), %ecx
testb $0x4, %cl
movl $0x1, %eax
cmovel 0xe0(%rsp), %eax
testb $0x2, %cl
cmovel %eax, %r14d
movw $0x77, 0x58(%rsp)
testl %r14d, %r14d
js 0x33bf5
leaq 0x59(%rsp), %rdi
cmpl $0x9, %r14d
movl $0x9, %edx
cmovll %r14d, %edx
leaq 0xf6a4(%rip), %rsi      # 0x43292
xorl %eax, %eax
callq 0x3400
leaq 0xb25c(%rip), %rsi      # 0x3ee58
movq 0x88(%rsp), %rbx
movq %rbx, %rdi
callq 0x35a0
testl %eax, %eax
movl (%rsp), %ebp
movq 0x8(%rsp), %r13
je 0x33c29
leaq 0xa61b(%rip), %rsi      # 0x3e23a
movq %rbx, %rdi
callq 0x1fdb8
jmp 0x33c46
movq 0x1b3c8(%rip), %rax     # 0x4eff8
movq (%rax), %rdi
callq 0x3030
leaq 0xa5fb(%rip), %rsi      # 0x3e23a
movl %eax, %edi
callq 0x1ff6b
movq %rax, %rbx
testq %rax, %rax
je 0x33e8e
movq %rbx, %rdi
movq 0x20(%rsp), %r14
movq %r14, %rsi
callq 0x230c9
movq %r14, %rdi
callq 0x22e78
testb $0x2, 0x4(%rsp)
jne 0x33c85
movl 0xd8(%rsp), %esi
movq %rbx, %rdi
movl $0x100, %edx            # imm = 0x100
callq 0x205f8
cmpl $0x2, %ebp
movq 0x80(%rsp), %rbp
jb 0x33caf
movq %rbp, %r14
shrq %r14
decq %r14
movq %r14, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x32323
addq $-0x1, %r14
jb 0x33c9b
cmpq $-0x1, 0x8(%r13)
je 0x33de5
movq 0x18(%r13), %r12
testb $0x1, 0x4(%rsp)
je 0x33d0f
movq %r12, %rdi
leaq 0xd07a(%rip), %rsi      # 0x40d49
callq 0x21c82
testq %rax, %rax
je 0x33ce4
movq %r12, %rdi
movq %rax, %rsi
callq 0x21e01
movslq (%r13), %rax
movq 0x28(%rsp), %rcx
movl (%rcx,%rax,4), %ecx
incl %ecx
movq 0x30(%rsp), %rdx
movq (%rdx,%rax,8), %r8
movq %r12, %rdi
leaq 0xd044(%rip), %rsi      # 0x40d49
movl $0x5a, %edx
callq 0x21bc8
movl 0x24(%r12), %edx
movq 0x30(%r12), %rcx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x23ba0
movq %r13, %r14
movslq (%r13), %r13
movq 0x10(%rsp), %rax
movq (%rax,%r13,8), %rdi
movq 0x38(%rsp), %rax
movq (%rax,%r13,8), %rsi
movq %r12, %rdx
callq 0x2a30d
testl %eax, %eax
js 0x33d7f
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 %rax, 0x8(%r14)
movq %r15, 0x10(%r14)
incq %r15
movq %r14, %r13
jmp 0x33dab
cmpl $-0x1, %eax
jne 0x33dbd
movq 0x8(%rsp), %r13
movq $-0x1, 0x8(%r13)
movq 0x30(%r12), %rdi
callq 0x3350
movq %r12, %rdi
callq 0x3350
movq $0x0, 0x18(%r13)
xorl %edi, %edi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x32323
jmp 0x33caf
movq 0x1b224(%rip), %rax     # 0x4efe8
movq (%rax), %rdi
movq 0x40(%rsp), %rax
movq (%rax,%r13,8), %rdx
leaq 0x103a0(%rip), %rsi     # 0x44177
xorl %eax, %eax
callq 0x3750
movq 0x8(%rsp), %r13
jmp 0x33dab
testb $0x1, 0x4(%rsp)
movl (%rsp), %eax
je 0x33e25
movl %eax, %ebp
testl %eax, %eax
movq 0x30(%rsp), %r12
je 0x33e11
movl %ebp, %r14d
xorl %r15d, %r15d
movq (%r12,%r15,8), %rdi
callq 0x3350
incq %r15
cmpq %r15, %r14
jne 0x33e00
movq %r12, %rdi
callq 0x3350
movq 0x28(%rsp), %rdi
callq 0x3350
movl %ebp, %eax
testl %eax, %eax
movq 0x10(%rsp), %r12
movq 0x38(%rsp), %rbp
je 0x33e54
movl %eax, %r14d
xorl %r15d, %r15d
movq (%rbp,%r15,8), %rdi
callq 0x2a2f1
movq (%r12,%r15,8), %rdi
callq 0x20aae
incq %r15
cmpq %r15, %r14
jne 0x33e39
movq %rbx, %rdi
callq 0x20aae
movq %r12, %rdi
callq 0x3350
movq %r13, %rdi
callq 0x3350
movq %rbp, %rdi
callq 0x3350
xorl %eax, %eax
jmp 0x33e98
movq %r15, %rdi
callq 0x551a
movq $0x0, 0x18(%rsp)
jmp 0x33694
callq 0x55d1
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x55ac
jmp 0x33afa
movq 0x1b12d(%rip), %rax     # 0x4efe8
movq (%rax), %rdi
leaq 0x101a1(%rip), %rsi     # 0x44066
movq 0x60(%rsp), %rdx
xorl %eax, %eax
callq 0x3750
testq %rbx, %rbx
jne 0x339ea
jmp 0x33e93