// File: /workspace/github_c_makefile_star2_O1/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 $0x28, %rsp
movq %rcx, %rax
movl %edx, %r10d
movq %rdi, %rbx
addq $0x2, %rdi
movzwl (%rbx), %edx
testq %rdx, %rdx
je 0x156b9
movq 0x68(%rsp), %r11
movq 0x8(%r11), %rcx
addq (%r11), %rcx
addq 0x10(%r11), %rcx
addq 0x18(%r11), %rcx
addq 0x20(%r11), %rcx
addq 0x28(%r11), %rcx
movq 0x8(%r9), %rbp
addq (%r9), %rbp
addq 0x10(%r9), %rbp
addq 0x18(%r9), %rbp
addq 0x20(%r9), %rbp
movq 0x60(%rsp), %r13
addq 0x28(%r9), %rbp
cmpq %rsi, %rcx
jle 0x15677
movl $0x0, (%r13)
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%r11)
movups %xmm0, 0x10(%r11)
movups %xmm0, (%r11)
xorl %ecx, %ecx
addq %rdi, %rdx
movq %rsi, %r14
subq %rcx, %r14
jne 0x156e2
movslq (%r13), %r9
addq %rdi, %r9
movups (%r11), %xmm0
movups 0x10(%r11), %xmm1
movups 0x20(%r11), %xmm2
movups %xmm2, 0x20(%r8)
movups %xmm1, 0x10(%r8)
movups %xmm0, (%r8)
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movq %r13, %r14
xorl %r13d, %r13d
movq %r9, %r12
jmp 0x15910
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%r8)
movups %xmm0, 0x10(%r8)
movups %xmm0, (%r8)
cmpq $0xf, %rax
jg 0x15727
shll $0x3, %eax
orl %r10d, %eax
movb %al, (%rdi)
movw $0x1, %r12w
jmp 0x15e97
movq %rbp, %r15
subq %rcx, %r15
movq %r15, %r12
sarq %r12
sarq $0x3, %r15
addq %r12, %r15
cmpq %r15, %r14
jle 0x15755
movups (%r9), %xmm0
movups 0x10(%r9), %xmm1
movups 0x20(%r9), %xmm2
movups %xmm2, 0x20(%r8)
movups %xmm1, 0x10(%r8)
movups %xmm0, (%r8)
cmpq %rsi, %rbp
jge 0x15858
movq %rdx, %r9
jmp 0x158c7
cmpq $0xff, %rax
jg 0x1580c
movl %eax, %ecx
shrl $0x3, %ecx
andl $0x38, %ecx
orl %r10d, %ecx
orb $-0x40, %cl
movb %cl, 0x2(%rbx)
andb $0x3f, %al
orb $-0x80, %al
movb %al, 0x3(%rbx)
movw $0x2, %r12w
jmp 0x15e97
movslq (%r13), %r12
addq %rdi, %r12
movups (%r11), %xmm0
movups 0x10(%r11), %xmm1
movups 0x20(%r11), %xmm2
movups %xmm2, 0x20(%r8)
movups %xmm1, 0x10(%r8)
movups %xmm0, (%r8)
cmpq %rcx, %rsi
jle 0x158ef
movzbl (%r12), %r15d
testb %r15b, %r15b
js 0x157aa
incq %r12
movl %r15d, %r13d
shrl $0x3, %r13d
andl $0x7, %r15d
addq %r13, %rcx
addq %r13, (%r8,%r15,8)
cmpq %rsi, %rcx
jl 0x15781
jmp 0x158f8
movl %r15d, %r9d
andl $-0x20, %r9d
cmpl $0xc0, %r9d
jne 0x157d5
movl %r15d, %r9d
andl $0x18, %r9d
movzbl 0x1(%r12), %r14d
andl $0x3f, %r14d
leaq (%r14,%r9,8), %r13
addq $0x2, %r12
jmp 0x15795
movl %r15d, %r9d
shrl $0x2, %r9d
andl $0x4, %r9d
movl %r15d, %r13d
shrl $0x3, %r13d
andl $0x1, %r13d
incq %r12
xorl $-0x3, %r9d
shlq $0x6, %r13
movzbl (%r12), %r14d
andl $0x3f, %r14d
orq %r14, %r13
incq %r12
incl %r9d
jne 0x157f2
jmp 0x15795
movl %r10d, %ecx
cmpq $0x7ffff, %rax          # imm = 0x7FFFF
jg 0x15b01
movl %eax, %edx
shrl $0xf, %edx
andl $0x18, %edx
orl %edx, %ecx
orb $-0x20, %cl
movb %cl, 0x2(%rbx)
movl %eax, %ecx
shrl $0xc, %ecx
andb $0x3f, %cl
orb $-0x80, %cl
movb %cl, 0x3(%rbx)
movl %eax, %ecx
shrl $0x6, %ecx
andb $0x3f, %cl
orb $-0x80, %cl
movb %cl, 0x4(%rbx)
andb $0x3f, %al
orb $-0x80, %al
movb %al, 0x5(%rbx)
movw $0x4, %r12w
jmp 0x15e97
xorl %ecx, %ecx
leaq 0x2710(%rip), %r15      # 0x17f71
xorl %r12d, %r12d
movq %rdx, %r9
movzbl -0x1(%r9), %r13d
movl %r13d, %r14d
andl $-0x40, %r14d
cmpl $0x80, %r14d
jne 0x1588b
andl $0x3f, %r13d
shlq %cl, %r13
orq %r13, %r12
addl $0x6, %ecx
jmp 0x158bf
movl %r13d, %r14d
shrl $0x3, %r14d
testb %r13b, %r13b
js 0x1589c
movl %r14d, %r14d
jmp 0x158ac
andl $0x7, %r14d
movzbl (%r14,%r15), %r14d
shrl $0x4, %r14d
shlq %cl, %r14
orq %r12, %r14
andl $0x7, %r13d
subq %r14, (%r8,%r13,8)
subq %r14, %rbp
xorl %r12d, %r12d
xorl %ecx, %ecx
decq %r9
cmpq %rsi, %rbp
jge 0x15867
movzbl (%r9), %r13d
movl %r13d, %r15d
andl $0x7, %r15d
testb %r13b, %r13b
js 0x15edc
leaq 0x1(%r9), %r12
shrl $0x3, %r13d
addq %r13, %rbp
addq %r13, (%r8,%r15,8)
movq %rbp, %rcx
jmp 0x1590b
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
xorl %r13d, %r13d
movq %r12, %r9
movb -0x1(%r9), %bpl
decq %r9
andb $-0x40, %bpl
negb %bpl
jo 0x158fb
movq 0x60(%rsp), %r14
movl %r9d, %ebp
subl %edi, %ebp
movl %ebp, (%r14)
movups (%r8), %xmm0
movups 0x10(%r8), %xmm1
movups 0x20(%r8), %xmm2
movups %xmm2, 0x20(%r11)
movups %xmm1, 0x10(%r11)
movups %xmm0, (%r11)
movslq %r15d, %rdi
subq %r13, (%r11,%rdi,8)
movq %r12, %rdi
subq %r9, %rdi
cmpq %rsi, %rcx
jne 0x15982
cmpl %r10d, %r15d
je 0x15982
cmpq %rdx, %r12
jae 0x15982
movzbl (%r12), %r9d
movl %r9d, %r11d
andl $0x7, %r11d
testb %r9b, %r9b
js 0x15eb2
leaq 0x1(%r12), %rbp
shrl $0x3, %r9d
cmpl %r10d, %r11d
jne 0x15982
movq %rbp, %rdi
subq %r12, %rdi
addq %r9, %rcx
addq %r9, (%r8,%r11,8)
jmp 0x1598b
movq %r12, %rbp
movq %r13, %r9
movl %r15d, %r11d
movq %rsi, %r14
subq %rcx, %r14
je 0x1599a
movslq %r11d, %r15
addq %r14, (%r8,%r15,8)
movslq %edi, %r8
movq %rbp, %r14
subq %r8, %r14
cmpl %r10d, %r11d
jne 0x159c7
addq %rax, %r9
cmpq $0xf, %r9
jg 0x159e6
shll $0x3, %r9d
orl %r11d, %r9d
movb %r9b, (%rsp)
movl $0x1, %r12d
jmp 0x15e6d
cmpq %rcx, %rsi
jne 0x15a1c
cmpq $0xf, %rax
jg 0x15a47
shll $0x3, %eax
orl %r10d, %eax
movb %al, (%rsp)
movl $0x1, %r12d
jmp 0x15c6b
cmpq $0xff, %r9
jg 0x15a77
movl %r9d, %eax
shrl $0x3, %eax
andl $0x38, %eax
orl %r11d, %eax
orb $-0x40, %al
movb %al, (%rsp)
andb $0x3f, %r9b
orb $-0x80, %r9b
movb %r9b, 0x1(%rsp)
movl $0x2, %r12d
jmp 0x15e6d
movq %r9, %r8
subq %rcx, %r8
addq %rsi, %r8
cmpq $0xf, %r8
jg 0x15acb
leal (,%r8,8), %ecx
orl %r11d, %ecx
movb %cl, (%rsp)
movl $0x1, %ecx
jmp 0x15cb4
cmpq $0xff, %rax
jg 0x15b44
movl %eax, %ecx
shrl $0x3, %ecx
andl $0x38, %ecx
orl %r10d, %ecx
orb $-0x40, %cl
movb %cl, (%rsp)
andb $0x3f, %al
orb $-0x80, %al
movb %al, 0x1(%rsp)
movl $0x2, %r12d
jmp 0x15c6b
movl %r11d, %eax
cmpq $0x7ffff, %r9           # imm = 0x7FFFF
jg 0x15b94
movl %r9d, %ecx
shrl $0xf, %ecx
andl $0x18, %ecx
orl %ecx, %eax
orb $-0x20, %al
movb %al, (%rsp)
movl %r9d, %eax
shrl $0xc, %eax
andb $0x3f, %al
orb $-0x80, %al
movb %al, 0x1(%rsp)
movl %r9d, %eax
shrl $0x6, %eax
andb $0x3f, %al
orb $-0x80, %al
movb %al, 0x2(%rsp)
andb $0x3f, %r9b
orb $-0x80, %r9b
movb %r9b, 0x3(%rsp)
movl $0x4, %r12d
jmp 0x15e6d
cmpq $0xff, %r8
jg 0x15bd3
movl %r8d, %ecx
shrl $0x3, %ecx
andl $0x38, %ecx
orl %r11d, %ecx
orb $-0x40, %cl
movb %cl, (%rsp)
movl %r8d, %ecx
andb $0x3f, %cl
orb $-0x80, %cl
movb %cl, 0x1(%rsp)
movl $0x2, %ecx
jmp 0x15cb4
movq %rax, %rdx
shrq $0x27, %rdx
andl $0x8, %edx
orl %edx, %ecx
orb $-0x10, %cl
movb %cl, 0x2(%rbx)
leaq 0x3(%rbx), %rdx
movl $0x24, %ecx
movq %rax, %rsi
shrq %cl, %rsi
andb $0x3f, %sil
orb $-0x80, %sil
movb %sil, (%rdx)
addq $-0x6, %rcx
incq %rdx
cmpq $-0x6, %rcx
jne 0x15b1c
movw $0x8, %r12w
jmp 0x15e97
movl %r10d, %ecx
cmpq $0x7ffff, %rax          # imm = 0x7FFFF
jg 0x15c2b
movl %eax, %esi
shrl $0xf, %esi
andl $0x18, %esi
orl %esi, %ecx
orb $-0x20, %cl
movb %cl, (%rsp)
movl %eax, %ecx
shrl $0xc, %ecx
andb $0x3f, %cl
orb $-0x80, %cl
movb %cl, 0x1(%rsp)
movl %eax, %ecx
shrl $0x6, %ecx
andb $0x3f, %cl
orb $-0x80, %cl
movb %cl, 0x2(%rsp)
andb $0x3f, %al
orb $-0x80, %al
movb %al, 0x3(%rsp)
movl $0x4, %r12d
jmp 0x15c6b
movq %r9, %rcx
shrq $0x27, %rcx
andl $0x8, %ecx
orl %ecx, %eax
orb $-0x10, %al
leaq 0x1(%rsp), %rsi
movb %al, -0x1(%rsi)
movl $0x24, %ecx
movq %r9, %rax
shrq %cl, %rax
andb $0x3f, %al
orb $-0x80, %al
movb %al, (%rsi)
addq $-0x6, %rcx
incq %rsi
cmpq $-0x6, %rcx
jne 0x15baf
movl $0x8, %r12d
jmp 0x15e6d
movl %r11d, %ecx
cmpq $0x7ffff, %r8           # imm = 0x7FFFF
jg 0x15c75
movl %r8d, %esi
shrl $0xf, %esi
andl $0x18, %esi
orl %esi, %ecx
orb $-0x20, %cl
movb %cl, (%rsp)
movl %r8d, %ecx
shrl $0xc, %ecx
andb $0x3f, %cl
orb $-0x80, %cl
movb %cl, 0x1(%rsp)
movl %r8d, %ecx
shrl $0x6, %ecx
andb $0x3f, %cl
orb $-0x80, %cl
movb %cl, 0x2(%rsp)
movl %r8d, %ecx
andb $0x3f, %cl
orb $-0x80, %cl
movb %cl, 0x3(%rsp)
movl $0x4, %ecx
jmp 0x15cb4
movq %rax, %rsi
shrq $0x27, %rsi
andl $0x8, %esi
orl %esi, %ecx
orb $-0x10, %cl
leaq 0x1(%rsp), %rsi
movb %cl, -0x1(%rsi)
movl $0x24, %ecx
movq %rax, %rdi
shrq %cl, %rdi
andb $0x3f, %dil
orb $-0x80, %dil
movb %dil, (%rsi)
addq $-0x6, %rcx
incq %rsi
cmpq $-0x6, %rcx
jne 0x15c47
movl $0x8, %r12d
xorl %edi, %edi
movq %rbp, %r14
jmp 0x15e6d
movq %r8, %rsi
shrq $0x27, %rsi
andl $0x8, %esi
orl %esi, %ecx
orb $-0x10, %cl
leaq 0x1(%rsp), %rsi
movb %cl, -0x1(%rsi)
movl $0x24, %ecx
movq %r8, %r15
shrq %cl, %r15
andb $0x3f, %r15b
orb $-0x80, %r15b
movb %r15b, (%rsi)
addq $-0x6, %rcx
incq %rsi
cmpq $-0x6, %rcx
jne 0x15c91
movl $0x8, %ecx
movl %ecx, %r12d
cmpq $0xf, %rax
jg 0x15cd1
shll $0x3, %eax
orl %r10d, %eax
movb %al, (%rsp,%r12)
movl $0x1, %eax
jmp 0x15d8d
cmpq $0xff, %rax
jg 0x15cfe
movl %eax, %ecx
shrl $0x3, %ecx
andl $0x38, %ecx
orl %r10d, %ecx
orb $-0x40, %cl
movb %cl, (%rsp,%r12)
andb $0x3f, %al
orb $-0x80, %al
movb %al, 0x1(%rsp,%r12)
movl $0x2, %eax
jmp 0x15d8d
movl %r10d, %ecx
cmpq $0x7ffff, %rax          # imm = 0x7FFFF
jg 0x15d4a
movl %eax, %esi
shrl $0xf, %esi
andl $0x18, %esi
orl %esi, %ecx
orb $-0x20, %cl
movb %cl, (%rsp,%r12)
movl %eax, %ecx
shrl $0xc, %ecx
andb $0x3f, %cl
orb $-0x80, %cl
movb %cl, 0x1(%rsp,%r12)
movl %eax, %ecx
shrl $0x6, %ecx
andb $0x3f, %cl
orb $-0x80, %cl
movb %cl, 0x2(%rsp,%r12)
andb $0x3f, %al
orb $-0x80, %al
movb %al, 0x3(%rsp,%r12)
movl $0x4, %eax
jmp 0x15d8d
movq %rax, %rsi
shrq $0x27, %rsi
andl $0x8, %esi
orl %esi, %ecx
orb $-0x10, %cl
movq %rsp, %rsi
addq %r12, %rsi
incq %rsi
movb %cl, -0x1(%rsi)
movl $0x24, %ecx
movq %rax, %r10
shrq %cl, %r10
andb $0x3f, %r10b
orb $-0x80, %r10b
movb %r10b, (%rsi)
addq $-0x6, %rcx
incq %rsi
cmpq $-0x6, %rcx
jne 0x15d6a
movl $0x8, %eax
addl %eax, %r12d
subq %r8, %r9
cmpq $0xf, %r9
jg 0x15dae
shll $0x3, %r9d
orl %r11d, %r9d
movb %r9b, (%rsp,%r12)
movl $0x1, %eax
jmp 0x15e6a
cmpq $0xff, %r9
jg 0x15de0
movl %r9d, %eax
shrl $0x3, %eax
andl $0x38, %eax
orl %r11d, %eax
orb $-0x40, %al
movb %al, (%rsp,%r12)
andb $0x3f, %r9b
orb $-0x80, %r9b
movb %r9b, 0x1(%rsp,%r12)
movl $0x2, %eax
jmp 0x15e6a
movl %r11d, %eax
cmpq $0x7ffff, %r9           # imm = 0x7FFFF
jg 0x15e2f
movl %r9d, %ecx
shrl $0xf, %ecx
andl $0x18, %ecx
orl %ecx, %eax
orb $-0x20, %al
movb %al, (%rsp,%r12)
movl %r9d, %eax
shrl $0xc, %eax
andb $0x3f, %al
orb $-0x80, %al
movb %al, 0x1(%rsp,%r12)
movl %r9d, %eax
shrl $0x6, %eax
andb $0x3f, %al
orb $-0x80, %al
movb %al, 0x2(%rsp,%r12)
andb $0x3f, %r9b
orb $-0x80, %r9b
movb %r9b, 0x3(%rsp,%r12)
movl $0x4, %eax
jmp 0x15e6a
movq %r9, %rcx
shrq $0x27, %rcx
andl $0x8, %ecx
orl %ecx, %eax
orb $-0x10, %al
leaq (%rsp,%r12), %rsi
incq %rsi
movb %al, -0x1(%rsi)
movl $0x24, %ecx
movq %r9, %rax
shrq %cl, %rax
andb $0x3f, %al
orb $-0x80, %al
movb %al, (%rsi)
addq $-0x6, %rcx
incq %rsi
cmpq $-0x6, %rcx
jne 0x15e4c
movl $0x8, %eax
addl %eax, %r12d
movl %r12d, %r15d
subl %edi, %r12d
je 0x15e89
movslq %edi, %rsi
addq %r14, %rsi
subq %rsi, %rdx
je 0x15e89
leaq (%r14,%r15), %rdi
callq 0x2210
movq %rsp, %rsi
movq %r14, %rdi
movq %r15, %rdx
callq 0x2180
addw (%rbx), %r12w
movw %r12w, (%rbx)
movzwl %r12w, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %r9d, %ebp
andl $-0x20, %ebp
cmpl $0xc0, %ebp
jne 0x15f03
andl $0x18, %r9d
movzbl 0x1(%r12), %r14d
andl $0x3f, %r14d
leaq (%r14,%r9,8), %r9
leaq 0x2(%r12), %rbp
jmp 0x1596e
movl %r13d, %ecx
andl $-0x20, %ecx
cmpl $0xc0, %ecx
jne 0x15f42
andl $0x18, %r13d
movzbl 0x1(%r9), %ecx
andl $0x3f, %ecx
leaq (%rcx,%r13,8), %r13
leaq 0x2(%r9), %r12
jmp 0x158e3
movl %r9d, %r14d
shrl $0x2, %r14d
andl $0x4, %r14d
shrl $0x3, %r9d
andl $0x1, %r9d
leaq 0x1(%r12), %rbp
xorl $-0x3, %r14d
movl %r14d, 0x24(%rsp)
shlq $0x6, %r9
movzbl (%rbp), %r14d
andl $0x3f, %r14d
orq %r14, %r9
incq %rbp
incl 0x24(%rsp)
jne 0x15f24
jmp 0x1596e
movl %r13d, %ecx
shrl $0x2, %ecx
andl $0x4, %ecx
shrl $0x3, %r13d
andl $0x1, %r13d
leaq 0x1(%r9), %r12
xorl $-0x3, %ecx
shlq $0x6, %r13
movzbl (%r12), %r14d
andl $0x3f, %r14d
orq %r14, %r13
incq %r12
incl %ecx
jne 0x15f5a
jmp 0x158e3