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

    PrimaryBTreeNodeReinterpret* reinterpretation = (PrimaryBTreeNodeReinterpret*)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';
        unsigned short 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], article, blockFile);
    }

    return { true, countTraversed };
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %esi, %ebp
movq 0x10(%rdi), %r15
movzwl (%r15), %esi
testl %esi, %esi
je 0x2d90
movq %rcx, %r13
movq %rdx, (%rsp)
leaq 0x4(%r15), %r12
movq %r12, %rdi
movl %ebp, %edx
callq 0x2de9
movq %rax, %r14
pushq $0x1
popq %rbx
testb $0x1, %r14b
jne 0x2d96
cmpw $0x0, 0x2(%r15)
je 0x2d90
movq %r14, %rcx
shrq $0x20, %rcx
movzwl (%r15), %eax
cmpl %eax, %ecx
jge 0x2d52
movq %r14, %rax
sarq $0x20, %rax
cmpl %ebp, (%r12,%rax,4)
jg 0x2d52
movabsq $0x100000000, %rax      # imm = 0x100000000
addq %rax, %r14
sarq $0x20, %r14
movq %r14, %rax
movzwl 0xaa4(%r15,%rax,2), %esi
shll $0xc, %esi
movq %r13, %rdi
xorl %edx, %edx
callq 0x10c0
movl $0x1000, %esi           # imm = 0x1000
movq %r15, %rdi
pushq $0x1
popq %rdx
movq %r13, %rcx
callq 0x10f0
movzwl (%r15), %esi
movq %r12, %rdi
movl %ebp, %edx
callq 0x2de9
movq %rax, %r14
incl %ebx
jmp 0x2d14
xorl %eax, %eax
xorl %ebx, %ebx
jmp 0x2dd0
leaq 0x1286(%rip), %rdi      # 0x4023
leaq 0x1294(%rip), %rsi      # 0x4038
callq 0x10d0
testq %rax, %rax
je 0x2dcd
movq %rax, %r15
movq %rax, %rdi
callq 0x1040
sarq $0x20, %r14
movl (%r12,%r14,4), %edi
movq (%rsp), %rsi
movq %r15, %rdx
callq 0x337c
pushq $0x1
popq %rax
shlq $0x20, %rbx
orq %rax, %rbx
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq