// File: adamjanovsky[P]PA193_test_parser_monero/src/parser.cpp
// Function: Parser::validate_block(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, bool)
int Parser::validate_block(string lower_filename, string higher_filename, bool print_hashes /* = false */) {
    // Initialize the blocks from given filenames
    if(lower_block.init_from_file(lower_filename) < 0 or
       higher_block.init_from_file(higher_filename) < 0) {
        std::cerr << "Parser: Failed to initialize block." << std::endl;
        return ERR_PARSER_FAILED_TO_INIT;
    }

    // Get the block hash and block id from the next block
    hash::hash_t lower_hash;
    hash::hash_t higher_prev_id;
    if(lower_block.get_block_hash(lower_hash) or higher_block.get_prev_id(higher_prev_id)) {
        std::cerr << "Parser: Failed to obtain hash result of lower block." << std::endl;
        return ERR_PARSER_FAILED_TO_VALIDATE;
    }

    // Print the hashes if desired
    if (print_hashes) {
        printf("Lower hash:  ");
        for(int i = 0; i < lower_hash.size(); i++) printf("%02x", lower_hash[i]);
        printf("\n");
        printf("Higher hash: ");
        for(int i = 0; i < higher_prev_id.size(); i++) printf("%02x", higher_prev_id[i]);
        printf("\n");
    }
    
    // Compare the hash values to validate the block
    if (memcmp(lower_hash.data(), higher_prev_id.data(), lower_hash.size()) == 0) {
        std::cout << "OK - The block is valid." << std::endl;
        return OK;
    } else {
        std::cout << "ERR - The block is not valid." << std::endl;
        return ERR_PARSER_BLOCK_INVALID;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movq %rdx, -0xe8(%rbp)
movb %cl, %al
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
andb $0x1, %al
movb %al, -0x21(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xe0(%rbp)
addq $0xa0, %rax
movq %rax, -0xd8(%rbp)
leaq -0x48(%rbp), %rdi
movq %rdi, -0xd0(%rbp)
callq 0x20c0
movq -0xd8(%rbp), %rdi
movq -0xd0(%rbp), %rsi
movb $0x0, -0x79(%rbp)
callq 0x3000
movl %eax, -0xc8(%rbp)
jmp 0x2bbc
movl -0xc8(%rbp), %ecx
movb $0x1, %al
cmpl $0x0, %ecx
movb %al, -0xe9(%rbp)
jl 0x2c1d
movq -0xe8(%rbp), %rsi
movq -0xe0(%rbp), %rax
movq %rax, -0xf8(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x20c0
jmp 0x2bef
movq -0xf8(%rbp), %rdi
movb $0x1, -0x79(%rbp)
leaq -0x78(%rbp), %rsi
callq 0x3000
movl %eax, -0xfc(%rbp)
jmp 0x2c0b
movl -0xfc(%rbp), %eax
cmpl $0x0, %eax
setl %al
movb %al, -0xe9(%rbp)
movb -0xe9(%rbp), %al
movb %al, -0xfd(%rbp)
testb $0x1, -0x79(%rbp)
jne 0x2c31
jmp 0x2c3a
leaq -0x78(%rbp), %rdi
callq 0x2100
leaq -0x48(%rbp), %rdi
callq 0x2100
movb -0xfd(%rbp), %al
testb $0x1, %al
jne 0x2c4f
jmp 0x2cb8
movq 0xa39a(%rip), %rdi      # 0xcff0
leaq 0x63d7(%rip), %rsi      # 0x9034
callq 0x2160
movq %rax, %rdi
movq 0xa354(%rip), %rsi      # 0xcfc0
callq 0x2190
movl $0xfffffffd, -0x4(%rbp) # imm = 0xFFFFFFFD
jmp 0x2ed0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x2caa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
testb $0x1, -0x79(%rbp)
jne 0x2c9f
jmp 0x2ca8
leaq -0x78(%rbp), %rdi
callq 0x2100
jmp 0x2caa
leaq -0x48(%rbp), %rdi
callq 0x2100
jmp 0x2edc
movq -0xe0(%rbp), %rdi
addq $0xa0, %rdi
leaq -0x99(%rbp), %rsi
callq 0x4020
cmpl $0x0, %eax
jne 0x2cef
movq -0xe0(%rbp), %rdi
leaq -0xb9(%rbp), %rsi
callq 0x2f80
cmpl $0x0, %eax
je 0x2d1d
movq 0xa2fa(%rip), %rdi      # 0xcff0
leaq 0x635b(%rip), %rsi      # 0x9058
callq 0x2160
movq %rax, %rdi
movq 0xa2b4(%rip), %rsi      # 0xcfc0
callq 0x2190
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2ed0
testb $0x1, -0x21(%rbp)
je 0x2e2f
leaq 0x635f(%rip), %rdi      # 0x908d
movb $0x0, %al
callq 0x2050
movl $0x0, -0xc0(%rbp)
movslq -0xc0(%rbp), %rax
movq %rax, -0x108(%rbp)
leaq -0x99(%rbp), %rdi
callq 0x2ef0
movq %rax, %rcx
movq -0x108(%rbp), %rax
cmpq %rcx, %rax
jae 0x2d9d
movslq -0xc0(%rbp), %rsi
leaq -0x99(%rbp), %rdi
callq 0x2f00
movzbl (%rax), %esi
leaq 0x6316(%rip), %rdi      # 0x909b
movb $0x0, %al
callq 0x2050
movl -0xc0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc0(%rbp)
jmp 0x2d3f
leaq 0x62fc(%rip), %rdi      # 0x90a0
movb $0x0, %al
callq 0x2050
leaq 0x62f0(%rip), %rdi      # 0x90a2
movb $0x0, %al
callq 0x2050
movl $0x0, -0xc4(%rbp)
movslq -0xc4(%rbp), %rax
movq %rax, -0x110(%rbp)
leaq -0xb9(%rbp), %rdi
callq 0x2ef0
movq %rax, %rcx
movq -0x110(%rbp), %rax
cmpq %rcx, %rax
jae 0x2e21
movslq -0xc4(%rbp), %rsi
leaq -0xb9(%rbp), %rdi
callq 0x2f00
movzbl (%rax), %esi
leaq 0x6292(%rip), %rdi      # 0x909b
movb $0x0, %al
callq 0x2050
movl -0xc4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc4(%rbp)
jmp 0x2dc3
leaq 0x6278(%rip), %rdi      # 0x90a0
movb $0x0, %al
callq 0x2050
leaq -0x99(%rbp), %rdi
callq 0x2f30
movq %rax, -0x120(%rbp)
leaq -0xb9(%rbp), %rdi
callq 0x2f30
movq %rax, -0x118(%rbp)
leaq -0x99(%rbp), %rdi
callq 0x2ef0
movq -0x120(%rbp), %rdi
movq -0x118(%rbp), %rsi
movq %rax, %rdx
callq 0x20d0
cmpl $0x0, %eax
jne 0x2ea7
movq 0xa14d(%rip), %rdi      # 0xcfd0
leaq 0x6226(%rip), %rsi      # 0x90b0
callq 0x2160
movq %rax, %rdi
movq 0xa127(%rip), %rsi      # 0xcfc0
callq 0x2190
movl $0x0, -0x4(%rbp)
jmp 0x2ed0
movq 0xa122(%rip), %rdi      # 0xcfd0
leaq 0x6214(%rip), %rsi      # 0x90c9
callq 0x2160
movq %rax, %rdi
movq 0xa0fc(%rip), %rsi      # 0xcfc0
callq 0x2190
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
movq -0x50(%rbp), %rdi
callq 0x22d0
nopw %cs:(%rax,%rax)
nop