// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
shrq $0x36, %rax
andq $0x1ff, %rax            # imm = 0x1FF
movq -0x8(%rbp), %rcx
cmpl (%rcx), %eax
jne 0x226e0
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x38(%rbp), %rdx
callq 0x22a60
movq %rax, -0x38(%rbp)
movq -0x48(%rbp), %rax
movq -0x38(%rbp), %rdx
movabsq $0x3fffffffffffff, %rcx # imm = 0x3FFFFFFFFFFFFF
andq 0x8(%rdx), %rcx
addq %rcx, %rax
cmpq -0x10(%rbp), %rax
jae 0x226de
movq -0x38(%rbp), %rcx
movabsq $0x3fffffffffffff, %rax # imm = 0x3FFFFFFFFFFFFF
andq 0x8(%rcx), %rax
addq -0x48(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x38(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq 0x10(%rax,%rcx,8), %rax
addq -0x50(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x38(%rbp), %rax
addq $0x40, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
jmp 0x226e0
movq -0x40(%rbp), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x227cc
movq -0x10(%rbp), %rax
subq -0x48(%rbp), %rax
movq -0x38(%rbp), %rdx
movabsq $0x3fffffffffffff, %rcx # imm = 0x3FFFFFFFFFFFFF
andq 0x8(%rdx), %rcx
shrq %rcx
cmpq %rcx, %rax
jbe 0x227cc
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
shrq $0x36, %rax
andq $0x1ff, %rax            # imm = 0x1FF
movl %eax, %ecx
subl $0x1, %ecx
movq -0x40(%rbp), %rax
movslq %ecx, %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rcx
movabsq $0x3fffffffffffff, %rax # imm = 0x3FFFFFFFFFFFFF
andq 0x8(%rcx), %rax
addq -0x48(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x38(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq 0x10(%rax,%rcx,8), %rax
addq -0x50(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x10(%rbp), %rax
jl 0x227be
movq -0x40(%rbp), %rax
movabsq $0x3fffffffffffff, %rcx # imm = 0x3FFFFFFFFFFFFF
andq 0x8(%rax), %rcx
movq -0x48(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq 0x10(%rax,%rcx,8), %rcx
movq -0x50(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x40(%rbp), %rax
addq $-0x40, %rax
movq %rax, -0x40(%rbp)
jmp 0x22771
movq -0x40(%rbp), %rax
addq $0x40, %rax
movq %rax, -0x40(%rbp)
jmp 0x2282c
jmp 0x227ce
movq -0x48(%rbp), %rax
movq -0x40(%rbp), %rdx
movabsq $0x3fffffffffffff, %rcx # imm = 0x3FFFFFFFFFFFFF
andq 0x8(%rdx), %rcx
addq %rcx, %rax
cmpq -0x10(%rbp), %rax
jae 0x2282a
movq -0x40(%rbp), %rcx
movabsq $0x3fffffffffffff, %rax # imm = 0x3FFFFFFFFFFFFF
andq 0x8(%rcx), %rax
addq -0x48(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq 0x10(%rax,%rcx,8), %rax
addq -0x50(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x40(%rbp), %rax
addq $0x40, %rax
movq %rax, -0x40(%rbp)
jmp 0x227ce
jmp 0x2282c
movq -0x40(%rbp), %rax
movq -0x30(%rbp), %rcx
subq %rcx, %rax
sarq $0x6, %rax
movq -0x30(%rbp), %rcx
movq 0x8(%rcx), %rcx
shrq $0x36, %rcx
andq $0x1ff, %rcx            # imm = 0x1FF
cmpq %rcx, %rax
jge 0x22855
jmp 0x22874
leaq 0x35c6(%rip), %rdi      # 0x25e22
leaq 0x2d00(%rip), %rsi      # 0x25563
movl $0x83, %edx
leaq 0x35c0(%rip), %rcx      # 0x25e2f
callq 0x20f0
cmpq $0x0, -0x38(%rbp)
je 0x228cf
movq -0x20(%rbp), %rdx
movq -0x38(%rbp), %rax
movslq -0x14(%rbp), %rcx
addq 0x10(%rax,%rcx,8), %rdx
movq %rdx, 0x10(%rax,%rcx,8)
movq -0x20(%rbp), %rcx
movq -0x38(%rbp), %rax
movabsq $0x3fffffffffffff, %rdx # imm = 0x3FFFFFFFFFFFFF
andq 0x8(%rax), %rdx
addq %rcx, %rdx
movq 0x8(%rax), %rcx
movabsq $0x3fffffffffffff, %rsi # imm = 0x3FFFFFFFFFFFFF
andq %rsi, %rdx
movabsq $-0x40000000000000, %rsi # imm = 0xFFC0000000000000
andq %rsi, %rcx
orq %rdx, %rcx
movq %rcx, 0x8(%rax)
movq -0x40(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
shrq $0x3f, %rax
cmpq $0x0, %rax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x2264a
movq -0x20(%rbp), %rdx
movq -0x8(%rbp), %rax
movslq -0x14(%rbp), %rcx
addq 0x8(%rax,%rcx,8), %rdx
movq %rdx, 0x8(%rax,%rcx,8)
cmpq $0x0, -0x28(%rbp)
je 0x22987
movq -0x28(%rbp), %rax
movq 0x38(%rax), %rax
cmpq -0x40(%rbp), %rax
je 0x2293a
movq -0x28(%rbp), %rdi
xorl %esi, %esi
movl $0x40, %edx
callq 0x2110
movq -0x40(%rbp), %rdi
movq -0x10(%rbp), %rsi
subq -0x48(%rbp), %rsi
movl -0x14(%rbp), %edx
movq -0x20(%rbp), %rcx
leaq -0x80(%rbp), %r8
movq -0x38(%rbp), %r9
addq $0x10, %r9
movq -0x28(%rbp), %r10
movq -0x28(%rbp), %rax
addq $0x8, %rax
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x23510
movl %eax, -0x84(%rbp)
movq -0x40(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x38(%rax)
jmp 0x229b1
movq -0x40(%rbp), %rdi
movq -0x10(%rbp), %rsi
subq -0x48(%rbp), %rsi
movl -0x14(%rbp), %edx
movq -0x20(%rbp), %rcx
leaq -0x80(%rbp), %r8
movq -0x38(%rbp), %r9
addq $0x10, %r9
callq 0x24020
movl %eax, -0x84(%rbp)
movslq -0x14(%rbp), %rax
movq -0x80(%rbp,%rax,8), %rax
addq -0x50(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x20(%rbp), %rdx
movq -0x38(%rbp), %rax
movslq -0x14(%rbp), %rcx
addq 0x10(%rax,%rcx,8), %rdx
movq %rdx, 0x10(%rax,%rcx,8)
movq -0x20(%rbp), %rcx
movq -0x38(%rbp), %rax
movabsq $0x3fffffffffffff, %rdx # imm = 0x3FFFFFFFFFFFFF
andq 0x8(%rax), %rdx
addq %rcx, %rdx
movq 0x8(%rax), %rcx
movabsq $0x3fffffffffffff, %rsi # imm = 0x3FFFFFFFFFFFFF
andq %rsi, %rdx
movabsq $-0x40000000000000, %rsi # imm = 0xFFC0000000000000
andq %rsi, %rcx
orq %rdx, %rcx
movq %rcx, 0x8(%rax)
movl -0x84(%rbp), %eax
addl $0x12, %eax
movq -0x8(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jle 0x22a52
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x38(%rbp), %rdx
callq 0x22a60
cmpq $0x0, -0x28(%rbp)
je 0x22a50
movq -0x28(%rbp), %rdi
xorl %esi, %esi
movl $0x40, %edx
callq 0x2110
jmp 0x22a52
movq -0x50(%rbp), %rax
addq $0xa0, %rsp
popq %rbp
retq
nop