// File: /workspace/github_cpp_makefile_star2_O3/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 0x33ef8
xorl %eax, %eax
xorl %r12d, %r12d
jmp 0x340e0
xorl %ebp, %ebp
leaq 0x26172f(%rip), %rdi    # 0x295630
xorl %r12d, %r12d
movq %r8, 0x8(%rsp)
movq %r9, 0x10(%rsp)
movzbl (%r9,%rbp), %eax
movb (%rax,%rdi), %bl
cmpb $0x4, %bl
jb 0x33f71
movq (%r14), %rax
movq 0x8(%r14), %rcx
cmpq 0x10(%r14), %rcx
jne 0x33f60
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x10(%r14)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
leaq 0x2616e1(%rip), %rdi    # 0x295630
movq 0x10(%rsp), %r9
movq 0x8(%rsp), %r8
movq %rax, (%r14)
movq 0x8(%r14), %rcx
leal (%r8,%rbp), %edx
leaq 0x1(%rcx), %rsi
movq %rsi, 0x8(%r14)
movl %edx, (%rax,%rcx,4)
xorl %ebx, %ebx
movzbl 0x1(%r9,%rbp), %eax
movb (%rax,%rdi), %r15b
cmpb $0x4, %r15b
jb 0x33fda
movq (%r14), %rax
movq 0x8(%r14), %rcx
cmpq 0x10(%r14), %rcx
jne 0x33fc6
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x10(%r14)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
leaq 0x26167b(%rip), %rdi    # 0x295630
movq 0x10(%rsp), %r9
movq 0x8(%rsp), %r8
movq %rax, (%r14)
movq 0x8(%r14), %rcx
leal (%r8,%rbp), %edx
incl %edx
leaq 0x1(%rcx), %rsi
movq %rsi, 0x8(%r14)
movl %edx, (%rax,%rcx,4)
xorl %r15d, %r15d
movzbl 0x2(%r9,%rbp), %eax
movb (%rax,%rdi), %r13b
cmpb $0x4, %r13b
jb 0x34044
movq (%r14), %rax
movq 0x8(%r14), %rcx
cmpq 0x10(%r14), %rcx
jne 0x3402f
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x10(%r14)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
leaq 0x261612(%rip), %rdi    # 0x295630
movq 0x10(%rsp), %r9
movq 0x8(%rsp), %r8
movq %rax, (%r14)
movq 0x8(%r14), %rcx
leal (%r8,%rbp), %edx
addl $0x2, %edx
leaq 0x1(%rcx), %rsi
movq %rsi, 0x8(%r14)
movl %edx, (%rax,%rcx,4)
xorl %r13d, %r13d
movzbl 0x3(%r9,%rbp), %eax
movb (%rax,%rdi), %al
cmpb $0x4, %al
jb 0x340aa
movq (%r14), %rax
movq 0x8(%r14), %rcx
cmpq 0x10(%r14), %rcx
jne 0x34096
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x10(%r14)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
leaq 0x2615ab(%rip), %rdi    # 0x295630
movq 0x10(%rsp), %r9
movq 0x8(%rsp), %r8
movq %rax, (%r14)
movq 0x8(%r14), %rcx
leal (%r8,%rbp), %edx
addl $0x3, %edx
leaq 0x1(%rcx), %rsi
movq %rsi, 0x8(%r14)
movl %edx, (%rax,%rcx,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 0x33f0e
subq %rax, 0x18(%rsp)
jbe 0x3418a
addq %rax, %r8
addq %rax, %r9
movl $0x6, %ebp
xorl %ebx, %ebx
leaq 0x261531(%rip), %rdi    # 0x295630
xorl %r15d, %r15d
movq %r8, 0x8(%rsp)
movzbl (%r9,%rbx), %eax
movb (%rax,%rdi), %al
cmpb $0x4, %al
jb 0x3416a
movq (%r14), %rax
movq 0x8(%r14), %rcx
cmpq 0x10(%r14), %rcx
jne 0x34159
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x10(%r14)
shlq $0x2, %rsi
movq %rax, %rdi
movq %r9, %r13
callq 0x2370
leaq 0x2614e6(%rip), %rdi    # 0x295630
movq %r13, %r9
movq 0x8(%rsp), %r8
movq %rax, (%r14)
movq 0x8(%r14), %rcx
leal (%r8,%rbx), %edx
leaq 0x1(%rcx), %rsi
movq %rsi, 0x8(%r14)
movl %edx, (%rax,%rcx,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 0x34107
movq 0x20(%rsp), %rax
movb %r15b, (%rax,%r12)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq