// File: /workspace/github_cpp_makefile_star2_O1/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 0x2e8d
cmpl $0x2, %edi
je 0x28e1
movq 0x10(%rbx), %rdi
callq 0x267f
movq 0x8(%rbx), %rsi
leaq 0xe8(%rsp), %rdi
leaq 0x178(%rsp), %rdx
callq 0x2ff8
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 0x3070
leaq 0x178(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0x3840
movq 0xc8(%rsp), %rdi
cmpq %r14, %rdi
je 0x297c
movq 0xd8(%rsp), %rsi
incq %rsi
callq 0x21e0
leaq 0x160(%rsp), %rdi
callq 0x343c
leaq 0x178(%rsp), %rdi
callq 0x39be
shrq $0x5, %rax
leaq 0x138(%rsp), %rdi
movl %eax, %esi
callq 0x3450
leaq 0x18(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x1715(%rip), %rdx      # 0x40ce
leaq 0x8(%rsp), %rdi
movq %rdx, %rsi
callq 0x3100
leaq 0x178(%rsp), %rbx
movb $0x0, (%rbx)
leaq -0x52c(%rip), %rsi      # 0x24ac
movl $0x2, %edi
callq 0x2210
xorl %ebp, %ebp
leaq 0x38(%rsp), %r13
leaq 0x138(%rsp), %r14
leaq 0x28(%rsp), %r15
leaq 0x8(%rsp), %r12
cmpb $0x0, 0x178(%rsp)
jne 0x2a3d
movq %r15, %rdi
movq %rbx, %rsi
callq 0x38b4
movq %r12, %rdi
movq %r15, %rsi
callq 0x2230
movq 0x28(%rsp), %rdi
cmpq %r13, %rdi
je 0x2a32
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21e0
movq %r14, %rdi
movq %r12, %rsi
callq 0x34c2
leal 0x1(%rbp), %eax
testl %ebp, %ebp
movl %eax, %ebp
je 0x29fb
cmpl $0x1, 0x138(%rsp)
jbe 0x2a83
movq %r14, %rdi
callq 0x35a6
cmpb $0x1, 0x178(%rsp)
movl $0x0, %ebp
jne 0x29fb
cmpl $0x2, 0x138(%rsp)
movl $0x0, %ebp
jb 0x29fb
leaq 0x138(%rsp), %rdi
callq 0x369c
leaq 0x178(%rsp), %rdi
callq 0x3892
leaq 0x28(%rsp), %rdi
leaq 0x138(%rsp), %rsi
callq 0x3786
leaq 0x138(%rsp), %rdi
callq 0x37c6
movb $0x1, 0x36fd(%rip)      # 0x61b3
leaq 0x68(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
xorl %r14d, %r14d
testb %r14b, %r14b
jne 0x2c97
leaq 0x30(%rsp), %r12
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
leaq 0xb8(%rsp), %r13
xorl %r14d, %r14d
leaq 0x108(%rsp), %rbp
leaq 0xa8(%rsp), %rbx
callq 0x2060
movq %rax, %r15
movzwl 0x28(%rsp), %eax
movw %ax, 0x78(%rsp)
leaq 0x90(%rsp), %rax
movq %rax, 0x80(%rsp)
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
addq %rsi, %rdx
leaq 0x80(%rsp), %rdi
callq 0x3070
movb 0x50(%rsp), %al
movb %al, 0xa0(%rsp)
movq %rbp, %rdi
leaq 0x160(%rsp), %rsi
leaq 0x78(%rsp), %rdx
callq 0x3352
movzwl 0x108(%rsp), %eax
movw %ax, 0x28(%rsp)
movq %r12, %rdi
leaq 0x110(%rsp), %rsi
callq 0x2230
movb 0x130(%rsp), %al
movb %al, 0x50(%rsp)
movq 0x110(%rsp), %rdi
leaq 0x120(%rsp), %rax
cmpq %rax, %rdi
je 0x2ba8
movq 0x120(%rsp), %rsi
incq %rsi
callq 0x21e0
movq 0x80(%rsp), %rdi
leaq 0x90(%rsp), %rax
cmpq %rax, %rdi
je 0x2bcd
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 0x58(%rsp), %rdi
movq %rbp, %rsi
callq 0x2230
movq 0x108(%rsp), %rdi
leaq 0x118(%rsp), %rax
cmpq %rax, %rdi
je 0x2c22
movq 0x118(%rsp), %rsi
incq %rsi
callq 0x21e0
movq %r13, 0xa8(%rsp)
movq 0x58(%rsp), %rsi
movq 0x60(%rsp), %rdx
addq %rsi, %rdx
movq %rbx, %rdi
callq 0x3070
movq %rbx, %rdi
callq 0x25c1
movq 0xa8(%rsp), %rdi
cmpq %r13, %rdi
je 0x2c64
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x21e0
movq %r15, %rax
shrq $0x3f, %rax
sarq $0x7, %r15
addq %rax, %r15
cvtsi2sd %r15, %xmm0
movsd (%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
incq %r14
cmpb $0x0, 0x3524(%rip)      # 0x61b3
jne 0x2aff
jmp 0x2ca0
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
movq 0x3331(%rip), %rdi      # 0x5fd8
movq (%rdi), %rax
addq -0x18(%rax), %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq 0x3316(%rip), %rdi      # 0x5fd8
callq 0x2040
movq %rax, %rdi
callq 0x2120
movq %rax, %rbx
leaq 0x140b(%rip), %rsi      # 0x40e4
movl $0x17, %edx
movq %rax, %rdi
callq 0x2200
movsd (%rsp), %xmm0
divsd 0x132d(%rip), %xmm0     # 0x4020
movq %rbx, %rdi
callq 0x22c0
movq %rax, %rbx
leaq 0x13f7(%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 0x3298(%rip), %rdi      # 0x5fd8
leaq 0x13b8(%rip), %rsi      # 0x40ff
movl $0x7, %edx
callq 0x2200
movq 0x3280(%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 0x3247(%rip), %rbx      # 0x5fd8
leaq 0x136f(%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 0x3215(%rip), %rdi      # 0x5fd8
callq 0x2040
movq %rax, %rdi
callq 0x2120
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
leaq 0x18(%rsp), %rbx
je 0x2df1
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x21e0
leaq 0x40(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2e0c
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x21e0
movq 0x8(%rsp), %rdi
cmpq %rbx, %rdi
je 0x2e23
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x21e0
movq 0x390(%rsp), %rdi
leaq 0x3a0(%rsp), %rax
cmpq %rax, %rdi
leaq 0x188(%rsp), %rbx
je 0x2e50
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 0x2e79
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 0x3144(%rip), %rdi      # 0x5fd8
leaq 0x122c(%rip), %rsi      # 0x40c7
callq 0x21c0
movq %rax, %rdi
callq 0x20e0
movq (%rbx), %rsi
movq %rax, %rdi
callq 0x21c0
leaq 0x1215(%rip), %rsi      # 0x40cf
movq %rax, %rdi
callq 0x21c0
movq %rax, %rdi
callq 0x20e0
xorl %edi, %edi
callq 0x2220
movq %rax, %rbx
jmp 0x2f8b
jmp 0x2f1a
jmp 0x2f04
jmp 0x2f04
jmp 0x2f04
jmp 0x2f04
movq %rax, %rbx
movq 0xc8(%rsp), %rdi
cmpq %r14, %rdi
je 0x2fc2
movq 0xd8(%rsp), %rsi
jmp 0x2fba
movq %rax, %rbx
jmp 0x2fc2
movq %rax, %rbx
jmp 0x2fcf
jmp 0x2f1a
jmp 0x2f1a
jmp 0x2f1a
movq %rax, %rbx
jmp 0x2fa6
jmp 0x2f6c
movq %rax, %rbx
movq 0x80(%rsp), %rdi
leaq 0x90(%rsp), %rax
cmpq %rax, %rdi
je 0x2f6f
movq 0x90(%rsp), %rsi
jmp 0x2f62
jmp 0x2f6c
jmp 0x2f6c
movq %rax, %rbx
movq 0xa8(%rsp), %rdi
cmpq %r13, %rdi
je 0x2f6f
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x21e0
jmp 0x2f6f
movq %rax, %rbx
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0x2f8b
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x21e0
leaq 0x40(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2fa6
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x21e0
leaq 0x18(%rsp), %rax
movq 0x8(%rsp), %rdi
cmpq %rax, %rdi
je 0x2fc2
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x21e0
leaq 0x178(%rsp), %rdi
callq 0x3040
leaq 0xf8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2ff0
movq 0xf8(%rsp), %rsi
incq %rsi
callq 0x21e0
movq %rbx, %rdi
callq 0x2300