// File: /workspace/github_c_makefile_star2_O0/OhYee[P]computer_application_programming_experiment/hash_search/array_search/../../utils/radix_tree/radix_tree_node.c
// Function: rtn_add
radix_tree_node *rtn_add(radix_tree_node *rtn, bits *value, int k,
                         boolean memory) {
    // int m = pow2(k);
    if (rtn == NULL) {
        // if add to null, then make node
        rtn = rtn_init(k);
        rtn->end_of_value = T;
        if (memory) {
            rtn->value =
                bits_init_with_minimum_bits(value, value->start, value->end);
        } else {
            rtn->value = value;
        }
        return rtn;
    }

    int              l_node = bits_len(rtn->value);
    int              l_value = bits_len(value);
    radix_tree_node *root = rtn;

    // get the prefix
    int prefix = bits_prefix(rtn->value, value, k);
    if (prefix == l_node && prefix == l_value) {
        // value is same as this node.

        // this node is same as the value
        rtn->end_of_value = T;
        return rtn;
    }

    if (prefix < l_node) {
        // node need split
        int key = rtn_get_child_key(rtn->value, prefix, k);

        root = rtn_init(k);
        root->value = bits_sub(rtn->value, 0, prefix);
        root->end_of_value = F;
        root->children[key] = rtn;
        rtn->value->start += prefix;
    }

    if (prefix < l_value) {
        // new value need split
        int key = rtn_get_child_key(value, prefix, k);
        value->start += prefix;
        root->children[key] = rtn_add(root->children[key], value, k, memory);
    }

    return root;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl %ecx, -0x20(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0x40cc
movl -0x1c(%rbp), %edi
callq 0x3fd0
movq %rax, -0x10(%rbp)
leaq 0x101c(%rip), %rax      # 0x509c
movl (%rax), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x8(%rax)
cmpl $0x0, -0x20(%rbp)
je 0x40b3
movq -0x18(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x8(%rax), %esi
movq -0x18(%rbp), %rax
movl 0xc(%rax), %edx
callq 0x1a30
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
jmp 0x40bf
movq -0x18(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x420e
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x1c20
movl %eax, -0x24(%rbp)
movq -0x18(%rbp), %rdi
callq 0x1c20
movl %eax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %edx
callq 0x1c40
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x24(%rbp), %eax
jne 0x4134
movl -0x34(%rbp), %eax
cmpl -0x28(%rbp), %eax
jne 0x4134
leaq 0xf7e(%rip), %rax       # 0x509c
movl (%rax), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x420e
movl -0x34(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x41af
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
movl -0x34(%rbp), %esi
movl -0x1c(%rbp), %edx
callq 0x3f10
movl %eax, -0x38(%rbp)
movl -0x1c(%rbp), %edi
callq 0x3fd0
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
movl -0x34(%rbp), %edx
xorl %esi, %esi
callq 0x1b10
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x10(%rax)
leaq 0xf1e(%rip), %rax       # 0x50a0
movl (%rax), %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x10(%rbp), %rdx
movq -0x30(%rbp), %rax
movq (%rax), %rax
movslq -0x38(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x34(%rbp), %ecx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
addl 0x8(%rax), %ecx
movl %ecx, 0x8(%rax)
movl -0x34(%rbp), %eax
cmpl -0x28(%rbp), %eax
jge 0x4206
movq -0x18(%rbp), %rdi
movl -0x34(%rbp), %esi
movl -0x1c(%rbp), %edx
callq 0x3f10
movl %eax, -0x3c(%rbp)
movl -0x34(%rbp), %ecx
movq -0x18(%rbp), %rax
addl 0x8(%rax), %ecx
movl %ecx, 0x8(%rax)
movq -0x30(%rbp), %rax
movq (%rax), %rax
movslq -0x3c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %edx
movl -0x20(%rbp), %ecx
callq 0x4050
movq %rax, %rdx
movq -0x30(%rbp), %rax
movq (%rax), %rax
movslq -0x3c(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x30(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax,%rax)