// File: /workspace/github_c_makefile_star2_O1/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, 0x1c(%rsp)
movq %rsi, %r15
callq 0xd53f
movq %rax, %rbx
leaq 0x1739c(%rip), %r12     # 0x28250
leaq 0x17431(%rip), %r13     # 0x282ec
movl $0x1, %edi
movl $0x30, %esi
movq %r12, %rdx
movl $0x158, %ecx            # imm = 0x158
movq %r13, %r8
callq 0x22cb4
movq %rax, %r14
movl $0xb, 0xc(%rax)
movl $0xb, %edi
callq 0x3620
movl $0x8, %eax
movl %eax, 0x20(%rsp)
movl %eax, 0x24(%rsp)
movq $0x10000, 0x30(%rsp)    # imm = 0x10000
movl $0x8, %edi
movl $0x28, %esi
movq %r12, %rdx
movl $0x15c, %ecx            # imm = 0x15C
movq %r13, %r8
callq 0x22cb4
movq %rax, 0x10(%r14)
movl $0x8, %edi
movl $0x10, %esi
movq %r12, %rdx
movl $0x15d, %ecx            # imm = 0x15D
movq %r13, %r8
callq 0x22cb4
movq %r14, 0x10(%rsp)
movq %rax, 0x20(%r14)
movq %rax, 0x48(%rsp)
movl $0x10000, %edi          # imm = 0x10000
movl $0x1, %esi
movq %r12, %rdx
movl $0x161, %ecx            # imm = 0x161
movq %r13, %r8
callq 0x22cb4
movq %rax, %r12
leaq 0x50(%rsp), %r14
movq %r14, %rdi
movq %r15, 0x40(%rsp)
movq %r15, %rsi
callq 0x32c0
movq %r14, %rdi
callq 0x3100
movl $0x6361702e, 0x50(%rsp,%rax) # imm = 0x6361702E
movb $0x0, 0x54(%rsp,%rax)
leaq 0x16969(%rip), %rdx     # 0x278fc
movq %r13, %rdi
movq %r14, %rsi
callq 0xdd8e
movq %rax, 0x38(%rsp)
movq %rbx, 0x28(%rsp)
movq %rbx, %rdi
callq 0xd61e
testl %eax, %eax
js 0x10ffc
leaq 0x30(%rsp), %r14
leaq 0x24(%rsp), %rbp
leaq 0x20(%rsp), %r13
movq 0x28(%rsp), %r15
movq 0x10(%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 0x10bff
movq %rax, %r12
movq %r15, %rdi
callq 0xd61e
testl %eax, %eax
jns 0x10fcd
cmpl $0x0, 0x1c(%rsp)
je 0x1100a
movq 0x10(%rsp), %r15
jmp 0x11074
movq 0x10(%rsp), %r15
movq (%r15), %r14
addq %r14, %r14
movq %r14, 0x30(%rsp)
leaq 0x1722f(%rip), %rdx     # 0x28250
leaq 0x172c4(%rip), %r8      # 0x282ec
movq %r12, %rdi
movq %r14, %rsi
movl $0x16d, %ecx            # imm = 0x16D
callq 0x22d64
movq %rax, %r12
movq (%r15), %rax
leaq (%r12,%rax), %rdi
subq %rax, %r14
xorl %esi, %esi
movq %r14, %rdx
callq 0x3140
movq (%r15), %rax
testq %rax, %rax
jle 0x11074
incq %rax
movb $0x15, %cl
subb -0x2(%r12,%rax), %cl
movq (%r15), %rdx
movb %cl, (%r12,%rdx)
incq (%r15)
decq %rax
cmpq $0x1, %rax
jg 0x1105a
movq (%r15), %rbp
movl $0x1, %esi
movq %r12, %rdi
movq %rbp, %rdx
movq 0x38(%rsp), %r13
movq %r13, %rcx
callq 0xe0fc
xorl %ebx, %ebx
leaq 0xf(%rsp), %r14
movb %bl, (%r14)
movl $0x1, %esi
movl $0x1, %edx
movq %r14, %rdi
movq %r13, %rcx
callq 0xe0fc
movb %bl, (%r14)
movl $0x1, %esi
movl $0x1, %edx
movq %r14, %rdi
movq %r13, %rcx
callq 0xe0fc
movq %r13, %rdi
callq 0xe3f3
movq %r13, %rdi
callq 0xe490
movq %r15, %rdi
movq 0x40(%rsp), %rsi
callq 0x10078
movq %r15, %rdi
callq 0x10b8c
movq 0x28(%rsp), %rdi
callq 0xd5c5
movq %r12, %rdi
callq 0x3390
movq %rbp, %rax
addq $0x458, %rsp            # imm = 0x458
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq