// File: /workspace/github_cpp_makefile_star2_O3/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 0x138(%rdi), %r13
movzwl (%r13), %ebp
testq %rbp, %rbp
je 0x28b9
movq %rsi, %r15
movq %rdx, 0x18(%rsp)
leaq 0x4(%r13), %r14
leaq 0x28(%rsp), %r12
movl $0x130, %edx            # imm = 0x130
movq %r12, %rdi
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 0x29a2
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 0x28c0
movq %r15, 0x10(%rsp)
movl $0x1, %ebp
cmpw $0x0, 0x2(%r13)
je 0x28b9
movzwl (%r13), %r15d
cmpl %r15d, %ebx
jge 0x27ff
movslq %ebx, %r15
imulq $0x130, %r15, %rsi      # imm = 0x130
addq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdi
callq 0x1100
testl %eax, %eax
js 0x27ff
shlq $0x20, %rbx
movabsq $0x100000000, %rax      # imm = 0x100000000
addq %rax, %rbx
sarq $0x20, %rbx
movq %rbx, %r15
movslq 0xe44(%r13,%r15,4), %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 0x29a2
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 0x27b4
jmp 0x28c5
xorl %r12d, %r12d
xorl %eax, %eax
jmp 0x2913
movl $0x1, %ebp
leaq 0x1757(%rip), %rdi      # 0x4023
leaq 0x1765(%rip), %rsi      # 0x4038
callq 0x10f0
movl $0x1, %r12d
testq %rax, %rax
je 0x2911
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 0x3a40
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