// File: /workspace/github_c_makefile_star2_O2/lh3[P]fermi-lite/rle.c
// Function: rle_insert_cached
int rle_insert_cached(uint8_t *block, int64_t x, int a, int64_t rl, int64_t cnt[6], const int64_t ec[6], int *beg, int64_t bc[6])
{
	uint16_t *nptr = (uint16_t*)block;
	int diff;

	block += 2; // skip the first 2 counting bytes
	if (*nptr == 0) {
		memset(cnt, 0, 48);
		diff = rle_enc1(block, a, rl);
	} else {
		uint8_t *p, *end = block + *nptr, *q;
		int64_t pre, z, l = 0, tot, beg_l;
		int c = -1, n_bytes = 0, n_bytes2, t = 0;
		uint8_t tmp[24];
		beg_l = bc[0] + bc[1] + bc[2] + bc[3] + bc[4] + bc[5];
		tot   = ec[0] + ec[1] + ec[2] + ec[3] + ec[4] + ec[5];
		if (x < beg_l) {
			beg_l = 0, *beg = 0;
			memset(bc, 0, 48);
		}
		if (x == beg_l) {
			p = q = block + (*beg); z = beg_l;
			memcpy(cnt, bc, 48);
		} else if (x - beg_l <= ((tot-beg_l)>>1) + ((tot-beg_l)>>3)) { // forward
			z = beg_l; p = block + (*beg);
			memcpy(cnt, bc, 48);
			while (z < x) {
				rle_dec1(p, c, l);
				z += l; cnt[c] += l;
			}
			for (q = p - 1; *q>>6 == 2; --q);
		} else { // backward
			memcpy(cnt, ec, 48);
			z = tot; p = end;
			while (z >= x) {
				--p;
				if (*p>>6 != 2) {
					l |= *p>>7? (int64_t)rle_auxtab[*p>>3&7]>>4 << t : *p>>3;
					z -= l; cnt[*p&7] -= l;
					l = 0; t = 0;
				} else {
					l |= (*p&0x3fL) << t;
					t += 6;
				}
			}
			q = p;
			rle_dec1(p, c, l);
			z += l; cnt[c] += l;
		}
		*beg = q - block;
		memcpy(bc, cnt, 48);
		bc[c] -= l;
		n_bytes = p - q;
		if (x == z && a != c && p < end) { // then try the next run
			int tc;
			int64_t tl;
			q = p;
			rle_dec1(q, tc, tl);
			if (a == tc)
				c = tc, n_bytes = q - p, l = tl, z += l, p = q, cnt[tc] += tl;
		}
		if (z != x) cnt[c] -= z - x;
		pre = x - (z - l); p -= n_bytes;
		if (a == c) { // insert to the same run
			n_bytes2 = rle_enc1(tmp, c, l + rl);
		} else if (x == z) { // at the end; append to the existing run
			p += n_bytes; n_bytes = 0;
			n_bytes2 = rle_enc1(tmp, a, rl);
		} else { // break the current run
			n_bytes2 = rle_enc1(tmp, c, pre);
			n_bytes2 += rle_enc1(tmp + n_bytes2, a, rl);
			n_bytes2 += rle_enc1(tmp + n_bytes2, c, l - pre);
		}
		if (n_bytes != n_bytes2 && end != p + n_bytes) // size changed
			memmove(p + n_bytes2, p + n_bytes, end - p - n_bytes);
		memcpy(p, tmp, n_bytes2);
		diff = n_bytes2 - n_bytes;
	}
	return (*nptr += diff);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %r14
addq $0x2, %rdi
movzwl (%r14), %r15d
testq %r15, %r15
je 0x13045
movq %rcx, 0x8(%rsp)
movq 0x78(%rsp), %rax
movdqu (%rax), %xmm0
movdqu 0x10(%rax), %xmm1
paddq %xmm0, %xmm1
pshufd $0xee, %xmm1, %xmm0     # xmm0 = xmm1[2,3,2,3]
paddq %xmm1, %xmm0
movq %xmm0, %r10
addq 0x20(%rax), %r10
addq 0x28(%rax), %r10
movq 0x8(%r9), %rbx
addq (%r9), %rbx
addq 0x10(%r9), %rbx
addq 0x18(%r9), %rbx
addq 0x20(%r9), %rbx
movq 0x70(%rsp), %r13
addq 0x28(%r9), %rbx
cmpq %rsi, %r10
jle 0x13006
andl $0x0, (%r13)
pxor %xmm0, %xmm0
movdqu %xmm0, 0x20(%rax)
movdqu %xmm0, 0x10(%rax)
movdqu %xmm0, (%rax)
xorl %r10d, %r10d
movl %edx, (%rsp)
addq %rdi, %r15
movq %rsi, %rcx
subq %r10, %rcx
jne 0x13068
movslq (%r13), %r11
addq %rdi, %r11
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movups %xmm2, 0x20(%r8)
movups %xmm1, 0x10(%r8)
movups %xmm0, (%r8)
pushq $-0x1
popq %r12
xorl %ecx, %ecx
movq %r11, %r9
movq %rsi, %r10
jmp 0x13203
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%r8)
movups %xmm0, 0x10(%r8)
movups %xmm0, (%r8)
movl %edx, %esi
movq %rcx, %rdx
callq 0x13471
movl %eax, %r15d
jmp 0x13390
movq %r14, %rdx
movq %rbx, %r11
subq %r10, %r11
movq %r11, %r14
sarq %r14
sarq $0x3, %r11
addq %r14, %r11
cmpq %r11, %rcx
jle 0x1313f
movups (%r9), %xmm0
movups 0x10(%r9), %xmm1
movups 0x20(%r9), %xmm2
movups %xmm2, 0x20(%r8)
movups %xmm1, 0x10(%r8)
movups %xmm0, (%r8)
leaq 0x1f07(%rip), %r9       # 0x14fb1
movq %r15, %r11
xorl %r10d, %r10d
xorl %ecx, %ecx
cmpq %rsi, %rbx
jl 0x13113
movzbl -0x1(%r11), %r14d
decq %r11
movl %r14d, %ebp
andl $-0x40, %ebp
cmpl $0x80, %ebp
jne 0x130dc
andl $0x3f, %r14d
shlq %cl, %r14
orq %r14, %r10
addl $0x6, %ecx
jmp 0x130b2
movq %r15, %r12
movl %r14d, %r15d
shrl $0x3, %r15d
testb %r14b, %r14b
js 0x130f0
movl %r15d, %r15d
jmp 0x13100
andl $0x7, %r15d
movzbl (%r15,%r9), %r15d
shrl $0x4, %r15d
shlq %cl, %r15
orq %r10, %r15
subq %r15, %rbx
andl $0x7, %r14d
subq %r15, (%r8,%r14,8)
movq %r12, %r15
jmp 0x130ad
movzbl (%r11), %ecx
movl %ecx, %r12d
andl $0x7, %r12d
testb %cl, %cl
js 0x133d0
leaq 0x1(%r11), %r9
shrl $0x3, %ecx
movq %rdx, %r14
addq %rcx, %rbx
addq %rcx, (%r8,%r12,8)
movq %rbx, %r10
jmp 0x13203
pushq $-0x1
popq %r12
movslq (%r13), %r9
addq %rdi, %r9
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movups %xmm2, 0x20(%r8)
movups %xmm1, 0x10(%r8)
movups %xmm0, (%r8)
xorl %ecx, %ecx
movq %rdx, %r14
cmpq %rsi, %r10
jge 0x131f2
movzbl (%r9), %r12d
testb %r12b, %r12b
js 0x13190
incq %r9
movl %r12d, %ecx
shrl $0x3, %ecx
andl $0x7, %r12d
addq %rcx, %r10
addq %rcx, (%r8,%r12,8)
jmp 0x13168
movl %r12d, %ecx
andl $-0x20, %ecx
cmpl $0xc0, %ecx
jne 0x131b7
movl %r12d, %ecx
andl $0x18, %ecx
movzbl 0x1(%r9), %r11d
andl $0x3f, %r11d
leaq (%r11,%rcx,8), %rcx
addq $0x2, %r9
jmp 0x13183
movl %r12d, %r11d
shrl $0x2, %r11d
andl $0x4, %r11d
movl %r12d, %ecx
shrl $0x3, %ecx
andl $0x1, %ecx
incq %r9
xorl $-0x3, %r11d
testl %r11d, %r11d
je 0x131ed
shlq $0x6, %rcx
movzbl (%r9), %ebx
andl $0x3f, %ebx
orq %rbx, %rcx
incq %r9
incl %r11d
jmp 0x131d2
movq %rdx, %r14
jmp 0x13183
movq %r9, %r11
movb -0x1(%r11), %bl
decq %r11
andb $-0x40, %bl
negb %bl
jo 0x131f5
movl %r11d, %ebx
subl %edi, %ebx
movl %ebx, (%r13)
movups (%r8), %xmm0
movups 0x10(%r8), %xmm1
movups 0x20(%r8), %xmm2
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
movslq %r12d, %rdx
subq %rcx, (%rax,%rdx,8)
movq %r9, %r13
subq %r11, %r13
cmpq %rsi, %r10
jne 0x1328e
movl (%rsp), %r11d
cmpl %r11d, %r12d
je 0x1327b
cmpq %r15, %r9
jae 0x1327b
movzbl (%r9), %ebp
movl %ebp, %eax
andl $0x7, %eax
testb %bpl, %bpl
js 0x133ab
leaq 0x1(%r9), %rbx
shrl $0x3, %ebp
movb $0x1, %dl
cmpl %r11d, %eax
jne 0x13283
movq %rbx, %r13
subq %r9, %r13
leaq (%rsi,%rbp), %r10
movl %r11d, %eax
addq %rbp, (%r8,%rax,8)
movl %r11d, %r12d
jmp 0x13298
movq %r9, %rbx
movq %rcx, %rbp
jmp 0x13298
movq %rcx, %rbp
movq %rsi, %r10
movq %r9, %rbx
jmp 0x132ab
movq %r9, %rbx
movq %rcx, %rbp
movl (%rsp), %r11d
movb $0x1, %dl
movq %rsi, %rax
subq %r10, %rax
je 0x132ab
movslq %r12d, %rcx
addq %rax, (%r8,%rcx,8)
xorl %edx, %edx
cmpl %r11d, %r12d
movq %r15, 0x30(%rsp)
jne 0x132d1
addq 0x8(%rsp), %rbp
leaq 0x10(%rsp), %rdi
movl %r11d, %esi
movq %rbp, %rdx
callq 0x13471
movl %eax, %ebp
jmp 0x13353
testb %dl, %dl
je 0x132ee
leaq 0x10(%rsp), %rdi
movl %r11d, %esi
movq 0x8(%rsp), %rdx
callq 0x13471
movl %eax, %ebp
xorl %r13d, %r13d
jmp 0x13360
movq %rbp, %r15
subq %r10, %r15
addq %rsi, %r15
leaq 0x10(%rsp), %rdi
movl %r12d, %esi
movq %r15, %rdx
callq 0x13471
movq %rbx, 0x28(%rsp)
movl %eax, %ebx
movl %eax, %eax
leaq (%rsp,%rax), %rdi
addq $0x10, %rdi
movl (%rsp), %esi
movq 0x8(%rsp), %rdx
callq 0x13471
movq %r14, (%rsp)
movl %eax, %r14d
addl %ebx, %r14d
movq 0x28(%rsp), %rbx
leaq (%rsp,%r14), %rdi
addq $0x10, %rdi
subq %r15, %rbp
movl %r12d, %esi
movq %rbp, %rdx
callq 0x13471
movl %eax, %ebp
addl %r14d, %ebp
movq (%rsp), %r14
movslq %r13d, %rax
subq %rax, %rbx
movl %r13d, %r15d
cmpl %eax, %ebp
je 0x1337d
movslq %r13d, %rsi
addq %rbx, %rsi
movq 0x30(%rsp), %rdx
subq %rsi, %rdx
je 0x1337a
movl %ebp, %edi
addq %rbx, %rdi
callq 0x2200
movl %ebp, %r15d
movl %r15d, %edx
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x2170
subl %r13d, %r15d
addw (%r14), %r15w
movw %r15w, (%r14)
movzwl %r15w, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %ebp, %edx
andl $-0x20, %edx
cmpl $0xc0, %edx
jne 0x133f9
andl $0x18, %ebp
movzbl 0x1(%r9), %edx
andl $0x3f, %edx
leaq (%rdx,%rbp,8), %rbp
leaq 0x2(%r9), %rbx
jmp 0x1325e
movl %ecx, %r9d
andl $-0x20, %r9d
cmpl $0xc0, %r9d
jne 0x1342f
andl $0x18, %ecx
movzbl 0x1(%r11), %r9d
andl $0x3f, %r9d
leaq (%r9,%rcx,8), %rcx
leaq 0x2(%r11), %r9
jmp 0x1312d
movl %ebp, %edx
shrl $0x2, %edx
andl $0x4, %edx
shrl $0x3, %ebp
andl $0x1, %ebp
leaq 0x1(%r9), %rbx
xorl $-0x3, %edx
testl %edx, %edx
je 0x13426
shlq $0x6, %rbp
movzbl (%rbx), %edi
andl $0x3f, %edi
orq %rdi, %rbp
incq %rbx
incl %edx
jmp 0x1340e
movl (%rsp), %r11d
jmp 0x1325e
movl %ecx, %r10d
shrl $0x2, %r10d
andl $0x4, %r10d
shrl $0x3, %ecx
andl $0x1, %ecx
xorl $-0x4, %r10d
incq %r10
pushq $0x1
popq %r9
leal (%r10,%r9), %ebp
cmpl $0x1, %ebp
je 0x13469
shlq $0x6, %rcx
movzbl (%r11,%r9), %r14d
andl $0x3f, %r14d
orq %r14, %rcx
incq %r9
jmp 0x1344b
addq %r11, %r9
jmp 0x1312d