// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movl $0x1, %edi
movl $0x140, %esi            # imm = 0x140
leaq 0x32157(%rip), %rdx     # 0x370d4
movl $0x7b, %ecx
leaq 0x32156(%rip), %r8      # 0x370df
callq 0x31bf0
movq %rax, -0x10(%rbp)
movq -0x8(%rbp), %rdi
callq 0x4ee0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x110(%rax)
movq -0x10(%rbp), %rax
movq 0x110(%rax), %rcx
addq $0x4, %rcx
subq $0x1, %rcx
shrq $0x2, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x118(%rax)
movq -0x8(%rbp), %rsi
leaq 0x32109(%rip), %rdi     # 0x370df
leaq 0x358b3(%rip), %rdx     # 0x3a890
callq 0x12740
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq 0x110(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
movl $0x1, %esi
leaq 0x320cf(%rip), %rdx     # 0x370d4
movl $0x83, %ecx
leaq 0x320ce(%rip), %r8      # 0x370df
callq 0x31bf0
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0x110(%rax), %rdi
addq $0x1, %rdi
movl $0x1, %esi
leaq 0x3209f(%rip), %rdx     # 0x370d4
movl $0x84, %ecx
leaq 0x3209e(%rip), %r8      # 0x370df
callq 0x31bf0
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rdi
addq $0x8, %rdi
xorl %esi, %esi
movl $0x108, %edx            # imm = 0x108
callq 0x3130
movq -0x18(%rbp), %rdi
movq -0x30(%rbp), %rdx
movq -0x38(%rbp), %rcx
movl $0x1, %esi
callq 0x12c70
movq -0x38(%rbp), %rdi
callq 0x131b0
movq $0x0, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x110(%rcx), %rax
jae 0x50de
movq -0x18(%rbp), %rax
movq -0x28(%rbp), %rcx
movb (%rax,%rcx), %dl
movq -0x20(%rbp), %rax
movq -0x28(%rbp), %rcx
movb %dl, (%rax,%rcx)
movq -0x10(%rbp), %rax
movq -0x20(%rbp), %rcx
movq -0x28(%rbp), %rdx
movzbl (%rcx,%rdx), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq 0x8(%rax,%rcx,8), %rdx
addq $0x1, %rdx
movq %rdx, 0x8(%rax,%rcx,8)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x5085
movq -0x18(%rbp), %rdi
callq 0x3380
movq $0x2, -0x28(%rbp)
cmpq $0x20, -0x28(%rbp)
jg 0x5127
movq -0x10(%rbp), %rax
movq -0x28(%rbp), %rcx
subq $0x1, %rcx
movq 0x8(%rax,%rcx,8), %rdx
movq -0x10(%rbp), %rax
movq -0x28(%rbp), %rcx
addq 0x8(%rax,%rcx,8), %rdx
movq %rdx, 0x8(%rax,%rcx,8)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x50ef
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x110(%rax), %rsi
callq 0x44d0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movq 0x118(%rax), %rdi
movl $0x4, %esi
leaq 0x31f78(%rip), %rdx     # 0x370d4
movl $0x9a, %ecx
leaq 0x31f77(%rip), %r8      # 0x370df
callq 0x31bf0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x120(%rax)
movq $0x0, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x110(%rcx), %rax
jae 0x51bd
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x20(%rbp), %rax
movq -0x28(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movl %eax, %edx
callq 0x4e30
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x5183
movq -0x20(%rbp), %rdi
callq 0x3380
movq -0x10(%rbp), %rax
addq $0x40, %rsp
popq %rbp
retq