// File: /workspace/github_c_makefile_star2_O3/OhYee[P]computer_application_programming_experiment/hash_search/array_search/../../utils/bplus_tree/bplus_tree_node.c
// Function: bptn_add
bplus_tree_node *bptn_add(bplus_tree_node *bptn, void *value, int max_length,
                          compare_function compare) {
    if (bptn->is_leaf) {
        // left
        // insert whatever

        int   l = strlen(value) + 1;
        void *p = mp_new(l);
        memcpy(p, value, l);
        _bplus_string_mem += l;
        bptn_add_to_node(bptn, p, compare);

        // splite (if need)
        bplus_tree_node *next = bptn_split(bptn, max_length);
        if (next != NULL) {
            bptn->pointers[0] = next; // next指针
        }
        return next;
    } else {
        // mid-node
        // find the sub-tree index
        int index = bptn_search_index(bptn, value, compare);
        if (compare(bptn->keys[index], value) == 0) {
            return NULL;
        }

        // insert whatever (recursive)
        bplus_tree_node *bptn2 =
            bptn_add(bptn->pointers[index], value, max_length, compare);

        // update the extremum
        bptn->keys[index] = bptn_get_extremum(bptn->pointers[index]);

        if (bptn2 != NULL) {
            // sub-tree has splited

            // insert the key to this node
            int idx = bptn_add_to_node(bptn, bptn_get_extremum(bptn2), compare);
            if (idx != -1) {
                bptn->pointers[idx] = bptn2;
            }

            // split (if need)
            bplus_tree_node *split_node = bptn_split(bptn, max_length);
            if (split_node != NULL) {
                split_node->is_leaf = F;
            }
            return split_node;
        }
    }
    return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rcx, %r14
movl %edx, %ebp
movq %rsi, %r15
movq %rdi, %rbx
cmpl $0x0, (%rdi)
je 0x21e4
movq %r15, %rdi
callq 0x10a0
movq %rax, %r12
incl %r12d
movl %r12d, %edi
callq 0x2740
movq %rax, %r13
movslq %r12d, %r12
movq %rax, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x1130
addl %r12d, 0x4fad(%rip)     # 0x7164
movq %rbx, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x1fb0
movq %rbx, %rdi
movl %ebp, %esi
callq 0x2093
testq %rax, %rax
je 0x22a6
movq 0x10(%rbx), %rcx
movq %rax, (%rcx)
jmp 0x22a8
movl 0x4(%rbx), %r12d
testl %r12d, %r12d
jle 0x2211
xorl %r12d, %r12d
movq 0x8(%rbx), %rax
movq (%rax,%r12,8), %rdi
movq %r15, %rsi
callq *%r14
testl %eax, %eax
jg 0x2216
incq %r12
movslq 0x4(%rbx), %rax
cmpq %rax, %r12
jl 0x21f0
movl %eax, %r12d
decl %r12d
jmp 0x2220
xorl %eax, %eax
subl $0x1, %r12d
cmovbl %eax, %r12d
movq 0x8(%rbx), %rax
movslq %r12d, %r12
movq (%rax,%r12,8), %rdi
movq %r15, %rsi
callq *%r14
testl %eax, %eax
je 0x22a6
movq 0x10(%rbx), %rax
movq (%rax,%r12,8), %rdi
movq %r15, %rsi
movl %ebp, %edx
movq %r14, %rcx
callq 0x216b
movq %rax, %r15
movq 0x8(%rbx), %rax
movq 0x10(%rbx), %rcx
movq (%rcx,%r12,8), %rcx
movq 0x8(%rcx), %rcx
movq (%rcx), %rcx
movq %rcx, (%rax,%r12,8)
testq %r15, %r15
je 0x22a6
movq 0x8(%r15), %rax
movq (%rax), %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x1fb0
cmpl $-0x1, %eax
je 0x228a
movq 0x10(%rbx), %rcx
cltq
movq %r15, (%rcx,%rax,8)
movq %rbx, %rdi
movl %ebp, %esi
callq 0x2093
testq %rax, %rax
je 0x22a6
leaq 0x1e1c(%rip), %rcx      # 0x40bc
movl (%rcx), %ecx
movl %ecx, (%rax)
jmp 0x22a8
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq