// File: adamjanovsky[P]PA193_test_parser_monero/src/block.cpp
// Function: Block::init_from_file(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
int Block::init_from_file(string filename) {
    
    // First check the file size
    FILE *p_file = NULL;
    p_file = fopen(filename.c_str(),"rb");
    fseek(p_file,0,SEEK_END);
    int size = ftell(p_file);
    fclose(p_file);
    std::cout<<"File size (in bytes): " << size << std::endl;
    if(size > 3000){
        std::cout << "Couldn not be a block" << std::endl;
        return ERR_BL_INIT_FILE_ERROR;
    }
   
    // Set init to false to be sure the block stays unitialized if we encounter an error in the middle of init
    initialized = false;
    
    // Open the in file
    std::ifstream in_file(filename);
    if (!in_file.is_open()) {
        std::cout << "Could not open file: " << filename << std::endl;
        return ERR_BL_INIT_FILE_ERROR;
    }
    
    // Save the filename
    this->filename = filename;
    
    
    // Try to parse header, miner_tx and tx_hashes
    
    if (load_header(in_file) < 0 || !in_file.good()) {
        std::cerr << "Block header: Could not parse block header." << std::endl;
        return ERR_BL_INIT_HEADER;
    }
    
    if (load_miner_tx(in_file) < 0 || !in_file.good()) {
        std::cerr << "Block header: Could not parse miner transaction." << std::endl;
        return ERR_BL_INIT_MINER_TX;
    }
    
    if (load_tx_hashes(in_file) < 0) {
        std::cerr << "Block header: Could not parse transaction hashes." << std::endl;
        return ERR_BL_INIT_TX_HASHES;
    }
    
    // Everything went fine...
    initialized = true;
    return OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2b0, %rsp            # imm = 0x2B0
movq %rsi, -0x258(%rbp)
movq %rdi, %rax
movq -0x258(%rbp), %rdi
movq %rdi, -0x250(%rbp)
movq %rax, -0x10(%rbp)
movq %rdi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x248(%rbp)
movq $0x0, -0x20(%rbp)
callq 0x20b0
movq %rax, %rdi
leaq 0x609a(%rip), %rsi      # 0x90e7
callq 0x21c0
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x21b0
movq -0x20(%rbp), %rdi
callq 0x2030
movl %eax, -0x24(%rbp)
movq -0x20(%rbp), %rdi
callq 0x2170
movq 0x9f4c(%rip), %rdi      # 0xcfd0
leaq 0x605f(%rip), %rsi      # 0x90ea
callq 0x2160
movq %rax, %rdi
movl -0x24(%rbp), %esi
callq 0x22b0
movq %rax, %rdi
movq 0x9f1b(%rip), %rsi      # 0xcfc0
callq 0x2190
cmpl $0xbb8, -0x24(%rbp)     # imm = 0xBB8
jle 0x30e1
movq 0x9f16(%rip), %rdi      # 0xcfd0
leaq 0x6040(%rip), %rsi      # 0x9101
callq 0x2160
movq %rax, %rdi
movq 0x9ef0(%rip), %rsi      # 0xcfc0
callq 0x2190
movl $0xfffffffc, -0x4(%rbp) # imm = 0xFFFFFFFC
jmp 0x3390
movq -0x250(%rbp), %rsi
movq -0x248(%rbp), %rax
movb $0x0, (%rax)
leaq -0x230(%rbp), %rdi
movq %rdi, -0x268(%rbp)
movl $0x8, %edx
callq 0x2270
movq -0x268(%rbp), %rdi
callq 0x2260
movb %al, -0x259(%rbp)
jmp 0x311e
movb -0x259(%rbp), %al
testb $0x1, %al
jne 0x31b4
jmp 0x312e
movq 0x9e9b(%rip), %rdi      # 0xcfd0
leaq 0x5fdb(%rip), %rsi      # 0x9117
callq 0x2160
movq %rax, -0x270(%rbp)
jmp 0x314a
movq -0x250(%rbp), %rsi
movq -0x270(%rbp), %rdi
callq 0x2140
movq %rax, -0x278(%rbp)
jmp 0x3166
movq -0x278(%rbp), %rdi
movq 0x9e4c(%rip), %rsi      # 0xcfc0
callq 0x2190
jmp 0x317b
movl $0xfffffffc, -0x4(%rbp) # imm = 0xFFFFFFFC
movl $0x1, -0x240(%rbp)
jmp 0x3384
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x238(%rbp)
movl %eax, -0x23c(%rbp)
leaq -0x230(%rbp), %rdi
callq 0x2060
jmp 0x339c
movq -0x250(%rbp), %rsi
movq -0x248(%rbp), %rdi
addq $0x8, %rdi
callq 0x2040
jmp 0x31cd
movq -0x248(%rbp), %rdi
leaq -0x230(%rbp), %rsi
callq 0x33b0
movl %eax, -0x27c(%rbp)
jmp 0x31e8
movl -0x27c(%rbp), %eax
cmpl $0x0, %eax
jl 0x321f
movq -0x230(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x230(%rbp,%rax), %rdi
callq 0x22a0
movb %al, -0x27d(%rbp)
jmp 0x3213
movb -0x27d(%rbp), %al
testb $0x1, %al
jne 0x3266
jmp 0x321f
movq 0x9dca(%rip), %rdi      # 0xcff0
leaq 0x5f00(%rip), %rsi      # 0x912d
callq 0x2160
movq %rax, -0x288(%rbp)
jmp 0x323b
movq -0x288(%rbp), %rdi
movq 0x9d77(%rip), %rsi      # 0xcfc0
callq 0x2190
jmp 0x3250
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x240(%rbp)
jmp 0x3384
movq -0x248(%rbp), %rdi
leaq -0x230(%rbp), %rsi
callq 0x39f0
movl %eax, -0x28c(%rbp)
jmp 0x3281
movl -0x28c(%rbp), %eax
cmpl $0x0, %eax
jl 0x32b8
movq -0x230(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x230(%rbp,%rax), %rdi
callq 0x22a0
movb %al, -0x28d(%rbp)
jmp 0x32ac
movb -0x28d(%rbp), %al
testb $0x1, %al
jne 0x32ff
jmp 0x32b8
movq 0x9d31(%rip), %rdi      # 0xcff0
leaq 0x5e93(%rip), %rsi      # 0x9159
callq 0x2160
movq %rax, -0x298(%rbp)
jmp 0x32d4
movq -0x298(%rbp), %rdi
movq 0x9cde(%rip), %rsi      # 0xcfc0
callq 0x2190
jmp 0x32e9
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
movl $0x1, -0x240(%rbp)
jmp 0x3384
movq -0x248(%rbp), %rdi
leaq -0x230(%rbp), %rsi
callq 0x3d50
movl %eax, -0x29c(%rbp)
jmp 0x331a
movl -0x29c(%rbp), %eax
cmpl $0x0, %eax
jge 0x3369
movq 0x9cc4(%rip), %rdi      # 0xcff0
leaq 0x5e57(%rip), %rsi      # 0x918a
callq 0x2160
movq %rax, -0x2a8(%rbp)
jmp 0x3341
movq -0x2a8(%rbp), %rdi
movq 0x9c71(%rip), %rsi      # 0xcfc0
callq 0x2190
jmp 0x3356
movl $0xfffffffd, -0x4(%rbp) # imm = 0xFFFFFFFD
movl $0x1, -0x240(%rbp)
jmp 0x3384
movq -0x248(%rbp), %rax
movb $0x1, (%rax)
movl $0x0, -0x4(%rbp)
movl $0x1, -0x240(%rbp)
leaq -0x230(%rbp), %rdi
callq 0x2060
movl -0x4(%rbp), %eax
addq $0x2b0, %rsp            # imm = 0x2B0
popq %rbp
retq
movq -0x238(%rbp), %rdi
callq 0x22d0
nopl (%rax,%rax)