// File: /workspace/github_c_makefile_star2_O3/ToniWestbrook[P]paladin/bntseq.c
// Function: bns_fasta2bntseq
int64_t bns_fasta2bntseq(gzFile fp_fa, const char * prefix, int for_only) {
	kseq_t *seq;
	char name[1024];
	bntseq_t *bns;
	uint8_t *pac = 0;
	int32_t m_seqs, m_holes;
	int64_t ret = -1, m_pac, l;
	bntamb1_t *q;
	FILE *fp;

	// Initialization of sequence related structures
	seq = kseq_init(fp_fa);
	bns = (bntseq_t*)calloc(1, sizeof(bntseq_t));
	bns->seed = 11;
	srand48(bns->seed);
	m_seqs = m_holes = 8; m_pac = 0x10000;
	bns->anns = (bntann1_t*)calloc(m_seqs, sizeof(bntann1_t));
	bns->ambs = (bntamb1_t*)calloc(m_holes, sizeof(bntamb1_t));
	q = bns->ambs;

	// Allocate packed buffer to size of sequence (for now - to be converted to 40-bit word)
	pac = calloc(m_pac, 1);

	// Open packed file for writing
	strcpy(name, prefix); strcat(name, ".pac");
	fp = xopen(name, "wb");

	// Read sequences into pac buffer
	while (kseq_read(seq) >= 0) pac = add1(seq, bns, pac, &m_pac, &m_seqs, &m_holes, &q);

	// Add reverse complement if not forward only
	if (!for_only) {
		m_pac = bns->l_pac * 2;
		pac = realloc(pac, m_pac);
		memset(pac + bns->l_pac, 0, m_pac - bns->l_pac);

		for (l = bns->l_pac - 1; l >= 0; --l, ++bns->l_pac) {
			pac[bns->l_pac] = VALUE_DEFINED - 1 - pac[l];
		}
	}

	ret = bns->l_pac;

	// Write pac file
	ubyte_t ct;
	err_fwrite(pac, 1, bns->l_pac, fp);

	// Pad to nearest 40-bit word
	ct = 0;
	err_fwrite(&ct, 1, 1, fp);

	ct = 0;
	err_fwrite(&ct, 1, 1, fp);

	// Close pac file
	err_fflush(fp);
	err_fclose(fp);

	bns_dump(bns, prefix);
	bns_destroy(bns);
	kseq_destroy(seq);
	free(pac);
	return ret;}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x458, %rsp            # imm = 0x458
movl %edx, 0x14(%rsp)
movq %rsi, %r15
callq 0xd7c8
movq %rax, %rbx
leaq 0x1715d(%rip), %r12     # 0x282e8
leaq 0x171f2(%rip), %r14     # 0x28384
movl $0x1, %edi
movl $0x30, %esi
movq %r12, %rdx
movl $0x158, %ecx            # imm = 0x158
movq %r14, %r8
callq 0x2319c
movq %rax, %r13
movl $0xb, 0xc(%rax)
movl $0xb, %edi
callq 0x3620
movl $0x8, %eax
movl %eax, 0x18(%rsp)
movl %eax, 0x1c(%rsp)
movq $0x10000, 0x30(%rsp)    # imm = 0x10000
movl $0x8, %edi
movl $0x28, %esi
movq %r12, %rdx
movl $0x15c, %ecx            # imm = 0x15C
movq %r14, %r8
callq 0x2319c
movq %rax, 0x10(%r13)
movl $0x8, %edi
movl $0x10, %esi
movq %r12, %rdx
movl $0x15d, %ecx            # imm = 0x15D
movq %r14, %r8
callq 0x2319c
movq %r13, 0x20(%rsp)
movq %rax, 0x20(%r13)
movq %rax, 0x48(%rsp)
movl $0x10000, %edi          # imm = 0x10000
movl $0x1, %esi
movq %r12, %rdx
movl $0x161, %ecx            # imm = 0x161
movq %r14, %r8
callq 0x2319c
movq %rax, %r12
leaq 0x50(%rsp), %r13
movq %r13, %rdi
movq %r15, 0x40(%rsp)
movq %r15, %rsi
callq 0x32c0
movq %r13, %rdi
callq 0x3100
movl $0x6361702e, 0x50(%rsp,%rax) # imm = 0x6361702E
movb $0x0, 0x54(%rsp,%rax)
leaq 0x16712(%rip), %rdx     # 0x2797c
movq %r14, %rdi
movq %r13, %rsi
callq 0xe023
movq %rax, 0x38(%rsp)
movq %rbx, 0x28(%rsp)
movq %rbx, %rdi
callq 0xd8a7
testl %eax, %eax
js 0x112d5
leaq 0x30(%rsp), %r14
leaq 0x1c(%rsp), %rbp
leaq 0x18(%rsp), %r13
movq 0x28(%rsp), %r15
movq 0x20(%rsp), %rbx
leaq 0x48(%rsp), %rax
movq %rax, (%rsp)
movq %r15, %rdi
movq %rbx, %rsi
movq %r12, %rdx
movq %r14, %rcx
movq %rbp, %r8
movq %r13, %r9
callq 0x10ed4
movq %rax, %r12
movq %r15, %rdi
callq 0xd8a7
testl %eax, %eax
jns 0x112a4
jmp 0x112da
movq 0x20(%rsp), %rbx
movq (%rbx), %r14
cmpl $0x0, 0x14(%rsp)
movq %rbx, %r15
jne 0x1134d
addq %r14, %r14
movq %r14, 0x30(%rsp)
leaq 0x16ff2(%rip), %rdx     # 0x282e8
leaq 0x17087(%rip), %r8      # 0x28384
movq %r12, %rdi
movq %r14, %rsi
movl $0x16d, %ecx            # imm = 0x16D
callq 0x2324c
movq %rax, %r12
movq (%r15), %rax
leaq (%r12,%rax), %rdi
subq %rax, %r14
xorl %esi, %esi
movq %r14, %rdx
callq 0x3140
movq (%r15), %r14
testq %r14, %r14
jle 0x1134d
leaq 0x1(%r14), %rax
movb $0x15, %cl
subb -0x2(%r12,%rax), %cl
movb %cl, (%r12,%r14)
movq (%r15), %r14
incq %r14
movq %r14, (%r15)
decq %rax
cmpq $0x1, %rax
ja 0x11330
movl $0x1, %esi
movq %r12, %rdi
movq %r14, %rdx
movq 0x38(%rsp), %rbp
movq %rbp, %rcx
callq 0xe384
xorl %ebx, %ebx
leaq 0x13(%rsp), %r13
movb %bl, (%r13)
movl $0x1, %esi
movl $0x1, %edx
movq %r13, %rdi
movq %rbp, %rcx
callq 0xe384
movb %bl, (%r13)
movl $0x1, %esi
movl $0x1, %edx
movq %r13, %rdi
movq %rbp, %rcx
callq 0xe384
movq %rbp, %rdi
callq 0xe681
movq %rbp, %rdi
callq 0xe71e
movq %r15, %rdi
movq 0x40(%rsp), %rsi
callq 0x103a4
movq %r15, %rdi
callq 0x10e61
movq 0x28(%rsp), %rdi
callq 0xd84e
movq %r12, %rdi
callq 0x3390
movq %r14, %rax
addq $0x458, %rsp            # imm = 0x458
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq