// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Process_Read.cpp
// Function: retrive_bqual(asg8_v*, unsigned char*, unsigned long, long, long, unsigned char, long)
int64_t retrive_bqual(asg8_v *dv, uint8_t *ds, uint64_t id, int64_t s, int64_t e, uint8_t rev, int64_t bitn)
{
	int64_t rl = Get_READ_LENGTH(R_INF, id), l; 
	if(s < 0) s = 0; if(e < 0) e = rl;
	if(s >= e || e > rl) return -1;

	uint8_t *da = NULL, *src = Get_QUAL(R_INF, id), mm = (((uint8_t)1)<<bitn)-1, mlf = 8 - bitn, mrf;
	int64_t bitr = 8/bitn, dk, sk, swk;
	l = e - s;
	if(dv) {
		kv_resize(uint8_t, *dv, ((uint64_t)l)); da = dv->a;
	} else {
		da = ds;
	}

	if(!rev) {
		dk = 0; sk = s;

		mrf = ((s%bitr)*bitn);
		// if(s == 21519 && e == 22332) {
		// 	fprintf(stderr, "+[M::%s] id::%lu, in::[%ld, %ld), rev::%u, bitn::%ld, bitr::%ld, mrf::%u\n", __func__, id, s, e, rev, bitn, bitr, mrf);
		// }
		if(mrf) {
			for (swk = sk/bitr; mrf < 8 && sk < e; mrf += bitn, sk++) da[dk++] = ((src[swk]<<mrf)>>mlf)&mm;
		}
		
		for (swk = sk/bitr; (sk + bitr) <= e; sk += bitr, swk++) {
			for (mrf = 0; mrf < 8; mrf += bitn) da[dk++] = ((src[swk]<<mrf)>>mlf)&mm;
		}

		if(sk < e) {
			for (mrf = 0; sk < e; mrf += bitn, sk++) da[dk++] = ((src[swk]<<mrf)>>mlf)&mm;
		}
		// if(dk != l) {
		// 	fprintf(stderr, "+[M::%s] id::%lu, in::[%ld, %ld), rev::%u, bitn::%ld, bitr::%ld\n", __func__, id, s, e, rev, bitn, bitr);
		// }
		assert(dk == l);
	} else {
		sk = s; s = e; e = sk;
		s = rl - s; e = rl - e;
		dk = l; sk = s; 

		mrf = ((s%bitr)*bitn);
		if(mrf) {
			for (swk = sk/bitr; mrf < 8 && sk < e; mrf += bitn, sk++) da[--dk] = ((src[swk]<<mrf)>>mlf)&mm;
		}
		
		for (swk = sk/bitr; (sk + bitr) <= e; sk += bitr, swk++) {
			for (mrf = 0; mrf < 8; mrf += bitn) da[--dk] = ((src[swk]<<mrf)>>mlf)&mm;
		}

		if(sk < e) {
			for (mrf = 0; sk < e; mrf += bitn, sk++) da[--dk] = ((src[swk]<<mrf)>>mlf)&mm;
		}
		
		assert(dk == 0);
	}
	dv->n = l;

	return l;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r8, %r12
movq %rdi, 0x8(%rsp)
leaq 0x256f57(%rip), %rdi    # 0x28b230
movq 0x18(%rdi), %rax
xorl %r8d, %r8d
testq %rcx, %rcx
cmovgq %rcx, %r8
movq (%rax,%rdx,8), %rbx
testq %r12, %r12
cmovsq %rbx, %r12
movq %r12, %r10
movq %r8, 0x20(%rsp)
subq %r8, %r10
setle %al
movq %rbx, 0x10(%rsp)
subq %r12, %rbx
setl %cl
orb %al, %cl
movq $-0x1, %rax
jne 0x345a1
movl %r9d, 0x1c(%rsp)
movq 0x70(%rsp), %r14
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movl %r14d, %ecx
shll %cl, %r15d
movq 0x30(%rdi), %rax
movq (%rax,%rdx,8), %r13
movb $0x8, %bpl
movl $0x8, %eax
xorl %edx, %edx
idivq %r14
movq %rax, %r11
cmpq $0x0, 0x8(%rsp)
je 0x343cb
movq 0x8(%rsp), %rax
cmpq %r10, 0x8(%rax)
jae 0x343c2
leaq -0x1(%r10), %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rsi
shrq $0x10, %rsi
orq %rax, %rsi
incq %rsi
movq 0x8(%rsp), %rax
movq %rsi, 0x8(%rax)
movq 0x10(%rax), %rdi
movq %r10, 0x30(%rsp)
movq %r11, 0x28(%rsp)
callq 0x2370
movq 0x28(%rsp), %r11
movq 0x30(%rsp), %r10
movq 0x8(%rsp), %rcx
movq %rax, 0x10(%rcx)
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rsi
notb %r15b
subb %r14b, %bpl
cmpb $0x0, 0x1c(%rsp)
je 0x344d5
movq %rbx, %rax
cqto
idivq %r11
movq 0x20(%rsp), %r8
subq %r8, 0x10(%rsp)
movl %r14d, %edi
imull %edx, %edi
testb %dil, %dil
je 0x3443e
cmpb $0x8, %dil
setae %cl
cmpq %r8, %r12
setle %dl
orb %cl, %dl
jne 0x3443e
movq %r10, %r8
movq 0x10(%rsp), %r12
movzbl (%r13,%rax), %edx
movl %edi, %ecx
shll %cl, %edx
movl %ebp, %ecx
shrl %cl, %edx
andb %r15b, %dl
movb %dl, -0x1(%rsi,%r8)
decq %r8
addb %r14b, %dil
incq %rbx
cmpb $0x7, %dil
ja 0x34446
cmpq %r12, %rbx
jl 0x34412
jmp 0x34446
movq %r10, %r8
movq 0x10(%rsp), %r12
movq %rbx, %rax
cqto
idivq %r11
leaq (%rbx,%r11), %rcx
cmpq %r12, %rcx
jg 0x34482
movq %rcx, %rbx
xorl %edx, %edx
movzbl (%r13,%rax), %edi
movl %edx, %ecx
shll %cl, %edi
movl %ebp, %ecx
shrl %cl, %edi
andb %r15b, %dil
movb %dil, -0x1(%rsi,%r8)
decq %r8
addb %r14b, %dl
cmpb $0x8, %dl
jb 0x3445c
incq %rax
jmp 0x3444e
cmpq %r12, %rbx
jge 0x344ad
xorl %edx, %edx
movzbl (%r13,%rax), %edi
movl %edx, %ecx
shll %cl, %edi
movl %ebp, %ecx
sarl %cl, %edi
andb %r15b, %dil
movb %dil, -0x1(%rsi,%r8)
decq %r8
addb %r14b, %dl
incq %rbx
cmpq %r12, %rbx
jl 0x34489
testq %r8, %r8
je 0x34596
leaq 0x1f0290(%rip), %rdi    # 0x22474d
leaq 0x1f021f(%rip), %rsi    # 0x2246e3
leaq 0x1f0229(%rip), %rcx    # 0x2246f4
movl $0x3ca, %edx            # imm = 0x3CA
callq 0x2110
movq 0x20(%rsp), %rbx
movq %rbx, %rax
cqto
idivq %r11
movl %r14d, %edi
imull %edx, %edi
testb %dil, %dil
je 0x3452c
cmpb $0x8, %dil
setae %cl
cmpq %rbx, %r12
setle %dl
orb %cl, %dl
jne 0x3452c
xorl %r8d, %r8d
movzbl (%r13,%rax), %edx
movl %edi, %ecx
shll %cl, %edx
movl %ebp, %ecx
shrl %cl, %edx
andb %r15b, %dl
movb %dl, (%rsi,%r8)
incq %r8
addb %r14b, %dil
incq %rbx
cmpb $0x7, %dil
ja 0x3452f
cmpq %r12, %rbx
jl 0x34501
jmp 0x3452f
xorl %r8d, %r8d
movq %rbx, %rax
cqto
idivq %r11
leaq (%rbx,%r11), %rcx
cmpq %r12, %rcx
jg 0x3456a
movq %rcx, %rbx
xorl %edx, %edx
movzbl (%r13,%rax), %edi
movl %edx, %ecx
shll %cl, %edi
movl %ebp, %ecx
shrl %cl, %edi
andb %r15b, %dil
movb %dil, (%rsi,%r8)
incq %r8
addb %r14b, %dl
cmpb $0x8, %dl
jb 0x34545
incq %rax
jmp 0x34537
subq %rbx, %r12
jle 0x34591
xorl %edx, %edx
movzbl (%r13,%rax), %edi
movl %edx, %ecx
shll %cl, %edi
movl %ebp, %ecx
sarl %cl, %edi
andb %r15b, %dil
movb %dil, (%rsi,%r8)
incq %r8
addb %r14b, %dl
decq %r12
jne 0x34571
cmpq %r10, %r8
jne 0x345b0
movq 0x8(%rsp), %rax
movq %r10, (%rax)
movq %r10, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1f0124(%rip), %rdi    # 0x2246db
leaq 0x1f0125(%rip), %rsi    # 0x2246e3
leaq 0x1f012f(%rip), %rcx    # 0x2246f4
movl $0x3b7, %edx            # imm = 0x3B7
callq 0x2110