// File: 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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movzwl (%rax), %eax
cmpl $0x0, %eax
jg 0x44b0
movb $0x0, -0x31(%rbp)
movl $0x0, -0x38(%rbp)
leaq -0x8(%rbp), %rdi
leaq -0x31(%rbp), %rsi
leaq -0x38(%rbp), %rdx
callq 0x31d0
jmp 0x463e
movq -0x30(%rbp), %rdi
addq $0x4, %rdi
movq -0x30(%rbp), %rax
movzwl (%rax), %esi
movl -0x14(%rbp), %edx
callq 0x4650
movq %rax, -0x40(%rbp)
movl $0x1, -0x44(%rbp)
movb -0x40(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x44e0
jmp 0x45e9
movq -0x30(%rbp), %rdi
callq 0x3620
testb $0x1, %al
jne 0x44ef
jmp 0x4510
movb $0x0, -0x45(%rbp)
movl $0x0, -0x4c(%rbp)
leaq -0x8(%rbp), %rdi
leaq -0x45(%rbp), %rsi
leaq -0x4c(%rbp), %rdx
callq 0x31d0
jmp 0x463e
movl -0x3c(%rbp), %eax
movq -0x30(%rbp), %rcx
movzwl (%rcx), %ecx
cmpl %ecx, %eax
jl 0x4537
movq -0x30(%rbp), %rax
movq -0x30(%rbp), %rcx
movzwl (%rcx), %ecx
movw 0xaa4(%rax,%rcx,2), %ax
movw %ax, -0x64(%rbp)
jmp 0x457f
movl -0x14(%rbp), %eax
movq -0x30(%rbp), %rcx
movslq -0x3c(%rbp), %rdx
cmpl 0x4(%rcx,%rdx,4), %eax
jge 0x455e
movq -0x30(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movw 0xaa4(%rax,%rcx,2), %ax
movw %ax, -0x66(%rbp)
jmp 0x4577
movq -0x30(%rbp), %rax
movl -0x3c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movw 0xaa4(%rax,%rcx,2), %ax
movw %ax, -0x66(%rbp)
movw -0x66(%rbp), %ax
movw %ax, -0x64(%rbp)
movw -0x64(%rbp), %ax
movw %ax, -0x4e(%rbp)
movq -0x28(%rbp), %rdi
movzwl -0x4e(%rbp), %eax
movl %eax, %esi
shlq $0xc, %rsi
xorl %edx, %edx
callq 0x10d0
movq -0x30(%rbp), %rdi
movq -0x28(%rbp), %rcx
movl $0x1000, %esi           # imm = 0x1000
movl $0x1, %edx
callq 0x1100
movq -0x30(%rbp), %rdi
addq $0x4, %rdi
movq -0x30(%rbp), %rax
movzwl (%rax), %esi
movl -0x14(%rbp), %edx
callq 0x4650
movq %rax, -0x58(%rbp)
leaq -0x40(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x3200
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x44d2
leaq 0x1a33(%rip), %rdi      # 0x6023
leaq 0x1a41(%rip), %rsi      # 0x6038
callq 0x10e0
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
je 0x4629
movq -0x60(%rbp), %rdi
callq 0x1040
movq -0x30(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movl 0x4(%rax,%rcx,4), %edi
movq -0x20(%rbp), %rsi
movq -0x60(%rbp), %rdx
callq 0x52d0
movb $0x1, -0x61(%rbp)
leaq -0x8(%rbp), %rdi
leaq -0x61(%rbp), %rsi
leaq -0x44(%rbp), %rdx
callq 0x3230
movq -0x8(%rbp), %rax
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax,%rax)