// File: /workspace/github_c_makefile_star2_O3/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, %r10 # imm = 0x3FFFFFFFFFFFFF
movq 0x38(%rdi), %rbp
movl %edx, 0xc(%rsp)
movslq %edx, %rbx
xorl %esi, %esi
xorl %r12d, %r12d
xorl %r15d, %r15d
xorl %r14d, %r14d
movq %rdi, 0x18(%rsp)
movq %rcx, 0x20(%rsp)
movq 0x8(%rbp), %r9
movq %r9, %rax
shrq $0x36, %rax
andl $0x1ff, %eax            # imm = 0x1FF
cmpl (%rdi), %eax
jne 0x1533a
movq %r12, %rdx
movq %r10, %r12
callq 0x1558b
movq %r12, %r10
movq 0x8(%rax), %r11
andq %r12, %r11
addq %r15, %r11
cmpq %r13, %r11
jae 0x1534e
addq 0x10(%rax,%rbx,8), %r14
movq 0x40(%rax), %rsi
addq $0x40, %rax
jmp 0x15354
testq %r12, %r12
je 0x153db
movq %r15, %r11
movq %rbp, %rsi
movq %r12, %rax
jmp 0x1535e
movq %r15, %r11
movq %rbp, %rsi
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rcx
movq %r13, %rdx
subq %r11, %rdx
movq 0x8(%rax), %r15
andq %r10, %r15
movq %r15, %r8
shrq %r8
movq 0x8(%rsi), %r9
cmpq %r8, %rdx
jbe 0x153b5
shrq $0x36, %r9
movl %r9d, %edx
andl $0x1ff, %edx            # imm = 0x1FF
shll $0x6, %edx
leaq (%rdx,%rsi), %r12
addq $-0x40, %r12
addq %r11, %r15
addq 0x10(%rax,%rbx,8), %r14
cmpq %r13, %r15
jl 0x153bc
movq 0x8(%r12), %rdx
andq %r10, %rdx
subq %rdx, %r15
subq 0x10(%r12,%rbx,8), %r14
addq $-0x40, %r12
jmp 0x1539a
xorl %edx, %edx
movq %r11, %r15
jmp 0x153e2
addq $0x40, %r12
movq %r12, %rdx
subq %rsi, %rdx
sarq $0x6, %rdx
andl $0x1ff, %r9d            # imm = 0x1FF
cmpq %r9, %rdx
jl 0x15431
jmp 0x1556c
movb $0x1, %dl
movq %rbp, %rsi
xorl %eax, %eax
movq %r9, %r8
andq %r10, %r8
addq %r15, %r8
movq %rsi, %r12
cmpq %r13, %r8
jae 0x1540c
movq %r8, %r15
addq 0x10(%r12,%rbx,8), %r14
movq 0x48(%r12), %r8
andq %r10, %r8
addq $0x40, %r12
addq %r15, %r8
jmp 0x153ee
movq %r12, %r11
subq %rsi, %r11
sarq $0x6, %r11
movq %r9, %r8
shrq $0x36, %r8
andl $0x1ff, %r8d            # imm = 0x1FF
cmpq %r8, %r11
jge 0x1556c
testb %dl, %dl
jne 0x15459
addq %rcx, 0x10(%rax,%rbx,8)
movq 0x8(%rax), %r9
leaq (%r9,%rcx), %rdx
andq %r10, %rdx
movabsq $-0x40000000000000, %r8 # imm = 0xFFC0000000000000
andq %r8, %r9
orq %rdx, %r9
movq %r9, 0x8(%rax)
movq 0x8(%rsi), %r9
movq (%r12), %rbp
testq %r9, %r9
jns 0x152fa
addq %rcx, 0x8(%rdi,%rbx,8)
movq 0x10(%rsp), %rax
testq %rax, %rax
movq %rsi, 0x28(%rsp)
je 0x154cb
cmpq %rbp, 0x38(%rax)
je 0x15492
xorps %xmm0, %xmm0
movups %xmm0, 0x30(%rax)
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
subq %r15, %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, %r15
callq 0x15ca4
addq $0x10, %rsp
movq %rbp, 0x38(%r13)
jmp 0x154ea
subq %r15, %r13
leaq 0x10(%r12), %r9
leaq 0x30(%rsp), %r8
movq %rbp, %rdi
movq %r13, %rsi
movl 0xc(%rsp), %edx
movq %rcx, %r15
callq 0x16879
movq 0x30(%rsp,%rbx,8), %r13
addq %r15, 0x10(%r12,%rbx,8)
movq 0x8(%r12), %rcx
addq %rcx, %r15
movabsq $0x3fffffffffffff, %rdx # imm = 0x3FFFFFFFFFFFFF
andq %rdx, %r15
movabsq $-0x40000000000000, %rdx # imm = 0xFFC0000000000000
andq %rcx, %rdx
orq %r15, %rdx
movq %rdx, 0x8(%r12)
addl $0x12, %eax
movq 0x18(%rsp), %rdi
cmpl 0x4(%rdi), %eax
movq 0x28(%rsp), %rsi
jle 0x15557
movq %r12, %rdx
callq 0x1558b
cmpq $0x0, 0x10(%rsp)
je 0x15557
xorps %xmm0, %xmm0
movq 0x10(%rsp), %rax
movups %xmm0, 0x30(%rax)
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
addq %r14, %r13
movq %r13, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2a2f(%rip), %rdi      # 0x17fa2
leaq 0x20a0(%rip), %rsi      # 0x1761a
leaq 0x2a2e(%rip), %rcx      # 0x17faf
movl $0x83, %edx
callq 0x20e0