// File: /workspace/github_cpp_makefile_star2_O1/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 $0x288, %rsp            # imm = 0x288
movq %rcx, 0x20(%rsp)
movq %rsi, 0x10(%rsp)
movq 0x138(%rdi), %r13
movzwl (%r13), %ebp
testq %rbp, %rbp
je 0x2720
movq %rdx, 0x18(%rsp)
leaq 0x4(%r13), %r14
leaq 0x28(%rsp), %r12
movl $0x130, %edx            # imm = 0x130
movq %r12, %rdi
movq 0x10(%rsp), %r15
movq %r15, %rsi
callq 0x1090
leaq 0x158(%rsp), %rbx
movl $0x130, %edx            # imm = 0x130
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1090
imulq $0x130, %rbp, %rax      # imm = 0x130
leaq (%rax,%r13), %rsi
addq $0x4, %rsi
movq %r14, %rdi
movq %rbx, %rdx
callq 0x2809
movq %rax, %rbx
movq %r14, 0x8(%rsp)
subq %r14, %rbx
sarq $0x4, %rbx
movabsq $-0x79435e50d79435e5, %rax # imm = 0x86BCA1AF286BCA1B
imulq %rax, %rbx
movslq %ebx, %rax
imulq $0x130, %rax, %rax      # imm = 0x130
leaq (%rax,%r13), %rdi
addq $0x4, %rdi
movq %r12, %rsi
callq 0x1100
testl %eax, %eax
je 0x2727
leaq 0xe44(%r13), %rax
movq %rax, (%rsp)
movl $0x1, %ebp
cmpw $0x0, 0x2(%r13)
je 0x2720
movzwl (%r13), %eax
cmpl %eax, %ebx
jge 0x2648
movq 0x10(%rsp), %rdi
movslq %ebx, %r15
imulq $0x130, %r15, %rsi      # imm = 0x130
addq 0x8(%rsp), %rsi
callq 0x1100
testl %eax, %eax
jns 0x2652
movq (%rsp), %rax
leaq (%rax,%r15,4), %rbx
jmp 0x266b
movq (%rsp), %rcx
leaq (%rcx,%rax,4), %rbx
jmp 0x266b
shlq $0x20, %rbx
movabsq $0x100000000, %rax      # imm = 0x100000000
addq %rax, %rbx
sarq $0x1e, %rbx
addq (%rsp), %rbx
movslq (%rbx), %rsi
shlq $0xc, %rsi
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
xorl %edx, %edx
callq 0x10d0
movl $0x1000, %esi           # imm = 0x1000
movl $0x1, %edx
movq %r13, %rdi
movq %rbx, %rcx
callq 0x1110
movzwl (%r13), %ebx
movl $0x130, %edx            # imm = 0x130
leaq 0x28(%rsp), %r15
movq %r15, %rdi
movq 0x10(%rsp), %r14
movq %r14, %rsi
callq 0x1090
movl $0x130, %edx            # imm = 0x130
leaq 0x158(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x1090
imulq $0x130, %rbx, %rsi      # imm = 0x130
movq 0x8(%rsp), %r14
addq %r14, %rsi
movq %r14, %rdi
movq %r12, %rdx
callq 0x2809
movq %rax, %rbx
subq %r14, %rbx
sarq $0x4, %rbx
movabsq $-0x79435e50d79435e5, %rax # imm = 0x86BCA1AF286BCA1B
imulq %rax, %rbx
movslq %ebx, %rax
imulq $0x130, %rax, %rdi      # imm = 0x130
addq %r14, %rdi
movq %r15, %rsi
callq 0x1100
incl %ebp
testl %eax, %eax
jne 0x260c
jmp 0x272c
xorl %r12d, %r12d
xorl %eax, %eax
jmp 0x277a
movl $0x1, %ebp
leaq 0x18f0(%rip), %rdi      # 0x4023
leaq 0x18fe(%rip), %rsi      # 0x4038
callq 0x10f0
movl $0x1, %r12d
testq %rax, %rax
je 0x2778
movq %rax, %r15
movq %rax, %rdi
callq 0x1040
movslq %ebx, %rax
imulq $0x130, %rax, %rax      # imm = 0x130
movq 0x8(%rsp), %rcx
movl 0x12c(%rcx,%rax), %edi
movq 0x18(%rsp), %rsi
movq %r15, %rdx
callq 0x3754
movl %ebp, %eax
shlq $0x20, %rax
orq %r12, %rax
addq $0x288, %rsp            # imm = 0x288
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq