// File: /workspace/github_cpp_makefile_star2_O1/statgen[P]statgen/lib/samtools-0.1.7a-hybrid/bam_sort.c
// Function: bam_merge_core
void bam_merge_core(int by_qname, const char *out, const char *headers, int n, char * const *fn, int add_RG)
{
	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;

	if (headers) {
		tamFile fpheaders = sam_open(headers);
		if (fpheaders == 0) {
			fprintf(stderr, "[bam_merge_core] Cannot open file `%s'. Continue anyway.\n", headers);
		} else {
			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));
	// prepare RG tag
	if (add_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) {
		heap1_t *h;
		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;
		}
		hin = bam_header_read(fp[i]);
		if (i == 0) { // the first SAM
			hout = 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", headers);
					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", hheaders->target_name[j], headers);
				}
				swap_header_text(hout, hheaders);
				bam_header_destroy(hheaders);
				hheaders = NULL;
			}
		} else { // validate multiple baf
			if (hout->n_targets != hin->n_targets) {
				fprintf(stderr, "[bam_merge_core] file '%s' has different number of target sequences. Abort!\n", fn[i]);
				exit(1);
			}
			for (j = 0; j < hout->n_targets; ++j) {
				if (strcmp(hout->target_name[j], hin->target_name[j])) {
					fprintf(stderr, "[bam_merge_core] different target sequence name: '%s' != '%s' in file '%s'. Abort!\n",
							hout->target_name[j], hin->target_name[j], fn[i]);
					exit(1);
				}
			}
			bam_header_destroy(hin);
		}
		h = heap + i;
		h->i = i;
		h->b = (bam1_t*)calloc(1, sizeof(bam1_t));
		if (bam_read1(fp[i], h->b) >= 0) {
			h->pos = ((uint64_t)h->b->core.tid<<32) | (uint32_t)h->b->core.pos<<1 | bam1_strand(h->b);
			h->idx = idx++;
		}
		else h->pos = HEAP_EMPTY;
	}
	fpout = strcmp(out, "-")? bam_open(out, "w") : bam_dopen(fileno(stdout), "w");
	assert(fpout);
	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 (add_RG && bam_aux_get(b, "RG") == 0)
			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_read1(fp[heap->i], b)) >= 0) {
			heap->pos = ((uint64_t)b->core.tid<<32) | (uint32_t)b->core.pos<<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 (add_RG) {
		for (i = 0; i != n; ++i) free(RG[i]);
		free(RG); free(RG_len);
	}
	for (i = 0; i != n; ++i) bam_close(fp[i]);
	bam_close(fpout);
	free(fp); free(heap);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movl %r9d, 0x1c(%rsp)
movq %r8, 0x68(%rsp)
movl %ecx, 0xc(%rsp)
movl %edi, %ebx
testq %rdx, %rdx
movq %rsi, 0x80(%rsp)
movq %rdx, 0x70(%rsp)
je 0x1de87
movq %rdx, %r12
movq %rdx, %rdi
callq 0x14442
testq %rax, %rax
je 0x1e4da
movq %rax, %r14
movq %rax, %rdi
callq 0x13417
movq %rax, %r15
movq %r14, %rdi
callq 0x144ed
jmp 0x1de8a
xorl %r15d, %r15d
movl %ebx, 0x12494(%rip)     # 0x30324
movl 0xc(%rsp), %ebx
movslq %ebx, %r14
movl $0x8, %esi
movq %r14, %rdi
callq 0x3530
movq %rax, %rbp
movl $0x20, %esi
movq %r14, %rdi
callq 0x3530
movq %rax, 0x20(%rsp)
movl %ebx, %r13d
cmpl $0x0, 0x1c(%rsp)
movq %r13, 0x48(%rsp)
movq %r14, 0x60(%rsp)
je 0x1dfc8
movl $0x8, %esi
movq %r14, %rdi
callq 0x3530
movq %rax, 0x40(%rsp)
movl $0x4, %esi
movq %r14, %rdi
callq 0x3530
movq %rax, 0x38(%rsp)
testl %r14d, %r14d
je 0x1dfda
movq %r15, 0x10(%rsp)
xorl %r12d, %r12d
leaq 0x9ca7(%rip), %rbx      # 0x27bb4
movq 0x68(%rsp), %rax
movq (%rax,%r12,8), %r14
movq %r14, %rdi
callq 0x32d0
movq %rax, %r15
cmpl $0x5, %r15d
jl 0x1df4c
movl %r15d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
leaq (%r14,%rax), %rdi
addq $-0x4, %rdi
movq %rbx, %rsi
callq 0x34e0
leal -0x4(%r15), %ecx
testl %eax, %eax
cmovnel %r15d, %ecx
movl %ecx, %r15d
movl %r15d, %r13d
movl %r15d, %eax
sarl $0x1f, %eax
andl %r15d, %eax
incl %r15d
movq %r13, %rcx
testl %ecx, %ecx
jle 0x1df73
decl %r15d
cmpb $0x2f, -0x1(%r14,%rcx)
leaq -0x1(%rcx), %rcx
jne 0x1df5e
jmp 0x1df76
movl %eax, %r15d
subl %r15d, %r13d
leal 0x1(%r13), %eax
movslq %eax, %rdi
movl $0x1, %esi
callq 0x3530
movq 0x40(%rsp), %rcx
movq %rax, (%rcx,%r12,8)
movq 0x38(%rsp), %rcx
movl %r13d, (%rcx,%r12,4)
movslq %r15d, %rcx
addq %rcx, %r14
movslq %r13d, %rdx
movq %rax, %rdi
movq %r14, %rsi
callq 0x3580
incq %r12
movq 0x48(%rsp), %r13
cmpq %r13, %r12
jne 0x1df0d
movq 0x10(%rsp), %r15
jmp 0x1dfda
movq $0x0, 0x40(%rsp)
movq $0x0, 0x38(%rsp)
cmpl $0x0, 0xc(%rsp)
movq %rbp, 0x50(%rsp)
je 0x1e26f
xorl %ebx, %ebx
xorl %r12d, %r12d
movq $0x0, 0x30(%rsp)
movq 0x68(%rsp), %rax
leaq (%rax,%rbx,8), %rax
movq %rax, 0x58(%rsp)
movq (%rax), %rdi
leaq 0x9fb7(%rip), %rsi      # 0x27fc7
callq 0xfa04
movq %rax, %r14
movq %rax, (%rbp,%rbx,8)
testq %rax, %rax
je 0x1e230
movq %r14, %rdi
callq 0x11d21
movq %rax, %rdi
testq %rbx, %rbx
movq %r14, 0x78(%rsp)
je 0x1e0c7
movl (%r12), %r13d
cmpl (%rdi), %r13d
jne 0x1e52d
testl %r13d, %r13d
jle 0x1e169
movq %rbx, 0x90(%rsp)
movq %r15, 0x10(%rsp)
movq %r12, 0x88(%rsp)
movq 0x8(%r12), %rbp
movq %rdi, 0x28(%rsp)
movq 0x8(%rdi), %r15
xorl %ebx, %ebx
movq (%rbp,%rbx,8), %r14
movq (%r15,%rbx,8), %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x34e0
testl %eax, %eax
jne 0x1e4fd
incq %rbx
cmpq %rbx, %r13
jne 0x1e07a
movq 0x28(%rsp), %rdi
movq 0x88(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x50(%rsp), %rbp
movq 0x10(%rsp), %r15
movq 0x90(%rsp), %rbx
jmp 0x1e16e
testq %r15, %r15
movq %rdi, 0x28(%rsp)
je 0x1e222
movl (%r15), %eax
testl %eax, %eax
jle 0x1e13e
cmpl %eax, (%rdi)
jne 0x1e247
cmpl $0x0, (%rdi)
jle 0x1e13e
xorl %r12d, %r12d
movq 0x8(%rdi), %rax
movq %rdi, %rbp
movq (%rax,%r12,8), %rdi
movq 0x8(%r15), %rax
movq (%rax,%r12,8), %r14
movq %r14, %rsi
callq 0x34e0
testl %eax, %eax
jne 0x1e11c
incq %r12
movslq (%rbp), %rax
cmpq %rax, %r12
movq %rbp, %rdi
jl 0x1e0ec
jmp 0x1e13e
movq 0x10ec5(%rip), %rax     # 0x2efe8
movq (%rax), %rdi
leaq 0x96aa(%rip), %rsi      # 0x277d7
movq %r14, %rdx
movq 0x70(%rsp), %rcx
xorl %eax, %eax
callq 0x3630
jmp 0x1e10b
movq 0x50(%rsp), %rbp
movl 0x30(%rdi), %eax
movl 0x30(%r15), %ecx
movl %ecx, 0x30(%rdi)
movl %eax, 0x30(%r15)
movq 0x38(%rdi), %rax
movq 0x38(%r15), %rcx
movq %rcx, 0x38(%rdi)
movq %rax, 0x38(%r15)
movq %r15, %rdi
xorl %r15d, %r15d
jmp 0x1e16e
movq %r12, 0x28(%rsp)
movq %r15, 0x10(%rsp)
callq 0x11c9f
movq %rbx, %r13
shlq $0x5, %rbx
movq 0x20(%rsp), %r15
leaq (%r15,%rbx), %r12
movl %r13d, (%r15,%rbx)
movl $0x1, %edi
movl $0x38, %esi
callq 0x3530
movq %rax, %r14
movq %rax, 0x18(%r15,%rbx)
movq %r13, %rbx
movq (%rbp,%r13,8), %rdi
movq %rax, %rsi
callq 0x1204b
testl %eax, %eax
js 0x1e1ee
movl (%r14), %eax
movl 0x4(%r14), %ecx
shlq $0x20, %rax
addl %ecx, %ecx
orq %rax, %rcx
movq 0x8(%r14), %rax
shrq $0x24, %rax
andl $0x1, %eax
orq %rcx, %rax
movq %rax, 0x8(%r12)
movq 0x30(%rsp), %rax
movq %rax, 0x10(%r12)
incq %rax
movq %rax, 0x30(%rsp)
jmp 0x1e1f7
movq $-0x1, 0x8(%r12)
movq 0x28(%rsp), %r12
movq 0x10(%rsp), %r15
movq 0x48(%rsp), %r13
movq 0x78(%rsp), %r14
testq %r14, %r14
je 0x1e2a7
incq %rbx
cmpq %r13, %rbx
jne 0x1dff8
jmp 0x1e27b
movq $0x0, 0x10(%rsp)
jmp 0x1e178
movq 0x58(%rsp), %rdi
movq %rbx, %rsi
movq %rbp, %rdx
movq 0x20(%rsp), %rcx
callq 0x4bff
jmp 0x1e20b
movq 0x10d9a(%rip), %rax     # 0x2efe8
movq %rdi, %r14
movq (%rax), %rdi
leaq 0x9525(%rip), %rsi      # 0x27780
movq 0x70(%rsp), %rdx
xorl %eax, %eax
callq 0x3630
movq %r14, %rdi
jmp 0x1e0e4
movq $0x0, 0x30(%rsp)
xorl %r12d, %r12d
leaq 0x6b34(%rip), %rsi      # 0x24db6
movq 0x80(%rsp), %rbx
movq %rbx, %rdi
callq 0x34e0
testl %eax, %eax
je 0x1e2b9
leaq 0x60f8(%rip), %rsi      # 0x24395
movq %rbx, %rdi
callq 0xfa04
jmp 0x1e2d6
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10d38(%rip), %rax     # 0x2eff8
movq (%rax), %rdi
callq 0x3030
leaq 0x60c6(%rip), %rsi      # 0x24395
movl %eax, %edi
callq 0xfb6d
movq %rax, %r15
movq 0x60(%rsp), %r14
testq %rax, %rax
je 0x1e537
movq %r15, %rdi
movq %r12, %rsi
callq 0x11ef4
movq %r12, %rdi
callq 0x11c9f
cmpl $0x2, 0xc(%rsp)
movq 0x20(%rsp), %r12
jb 0x1e323
movq %r14, %rbx
shrq %rbx
decq %rbx
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x1cb56
addq $-0x1, %rbx
jb 0x1e30f
cmpq $-0x1, 0x8(%r12)
je 0x1e457
leaq 0x95e7(%rip), %rbx      # 0x2791d
leaq 0x6b9c(%rip), %r14      # 0x24ed9
movq 0x18(%r12), %r13
cmpl $0x0, 0x1c(%rsp)
je 0x1e380
movq %r13, %rdi
movq %r14, %rsi
callq 0x1110a
testq %rax, %rax
jne 0x1e380
movslq (%r12), %rax
movq 0x38(%rsp), %rcx
movl (%rcx,%rax,4), %ecx
incl %ecx
movq 0x40(%rsp), %rdx
movq (%rdx,%rax,8), %r8
movq %r13, %rdi
movq %r14, %rsi
movl $0x5a, %edx
callq 0x11050
movl 0x24(%r13), %edx
movq 0x30(%r13), %rcx
movq %r15, %rdi
movq %r13, %rsi
callq 0x122fa
movslq (%r12), %rbp
movq 0x50(%rsp), %rax
movq (%rax,%rbp,8), %rdi
movq %r13, %rsi
callq 0x1204b
testl %eax, %eax
js 0x1e3e4
movl (%r13), %eax
movl 0x4(%r13), %ecx
shlq $0x20, %rax
addl %ecx, %ecx
orq %rax, %rcx
movq 0x8(%r13), %rax
shrq $0x24, %rax
andl $0x1, %eax
orq %rcx, %rax
movq %rax, 0x8(%r12)
movq 0x30(%rsp), %rax
movq %rax, 0x10(%r12)
incq %rax
movq %rax, 0x30(%rsp)
jmp 0x1e411
cmpl $-0x1, %eax
jne 0x1e433
movq 0x20(%rsp), %r12
movq $-0x1, 0x8(%r12)
movq 0x30(%r13), %rdi
callq 0x32c0
movq %r13, %rdi
callq 0x32c0
movq $0x0, 0x18(%r12)
xorl %edi, %edi
movq 0x60(%rsp), %rsi
movq %r12, %rdx
callq 0x1cb56
cmpq $-0x1, 0x8(%r12)
movq 0x50(%rsp), %rbp
jne 0x1e33d
jmp 0x1e457
movq 0x10bae(%rip), %rax     # 0x2efe8
movq (%rax), %rdi
movq 0x68(%rsp), %rax
movq (%rax,%rbp,8), %rdx
movq %rbx, %rsi
xorl %eax, %eax
callq 0x3630
movq 0x20(%rsp), %r12
jmp 0x1e411
cmpl $0x0, 0x1c(%rsp)
movq 0x48(%rsp), %r14
je 0x1e494
cmpl $0x0, 0xc(%rsp)
movq 0x40(%rsp), %r12
je 0x1e482
xorl %ebx, %ebx
movq (%r12,%rbx,8), %rdi
callq 0x32c0
incq %rbx
cmpq %rbx, %r14
jne 0x1e471
movq %r12, %rdi
callq 0x32c0
movq 0x38(%rsp), %rdi
callq 0x32c0
cmpl $0x0, 0xc(%rsp)
je 0x1e4af
xorl %ebx, %ebx
movq (%rbp,%rbx,8), %rdi
callq 0x1050e
incq %rbx
cmpq %rbx, %r14
jne 0x1e49d
movq %r15, %rdi
callq 0x1050e
movq %rbp, %rdi
callq 0x32c0
movq 0x20(%rsp), %rdi
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x32c0
movq 0x10b07(%rip), %rax     # 0x2efe8
movq (%rax), %rdi
leaq 0x9234(%rip), %rsi      # 0x2771f
xorl %r15d, %r15d
movq %r12, %rdx
xorl %eax, %eax
callq 0x3630
jmp 0x1de8a
movq 0x10ae4(%rip), %rax     # 0x2efe8
movq (%rax), %rdi
movq 0x58(%rsp), %rax
movq (%rax), %r8
leaq 0x9354(%rip), %rsi      # 0x2786a
movq %r14, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x3630
movl $0x1, %edi
callq 0x3130
movq 0x58(%rsp), %rdi
callq 0x4bd8
leaq 0x9380(%rip), %rdi      # 0x278be
leaq 0x937f(%rip), %rsi      # 0x278c4
leaq 0x9383(%rip), %rcx      # 0x278cf
movl $0xa7, %edx
callq 0x3140