// File: /workspace/github_cpp_makefile_star2_O3/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
movq 0x10(%rdi), %r15
movzwl (%r15), %eax
testq %rax, %rax
je 0x34aa
movq %rcx, %r14
movl %esi, %ebp
movq %rdx, (%rsp)
leaq 0x4(%r15), %r13
movq %r13, %rcx
movq %rax, %rdx
movq %rdx, %rsi
shrq %rsi
movq %rsi, %rdi
notq %rdi
addq %rdx, %rdi
leaq (%rcx,%rsi,4), %rdx
addq $0x4, %rdx
cmpl %ebp, -0x4(%rdx)
cmovgeq %rsi, %rdi
cmovlq %rdx, %rcx
movq %rdi, %rdx
testq %rdi, %rdi
jg 0x33a7
subq %r13, %rcx
movq %rcx, %r12
shlq $0x1e, %r12
shrq $0x2, %rcx
movslq %ecx, %rcx
movl $0x1, %ebx
cmpl %ebp, (%r13,%rcx,4)
je 0x34b1
cmpw $0x0, 0x2(%r15)
je 0x34aa
movq %r12, %rcx
shrq $0x20, %rcx
movzwl %ax, %eax
cmpl %eax, %ecx
jge 0x342e
movq %r12, %rax
sarq $0x20, %rax
cmpl %ebp, (%r13,%rax,4)
jg 0x342e
movabsq $0x100000000, %rax      # imm = 0x100000000
addq %rax, %r12
sarq $0x20, %r12
movq %r12, %rax
movzwl 0xaa4(%r15,%rax,2), %esi
shll $0xc, %esi
movq %r14, %rdi
xorl %edx, %edx
callq 0x10d0
movl $0x1000, %esi           # imm = 0x1000
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x1110
movzwl (%r15), %eax
movq %r13, %rcx
testq %rax, %rax
je 0x3492
movq %rax, %rdx
movq %rdx, %rsi
shrq %rsi
movq %rsi, %rdi
notq %rdi
addq %rdx, %rdi
leaq (%rcx,%rsi,4), %rdx
addq $0x4, %rdx
cmpl %ebp, -0x4(%rdx)
cmovgeq %rsi, %rdi
cmovlq %rdx, %rcx
movq %rdi, %rdx
testq %rdi, %rdi
jg 0x3468
subq %r13, %rcx
movq %rcx, %r12
shlq $0x1e, %r12
shrq $0x2, %rcx
movslq %ecx, %rcx
incl %ebx
jmp 0x33e7
xorl %r15d, %r15d
xorl %eax, %eax
jmp 0x34f1
leaq 0xb6b(%rip), %rdi       # 0x4023
leaq 0xb79(%rip), %rsi       # 0x4038
callq 0x10f0
movl $0x1, %r15d
testq %rax, %rax
je 0x34ef
movq %rax, %r14
movq %rax, %rdi
callq 0x1040
sarq $0x20, %r12
movl (%r13,%r12,4), %edi
movq (%rsp), %rsi
movq %r14, %rdx
callq 0x3a40
movl %ebx, %eax
shlq $0x20, %rax
orq %r15, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop