// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Process_Read.cpp
// Function: ha_encode_base(unsigned char*, char*, unsigned long, N_t*, unsigned long)
void ha_encode_base(uint8_t* dest, char* src, uint64_t src_l, N_t *nn, uint64_t nn_offset)
{
    uint64_t i = 0;
    uint64_t dest_i = 0;
    uint8_t tmp = 0;
    uint8_t c = 0;

    while (i + 4 <= src_l) {
        tmp = 0;
		// fprintf(stderr, "i->%lu, src_l->%ld, src[i]->%c, (uint8_t)src[i]->%u\n", i, src_l, src[i], (uint8_t)src[i]);
        c = seq_nt6_table[(uint8_t)src[i]];
		if (c >= 4) {
			c = 0; kv_push(uint32_t, *nn, i+nn_offset);
		}
		i++;
        tmp = tmp | (c<<6);

        c = seq_nt6_table[(uint8_t)src[i]];
		if (c >= 4) {
			c = 0; kv_push(uint32_t, *nn, i+nn_offset);
		}
		i++;
        tmp = tmp | (c<<4);

        c = seq_nt6_table[(uint8_t)src[i]];
		if (c >= 4) {
			c = 0; kv_push(uint32_t, *nn, i+nn_offset);
		}
		i++;
        tmp = tmp | (c<<2);

        c = seq_nt6_table[(uint8_t)src[i]];
		if (c >= 4) {
			c = 0; kv_push(uint32_t, *nn, i+nn_offset);
		}
		i++;
        tmp = tmp | c;

        dest[dest_i] = tmp;

        dest_i++;
    }

    //at most 3 bases here
    uint64_t shift = 6;
    if (i < src_l) {
        tmp = 0;
        while (i < src_l) {
            c = seq_nt6_table[(uint8_t)src[i]];
			if (c >= 4) {
				c = 0; kv_push(uint32_t, *nn, i+nn_offset);
			}
			i++;
            tmp = tmp | (c << shift);
            shift = shift -2;
        }
        
        dest[dest_i] = tmp;
        dest_i++;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rcx, %r14
movq %rsi, %r9
movq %rdi, 0x20(%rsp)
movq %rdx, 0x18(%rsp)
cmpq $0x4, %rdx
jae 0x348cf
xorl %eax, %eax
xorl %r12d, %r12d
jmp 0x34ab7
xorl %ebp, %ebp
leaq 0x255d58(%rip), %rdi    # 0x28a630
xorl %r12d, %r12d
movq %r8, 0x8(%rsp)
movq %r9, 0x10(%rsp)
movzbl (%r9,%rbp), %eax
movb (%rax,%rdi), %bl
cmpb $0x4, %bl
jb 0x34948
movq 0x10(%r14), %rax
cmpq %rax, 0x8(%r14)
jne 0x34930
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x10(%r14)
movq (%r14), %rdi
shlq $0x2, %rsi
callq 0x2370
leaq 0x255d0d(%rip), %rdi    # 0x28a630
movq 0x10(%rsp), %r9
movq 0x8(%rsp), %r8
movq %rax, (%r14)
leal (%r8,%rbp), %eax
movq (%r14), %rcx
movq 0x8(%r14), %rdx
leaq 0x1(%rdx), %rsi
movq %rsi, 0x8(%r14)
movl %eax, (%rcx,%rdx,4)
xorl %ebx, %ebx
movzbl 0x1(%r9,%rbp), %eax
movb (%rax,%rdi), %r15b
cmpb $0x4, %r15b
jb 0x349b1
movq 0x10(%r14), %rax
cmpq %rax, 0x8(%r14)
jne 0x34996
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x10(%r14)
movq (%r14), %rdi
shlq $0x2, %rsi
callq 0x2370
leaq 0x255ca7(%rip), %rdi    # 0x28a630
movq 0x10(%rsp), %r9
movq 0x8(%rsp), %r8
movq %rax, (%r14)
leal (%r8,%rbp), %eax
incl %eax
movq (%r14), %rcx
movq 0x8(%r14), %rdx
leaq 0x1(%rdx), %rsi
movq %rsi, 0x8(%r14)
movl %eax, (%rcx,%rdx,4)
xorl %r15d, %r15d
movzbl 0x2(%r9,%rbp), %eax
movb (%rax,%rdi), %r13b
cmpb $0x4, %r13b
jb 0x34a1b
movq 0x10(%r14), %rax
cmpq %rax, 0x8(%r14)
jne 0x349ff
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x10(%r14)
movq (%r14), %rdi
shlq $0x2, %rsi
callq 0x2370
leaq 0x255c3e(%rip), %rdi    # 0x28a630
movq 0x10(%rsp), %r9
movq 0x8(%rsp), %r8
movq %rax, (%r14)
leal (%r8,%rbp), %eax
addl $0x2, %eax
movq (%r14), %rcx
movq 0x8(%r14), %rdx
leaq 0x1(%rdx), %rsi
movq %rsi, 0x8(%r14)
movl %eax, (%rcx,%rdx,4)
xorl %r13d, %r13d
movzbl 0x3(%r9,%rbp), %eax
movb (%rax,%rdi), %al
cmpb $0x4, %al
jb 0x34a81
movq 0x10(%r14), %rax
cmpq %rax, 0x8(%r14)
jne 0x34a66
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x10(%r14)
movq (%r14), %rdi
shlq $0x2, %rsi
callq 0x2370
leaq 0x255bd7(%rip), %rdi    # 0x28a630
movq 0x10(%rsp), %r9
movq 0x8(%rsp), %r8
movq %rax, (%r14)
leal (%r8,%rbp), %eax
addl $0x3, %eax
movq (%r14), %rcx
movq 0x8(%r14), %rdx
leaq 0x1(%rdx), %rsi
movq %rsi, 0x8(%r14)
movl %eax, (%rcx,%rdx,4)
xorl %eax, %eax
shlb $0x6, %bl
shlb $0x4, %r15b
orb %bl, %r15b
shlb $0x2, %r13b
orb %r15b, %r13b
orb %al, %r13b
movq 0x20(%rsp), %rax
movb %r13b, (%rax,%r12)
incq %r12
leaq 0x4(%rbp), %rax
addq $0x8, %rbp
cmpq 0x18(%rsp), %rbp
movq %rax, %rbp
jbe 0x348e5
subq %rax, 0x18(%rsp)
jbe 0x34b61
addq %rax, %r8
addq %rax, %r9
movl $0x6, %ebp
xorl %ebx, %ebx
leaq 0x255b5a(%rip), %rdi    # 0x28a630
xorl %r15d, %r15d
movq %r8, 0x8(%rsp)
movzbl (%r9,%rbx), %eax
movb (%rax,%rdi), %al
cmpb $0x4, %al
jb 0x34b41
movq 0x10(%r14), %rax
cmpq %rax, 0x8(%r14)
jne 0x34b29
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x10(%r14)
movq (%r14), %rdi
shlq $0x2, %rsi
movq %r9, %r13
callq 0x2370
leaq 0x255b12(%rip), %rdi    # 0x28a630
movq %r13, %r9
movq 0x8(%rsp), %r8
movq %rax, (%r14)
leal (%r8,%rbx), %eax
movq (%r14), %rcx
movq 0x8(%r14), %rdx
leaq 0x1(%rdx), %rsi
movq %rsi, 0x8(%r14)
movl %eax, (%rcx,%rdx,4)
xorl %eax, %eax
movzbl %al, %eax
movl %ebp, %ecx
shll %cl, %eax
orb %al, %r15b
addl $-0x2, %ebp
incq %rbx
cmpq %rbx, 0x18(%rsp)
jne 0x34ade
movq 0x20(%rsp), %rax
movb %r15b, (%rax,%r12)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq