// File: zhjw1998[P]Merkle-Tree/src/merkle_tree.cpp
// Function: MerkleTree::createMerkleTree(std::vector<Node*, std::allocator<Node*>>)
void MerkleTree::createMerkleTree(std::vector<Node*> blocks){
    std::vector<Node*> nodes;
    while (blocks.size() != 1) {
        for (unsigned int l = 0, n = 0; l < blocks.size(); l = l + 2, n++) {
            if (l != blocks.size() - 1) { // checks for adjacent block
                nodes.push_back(new Node(hash_sha256(blocks[l]->hash + blocks[l+1]->hash))); // combine and hash adjacent blocks
                nodes[n]->left = blocks[l]; // assign children
                nodes[n]->right = blocks[l+1];
            } else {
                nodes.push_back(new Node(blocks[l]->hash));
                nodes[n]->left = blocks[l];
                nodes[n]->right = nullptr;
            }
        }
        blocks = nodes;
        nodes.clear();
    }
    // printNodeHashes(blocks);
    this->root = blocks[0];
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rsi, -0xd0(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xc8(%rbp)
leaq -0x28(%rbp), %rdi
callq 0xa400
movq -0xd0(%rbp), %rdi
callq 0xab90
cmpq $0x1, %rax
je 0x8edc
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movq -0xd0(%rbp), %rdi
movl -0x2c(%rbp), %eax
movq %rax, -0xd8(%rbp)
callq 0xab90
movq %rax, %rcx
movq -0xd8(%rbp), %rax
cmpq %rcx, %rax
jae 0x8ebc
movq -0xd0(%rbp), %rdi
movl -0x2c(%rbp), %eax
movq %rax, -0xe0(%rbp)
callq 0xab90
movq %rax, %rcx
movq -0xe0(%rbp), %rax
subq $0x1, %rcx
cmpq %rcx, %rax
je 0x8d89
movl $0x30, %edi
callq 0x21c0
movq %rax, -0xe8(%rbp)
jmp 0x8bfb
movq -0xd0(%rbp), %rdi
movb $0x1, -0x89(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, %esi
callq 0xac30
movq -0xd0(%rbp), %rdi
movq (%rax), %rax
movq %rax, -0xf0(%rbp)
movl -0x2c(%rbp), %eax
incl %eax
movl %eax, %esi
callq 0xac30
movq -0xf0(%rbp), %rsi
movq (%rax), %rdx
leaq -0x88(%rbp), %rdi
callq 0xabb0
jmp 0x8c48
leaq -0x68(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x3d10
jmp 0x8c5a
movq -0xe8(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0xf1c0
jmp 0x8c6c
movq -0xe8(%rbp), %rax
movb $0x0, -0x89(%rbp)
movq %rax, -0x38(%rbp)
leaq -0x28(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0xa4e0
jmp 0x8c8d
leaq -0x68(%rbp), %rdi
callq 0x20f0
leaq -0x88(%rbp), %rdi
callq 0x20f0
movq -0xd0(%rbp), %rdi
movl -0x2c(%rbp), %eax
movl %eax, %esi
callq 0xac30
movq (%rax), %rax
movq %rax, -0x100(%rbp)
movl -0x30(%rbp), %eax
movl %eax, %esi
leaq -0x28(%rbp), %rdi
callq 0xac30
movq -0x100(%rbp), %rcx
movq -0xd0(%rbp), %rdi
movq (%rax), %rax
movq %rcx, 0x20(%rax)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %esi
callq 0xac30
movq (%rax), %rax
movq %rax, -0xf8(%rbp)
movl -0x30(%rbp), %eax
movl %eax, %esi
leaq -0x28(%rbp), %rdi
callq 0xac30
movq -0xf8(%rbp), %rcx
movq (%rax), %rax
movq %rcx, 0x28(%rax)
jmp 0x8ea3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x8f0e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x8d68
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x8d5c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x20f0
leaq -0x88(%rbp), %rdi
callq 0x20f0
testb $0x1, -0x89(%rbp)
jne 0x8d73
jmp 0x8d84
movq -0xe8(%rbp), %rdi
movl $0x30, %esi
callq 0x21e0
jmp 0x8f0e
movl $0x30, %edi
callq 0x21c0
movq %rax, -0x108(%rbp)
jmp 0x8d9c
movq -0xd0(%rbp), %rdi
movb $0x1, -0xb9(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, %esi
callq 0xac30
movq (%rax), %rsi
leaq -0xb8(%rbp), %rdi
callq 0x2090
jmp 0x8dc5
movq -0x108(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0xf1c0
jmp 0x8dda
movq -0x108(%rbp), %rax
movb $0x0, -0xb9(%rbp)
movq %rax, -0x98(%rbp)
leaq -0x28(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0xa4e0
jmp 0x8e01
leaq -0xb8(%rbp), %rdi
callq 0x20f0
movq -0xd0(%rbp), %rdi
movl -0x2c(%rbp), %eax
movl %eax, %esi
callq 0xac30
movq (%rax), %rax
movq %rax, -0x110(%rbp)
movl -0x30(%rbp), %eax
movl %eax, %esi
leaq -0x28(%rbp), %rdi
callq 0xac30
movq -0x110(%rbp), %rcx
movq (%rax), %rax
movq %rcx, 0x20(%rax)
movl -0x30(%rbp), %eax
movl %eax, %esi
leaq -0x28(%rbp), %rdi
callq 0xac30
movq (%rax), %rax
movq $0x0, 0x28(%rax)
jmp 0x8ea3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x8e85
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x20f0
testb $0x1, -0xb9(%rbp)
jne 0x8e90
jmp 0x8ea1
movq -0x108(%rbp), %rdi
movl $0x30, %esi
callq 0x21e0
jmp 0x8f0e
jmp 0x8ea5
movl -0x2c(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x2c(%rbp)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x8b92
movq -0xd0(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0xac50
jmp 0x8ece
leaq -0x28(%rbp), %rdi
callq 0xaea0
jmp 0x8b6e
movq -0xd0(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xac30
movq %rax, %rcx
movq -0xc8(%rbp), %rax
movq (%rcx), %rcx
movq %rcx, (%rax)
leaq -0x28(%rbp), %rdi
callq 0xa660
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
leaq -0x28(%rbp), %rdi
callq 0xa660
movq -0x40(%rbp), %rdi
callq 0x2340