// File: /workspace/github_cpp_makefile_star2_O2/m0Mh42[P]mash/./src/main.cpp
// Function: main
int main(int argc, char* argv[]) {

    // Filename is a must
    if (argc < 2){
        cout << "Usage: " << endl << argv[0] << " [file] [difficulty]" << endl;
        exit(NOFILE);
    }

    // Checking difficulty declaration
    if (argc > 2){
        difficulty_set(argv[2]);
    }

    string filename = argv[1];
    
    // File object
    File file;
    file.open_file(filename);

    // Mashing Object
    Mash mash;

    // Random seed
    mash.rand_seed();

    // Calculating chunk size.
    unsigned long len = file.get_file_length();
    unsigned long chunksize = len / 32;

    // Mashtree Object
    Mashtree mashtree(chunksize);
    string chunk = "";
    file.is_eof = false;

    signal(SIGINT, intsig);

    while (true){
        for (uint l = 0; l < 2; l++){
            if (!file.is_eof){
                chunk = file.read_chunk();
                mashtree.add_node(chunk);
            }
        }
        if (mashtree.tree_size > 1)
            mashtree.mash_k_matching_nodes();
        else
            break;
        if (file.is_eof && mashtree.tree_size > 1){
            mashtree.mash_remaining();
            break;
        }
    }

    // we don't need the file anymore, so we close it.
    file.close_file();

    // We get the resulted mash node, so we can mash it again, without using the nodetree.
    MashNode mashnode = mashtree.return_result();

    // And then we delete the nodetree to free memory.
    mashtree.delete_nodetree();

    // cout << endl << "Size of mashnode: " << sizeof(MashNode) << endl;
    // cout << "Size of mashtree: " << (sizeof(MashNode) * (chunksize)) << endl;
    // cout << "Size of mash class: " << sizeof(Mash) << endl;
    // cout << "Size of Mashtree class: " << sizeof(Mashtree) << endl;

    // Some initializations
    double microsecs = 0;
    run = true;
    string out;
    u_long count = 0;

    while (run) {
        // Measuring time taken to mash
        auto t1 = high_resolution_clock::now();

        mashnode = mash.mash(mashnode);

        auto t2 = high_resolution_clock::now();
        auto duration = duration_cast<microseconds>(t2 - t1);
        microsecs += duration.count();

        out = string_to_hex(mashnode.value);
        check_output(out);

        count++;
    }

    cout << endl << "Time taken by Mashing: " << (microsecs / 1000) << "ms" << endl;
    cout << "Count: " << count << endl;
    cout << "Exiting..." << endl;

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3b8, %rsp            # imm = 0x3B8
movq %rsi, %rbx
cmpl $0x1, %edi
jle 0x2a8b
cmpl $0x2, %edi
je 0x2742
movq 0x10(%rbx), %rdi
callq 0x25d6
movq 0x8(%rbx), %rsi
leaq 0xc0(%rsp), %rdi
leaq 0x178(%rsp), %rdx
callq 0x2b88
leaq 0x178(%rsp), %rdi
callq 0x2bce
leaq 0xa0(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x2250
leaq 0x178(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x3344
leaq 0xa0(%rsp), %rdi
callq 0x20d0
leaq 0xe0(%rsp), %rdi
callq 0x3020
leaq 0x178(%rsp), %rdi
callq 0x349e
shrq $0x5, %rax
leaq 0x8(%rsp), %rdi
movl %eax, %esi
callq 0x3034
leaq 0x18fe(%rip), %rsi      # 0x40ce
leaq 0x80(%rsp), %rdi
leaq 0x30(%rsp), %rdx
callq 0x2b88
leaq 0x178(%rsp), %rbx
movb $0x0, (%rbx)
leaq -0x348(%rip), %rsi      # 0x24ac
pushq $0x2
popq %rdi
callq 0x2200
xorl %ebp, %ebp
leaq 0x8(%rsp), %r14
leaq 0x30(%rsp), %r15
leaq 0x80(%rsp), %r12
cmpl $0x2, %ebp
jne 0x283c
cmpl $0x1, 0x8(%rsp)
jbe 0x287d
movq %r14, %rdi
callq 0x3132
cmpb $0x1, 0x178(%rsp)
movl $0x0, %ebp
jne 0x2810
cmpl $0x2, 0x8(%rsp)
jb 0x2810
jmp 0x2873
cmpb $0x0, 0x178(%rsp)
jne 0x286f
movq %r15, %rdi
movq %rbx, %rsi
callq 0x33b6
movq %r12, %rdi
movq %r15, %rsi
callq 0x2220
movq %r15, %rdi
callq 0x20d0
movq %r14, %rdi
movq %r12, %rsi
callq 0x3092
incl %ebp
jmp 0x2810
leaq 0x8(%rsp), %rdi
callq 0x31e0
leaq 0x178(%rsp), %rdi
callq 0x3394
leaq 0x30(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x3278
leaq 0x8(%rsp), %rdi
callq 0x328a
leaq 0x70(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x1, 0x38fb(%rip)      # 0x61b3
movb $0x0, (%rax)
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
movb $0x1, %al
xorl %r14d, %r14d
leaq 0x118(%rsp), %rbp
leaq 0x148(%rsp), %rbx
leaq 0xf8(%rsp), %r15
testb $0x1, %al
je 0x29b4
callq 0x2050
movq %rax, %r13
movq %rbp, %rdi
leaq 0x30(%rsp), %rsi
callq 0x2bfa
movq %rbx, %rdi
leaq 0xe0(%rsp), %rsi
movq %rbp, %r12
movq %rbp, %rdx
callq 0x2f74
leaq 0x30(%rsp), %rdi
movq %rbx, %rsi
callq 0x2c26
leaq 0x150(%rsp), %rdi
callq 0x20d0
leaq 0x120(%rsp), %rdi
callq 0x20d0
callq 0x2050
subq %r13, %rax
cqto
movl $0x3e8, %ecx            # imm = 0x3E8
idivq %rcx
movq %rax, %r13
movq %rbx, %rdi
leaq 0x38(%rsp), %rsi
callq 0x24dd
leaq 0x60(%rsp), %rbp
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x2220
movq %rbx, %rdi
callq 0x20d0
movq %r15, %rdi
movq %rbp, %rsi
callq 0x2250
movq %r15, %rdi
callq 0x2574
cvtsi2sd %r13, %xmm0
movsd (%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
movq %r15, %rdi
callq 0x20d0
incq %r14
movb 0x3807(%rip), %al       # 0x61b3
movq %r12, %rbp
jmp 0x28e1
movq 0x3615(%rip), %rdi      # 0x5fd0
callq 0x20e0
leaq 0x171d(%rip), %rsi      # 0x40e4
movq %rax, %rdi
callq 0x21b0
movsd (%rsp), %xmm0
divsd 0x1644(%rip), %xmm0     # 0x4020
movq %rax, %rdi
callq 0x22b0
leaq 0x1711(%rip), %rsi      # 0x40fc
movq %rax, %rdi
callq 0x21b0
movq %rax, %rdi
callq 0x20e0
movq 0x35ce(%rip), %rdi      # 0x5fd0
leaq 0x16f6(%rip), %rsi      # 0x40ff
callq 0x21b0
movq %rax, %rdi
movq %r14, %rsi
callq 0x2110
movq %rax, %rdi
callq 0x20e0
movq 0x35a8(%rip), %rdi      # 0x5fd0
leaq 0x16d8(%rip), %rsi      # 0x4107
callq 0x21b0
movq %rax, %rdi
callq 0x20e0
leaq 0x60(%rsp), %rdi
callq 0x20d0
leaq 0x38(%rsp), %rdi
callq 0x20d0
leaq 0x80(%rsp), %rdi
callq 0x20d0
leaq 0x178(%rsp), %rdi
callq 0x2c56
leaq 0xc0(%rsp), %rdi
callq 0x20d0
xorl %eax, %eax
addq $0x3b8, %rsp            # imm = 0x3B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x353e(%rip), %rdi      # 0x5fd0
leaq 0x162e(%rip), %rsi      # 0x40c7
callq 0x21b0
movq %rax, %rdi
callq 0x20e0
movq (%rbx), %rsi
movq %rax, %rdi
callq 0x21b0
leaq 0x1617(%rip), %rsi      # 0x40cf
movq %rax, %rdi
callq 0x21b0
movq %rax, %rdi
callq 0x20e0
xorl %edi, %edi
callq 0x2210
movq %rax, %rbx
jmp 0x2b32
jmp 0x2afd
jmp 0x2aeb
jmp 0x2aeb
jmp 0x2aeb
jmp 0x2aeb
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
jmp 0x2b44
movq %rax, %rbx
jmp 0x2b49
movq %rax, %rbx
jmp 0x2b56
jmp 0x2afd
jmp 0x2afd
jmp 0x2afd
jmp 0x2b25
movq %rax, %rbx
jmp 0x2b3c
movq %rax, %rbx
leaq 0xf8(%rsp), %rdi
jmp 0x2b1c
jmp 0x2b25
movq %rax, %rbx
leaq 0x120(%rsp), %rdi
callq 0x20d0
jmp 0x2b28
jmp 0x2b25
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x20d0
leaq 0x38(%rsp), %rdi
callq 0x20d0
leaq 0x80(%rsp), %rdi
callq 0x20d0
leaq 0x178(%rsp), %rdi
callq 0x2c56
leaq 0xc0(%rsp), %rdi
callq 0x20d0
movq %rbx, %rdi
callq 0x22f0