// File: /workspace/github_c_makefile_star2_O1/ToniWestbrook[P]paladin/bwa.c
// Function: bwa_idx2mem
int bwa_idx2mem(bwaidx_t *idx)
{
	int i;
	int64_t k, x, tmp;
	uint8_t *mem;

	// copy idx->bwt
	x = idx->bwt->bwt_size * 4;
	mem = realloc(idx->bwt->bwt, sizeof(bwt_t) + x); idx->bwt->bwt = 0;
	memmove(mem + sizeof(bwt_t), mem, x);
	memcpy(mem, idx->bwt, sizeof(bwt_t)); k = sizeof(bwt_t) + x;
	x = idx->bwt->n_sa * sizeof(bwtint_t); mem = realloc(mem, k + x); memcpy(mem + k, idx->bwt->sa, x); k += x;
	free(idx->bwt->sa);
	free(idx->bwt); idx->bwt = 0;

	// copy idx->bns
	tmp = idx->bns->n_seqs * sizeof(bntann1_t) + idx->bns->n_holes * sizeof(bntamb1_t);
	for (i = 0; i < idx->bns->n_seqs; ++i) // compute the size of heap-allocated memory
		tmp += strlen(idx->bns->anns[i].name) + strlen(idx->bns->anns[i].anno) + 2;
	mem = realloc(mem, k + sizeof(bntseq_t) + tmp);
	x = sizeof(bntseq_t); memcpy(mem + k, idx->bns, x); k += x;
	x = idx->bns->n_holes * sizeof(bntamb1_t); memcpy(mem + k, idx->bns->ambs, x); k += x;
	free(idx->bns->ambs);
	x = idx->bns->n_seqs * sizeof(bntann1_t); memcpy(mem + k, idx->bns->anns, x); k += x;
	for (i = 0; i < idx->bns->n_seqs; ++i) {
		x = strlen(idx->bns->anns[i].name) + 1; memcpy(mem + k, idx->bns->anns[i].name, x); k += x;
		x = strlen(idx->bns->anns[i].anno) + 1; memcpy(mem + k, idx->bns->anns[i].anno, x); k += x;
		free(idx->bns->anns[i].name); free(idx->bns->anns[i].anno);
	}
	free(idx->bns->anns);

	// copy idx->pac
	//x = idx->bns->l_pac/4+1;
	x = idx->bns->l_pac+1;
	mem = realloc(mem, k + x);
	memcpy(mem + k, idx->pac, x); k += x;
	free(idx->bns); idx->bns = 0;
	free(idx->pac); idx->pac = 0;

	return bwa_mem2idx(k, mem, idx);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movq %rdi, 0x8(%rsp)
movq (%rdi), %rax
movq 0x118(%rax), %r12
movq 0x120(%rax), %rdi
leaq (,%r12,4), %r15
movl $0x140, %ebp            # imm = 0x140
leaq (%r15,%rbp), %rsi
leaq 0x15669(%rip), %r14     # 0x284d5
leaq 0x157ab(%rip), %r8      # 0x2861e
movq %r14, %rdx
movl $0x15a, %ecx            # imm = 0x15A
callq 0x22d64
movq %rax, %r13
movq (%rbx), %rax
xorl %ecx, %ecx
movq %rcx, 0x120(%rax)
movq %r13, %rdi
addq %rbp, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x3600
movq (%rbx), %rsi
movl $0x140, %edx            # imm = 0x140
movq %r13, %rdi
callq 0x3260
movq (%rbx), %rax
movq 0x130(%rax), %rbp
leaq (%r15,%rbp,8), %rsi
addq $0x140, %rsi            # imm = 0x140
shlq $0x3, %rbp
movq %r13, %rdi
movq %rsi, 0x10(%rsp)
movq %r14, %rdx
movl $0x15d, %ecx            # imm = 0x15D
leaq 0x1573e(%rip), %r8      # 0x2861e
callq 0x22d64
movq %rax, (%rsp)
leaq (%rax,%r12,4), %rdi
addq $0x140, %rdi            # imm = 0x140
movq (%rbx), %rax
movq 0x138(%rax), %rsi
movq %rbp, %rdx
callq 0x3260
movq (%rbx), %rax
movq 0x138(%rax), %rdi
callq 0x3390
movq (%rbx), %rdi
callq 0x3390
xorl %eax, %eax
movq %rax, (%rbx)
movq 0x8(%rbx), %rcx
movslq 0x8(%rcx), %rax
movslq 0x18(%rcx), %rdx
shlq $0x4, %rdx
leaq (%rax,%rax,4), %rsi
leaq (%rdx,%rsi,8), %r13
testq %rax, %rax
jle 0x12f79
movq 0x10(%rcx), %rbp
shlq $0x3, %rax
leaq (%rax,%rax,4), %r14
xorl %r15d, %r15d
movq 0x18(%rbp,%r15), %rdi
callq 0x3100
movq %rax, %r12
movq 0x20(%rbp,%r15), %rdi
callq 0x3100
addq %r13, %r12
leaq (%rax,%r12), %r13
addq $0x2, %r13
addq $0x28, %r15
cmpq %r15, %r14
jne 0x12f4e
movq 0x10(%rsp), %r14
leaq (%r14,%r13), %rsi
addq $0x30, %rsi
leaq 0x15548(%rip), %rdx     # 0x284d5
leaq 0x1568a(%rip), %r8      # 0x2861e
movq (%rsp), %rdi
movl $0x165, %ecx            # imm = 0x165
callq 0x22d64
movq %rax, %rbx
movq 0x8(%rsp), %r13
movq 0x8(%r13), %rax
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
leaq 0x30(%rbx,%r14), %rdi
movups %xmm0, -0x30(%rdi)
movups %xmm1, -0x20(%rdi)
movups %xmm2, -0x10(%rdi)
movq 0x8(%r13), %rax
movslq 0x18(%rax), %r12
shlq $0x4, %r12
movq 0x20(%rax), %rsi
movq %r12, %rdx
callq 0x3260
addq %r12, %r14
addq $0x30, %r14
movq 0x8(%r13), %rax
movq 0x20(%rax), %rdi
callq 0x3390
movq 0x8(%r13), %rax
movslq 0x8(%rax), %rcx
shlq $0x3, %rcx
leaq (%rcx,%rcx,4), %r15
leaq (%rbx,%r14), %rdi
movq 0x10(%rax), %rsi
movq %r15, %rdx
callq 0x3260
addq %r14, %r15
movq 0x8(%r13), %rax
cmpl $0x0, 0x8(%rax)
movq %rbx, (%rsp)
jle 0x130d2
xorl %ebp, %ebp
xorl %r14d, %r14d
movq 0x8(%rsp), %rbx
movq 0x10(%rax), %rax
movq 0x18(%rax,%rbp), %r12
movq %r12, %rdi
callq 0x3100
movq %rax, %r13
leaq 0x1(%rax), %rdx
movq (%rsp), %rax
leaq (%rax,%r15), %rdi
movq %r12, %rsi
callq 0x3260
addq %r15, %r13
incq %r13
movq 0x8(%rbx), %rax
movq 0x10(%rax), %rax
movq 0x20(%rax,%rbp), %r15
movq %r15, %rdi
callq 0x3100
movq %rax, %r12
leaq 0x1(%rax), %rdx
movq (%rsp), %rax
leaq (%rax,%r13), %rdi
movq %r15, %rsi
callq 0x3260
leaq (%r12,%r13), %r15
incq %r15
movq 0x8(%rbx), %rax
movq 0x10(%rax), %rax
movq 0x18(%rax,%rbp), %rdi
callq 0x3390
movq 0x8(%rbx), %rax
movq 0x10(%rax), %rax
movq 0x20(%rax,%rbp), %rdi
callq 0x3390
incq %r14
movq 0x8(%rbx), %rax
movslq 0x8(%rax), %rcx
addq $0x28, %rbp
cmpq %rcx, %r14
jl 0x13035
movq 0x10(%rax), %rdi
callq 0x3390
movq 0x8(%rsp), %rbx
movq 0x8(%rbx), %rax
movq (%rax), %r13
leaq (%r15,%r13), %r12
incq %r12
incq %r13
leaq 0x153dd(%rip), %rdx     # 0x284d5
leaq 0x1551f(%rip), %r8      # 0x2861e
movq (%rsp), %rdi
movq %r12, %rsi
movl $0x174, %ecx            # imm = 0x174
callq 0x22d64
movq %rax, %r14
addq %rax, %r15
movq 0x10(%rbx), %rsi
movq %r15, %rdi
movq %r13, %rdx
callq 0x3260
movq 0x8(%rbx), %rdi
callq 0x3390
xorl %r15d, %r15d
movq %r15, 0x8(%rbx)
movq 0x10(%rbx), %rdi
callq 0x3390
movq %r15, 0x10(%rbx)
movq %r12, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x12c63
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq