// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x208, %rsp            # imm = 0x208
movq %rsi, %r14
movq %rdi, %rbx
movq (%rsi), %rdi
leaq 0x26f6(%rip), %rsi      # 0x5130
callq 0x2170
movq %rax, %r15
movq %rax, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x2150
movq %r15, %rdi
callq 0x2030
movq %rax, %r12
movq %r15, %rdi
callq 0x2130
movq 0x5565(%rip), %r15      # 0x7fd0
leaq 0x26c1(%rip), %rsi      # 0x5133
movl $0x16, %edx
movq %r15, %rdi
callq 0x2160
movq %r15, %rdi
movl %r12d, %esi
callq 0x2200
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x2120
movsbl %al, %esi
movq %r13, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x20d0
cmpl $0xbb9, %r12d           # imm = 0xBB9
jl 0x2b02
leaq 0x2686(%rip), %rsi      # 0x514a
movl $0x15, %edx
movq %r15, %rdi
callq 0x2160
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x2120
movsbl %al, %esi
movq %r15, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x20d0
movl $0xfffffffc, %ebx       # imm = 0xFFFFFFFC
jmp 0x2c18
movb $0x0, (%rbx)
leaq -0x230(%rbp), %rdi
movq %r14, %rsi
movl $0x8, %edx
callq 0x21e0
leaq -0x1b8(%rbp), %rdi
callq 0x2060
testb %al, %al
je 0x2bb2
leaq 0x8(%rbx), %rdi
movq %r14, %rsi
callq 0x20b0
leaq -0x230(%rbp), %rsi
movq %rbx, %rdi
callq 0x2d06
testl %eax, %eax
js 0x2b65
movq -0x230(%rbp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, -0x210(%rbp,%rax)
je 0x2c2c
movq 0x5484(%rip), %rbx      # 0x7ff0
leaq 0x2603(%rip), %rsi      # 0x5176
movl $0x2b, %edx
movq %rbx, %rdi
callq 0x2160
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2120
movsbl %al, %esi
movq 0x5452(%rip), %rdi      # 0x7ff0
callq 0x2040
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq %rax, %rdi
callq 0x20d0
jmp 0x2c0c
movq 0x5417(%rip), %rdi      # 0x7fd0
leaq 0x25a0(%rip), %rsi      # 0x5160
movl $0x15, %edx
callq 0x2160
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq 0x53f8(%rip), %rdi      # 0x7fd0
callq 0x2160
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2120
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movl $0xfffffffc, %ebx       # imm = 0xFFFFFFFC
movq %rax, %rdi
callq 0x20d0
leaq -0x230(%rbp), %rdi
callq 0x2070
movl %ebx, %eax
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq -0x230(%rbp), %rsi
movq %rbx, %rdi
callq 0x319a
testl %eax, %eax
js 0x2c54
movq -0x230(%rbp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, -0x210(%rbp,%rax)
je 0x2ca4
movq 0x5395(%rip), %rbx      # 0x7ff0
leaq 0x2540(%rip), %rsi      # 0x51a2
movl $0x30, %edx
movq %rbx, %rdi
callq 0x2160
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2120
movsbl %al, %esi
movq 0x5363(%rip), %rdi      # 0x7ff0
callq 0x2040
movl $0xfffffffe, %ebx       # imm = 0xFFFFFFFE
movq %rax, %rdi
callq 0x20d0
jmp 0x2c0c
leaq -0x230(%rbp), %rsi
movq %rbx, %rdi
callq 0x3418
testl %eax, %eax
js 0x2cc1
movb $0x1, (%rbx)
xorl %ebx, %ebx
jmp 0x2c0c
movq 0x5328(%rip), %rdi      # 0x7ff0
leaq 0x2504(%rip), %rsi      # 0x51d3
movl $0x31, %edx
callq 0x2160
movl $0xfffffffd, %ebx       # imm = 0xFFFFFFFD
movq 0x530b(%rip), %rdi      # 0x7ff0
callq 0x2090
jmp 0x2c0c
movq %rax, %rbx
leaq -0x230(%rbp), %rdi
callq 0x2070
movq %rbx, %rdi
callq 0x2220