// File: /workspace/github_c_makefile_star2_O2/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 0x1bd18(%rip), %r13     # 0x200d4
leaq 0x1bd1c(%rip), %r14     # 0x200df
pushq $0x1
popq %rbp
pushq $0x7b
popq %rcx
movl $0x140, %esi            # imm = 0x140
movq %rbp, %rdi
movq %r13, %rdx
movq %r14, %r8
callq 0x1dfd0
movq %rax, %rbx
movq %r15, %rdi
callq 0x4348
movq %rax, 0x110(%rbx)
addq $0x3, %rax
shrq $0x2, %rax
movq %rax, 0x118(%rbx)
leaq 0x1f624(%rip), %rdx     # 0x23a28
movq %r14, %rdi
movq %r15, %rsi
callq 0xc931
movq %rax, (%rsp)
movq 0x110(%rbx), %r15
movq %r15, %rdi
movq %rbp, %rsi
movq %r13, %rdx
movl $0x83, %ecx
movq %r14, %r8
callq 0x1dfd0
movq %rax, %r12
movq 0x110(%rbx), %rdi
incq %rdi
movq %rbp, %rsi
movq %r13, %rdx
movl $0x84, %ecx
movq %r14, %r8
callq 0x1dfd0
movq %rax, %r14
movq %rbx, %r13
addq $0x8, %r13
xorl %ebp, %ebp
movl $0x108, %edx            # imm = 0x108
movq %r13, %rdi
xorl %esi, %esi
callq 0x3140
movq %r12, %rdi
pushq $0x1
popq %rsi
movq %r15, %rdx
movq (%rsp), %r15
movq %r15, %rcx
callq 0xccb2
movq %r15, %rdi
callq 0xd01b
cmpq 0x110(%rbx), %rbp
jae 0x44a4
movzbl (%r12,%rbp), %eax
movb %al, (%r14,%rbp)
incq 0x8(%r13,%rax,8)
incq %rbp
jmp 0x4488
movq %r12, %rdi
callq 0x3390
pushq $0x3
popq %rax
cmpq $0x22, %rax
je 0x44c3
movq -0x8(%rbx,%rax,8), %rcx
addq %rcx, (%rbx,%rax,8)
incq %rax
jmp 0x44af
movq 0x110(%rbx), %rsi
movq %r14, %rdi
callq 0x3e90
movq %rax, (%rbx)
movq 0x118(%rbx), %rdi
leaq 0x1bbf1(%rip), %rdx     # 0x200d4
leaq 0x1bbf5(%rip), %r8      # 0x200df
pushq $0x4
popq %rsi
movl $0x9a, %ecx
callq 0x1dfd0
movq %rax, 0x120(%rbx)
movq 0x110(%rbx), %rdx
xorl %esi, %esi
xorl %edi, %edi
cmpq %rdi, %rdx
je 0x4531
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
jmp 0x4509
movq %r14, %rdi
callq 0x3390
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq