// File: /workspace/github_cpp_makefile_star2_O3/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, 0x70(%rsp)
movl %ecx, %ebp
movl %edi, %ebx
testq %rdx, %rdx
movq %rsi, 0x88(%rsp)
movq %rdx, 0x50(%rsp)
je 0x1ddfb
movq %rdx, %rdi
callq 0x14b45
testq %rax, %rax
je 0x1e44f
movq %rax, %r14
movq %rax, %rdi
callq 0x13ad0
movq %rax, 0x28(%rsp)
movq %r14, %rdi
callq 0x14be8
jmp 0x1de04
movq $0x0, 0x28(%rsp)
movl %ebx, 0x1151a(%rip)     # 0x2f324
movslq %ebp, %r14
movl $0x8, %esi
movq %r14, %rdi
callq 0x3530
movq %rax, %rbx
movl $0x20, %esi
movq %r14, %rdi
callq 0x3530
movq %rax, %r15
cmpl $0x0, 0x1c(%rsp)
movl %ebp, 0xc(%rsp)
movq %rbx, 0x20(%rsp)
movq %rax, 0x38(%rsp)
movq %r14, 0x30(%rsp)
je 0x1df3f
movl $0x8, %esi
movq %r14, %rdi
callq 0x3530
movq %rax, %rbx
movl $0x4, %esi
movq %r14, %rdi
callq 0x3530
movq %rax, 0x40(%rsp)
testl %r14d, %r14d
je 0x1e20b
movq %rbx, 0x10(%rsp)
movl %ebp, %r13d
xorl %ebp, %ebp
movq 0x70(%rsp), %rax
movq (%rax,%rbp,8), %r14
movq %r14, %rdi
callq 0x32d0
movq %rax, %r12
cmpl $0x5, %r12d
jl 0x1dec4
movl %r12d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
leaq (%r14,%rax), %rdi
addq $-0x4, %rdi
leaq 0x8e03(%rip), %rsi      # 0x26cb4
callq 0x34e0
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 (%r14,%rax), %rcx
decq %rcx
movl %r12d, %eax
testl %eax, %eax
jle 0x1deed
decl %eax
cmpb $0x2f, (%rcx)
leaq -0x1(%rcx), %rcx
jne 0x1deda
incl %eax
movl %eax, %ebx
subl %ebx, %r12d
leal 0x1(%r12), %eax
movslq %eax, %rdi
movl $0x1, %esi
callq 0x3530
movq 0x10(%rsp), %rcx
movq %rax, (%rcx,%rbp,8)
movq 0x40(%rsp), %rcx
movl %r12d, (%rcx,%rbp,4)
movslq %ebx, %rcx
addq %rcx, %r14
movslq %r12d, %rdx
movq %rax, %rdi
movq %r14, %rsi
callq 0x3580
incq %rbp
cmpq %r13, %rbp
jne 0x1de80
movl 0xc(%rsp), %ebp
movq 0x20(%rsp), %rbx
jmp 0x1df51
movq $0x0, 0x10(%rsp)
movq $0x0, 0x40(%rsp)
testl %ebp, %ebp
sete 0xb(%rsp)
je 0x1e1e2
movl %ebp, %eax
movq %rax, 0x90(%rsp)
xorl %r13d, %r13d
xorl %r12d, %r12d
movq $0x0, 0x60(%rsp)
movq 0x30(%rsp), %r14
movq 0x70(%rsp), %rax
leaq (%rax,%r13,8), %rax
movq %rax, 0x58(%rsp)
movq (%rax), %rdi
leaq 0x9133(%rip), %rsi      # 0x270c7
callq 0x10048
movq %rax, %rbp
movq %rax, (%rbx,%r13,8)
testq %rax, %rax
je 0x1e0b2
movq %rbp, %rdi
callq 0x12383
movq %rax, %r14
testq %r13, %r13
movq %r13, 0x48(%rsp)
movq %rbp, 0x68(%rsp)
je 0x1e03c
movq %r12, 0x78(%rsp)
movl (%r12), %ebp
movq %r14, 0x80(%rsp)
cmpl (%r14), %ebp
jne 0x1e4aa
testl %ebp, %ebp
jle 0x1e01b
movq 0x78(%rsp), %rax
movq 0x8(%rax), %r15
movq 0x80(%rsp), %rax
movq 0x8(%rax), %r12
xorl %ebx, %ebx
movq (%r15,%rbx,8), %r14
movq (%r12,%rbx,8), %r13
movq %r14, %rdi
movq %r13, %rsi
callq 0x34e0
testl %eax, %eax
jne 0x1e47a
incq %rbx
cmpq %rbx, %rbp
jne 0x1dff8
movq 0x80(%rsp), %rdi
callq 0x122ff
movq 0x68(%rsp), %rbp
movq 0x78(%rsp), %r12
movq 0x38(%rsp), %r15
jmp 0x1e132
movq 0x28(%rsp), %rbx
testq %rbx, %rbx
je 0x1e0ad
movl (%rbx), %ebp
testl %ebp, %ebp
jle 0x1e0f2
cmpl %ebp, (%r14)
jne 0x1e0ca
xorl %r13d, %r13d
movq %r14, %r12
movq 0x8(%r14), %rax
movq (%rax,%r13,8), %rdi
movq 0x8(%rbx), %rax
movq (%rax,%r13,8), %r14
movq %r14, %rsi
callq 0x34e0
testl %eax, %eax
jne 0x1e087
incq %r13
movslq %ebp, %rax
cmpq %rax, %r13
movq %r12, %r14
jl 0x1e058
jmp 0x1e0f2
movq 0xff5a(%rip), %rax      # 0x2dfe8
movq (%rax), %rdi
leaq 0x883f(%rip), %rsi      # 0x268d7
movq %r14, %rdx
movq 0x50(%rsp), %rcx
xorl %eax, %eax
callq 0x3630
movl (%r12), %ebp
jmp 0x1e077
movq %r14, %r12
jmp 0x1e129
movq 0x58(%rsp), %rdi
movq %r13, %rsi
movq %rbx, %rdx
movq %r15, %rcx
callq 0x4ea6
jmp 0x1e1bd
movq 0xff17(%rip), %rax      # 0x2dfe8
movq (%rax), %rdi
leaq 0x87a5(%rip), %rsi      # 0x26880
movq 0x50(%rsp), %rdx
xorl %eax, %eax
callq 0x3630
movl (%r14), %ebp
testl %ebp, %ebp
jg 0x1e055
movl 0x30(%r14), %eax
movl 0x30(%rbx), %ecx
movl %ecx, 0x30(%r14)
movl %eax, 0x30(%rbx)
movq 0x38(%r14), %rax
movq 0x38(%rbx), %rcx
movq %r14, %r12
movq %rcx, 0x38(%r14)
movq %rax, 0x38(%rbx)
movq %rbx, %rdi
callq 0x122ff
movq 0x20(%rsp), %rax
movq 0x48(%rsp), %rcx
movq (%rax,%rcx,8), %rbp
movq $0x0, 0x28(%rsp)
movq 0x48(%rsp), %rax
movq %rax, %rbx
shlq $0x5, %rbx
leaq (%r15,%rbx), %r13
movl %eax, (%r15,%rbx)
movl $0x1, %edi
movl $0x38, %esi
callq 0x3530
movq %rax, %r14
movq %rax, 0x18(%r15,%rbx)
movq %rbp, %rdi
movq %rax, %rsi
callq 0x126b5
testl %eax, %eax
js 0x1e1a1
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(%r13)
movq 0x60(%rsp), %rax
movq %rax, 0x10(%r13)
incq %rax
movq %rax, 0x60(%rsp)
jmp 0x1e1a9
movq $-0x1, 0x8(%r13)
movq 0x20(%rsp), %rbx
movq 0x30(%rsp), %r14
movq 0x48(%rsp), %r13
movq 0x68(%rsp), %rbp
testq %rbp, %rbp
je 0x1e1f9
incq %r13
cmpq 0x90(%rsp), %r13
jne 0x1df7c
movq %r12, %r13
movq 0x10(%rsp), %rbx
movq 0x60(%rsp), %r12
jmp 0x1e216
xorl %r12d, %r12d
movb $0x1, 0xb(%rsp)
xorl %r13d, %r13d
movq 0x10(%rsp), %rbx
movq 0x30(%rsp), %r14
jmp 0x1e216
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x1, 0xb(%rsp)
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x88(%rsp), %rdi
cmpb $0x2d, (%rdi)
jne 0x1e22d
cmpb $0x0, 0x1(%rdi)
je 0x1e42d
leaq 0x5179(%rip), %rsi      # 0x233ad
callq 0x10048
movq %rax, %rbp
movq %rbx, 0x10(%rsp)
testq %rax, %rax
je 0x1e4b4
movq %rbp, %rdi
movq %r13, %rsi
callq 0x1255d
movq %r13, %rdi
callq 0x122ff
cmpl $0x2, 0xc(%rsp)
jb 0x1e281
movq %r14, %rbx
shrq %rbx
decq %rbx
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x1d1a4
addq $-0x1, %rbx
jb 0x1e26d
cmpq $-0x1, 0x8(%r15)
je 0x1e3a0
leaq 0x5c9e(%rip), %r14      # 0x23f31
movq 0x18(%r15), %r13
cmpl $0x0, 0x1c(%rsp)
je 0x1e2d4
movq %r13, %rdi
movq %r14, %rsi
callq 0x1175c
testq %rax, %rax
jne 0x1e2d4
movslq (%r15), %rax
movq 0x40(%rsp), %rcx
movl (%rcx,%rax,4), %ecx
incl %ecx
movq 0x10(%rsp), %rdx
movq (%rdx,%rax,8), %r8
movq %r13, %rdi
movq %r14, %rsi
movl $0x5a, %edx
callq 0x116a0
movl 0x24(%r13), %edx
movq 0x30(%r13), %rcx
movq %rbp, %rbx
movq %rbp, %rdi
movq %r13, %rsi
callq 0x12964
movslq (%r15), %rbp
movq 0x20(%rsp), %rax
movq (%rax,%rbp,8), %rdi
movq %r13, %rsi
callq 0x126b5
testl %eax, %eax
js 0x1e32e
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(%r15)
movq %r12, 0x10(%r15)
incq %r12
jmp 0x1e359
cmpl $-0x1, %eax
jne 0x1e378
movq 0x38(%rsp), %r15
movq $-0x1, 0x8(%r15)
movq 0x30(%r13), %rdi
callq 0x32c0
movq %r13, %rdi
callq 0x32c0
movq $0x0, 0x18(%r15)
movq %rbx, %rbp
xorl %edi, %edi
movq 0x30(%rsp), %rsi
movq %r15, %rdx
callq 0x1d1a4
cmpq $-0x1, 0x8(%r15)
jne 0x1e293
jmp 0x1e3a0
movq 0xfc69(%rip), %rax      # 0x2dfe8
movq (%rax), %rdi
movq 0x70(%rsp), %rax
movq (%rax,%rbp,8), %rdx
leaq 0x868b(%rip), %rsi      # 0x26a1d
xorl %eax, %eax
callq 0x3630
movq 0x38(%rsp), %r15
jmp 0x1e359
cmpl $0x0, 0x1c(%rsp)
movq 0x20(%rsp), %r15
je 0x1e3e3
cmpb $0x0, 0xb(%rsp)
movq 0x10(%rsp), %r13
jne 0x1e3d1
movl 0xc(%rsp), %ebx
xorl %r14d, %r14d
movq (%r13,%r14,8), %rdi
callq 0x32c0
incq %r14
cmpq %r14, %rbx
jne 0x1e3bf
movq %r13, %rdi
callq 0x32c0
movq 0x40(%rsp), %rdi
callq 0x32c0
cmpb $0x0, 0xb(%rsp)
jne 0x1e402
movl 0xc(%rsp), %ebx
xorl %r14d, %r14d
movq (%r15,%r14,8), %rdi
callq 0x10d25
incq %r14
cmpq %r14, %rbx
jne 0x1e3f1
movq %rbp, %rdi
callq 0x10d25
movq %r15, %rdi
callq 0x32c0
movq 0x38(%rsp), %rdi
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x32c0
movq 0xfbc4(%rip), %rax      # 0x2dff8
movq (%rax), %rdi
callq 0x3030
leaq 0x4f6a(%rip), %rsi      # 0x233ad
movl %eax, %edi
callq 0x101a3
jmp 0x1e239
movq 0xfb92(%rip), %rax      # 0x2dfe8
movq (%rax), %rdi
leaq 0x83bf(%rip), %rsi      # 0x2681f
movq $0x0, 0x28(%rsp)
movq 0x50(%rsp), %rdx
xorl %eax, %eax
callq 0x3630
jmp 0x1de04
movq 0xfb67(%rip), %rax      # 0x2dfe8
movq (%rax), %rdi
movq 0x58(%rsp), %rax
movq (%rax), %r8
leaq 0x84d7(%rip), %rsi      # 0x2696a
movq %r14, %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x3630
movl $0x1, %edi
callq 0x3130
movq 0x58(%rsp), %rdi
callq 0x4e7f
leaq 0x8503(%rip), %rdi      # 0x269be
leaq 0x8502(%rip), %rsi      # 0x269c4
leaq 0x8506(%rip), %rcx      # 0x269cf
movl $0xa7, %edx
callq 0x3140