// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3a8, %rsp            # imm = 0x3A8
movq 0x138(%rdi), %rbx
movzwl (%rbx), %r14d
testl %r14d, %r14d
je 0x238f
movq %rcx, %r13
movq %rsi, %rbp
movq %rdx, 0x140(%rsp)
leaq 0x4(%rbx), %r12
leaq 0x278(%rsp), %r15
movl $0x130, %edx            # imm = 0x130
movq %r15, %rdi
callq 0x1070
pushq $0x26
popq %rcx
movq %rsp, %rdi
movq %r15, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %r12, 0x138(%rsp)
movq %r12, %rdi
movl %r14d, %esi
callq 0x2402
movq %rax, %r12
pushq $0x1
popq %r14
testb $0x1, %r12b
jne 0x2396
cmpw $0x0, 0x2(%rbx)
je 0x238f
movq %r12, %rax
shrq $0x20, %rax
movzwl (%rbx), %r15d
cmpl %r15d, %eax
jge 0x2323
movq %r12, %r15
sarq $0x20, %r15
imulq $0x130, %r15, %rsi      # imm = 0x130
addq 0x138(%rsp), %rsi
movq %rbp, %rdi
callq 0x179e
testb %al, %al
jne 0x2323
movabsq $0x100000000, %rax      # imm = 0x100000000
addq %rax, %r12
sarq $0x20, %r12
movq %r12, %r15
movslq 0xe44(%rbx,%r15,4), %rsi
shlq $0xc, %rsi
movq %r13, %rdi
xorl %edx, %edx
callq 0x10c0
movl $0x1000, %esi           # imm = 0x1000
movq %rbx, %rdi
pushq $0x1
popq %rdx
movq %r13, %rcx
callq 0x10f0
movzwl (%rbx), %r15d
movl $0x130, %edx            # imm = 0x130
leaq 0x148(%rsp), %r12
movq %r12, %rdi
movq %rbp, %rsi
callq 0x1070
pushq $0x26
popq %rcx
movq %rsp, %rdi
movq %r12, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq 0x138(%rsp), %rdi
movl %r15d, %esi
callq 0x2402
movq %rax, %r12
incl %r14d
jmp 0x22c8
xorl %eax, %eax
xorl %r14d, %r14d
jmp 0x23e6
leaq 0x1c86(%rip), %rdi      # 0x4023
leaq 0x1c94(%rip), %rsi      # 0x4038
callq 0x10d0
testq %rax, %rax
je 0x23e3
movq %rax, %rbx
movq %rax, %rdi
callq 0x1040
sarq $0x20, %r12
imulq $0x130, %r12, %rax      # imm = 0x130
movq 0x138(%rsp), %rcx
movl 0x12c(%rcx,%rax), %edi
movq 0x140(%rsp), %rsi
movq %rbx, %rdx
callq 0x337c
pushq $0x1
popq %rax
shlq $0x20, %r14
orq %rax, %r14
movq %r14, %rax
addq $0x3a8, %rsp            # imm = 0x3A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq