// File: ruankotovich[P]B-Tree/src/secondarybtree.cpp
// Function: SecondaryBTree::getArticle(SecondaryBTreeDataMap&, Article_t*, _IO_FILE*)
std::pair<bool, int> SecondaryBTree::getArticle(SecondaryBTreeDataMap& key, Article_t* article, FILE* indexFile)
{

    SecondaryBTreeNodeReinterpret* reinterpretation = (SecondaryBTreeNodeReinterpret*)this->root;

    if (reinterpretation->node.count <= 0) {
        //std::cout << "Not Found" << '\n';
        return { false, 0 };
    }

    auto currentPointer = binarySearch(reinterpretation->node.keys, reinterpretation->node.count, key);
    // std::cout << "Searching for key " << key << " at the root(" << this->rootOffset << ")\n";

    int countTraversed = 1;

    while (!currentPointer.first) {
        if (reinterpretation->node.isLeaf()) {
            //std::cout << "Not Found" << '\n';
            return { false, 0 };
        }
        //std::cout << "It was supposed to be in this position in the block : " << currentPointer.second << '\n';
        int toSeek = currentPointer.second >= reinterpretation->node.count ? reinterpretation->node.blockPointers[reinterpretation->node.count] : (key < reinterpretation->node.keys[currentPointer.second] ? reinterpretation->node.blockPointers[currentPointer.second] : reinterpretation->node.blockPointers[currentPointer.second + 1]);
        // std::cout << "Seeking between " << reinterpretation->node.blockPointers[currentPointer.second] << " and " << reinterpretation->node.blockPointers[currentPointer.second + 1] << '\n';
        //std::cout << "New block to search : " << toSeek << "\n\n";

        fseek(indexFile, sizeof(AbstractBlock_t) * toSeek, SEEK_SET);
        fread(&reinterpretation->block, sizeof(AbstractBlock_t), 1, indexFile);

        currentPointer = binarySearch(reinterpretation->node.keys, reinterpretation->node.count, key);
        ++countTraversed;
    }

    //std::cout << "Found at the position " << currentPointer.second << '\n';

    FILE* blockFile = fopen("files/data.block", "rb+");
    if (blockFile != NULL) {
        rewind(blockFile);
        getArticleFromHash(reinterpretation->node.keys[currentPointer.second].dataPointer, article, blockFile);
    }

    return { true, countTraversed };
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x420, %rsp            # imm = 0x420
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x138(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movzwl (%rax), %eax
cmpl $0x0, %eax
jg 0x2d07
movb $0x0, -0x31(%rbp)
movl $0x0, -0x38(%rbp)
leaq -0x8(%rbp), %rdi
leaq -0x31(%rbp), %rsi
leaq -0x38(%rbp), %rdx
callq 0x31d0
jmp 0x2f71
movq -0x30(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x2d0(%rbp)
movq -0x30(%rbp), %rax
movzwl (%rax), %eax
movl %eax, -0x2d4(%rbp)
movq -0x18(%rbp), %rsi
leaq -0x170(%rbp), %rdi
movl $0x130, %edx            # imm = 0x130
callq 0x1070
movq %rsp, %rdi
movl $0x26, %ecx
leaq -0x170(%rbp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movl -0x2d4(%rbp), %esi
movq -0x2d0(%rbp), %rdi
callq 0x2f80
movq %rax, -0x40(%rbp)
movl $0x1, -0x174(%rbp)
movb -0x40(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x2d78
jmp 0x2ef7
movq -0x30(%rbp), %rdi
callq 0x1b70
testb $0x1, %al
jne 0x2d87
jmp 0x2db4
movb $0x0, -0x175(%rbp)
movl $0x0, -0x17c(%rbp)
leaq -0x8(%rbp), %rdi
leaq -0x175(%rbp), %rsi
leaq -0x17c(%rbp), %rdx
callq 0x31d0
jmp 0x2f71
movl -0x3c(%rbp), %eax
movq -0x30(%rbp), %rcx
movzwl (%rcx), %ecx
cmpl %ecx, %eax
jl 0x2ddc
movq -0x30(%rbp), %rax
movq -0x30(%rbp), %rcx
movzwl (%rcx), %ecx
movl 0xe44(%rax,%rcx,4), %eax
movl %eax, -0x2d8(%rbp)
jmp 0x2e3e
movq -0x18(%rbp), %rdi
movq -0x30(%rbp), %rsi
addq $0x4, %rsi
movslq -0x3c(%rbp), %rax
imulq $0x130, %rax, %rax      # imm = 0x130
addq %rax, %rsi
callq 0x1b10
testb $0x1, %al
jne 0x2e01
jmp 0x2e18
movq -0x30(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movl 0xe44(%rax,%rcx,4), %eax
movl %eax, -0x2dc(%rbp)
jmp 0x2e32
movq -0x30(%rbp), %rax
movl -0x3c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movl 0xe44(%rax,%rcx,4), %eax
movl %eax, -0x2dc(%rbp)
movl -0x2dc(%rbp), %eax
movl %eax, -0x2d8(%rbp)
movl -0x2d8(%rbp), %eax
movl %eax, -0x180(%rbp)
movq -0x28(%rbp), %rdi
movslq -0x180(%rbp), %rsi
shlq $0xc, %rsi
xorl %edx, %edx
callq 0x10d0
movq -0x30(%rbp), %rdi
movq -0x28(%rbp), %rcx
movl $0x1000, %esi           # imm = 0x1000
movl $0x1, %edx
callq 0x1100
movq -0x30(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x2e8(%rbp)
movq -0x30(%rbp), %rax
movzwl (%rax), %eax
movl %eax, -0x2ec(%rbp)
movq -0x18(%rbp), %rsi
leaq -0x2b8(%rbp), %rdi
movl $0x130, %edx            # imm = 0x130
callq 0x1070
movq %rsp, %rdi
movl $0x26, %ecx
leaq -0x2b8(%rbp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movl -0x2ec(%rbp), %esi
movq -0x2e8(%rbp), %rdi
callq 0x2f80
movq %rax, -0x188(%rbp)
leaq -0x40(%rbp), %rdi
leaq -0x188(%rbp), %rsi
callq 0x3200
movl -0x174(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x174(%rbp)
jmp 0x2d6a
leaq 0x3125(%rip), %rdi      # 0x6023
leaq 0x3133(%rip), %rsi      # 0x6038
callq 0x10e0
movq %rax, -0x2c0(%rbp)
cmpq $0x0, -0x2c0(%rbp)
je 0x2f53
movq -0x2c0(%rbp), %rdi
callq 0x1040
movq -0x30(%rbp), %rax
addq $0x4, %rax
movslq -0x3c(%rbp), %rcx
imulq $0x130, %rcx, %rcx      # imm = 0x130
addq %rcx, %rax
movl 0x12c(%rax), %edi
movq -0x20(%rbp), %rsi
movq -0x2c0(%rbp), %rdx
callq 0x52d0
movb $0x1, -0x2c1(%rbp)
leaq -0x8(%rbp), %rdi
leaq -0x2c1(%rbp), %rsi
leaq -0x174(%rbp), %rdx
callq 0x3230
movq -0x8(%rbp), %rax
addq $0x420, %rsp            # imm = 0x420
popq %rbp
retq
nop