// File: /workspace/github_c_makefile_star2_O1/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 $0x68, %rsp
movq %r8, 0x10(%rsp)
movq %rsi, %r13
movabsq $0x3fffffffffffff, %r9  # imm = 0x3FFFFFFFFFFFFF
movq 0x38(%rdi), %rbp
movl %edx, 0xc(%rsp)
movslq %edx, %rbx
xorl %esi, %esi
xorl %r12d, %r12d
xorl %r14d, %r14d
xorl %r15d, %r15d
movq %rdi, 0x18(%rsp)
movq %rcx, 0x20(%rsp)
movq 0x8(%rbp), %rax
shrq $0x36, %rax
andl $0x1ff, %eax            # imm = 0x1FF
cmpl (%rdi), %eax
jne 0x14cd7
movq %r12, %rdx
movq %r9, %r12
callq 0x14f02
movq %r12, %r9
movq 0x8(%rax), %r8
andq %r12, %r8
addq %r14, %r8
cmpq %r13, %r8
jae 0x14ce2
addq 0x10(%rax,%rbx,8), %r15
movq 0x40(%rax), %rsi
addq $0x40, %rax
jmp 0x14ce8
movq %r12, %rax
movq %rbp, %rsi
movq %r14, %r8
jmp 0x14cf2
movq %rbp, %rsi
movq %r14, %r8
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rcx
testq %rax, %rax
je 0x14d4b
movq %r13, %rdx
subq %r8, %rdx
movq 0x8(%rax), %r14
andq %r9, %r14
movq %r14, %r10
shrq %r10
cmpq %r10, %rdx
jbe 0x14d4b
movq 0x8(%rsi), %rdx
shrq $0x36, %rdx
andl $0x1ff, %edx            # imm = 0x1FF
shll $0x6, %edx
leaq (%rdx,%rsi), %r12
addq $-0x40, %r12
addq %r8, %r14
addq 0x10(%rax,%rbx,8), %r15
cmpq %r13, %r14
jl 0x14d80
movq 0x8(%r12), %rdx
andq %r9, %rdx
subq %rdx, %r14
subq 0x10(%r12,%rbx,8), %r15
addq $-0x40, %r12
jmp 0x14d30
movq 0x8(%rsi), %rdx
andq %r9, %rdx
addq %r8, %rdx
movq %rsi, %r12
cmpq %r13, %rdx
jae 0x14d7b
movq %rdx, %r14
addq 0x10(%r12,%rbx,8), %r15
movq 0x48(%r12), %rdx
andq %r9, %rdx
addq $0x40, %r12
addq %r14, %rdx
cmpq %r13, %rdx
jb 0x14d5d
jmp 0x14d84
movq %r8, %r14
jmp 0x14d84
addq $0x40, %r12
movq %r12, %r8
subq %rsi, %r8
sarq $0x6, %r8
movq 0x8(%rsi), %rdx
shrq $0x36, %rdx
andl $0x1ff, %edx            # imm = 0x1FF
cmpq %rdx, %r8
jge 0x14ee3
testq %rax, %rax
je 0x14dce
addq %rcx, 0x10(%rax,%rbx,8)
movq 0x8(%rax), %r10
leaq (%r10,%rcx), %rdx
andq %r9, %rdx
movabsq $-0x40000000000000, %r8 # imm = 0xFFC0000000000000
andq %r8, %r10
orq %rdx, %r10
movq %r10, 0x8(%rax)
movq (%r12), %rbp
cmpq $0x0, 0x8(%rsi)
jns 0x14c9a
addq %rcx, 0x8(%rdi,%rbx,8)
movq 0x10(%rsp), %rax
testq %rax, %rax
movq %rsi, 0x28(%rsp)
je 0x14e42
cmpq %rbp, 0x38(%rax)
je 0x14e09
xorps %xmm0, %xmm0
movups %xmm0, 0x30(%rax)
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
subq %r14, %r13
leaq 0x10(%r12), %r9
movq %rax, %rdx
addq $0x8, %rax
movq %rdx, %r10
leaq 0x30(%rsp), %r8
movq %rbp, %rdi
movq %r13, %rsi
movl 0xc(%rsp), %edx
pushq %rax
movq %r10, %r13
pushq %r10
movq %rcx, %r14
callq 0x155f8
addq $0x10, %rsp
movq %rbp, 0x38(%r13)
jmp 0x14e61
subq %r14, %r13
leaq 0x10(%r12), %r9
leaq 0x30(%rsp), %r8
movq %rbp, %rdi
movq %r13, %rsi
movl 0xc(%rsp), %edx
movq %rcx, %r14
callq 0x15f76
movq 0x30(%rsp,%rbx,8), %r13
addq %r14, 0x10(%r12,%rbx,8)
movq 0x8(%r12), %rcx
addq %rcx, %r14
movabsq $0x3fffffffffffff, %rdx # imm = 0x3FFFFFFFFFFFFF
andq %rdx, %r14
movabsq $-0x40000000000000, %rdx # imm = 0xFFC0000000000000
andq %rcx, %rdx
orq %r14, %rdx
movq %rdx, 0x8(%r12)
addl $0x12, %eax
movq 0x18(%rsp), %rdi
cmpl 0x4(%rdi), %eax
movq 0x28(%rsp), %rsi
jle 0x14ece
movq %r12, %rdx
callq 0x14f02
cmpq $0x0, 0x10(%rsp)
je 0x14ece
xorps %xmm0, %xmm0
movq 0x10(%rsp), %rax
movups %xmm0, 0x30(%rax)
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
addq %r15, %r13
movq %r13, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2ff0(%rip), %rdi      # 0x17eda
leaq 0x270e(%rip), %rsi      # 0x175ff
leaq 0x2fef(%rip), %rcx      # 0x17ee7
movl $0x83, %edx
callq 0x20e0