// File: /workspace/github_c_makefile_star2_O1/ToniWestbrook[P]paladin/bwtindex.c
// Function: bwt_pac2bwt
bwt_t * bwt_pac2bwt(const char *fn_pac) {
	bwt_t *bwt;
	ubyte_t * packedBuf, * unpackedBuf;
	int64_t i, packedSize;
	FILE *fp;

	// Initialize BWT structure
	bwt = (bwt_t*)calloc(1, sizeof(bwt_t));
	bwt->seq_len = bwa_seq_len(fn_pac);
	bwt->bwt_size = (bwt->seq_len + sizeof(uint32_t) - 1) / sizeof(uint32_t);

	fp = xopen(fn_pac, "rb");

	// Prepare buffers
	packedSize = bwt->seq_len;
	packedBuf = (ubyte_t*)calloc(packedSize, 1);
	unpackedBuf = (ubyte_t*)calloc(bwt->seq_len + 1, 1);
	memset(bwt->L2, 0, (VALUE_DOMAIN + 1) * sizeof(bwtint_t));

	err_fread_noeof(packedBuf, 1, packedSize, fp);
	err_fclose(fp);

	// Unpack sequence, record occurrence
	for (i = 0; i < bwt->seq_len; ++i) {
		unpackedBuf[i] = packedBuf[i];
		++bwt->L2[unpackedBuf[i] + 1];
	}
	free(packedBuf);

	// Accumulate lower occurrences
	for (i = 2; i <= VALUE_DOMAIN ; ++i) {
		bwt->L2[i] += bwt->L2[i-1];
	}

	// Burrows-Wheeler Transform
	bwt->primary = is_bwt(unpackedBuf, bwt->seq_len);

	// Pack result into BWT
	bwt->bwt = (u_int32_t*)calloc(bwt->bwt_size, sizeof(uint32_t));
	for (i = 0; i < bwt->seq_len; ++i) {
		packValue(bwt, i, unpackedBuf[i]);
	}

	free(unpackedBuf);

	return bwt;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %r15
leaq 0x1f73b(%rip), %r12     # 0x250d4
leaq 0x1f73f(%rip), %r14     # 0x250df
movl $0x1, %edi
movl $0x140, %esi            # imm = 0x140
movq %r12, %rdx
movl $0x7b, %ecx
movq %r14, %r8
callq 0x22cb4
movq %rax, %rbx
movq %r15, %rdi
callq 0x591b
movq %rax, 0x110(%rbx)
addq $0x3, %rax
shrq $0x2, %rax
movq %rax, 0x118(%rbx)
leaq 0x22fc6(%rip), %rdx     # 0x289a8
movq %r14, %rdi
movq %r15, %rsi
callq 0xdd8e
movq %rax, %r13
movq 0x110(%rbx), %rbp
movl $0x1, %esi
movq %rbp, %rdi
movq %r12, %rdx
movl $0x83, %ecx
movq %r14, %r8
callq 0x22cb4
movq %rax, %r15
movq 0x110(%rbx), %rdi
incq %rdi
movl $0x1, %esi
movq %r12, %rdx
movl $0x84, %ecx
movq %r14, %r8
callq 0x22cb4
movq %rax, %r14
leaq 0x8(%rbx), %rdi
movl $0x108, %edx            # imm = 0x108
xorl %esi, %esi
callq 0x3140
movl $0x1, %esi
movq %r15, %rdi
movq %rbp, %rdx
movq %r13, %rcx
callq 0xe127
movq %r13, %rdi
callq 0xe490
cmpq $0x0, 0x110(%rbx)
je 0x5a85
xorl %eax, %eax
movzbl (%r15,%rax), %ecx
movb %cl, (%r14,%rax)
incq 0x10(%rbx,%rcx,8)
incq %rax
cmpq 0x110(%rbx), %rax
jb 0x5a6b
movq %r15, %rdi
callq 0x3390
movq 0x10(%rbx), %rax
movl $0x3, %ecx
addq (%rbx,%rcx,8), %rax
movq %rax, (%rbx,%rcx,8)
incq %rcx
cmpq $0x22, %rcx
jne 0x5a96
movq 0x110(%rbx), %rsi
movq %r14, %rdi
callq 0x5474
movq %rax, (%rbx)
movq 0x118(%rbx), %rdi
leaq 0x1f60d(%rip), %rdx     # 0x250d4
leaq 0x1f611(%rip), %r8      # 0x250df
movl $0x4, %esi
movl $0x9a, %ecx
callq 0x22cb4
movq %rax, 0x120(%rbx)
movq 0x110(%rbx), %rdx
testq %rdx, %rdx
je 0x5b1a
xorl %esi, %esi
xorl %edi, %edi
movzbl (%r14,%rdi), %r8d
movl %esi, %ecx
notb %cl
andb $0x18, %cl
movq %rdi, %r9
shll %cl, %r8d
andq $-0x4, %r9
orl %r8d, (%rax,%r9)
incq %rdi
addq $0x8, %rsi
cmpq %rdi, %rdx
jne 0x5af4
movq %r14, %rdi
callq 0x3390
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq