// File: /workspace/github_cpp_makefile_star2_O3/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 0x2e5a
cmpl $0x2, %edi
je 0x28cc
movq 0x10(%rbx), %rdi
callq 0x2669
movq 0x8(%rbx), %rsi
leaq 0xe8(%rsp), %rdi
leaq 0x178(%rsp), %rdx
callq 0x2fce
leaq 0x188(%rsp), %rdi
callq 0x21f0
leaq 0x3a0(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0xd8(%rsp), %r14
movq %r14, -0x10(%r14)
movq 0xe8(%rsp), %rsi
movq 0xf0(%rsp), %rdx
addq %rsi, %rdx
leaq 0xc8(%rsp), %rdi
callq 0x3046
leaq 0x178(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0x384c
movq 0xc8(%rsp), %rdi
cmpq %r14, %rdi
je 0x2967
movq 0xd8(%rsp), %rsi
incq %rsi
callq 0x21e0
leaq 0x160(%rsp), %rdi
callq 0x3446
leaq 0x178(%rsp), %rdi
callq 0x39c6
shrq $0x5, %rax
leaq 0x138(%rsp), %rdi
movl %eax, %esi
callq 0x345a
leaq 0x10(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x172a(%rip), %rdx      # 0x40ce
movq %rsp, %rdi
movq %rdx, %rsi
callq 0x30dc
leaq 0x178(%rsp), %rbx
movb $0x0, (%rbx)
leaq -0x515(%rip), %rsi      # 0x24ac
movl $0x2, %edi
callq 0x2210
movb $0x1, %bpl
leaq 0x58(%rsp), %r13
leaq 0x138(%rsp), %r14
leaq 0x48(%rsp), %r15
movq %rsp, %r12
cmpb $0x0, 0x178(%rsp)
jne 0x2a25
movq %r15, %rdi
movq %rbx, %rsi
callq 0x38c0
movq %r12, %rdi
movq %r15, %rsi
callq 0x2230
movq 0x48(%rsp), %rdi
cmpq %r13, %rdi
je 0x2a1a
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x21e0
movq %r14, %rdi
movq %r12, %rsi
callq 0x34cc
testb $0x1, %bpl
jne 0x2a51
cmpl $0x1, 0x138(%rsp)
jbe 0x2a64
movq %r14, %rdi
callq 0x35ac
cmpb $0x1, 0x178(%rsp)
jne 0x2a51
cmpl $0x2, 0x138(%rsp)
jae 0x2a57
xorb $0x1, %bpl
jmp 0x29e3
leaq 0x138(%rsp), %rdi
callq 0x36a6
leaq 0x178(%rsp), %rdi
callq 0x389e
leaq 0x48(%rsp), %rdi
leaq 0x138(%rsp), %rsi
callq 0x3792
leaq 0x138(%rsp), %rdi
callq 0x37d2
movb $0x1, 0x371c(%rip)      # 0x61b3
leaq 0x38(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x50(%rsp), %r12
xorpd %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
leaq 0xb8(%rsp), %r13
xorl %r14d, %r14d
leaq 0x108(%rsp), %rbp
leaq 0xa8(%rsp), %rbx
callq 0x2060
movq %rax, %r15
movzwl 0x48(%rsp), %eax
movw %ax, 0x78(%rsp)
leaq 0x90(%rsp), %rax
movq %rax, 0x80(%rsp)
movq 0x50(%rsp), %rsi
movq 0x58(%rsp), %rdx
addq %rsi, %rdx
leaq 0x80(%rsp), %rdi
callq 0x3046
movb 0x70(%rsp), %al
movb %al, 0xa0(%rsp)
movq %rbp, %rdi
leaq 0x160(%rsp), %rsi
leaq 0x78(%rsp), %rdx
callq 0x335c
movzwl 0x108(%rsp), %eax
movw %ax, 0x48(%rsp)
movq %r12, %rdi
leaq 0x110(%rsp), %rsi
callq 0x2230
movb 0x130(%rsp), %al
movb %al, 0x70(%rsp)
movq 0x110(%rsp), %rdi
leaq 0x120(%rsp), %rax
cmpq %rax, %rdi
je 0x2b7e
movq 0x120(%rsp), %rsi
incq %rsi
callq 0x21e0
movq 0x80(%rsp), %rdi
leaq 0x90(%rsp), %rax
cmpq %rax, %rdi
je 0x2ba3
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x21e0
callq 0x2060
subq %r15, %rax
movabsq $0x20c49ba5e353f7cf, %rcx # imm = 0x20C49BA5E353F7CF
imulq %rcx
movq %rdx, %r15
movq %rbp, %rdi
movq %r12, %rsi
callq 0x2510
leaq 0x28(%rsp), %rdi
movq %rbp, %rsi
callq 0x2230
movq 0x108(%rsp), %rdi
leaq 0x118(%rsp), %rax
cmpq %rax, %rdi
je 0x2bf8
movq 0x118(%rsp), %rsi
incq %rsi
callq 0x21e0
movq %r13, 0xa8(%rsp)
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
addq %rsi, %rdx
movq %rbx, %rdi
callq 0x3046
movq %rbx, %rdi
callq 0x25c1
movq 0xa8(%rsp), %rdi
cmpq %r13, %rdi
je 0x2c3a
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x21e0
movq %r15, %rax
shrq $0x3f, %rax
sarq $0x7, %r15
addq %rax, %r15
cvtsi2sd %r15, %xmm0
movsd 0x20(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x20(%rsp)
incq %r14
cmpb $0x0, 0x354c(%rip)      # 0x61b3
jne 0x2ad5
movq 0x3364(%rip), %rdi      # 0x5fd8
movq (%rdi), %rax
addq -0x18(%rax), %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq 0x3349(%rip), %rdi      # 0x5fd8
callq 0x2040
movq %rax, %rdi
callq 0x2120
movq %rax, %rbx
leaq 0x143e(%rip), %rsi      # 0x40e4
movl $0x17, %edx
movq %rax, %rdi
callq 0x2200
movsd 0x20(%rsp), %xmm0
divsd 0x135f(%rip), %xmm0     # 0x4020
movq %rbx, %rdi
callq 0x22c0
movq %rax, %rbx
leaq 0x1429(%rip), %rsi      # 0x40fc
movl $0x2, %edx
movq %rax, %rdi
callq 0x2200
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2120
movq 0x32ca(%rip), %rdi      # 0x5fd8
leaq 0x13ea(%rip), %rsi      # 0x40ff
movl $0x7, %edx
callq 0x2200
movq 0x32b2(%rip), %rdi      # 0x5fd8
movq %r14, %rsi
callq 0x2110
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2120
movq 0x3279(%rip), %rbx      # 0x5fd8
leaq 0x13a1(%rip), %rsi      # 0x4107
movl $0xa, %edx
movq %rbx, %rdi
callq 0x2200
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq 0x3247(%rip), %rdi      # 0x5fd8
callq 0x2040
movq %rax, %rdi
callq 0x2120
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
leaq 0x10(%rsp), %rbx
je 0x2dbf
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21e0
leaq 0x60(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2dda
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x21e0
movq (%rsp), %rdi
cmpq %rbx, %rdi
je 0x2df0
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x21e0
movq 0x390(%rsp), %rdi
leaq 0x3a0(%rsp), %rax
cmpq %rax, %rdi
leaq 0x188(%rsp), %rbx
je 0x2e1d
movq 0x3a0(%rsp), %rsi
incq %rsi
callq 0x21e0
movq %rbx, %rdi
callq 0x2090
leaq 0xf8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2e46
movq 0xf8(%rsp), %rsi
incq %rsi
callq 0x21e0
xorl %eax, %eax
addq $0x3b8, %rsp            # imm = 0x3B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x3177(%rip), %rdi      # 0x5fd8
leaq 0x125f(%rip), %rsi      # 0x40c7
callq 0x21c0
movq %rax, %rdi
callq 0x20e0
movq (%rbx), %rsi
movq %rax, %rdi
callq 0x21c0
leaq 0x1248(%rip), %rsi      # 0x40cf
movq %rax, %rdi
callq 0x21c0
movq %rax, %rdi
callq 0x20e0
xorl %edi, %edi
callq 0x2220
movq %rax, %rbx
leaq 0x10(%rsp), %r14
jmp 0x2f67
jmp 0x2eec
jmp 0x2ed6
jmp 0x2ed6
jmp 0x2ed6
jmp 0x2ed6
movq %rax, %rbx
movq 0xc8(%rsp), %rdi
cmpq %r14, %rdi
je 0x2f98
movq 0xd8(%rsp), %rsi
jmp 0x2f90
movq %rax, %rbx
jmp 0x2f98
movq %rax, %rbx
jmp 0x2fa5
jmp 0x2eec
jmp 0x2eec
jmp 0x2eec
movq %rax, %rbx
leaq 0x10(%rsp), %r14
jmp 0x2f82
jmp 0x2f21
jmp 0x2f21
movq %rax, %rbx
movq 0x80(%rsp), %rdi
leaq 0x90(%rsp), %rax
cmpq %rax, %rdi
je 0x2f46
movq 0x90(%rsp), %rsi
jmp 0x2f3e
jmp 0x2f21
movq %rax, %rbx
jmp 0x2f46
movq %rax, %rbx
movq 0xa8(%rsp), %rdi
cmpq %r13, %rdi
je 0x2f46
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x21e0
leaq 0x10(%rsp), %r14
leaq 0x38(%rsp), %rax
movq 0x28(%rsp), %rdi
cmpq %rax, %rdi
je 0x2f67
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21e0
leaq 0x60(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2f82
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x21e0
movq (%rsp), %rdi
cmpq %r14, %rdi
je 0x2f98
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x21e0
leaq 0x178(%rsp), %rdi
callq 0x3016
leaq 0xf8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2fc6
movq 0xf8(%rsp), %rsi
incq %rsi
callq 0x21e0
movq %rbx, %rdi
callq 0x2300