// File: /workspace/github_c_makefile_star2_O2/lh3[P]fermi-lite/rld0.c
// Function: rld_locate_blk
static inline uint64_t rld_locate_blk(const rld_t *e, rlditr_t *itr, uint64_t k, uint64_t *cnt, uint64_t *sum)
{
	int j;
	uint64_t c = 0, *q, *z = e->frame + (k>>e->ibits) * e->asize1;
	itr->i = e->z + (*z>>RLD_LBITS);
	q = itr->p = *itr->i + (*z&RLD_LMASK);
	for (j = 1, *sum = 0; j < e->asize1; ++j) *sum += (cnt[j-1] = z[j]);
	while (1) { // seek to the small block
		int type;
		q += e->ssize;
		if (q - *itr->i == RLD_LSIZE) q = *++itr->i;
		type = rld_block_type(*q);
		c = type == 2? *q&0x3fffffffffffffffULL : type == 1? *(uint32_t*)q : *(uint16_t*)q;
		if (*sum + c > k) break;
		if (type == 0) {
			uint16_t *p = (uint16_t*)q + 1;
#ifdef _DNA_ONLY
			cnt[0] += p[0]; cnt[1] += p[1]; cnt[2] += p[2]; cnt[3] += p[3]; cnt[4] += p[4]; cnt[5] += p[5];
#else
			for (j = 0; j < e->asize; ++j) cnt[j] += p[j];
#endif
		} else if (type == 1) {
			uint32_t *p = (uint32_t*)q + 1;
			for (j = 0; j < e->asize; ++j) cnt[j] += p[j] & 0x3fffffff;
		} else {
			uint64_t *p = (uint64_t*)q + 1;
			for (j = 0; j < e->asize; ++j) cnt[j] += p[j];
		}
		*sum += c;
		itr->p = q;
	}
	itr->shead = itr->p;
	itr->stail = rld_get_stail(e, itr);
	itr->p += e->offset0[rld_block_type(*itr->shead)];
	itr->q = (uint8_t*)itr->p;
	itr->r = 64;
	return c + *sum;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rcx, %r9
movq 0x38(%rdi), %rax
movb 0x4(%rdi), %cl
movq %rdx, %r11
shrq %cl, %r11
movzbl 0x1(%rdi), %r10d
imulq %r10, %r11
leaq (%rax,%r11,8), %r11
movq %rdi, -0x8(%rsp)
movq 0x18(%rdi), %rax
movq (%r11), %r15
movq %r15, %rcx
shrq $0x17, %rcx
leaq (%rax,%rcx,8), %rcx
movq %rcx, 0x28(%rsi)
andl $0x7fffff, %r15d        # imm = 0x7FFFFF
shll $0x3, %r15d
addq (%rcx), %r15
movq %r15, 0x10(%rsi)
andq $0x0, (%r8)
movabsq $0x1, %rdi
xorl %eax, %eax
cmpq %r10, %rdi
jae 0x82de
movq (%r11,%rdi,8), %rax
movq %rax, -0x8(%r9,%rdi,8)
addq (%r8), %rax
movq %rax, (%r8)
incq %rdi
jmp 0x82c5
movabsq $0x3fffffffffffffff, %r10 # imm = 0x3FFFFFFFFFFFFFFF
movq -0x8(%rsp), %rdi
movslq 0x8(%rdi), %r11
leaq 0x1(%r10), %r14
movapd 0xc433(%rip), %xmm0     # 0x14730
pxor %xmm1, %xmm1
leaq (%r15,%r11,8), %rbp
movq %rbp, %rdi
subq (%rcx), %rdi
cmpq $0x4000000, %rdi        # imm = 0x4000000
jne 0x8325
leaq 0x8(%rcx), %rdi
movq %rdi, 0x28(%rsi)
movq 0x8(%rcx), %r12
movq %rdi, %rcx
jmp 0x8328
movq %rbp, %r12
movq (%r12), %rdi
movq %rdi, %rbx
shrq $0x3e, %rbx
cmpq $0x1, %rbx
movl $0xffff, %r13d          # imm = 0xFFFF
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
cmoveq %r10, %r13
cmpq $0x2, %rbx
movabsq $0x3fffffffffffffff, %r10 # imm = 0x3FFFFFFFFFFFFFFF
cmoveq %r10, %r13
andq %rdi, %r13
addq %r13, %rax
cmpq %rdx, %rax
ja 0x8418
cmpq %r14, %rdi
jae 0x83dd
movq %rdi, %xmm2
pshufd $0x44, %xmm2, %xmm3     # xmm3 = xmm2[0,1,0,1]
psrlq $0x10, %xmm2
psrlq $0x20, %xmm3
movsd %xmm2, %xmm3            # xmm3 = xmm2[0],xmm3[1]
andpd %xmm0, %xmm3
shrq $0x30, %rdi
movdqu (%r9), %xmm2
paddq %xmm3, %xmm2
movdqu 0x18(%r9), %xmm3
movdqu %xmm2, (%r9)
addq %rdi, 0x10(%r9)
movd 0x8(%r12), %xmm2
punpcklwd %xmm1, %xmm2    # xmm2 = xmm2[0],xmm1[0],xmm2[1],xmm1[1],xmm2[2],xmm1[2],xmm2[3],xmm1[3]
punpckldq %xmm1, %xmm2    # xmm2 = xmm2[0],xmm1[0],xmm2[1],xmm1[1]
paddq %xmm3, %xmm2
movdqu %xmm2, 0x18(%r9)
movzwl 0xc(%r12), %eax
addq %rax, 0x28(%r9)
addq (%r8), %r13
movq %r13, (%r8)
movq %r12, 0x10(%rsi)
movq %r13, %rax
movq %r12, %r15
jmp 0x8301
movq -0x8(%rsp), %rax
movzbl (%rax), %eax
xorl %edi, %edi
cmpl $0x1, %ebx
jne 0x8405
cmpq %rdi, %rax
je 0x83c8
movl 0x4(%r12,%rdi,4), %ebx
andl $0x3fffffff, %ebx       # imm = 0x3FFFFFFF
addq %rbx, (%r9,%rdi,8)
incq %rdi
jmp 0x83ec
cmpq %rdi, %rax
je 0x83c8
movq 0x8(%r12,%rdi,8), %rbx
addq %rbx, (%r9,%rdi,8)
incq %rdi
jmp 0x8405
movq %r15, 0x18(%rsi)
movq %rbp, %rdx
subq (%rcx), %rdx
xorl %ecx, %ecx
cmpq $0x4000000, %rdx        # imm = 0x4000000
setne %cl
shll $0x3, %ecx
orq $-0x10, %rcx
addq %rbp, %rcx
movq %rcx, 0x20(%rsi)
movq (%r15), %rcx
shrq $0x3e, %rcx
movq -0x8(%rsp), %rdx
movsbq 0x5(%rdx,%rcx), %rcx
leaq (%r15,%rcx,8), %rcx
movq %rcx, 0x10(%rsi)
movq %rcx, 0x30(%rsi)
movl $0x40, (%rsi)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq