// File: /workspace/github_cpp_makefile_star2_O1/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
subq $0x18, %rsp
movq %rcx, %r14
movq 0x10(%rdi), %r15
movzwl (%r15), %ecx
testq %rcx, %rcx
je 0x3200
movl %esi, %ebp
movq %rdx, 0x10(%rsp)
leaq 0x4(%r15), %r13
movq %r13, %rax
movq %rcx, %rdx
shrq %rdx
movq %rdx, %rsi
notq %rsi
addq %rcx, %rsi
leaq (%rax,%rdx,4), %rcx
addq $0x4, %rcx
cmpl %ebp, -0x4(%rcx)
cmovgeq %rdx, %rsi
cmovlq %rcx, %rax
movq %rsi, %rcx
testq %rsi, %rsi
jg 0x309e
subq %r13, %rax
movq %rax, %r12
shlq $0x1e, %r12
shrq $0x2, %rax
cltq
movl $0x1, %ebx
cmpl %ebp, (%r13,%rax,4)
je 0x31bd
leaq 0xaa4(%r15), %rax
movq %rax, 0x8(%rsp)
cmpw $0x0, 0x2(%r15)
je 0x3200
movq %r12, %rcx
shrq $0x20, %rcx
movzwl (%r15), %eax
cmpl %eax, %ecx
jge 0x311d
movq %r12, %rax
sarq $0x20, %rax
cmpl %ebp, (%r13,%rax,4)
jle 0x3128
movq 0x8(%rsp), %rcx
leaq (%rcx,%rax,2), %rax
jmp 0x3142
movabsq $0x100000000, %rax      # imm = 0x100000000
addq %rax, %r12
sarq $0x20, %r12
movq 0x8(%rsp), %rax
leaq (%rax,%r12,2), %rax
movzwl (%rax), %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), %ecx
movq %r13, %rax
testq %rcx, %rcx
je 0x31a0
movq %r13, %rax
movq %rcx, %rdx
shrq %rdx
movq %rdx, %rsi
notq %rsi
addq %rcx, %rsi
leaq (%rax,%rdx,4), %rcx
addq $0x4, %rcx
cmpl %ebp, -0x4(%rcx)
cmovgeq %rdx, %rsi
cmovlq %rcx, %rax
movq %rsi, %rcx
testq %rsi, %rsi
jg 0x3176
subq %r13, %rax
movq %rax, %r12
shlq $0x1e, %r12
shrq $0x2, %rax
cltq
incl %ebx
cmpl %ebp, (%r13,%rax,4)
jne 0x30f4
leaq 0xe5f(%rip), %rdi       # 0x4023
leaq 0xe6d(%rip), %rsi       # 0x4038
callq 0x10f0
movl $0x1, %r15d
testq %rax, %rax
je 0x31fc
movq %rax, %r14
movq %rax, %rdi
callq 0x1040
sarq $0x20, %r12
movl (%r13,%r12,4), %edi
movq 0x10(%rsp), %rsi
movq %r14, %rdx
callq 0x3754
movl %ebx, %eax
jmp 0x3205
xorl %r15d, %r15d
xorl %eax, %eax
shlq $0x20, %rax
orq %r15, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop