// File: zhjw1998[P]Merkle-Tree/src/merkle_tree.cpp
// Function: MerkleTree::Remove(TrieTreeNode*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, int, int)
bool MerkleTree::Remove(TrieTreeNode *treeNode, const std::string& word,int pos, int n) {
    if (pos == n){
        treeNode->isEnd = false;//set the node not to be an end
        //if the last node contains the last char is a leaf,return true to delete it
        return treeNode->childCnt == 0;
    }
    //not found, not delete this node
    if (treeNode->child[GETINDEX(word[pos])] == nullptr) 
        return false;
    //if true, the child is a leaf, delete the child
    if ( Remove( treeNode->child[GETINDEX(word[pos])], word, pos+1, n)){ 
        delete treeNode->child[GETINDEX(word[pos])];
        treeNode->child[GETINDEX(word[pos])] = nullptr;
 
        treeNode->prefixCnt--;
        //if the node becomes a leaf and is not an end,return true to delete it
        if (--treeNode->childCnt == 0 && treeNode->isEnd == false)
            return true;
    }
 
    //other not delete
    return false;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movl %r8d, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x30(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x28(%rbp), %eax
jne 0x9c0b
movq -0x18(%rbp), %rax
movb $0x0, 0x1(%rax)
movq -0x18(%rbp), %rax
cmpl $0x0, 0x4(%rax)
sete %al
andb $0x1, %al
movb %al, -0x1(%rbp)
jmp 0x9e0f
movq -0x18(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x38(%rbp)
movq -0x20(%rbp), %rdi
movslq -0x24(%rbp), %rsi
callq 0x2250
movsbl (%rax), %eax
cmpl $0x39, %eax
jle 0x9c47
movq -0x20(%rbp), %rdi
movslq -0x24(%rbp), %rsi
callq 0x2250
movsbl (%rax), %eax
subl $0x61, %eax
addl $0xa, %eax
movl %eax, -0x3c(%rbp)
jmp 0x9c5d
movq -0x20(%rbp), %rdi
movslq -0x24(%rbp), %rsi
callq 0x2250
movsbl (%rax), %eax
subl $0x30, %eax
movl %eax, -0x3c(%rbp)
movq -0x38(%rbp), %rax
movl -0x3c(%rbp), %ecx
movslq %ecx, %rcx
cmpq $0x0, (%rax,%rcx,8)
jne 0x9c77
movb $0x0, -0x1(%rbp)
jmp 0x9e0f
movq -0x18(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x48(%rbp)
movq -0x20(%rbp), %rdi
movslq -0x24(%rbp), %rsi
callq 0x2250
movsbl (%rax), %eax
cmpl $0x39, %eax
jle 0x9cb3
movq -0x20(%rbp), %rdi
movslq -0x24(%rbp), %rsi
callq 0x2250
movsbl (%rax), %eax
subl $0x61, %eax
addl $0xa, %eax
movl %eax, -0x4c(%rbp)
jmp 0x9cc9
movq -0x20(%rbp), %rdi
movslq -0x24(%rbp), %rsi
callq 0x2250
movsbl (%rax), %eax
subl $0x30, %eax
movl %eax, -0x4c(%rbp)
movq -0x30(%rbp), %rdi
movq -0x48(%rbp), %rax
movl -0x4c(%rbp), %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rsi
movq -0x20(%rbp), %rdx
movl -0x24(%rbp), %ecx
addl $0x1, %ecx
movl -0x28(%rbp), %r8d
callq 0x9bc0
testb $0x1, %al
jne 0x9cf7
jmp 0x9e0b
movq -0x18(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x58(%rbp)
movq -0x20(%rbp), %rdi
movslq -0x24(%rbp), %rsi
callq 0x2250
movsbl (%rax), %eax
cmpl $0x39, %eax
jle 0x9d33
movq -0x20(%rbp), %rdi
movslq -0x24(%rbp), %rsi
callq 0x2250
movsbl (%rax), %eax
subl $0x61, %eax
addl $0xa, %eax
movl %eax, -0x5c(%rbp)
jmp 0x9d49
movq -0x20(%rbp), %rdi
movslq -0x24(%rbp), %rsi
callq 0x2250
movsbl (%rax), %eax
subl $0x30, %eax
movl %eax, -0x5c(%rbp)
movq -0x58(%rbp), %rax
movl -0x5c(%rbp), %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x68(%rbp)
cmpq $0x0, %rax
je 0x9d6f
movq -0x68(%rbp), %rdi
movl $0x90, %esi
callq 0x21e0
movq -0x18(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x70(%rbp)
movq -0x20(%rbp), %rdi
movslq -0x24(%rbp), %rsi
callq 0x2250
movsbl (%rax), %eax
cmpl $0x39, %eax
jle 0x9dab
movq -0x20(%rbp), %rdi
movslq -0x24(%rbp), %rsi
callq 0x2250
movsbl (%rax), %eax
subl $0x61, %eax
addl $0xa, %eax
movl %eax, -0x74(%rbp)
jmp 0x9dc1
movq -0x20(%rbp), %rdi
movslq -0x24(%rbp), %rsi
callq 0x2250
movsbl (%rax), %eax
subl $0x30, %eax
movl %eax, -0x74(%rbp)
movq -0x70(%rbp), %rax
movl -0x74(%rbp), %ecx
movslq %ecx, %rcx
movq $0x0, (%rax,%rcx,8)
movq -0x18(%rbp), %rax
movl 0x8(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x8(%rax)
movq -0x18(%rbp), %rcx
movl 0x4(%rcx), %eax
addl $-0x1, %eax
movl %eax, 0x4(%rcx)
cmpl $0x0, %eax
jne 0x9e09
movq -0x18(%rbp), %rax
movb 0x1(%rax), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x9e09
movb $0x1, -0x1(%rbp)
jmp 0x9e0f
jmp 0x9e0b
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax)