// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, (%rax)
je 0x2a64
movq -0x18(%rbp), %rdi
callq 0x1080
addq $0x1, %rax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %edi
callq 0x32d0
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
movq -0x18(%rbp), %rsi
movslq -0x2c(%rbp), %rdx
callq 0x1120
movl -0x2c(%rbp), %eax
addl 0x5742(%rip), %eax      # 0x815c
movl %eax, 0x573c(%rip)      # 0x815c
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x26a0
movq -0x10(%rbp), %rdi
movl -0x1c(%rbp), %esi
callq 0x2810
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x2a57
movq -0x40(%rbp), %rcx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rcx, (%rax)
movq -0x40(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x2b6f
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x2910
movl %eax, -0x44(%rbp)
movq -0x28(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x8(%rcx), %rcx
movslq -0x44(%rbp), %rdx
movq (%rcx,%rdx,8), %rdi
movq -0x18(%rbp), %rsi
callq *%rax
cmpl $0x0, %eax
jne 0x2aa4
movq $0x0, -0x8(%rbp)
jmp 0x2b6f
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x44(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %edx
movq -0x28(%rbp), %rcx
callq 0x29c0
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x44(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x29a0
movq %rax, %rdx
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x44(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
cmpq $0x0, -0x50(%rbp)
je 0x2b65
movq -0x10(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x50(%rbp), %rdi
callq 0x29a0
movq -0x68(%rbp), %rdi
movq %rax, %rsi
movq -0x28(%rbp), %rdx
callq 0x26a0
movl %eax, -0x54(%rbp)
cmpl $-0x1, -0x54(%rbp)
je 0x2b35
movq -0x50(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x54(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x10(%rbp), %rdi
movl -0x1c(%rbp), %esi
callq 0x2810
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
je 0x2b5b
leaq 0x254d(%rip), %rax      # 0x50a0
movl (%rax), %ecx
movq -0x60(%rbp), %rax
movl %ecx, (%rax)
movq -0x60(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x2b6f
jmp 0x2b67
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax)