// File: /workspace/github_c_makefile_star2_O2/lh3[P]fermi-lite/rope.c
// Function: rope_insert_run
int64_t rope_insert_run(rope_t *rope, int64_t x, int a, int64_t rl, rpcache_t *cache)
{ // insert $a after $x symbols in $rope and the returns rank(a, x)
	rpnode_t *u = 0, *v = 0, *p = rope->root; // $v is the parent of $p; $u and $v are at the same level and $u is the first node in the bucket
	int64_t y = 0, z = 0, cnt[6];
	int n_runs;
	do { // top-down update. Searching and node splitting are done together in one pass.
		if (p->n == rope->max_nodes) { // node is full; split
			v = split_node(rope, u, v); // $v points to the parent of $p; when a new root is added, $v points to the root
			if (y + v->l < x) // if $v is not long enough after the split, we need to move both $p and its parent $v
				y += v->l, z += v->c[a], ++v, p = v->p;
		}
		u = p;
		if (v && x - y > v->l>>1) { // then search backwardly for the right node to descend
			p += p->n - 1; y += v->l; z += v->c[a];
			for (; y >= x; --p) y -= p->l, z -= p->c[a];
			++p;
		} else for (; y + p->l < x; ++p) y += p->l, z += p->c[a]; // then search forwardly
		assert(p - u < u->n);
		if (v) v->c[a] += rl, v->l += rl; // we should not change p->c[a] because this may cause troubles when p's child is split
		v = p; p = p->p; // descend
	} while (!u->is_bottom);
	rope->c[a] += rl; // $rope->c should be updated after the loop as adding a new root needs the old $rope->c counts
	if (cache) {
		if (cache->p != (uint8_t*)p) memset(cache, 0, sizeof(rpcache_t));
		n_runs = rle_insert_cached((uint8_t*)p, x - y, a, rl, cnt, v->c, &cache->beg, cache->bc);
		cache->p = (uint8_t*)p;
	} else n_runs = rle_insert((uint8_t*)p, x - y, a, rl, cnt, v->c);
	z += cnt[a];
	v->c[a] += rl; v->l += rl; // this should be after rle_insert(); otherwise rle_insert() won't work
	if (n_runs + RLE_MIN_SPACE > rope->block_len) {
		split_node(rope, u, v);
		if (cache) memset(cache, 0, sizeof(rpcache_t));
	}
	return z;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %r8, 0x8(%rsp)
movq %rcx, 0x20(%rsp)
movq %rsi, %r13
movabsq $0x3fffffffffffff, %r15 # imm = 0x3FFFFFFFFFFFFF
movq 0x38(%rdi), %rsi
movl %edx, 0x4(%rsp)
movslq %edx, %r9
leaq 0x10(,%r9,8), %rax
movq %rax, 0x38(%rsp)
leaq -0x30(,%r9,8), %rax
movq %rax, 0x30(%rsp)
xorl %r12d, %r12d
xorl %ebp, %ebp
xorl %ebx, %ebx
xorl %r14d, %r14d
movq %rdi, 0x18(%rsp)
movq %r9, 0x10(%rsp)
movq 0x8(%rsi), %rax
shrq $0x36, %rax
andl $0x1ff, %eax            # imm = 0x1FF
cmpl (%rdi), %eax
jne 0x12798
movq %rsi, 0x28(%rsp)
movq %r12, %rsi
movq %rbp, %rdx
callq 0x129e3
movq 0x8(%rax), %rdx
andq %r15, %rdx
addq %rbx, %rdx
cmpq %r13, %rdx
jae 0x127ac
movq 0x10(%rsp), %r9
addq 0x10(%rax,%r9,8), %r14
movq 0x40(%rax), %r12
addq $0x40, %rax
movq 0x18(%rsp), %rdi
jmp 0x127be
testq %rbp, %rbp
je 0x12830
movq %rbx, %rdx
movq %rsi, %r12
movq %rbp, %rax
jmp 0x127be
movq %rbx, %rdx
movq 0x28(%rsp), %r12
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %r9
movq %r13, %rcx
subq %rdx, %rcx
movq 0x8(%rax), %rbx
andq %r15, %rbx
movq %rbx, %rsi
shrq %rsi
cmpq %rsi, %rcx
jbe 0x12814
movq 0x8(%r12), %rcx
shrq $0x36, %rcx
andl $0x1ff, %ecx            # imm = 0x1FF
addq 0x10(%rax,%r9,8), %r14
addq %rdx, %rbx
movl %ecx, %ebp
shll $0x6, %ebp
movq 0x30(%rsp), %rdx
addq %r12, %rdx
cmpq %r13, %rbx
jl 0x1281b
movq -0x38(%r12,%rbp), %rsi
andq %r15, %rsi
subq %rsi, %rbx
subq (%rdx,%rbp), %r14
addq $-0x40, %rbp
jmp 0x127fa
xorl %ecx, %ecx
movq %rdx, %rbx
jmp 0x12837
movq %rbp, %rdx
sarq $0x6, %rdx
cmpq %rcx, %rdx
jge 0x129c4
addq %r12, %rbp
jmp 0x12887
movb $0x1, %cl
movq %rsi, %r12
xorl %eax, %eax
movq 0x38(%rsp), %rdx
addq %r12, %rdx
xorl %ebp, %ebp
movq 0x8(%r12,%rbp), %rsi
andq %r15, %rsi
addq %rbx, %rsi
cmpq %r13, %rsi
jae 0x1285e
addq (%rdx,%rbp), %r14
addq $0x40, %rbp
movq %rsi, %rbx
jmp 0x12841
movq 0x8(%r12), %rdx
movq %rdx, %rsi
shrq $0x36, %rsi
andl $0x1ff, %esi            # imm = 0x1FF
movq %rbp, %r8
sarq $0x6, %r8
cmpq %rsi, %r8
jge 0x129c4
addq %r12, %rbp
testb %cl, %cl
jne 0x128b4
movq 0x20(%rsp), %rdx
addq %rdx, 0x10(%rax,%r9,8)
movq 0x8(%rax), %rcx
addq %rcx, %rdx
andq %r15, %rdx
movabsq $-0x40000000000000, %rsi # imm = 0xFFC0000000000000
andq %rsi, %rcx
orq %rdx, %rcx
movq %rcx, 0x8(%rax)
movq 0x8(%r12), %rdx
movq (%rbp), %rsi
testq %rdx, %rdx
jns 0x1274f
movq 0x20(%rsp), %rcx
addq %rcx, 0x8(%rdi,%r9,8)
movq 0x8(%rsp), %rax
testq %rax, %rax
je 0x1292d
cmpq %rsi, 0x38(%rax)
je 0x128ed
xorps %xmm0, %xmm0
movups %xmm0, 0x30(%rax)
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
subq %rbx, %r13
leaq 0x10(%rbp), %r9
movq %rax, %rbx
addq $0x8, %rax
leaq 0x40(%rsp), %r8
movq %rsi, %rdi
movq %rsi, 0x28(%rsp)
movq %r13, %rsi
movl 0x4(%rsp), %edx
movq 0x20(%rsp), %r13
movq %r13, %rcx
pushq %rax
pushq %rbx
callq 0x12f7c
addq $0x10, %rsp
movq 0x28(%rsp), %rcx
movq %rcx, 0x38(%rbx)
jmp 0x1294b
subq %rbx, %r13
leaq 0x10(%rbp), %r9
leaq 0x40(%rsp), %r8
movq %rsi, %rdi
movq %r13, %rsi
movl 0x4(%rsp), %edx
movq %rcx, %r13
callq 0x1351d
movq 0x10(%rsp), %rcx
movq 0x40(%rsp,%rcx,8), %rbx
addq %r13, 0x10(%rbp,%rcx,8)
movq 0x8(%rbp), %rcx
addq %rcx, %r13
andq %r15, %r13
movabsq $-0x40000000000000, %rdx # imm = 0xFFC0000000000000
andq %rcx, %rdx
orq %r13, %rdx
movq %rdx, 0x8(%rbp)
addl $0x12, %eax
movq 0x18(%rsp), %rdi
cmpl 0x4(%rdi), %eax
jle 0x129af
movq %r12, %rsi
movq %rbp, %rdx
callq 0x129e3
cmpq $0x0, 0x8(%rsp)
je 0x129af
xorps %xmm0, %xmm0
movq 0x8(%rsp), %rax
movups %xmm0, 0x30(%rax)
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
addq %r14, %rbx
movq %rbx, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x254f(%rip), %rdi      # 0x14f1a
leaq 0x1c29(%rip), %rsi      # 0x145fb
leaq 0x254e(%rip), %rcx      # 0x14f27
movl $0x83, %edx
callq 0x20e0