// File: ruankotovich[P]B-Tree/src/seek2.cpp
// Function: main
int main(int argc, char** argv)
{
    SecondaryBTreeDataMap searchKey;

    if (argc > 1) {
        std::memcpy(searchKey.key, argv[1], SECONDARY_KEY_LENGTH);
    } else {
        std::string buffer;
        std::getline(std::cin, buffer);
        std::memcpy(searchKey.key, buffer.c_str(), SECONDARY_KEY_LENGTH);
        std::cout << "\"" << buffer << "\"\n";
    }

    SecondaryBTree btree;

    Article_t a;
    FILE* indexFile = fopen("files/secondaryindex.block", "rb");

    btree.readRoot(indexFile);

    if (indexFile != NULL) {
        auto result = btree.getArticle(searchKey, &a, indexFile);

        if (result.first) {
            cout << a.toString();
            cout << "\n\nNumber of block read in PrimaryBTree Index to find the record: " << result.second + 1;
            fseek(indexFile, 0, SEEK_END);
            cout << "\n\nTotal number of blocks in primary index file: " << ((ftell(indexFile) / sizeof(AbstractBlock_t))) << "\n";
        } else {
            cout << "Record not found.\n";
        }
    } else {
        cout << "There isn't a primary index file.\n";
    }

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc60, %rsp            # imm = 0xC60
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x1, -0x8(%rbp)
jle 0x29cd
leaq -0x140(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
movl $0x12c, %edx            # imm = 0x12C
callq 0x20b0
jmp 0x2a93
leaq -0x160(%rbp), %rdi
movq %rdi, -0xc40(%rbp)
callq 0x2140
movq -0xc40(%rbp), %rsi
movq 0x45f2(%rip), %rdi      # 0x6fe0
callq 0x2050
jmp 0x29f5
leaq -0x160(%rbp), %rdi
callq 0x2070
movq %rax, %rsi
leaq -0x140(%rbp), %rdi
movl $0x12c, %edx            # imm = 0x12C
callq 0x20b0
movq 0x45b4(%rip), %rdi      # 0x6fd0
leaq 0x26f0(%rip), %rsi      # 0x5113
callq 0x20f0
movq %rax, -0xc48(%rbp)
jmp 0x2a31
movq -0xc48(%rbp), %rdi
leaq -0x160(%rbp), %rsi
callq 0x20d0
movq %rax, -0xc50(%rbp)
jmp 0x2a4d
movq -0xc50(%rbp), %rdi
leaq 0x25c6(%rip), %rsi      # 0x5021
callq 0x20f0
jmp 0x2a62
leaq -0x160(%rbp), %rdi
callq 0x2080
jmp 0x2a93
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x168(%rbp)
movl %eax, -0x16c(%rbp)
leaq -0x160(%rbp), %rdi
callq 0x2080
jmp 0x2c2c
leaq -0x2b8(%rbp), %rdi
callq 0x2ed0
leaq -0xc04(%rbp), %rdi
callq 0x47f0
leaq 0x2572(%rip), %rdi      # 0x5024
leaq 0x2586(%rip), %rsi      # 0x503f
callq 0x2130
movq %rax, -0xc10(%rbp)
movq -0xc10(%rbp), %rsi
leaq -0x2b8(%rbp), %rdi
callq 0x2fd0
cmpq $0x0, -0xc10(%rbp)
je 0x2c0e
movq -0xc10(%rbp), %rcx
leaq -0x2b8(%rbp), %rdi
leaq -0x140(%rbp), %rsi
leaq -0xc04(%rbp), %rdx
callq 0x3ee0
movq %rax, -0xc18(%rbp)
testb $0x1, -0xc18(%rbp)
je 0x2bf9
leaq -0xc38(%rbp), %rdi
movq %rdi, -0xc58(%rbp)
leaq -0xc04(%rbp), %rsi
callq 0x48c0
movq -0xc58(%rbp), %rsi
movq 0x448d(%rip), %rdi      # 0x6fd0
callq 0x20d0
jmp 0x2b4a
leaq -0xc38(%rbp), %rdi
callq 0x2080
movq 0x4473(%rip), %rdi      # 0x6fd0
leaq 0x24de(%rip), %rsi      # 0x5042
callq 0x20f0
movq %rax, %rdi
movl -0xc14(%rbp), %esi
addl $0x1, %esi
callq 0x2190
movq -0xc10(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x2120
movq 0x443a(%rip), %rdi      # 0x6fd0
leaq 0x24e7(%rip), %rsi      # 0x5084
callq 0x20f0
movq %rax, -0xc60(%rbp)
movq -0xc10(%rbp), %rdi
callq 0x2030
movq -0xc60(%rbp), %rdi
movq %rax, %rsi
shrq $0xc, %rsi
callq 0x2060
movq %rax, %rdi
leaq 0x2536(%rip), %rsi      # 0x5108
callq 0x20f0
jmp 0x2c0c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x168(%rbp)
movl %eax, -0x16c(%rbp)
leaq -0xc38(%rbp), %rdi
callq 0x2080
jmp 0x2c2c
movq 0x43d0(%rip), %rdi      # 0x6fd0
leaq 0x24ae(%rip), %rsi      # 0x50b5
callq 0x20f0
jmp 0x2c21
movq 0x43bb(%rip), %rdi      # 0x6fd0
leaq 0x24ac(%rip), %rsi      # 0x50c8
callq 0x20f0
xorl %eax, %eax
addq $0xc60, %rsp            # imm = 0xC60
popq %rbp
retq
movq -0x168(%rbp), %rdi
callq 0x21a0
nopl (%rax,%rax)