// File: /workspace/github_c_makefile_star2_O2/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 0x1e8c
movq %r15, %rdi
callq 0x10a0
movq %rax, %r12
incl %r12d
movl %r12d, %edi
callq 0x232d
movq %rax, %r13
movslq %r12d, %r12
movq %rax, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x1130
addl %r12d, 0x5305(%rip)     # 0x7164
movq %rbx, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x1c8f
movq %rbx, %rdi
movl %ebp, %esi
callq 0x1d3f
testq %rax, %rax
je 0x1f20
movq 0x10(%rbx), %rcx
movq %rax, (%rcx)
jmp 0x1f22
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x1dba
movq 0x8(%rbx), %rcx
movslq %eax, %r12
movq (%rcx,%r12,8), %rdi
movq %r15, %rsi
callq *%r14
testl %eax, %eax
je 0x1f20
movq 0x10(%rbx), %rax
movq (%rax,%r12,8), %rdi
movq %r15, %rsi
movl %ebp, %edx
movq %r14, %rcx
callq 0x1e13
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 0x1f20
movq 0x8(%r15), %rax
movq (%rax), %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x1c8f
cmpl $-0x1, %eax
je 0x1f04
movq 0x10(%rbx), %rcx
cltq
movq %r15, (%rcx,%rax,8)
movq %rbx, %rdi
movl %ebp, %esi
callq 0x1d3f
testq %rax, %rax
je 0x1f20
leaq 0x2186(%rip), %rcx      # 0x40a0
movl (%rcx), %ecx
movl %ecx, (%rax)
jmp 0x1f22
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq