// File: /workspace/github_cpp_makefile_star2_O1/ethanmick[P]Blowfish482/Crypter.cpp
// Function: main
int main( int argc, char* argv[] ){
    Blowfish b;

    if (argc <= 4) {
        cout << "usage: Crypter <input-file> <output-file> <key>" << endl;
        return 0;
    }

    /*string str(argv[1]);
    istringstream is(str);
    uint32_t numIter = 0;
    is >> numIter;*/

    uint8_t* key = new uint8_t[ b.keySize() ];
    
    for( unsigned int i = 0; i < b.keySize(); i++ ){
        char* p;
        key[ i ] = (uint8_t)strtol( argv[ 3 + i ], &p, 16);
        if (*p != 0) {
            cout << "ADJUDICATE!" << endl;
        }
    }
    
    print_uint8_hex(key, b.keySize(), "Key: ");
    b.setKey( key );

    
    FILE* input = fopen(argv[1], "r");
    FILE* output = fopen(argv[2], "w+");

    fseek(input, 0, SEEK_END);
    long size = ftell(input);
    rewind(input);

    uint8_t* text;
    time_t start = time(NULL) * 1000;
    for (int i = 0; i < size; i+= b.blockSize()) {
        text = new uint8_t[ b.blockSize() ];

        fseek(input, i, SEEK_SET);
        size_t readin = fread(text, b.blockSize(), 1, input); 

        long s = ftell(input);

        size_t delta = b.blockSize() - readin; 
        if (delta != 0) {
            for (unsigned int j = readin; j < b.blockSize(); j++) {
                text[j] = 0x00;
            }
        }
    
        b.encrypt( text );

        fseek(output, i, SEEK_SET);
        fwrite(text, b.blockSize(), 1, output);

        delete[] text;
    }

    fclose(input);
    fclose(output);
    time_t end = time(NULL) * 1000;

    cout << "Running Time: " << (end - start) << " msec" << endl;
    
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1078, %rsp           # imm = 0x1078
movq %rsi, %r14
movl %edi, %ebx
leaq 0x20(%rsp), %rdi
callq 0x12fc
cmpl $0x4, %ebx
jg 0x1a8c
movq 0x2560(%rip), %rbx      # 0x3fd8
leaq 0x5cc(%rip), %rsi       # 0x204b
movl $0x2f, %edx
movq %rbx, %rdi
jmp 0x1cf8
leaq 0x20(%rsp), %r15
movq %r15, %rdi
callq 0x1314
movl %eax, %edi
callq 0x1040
movq %rax, %rbx
movq %r15, %rdi
callq 0x1314
testl %eax, %eax
je 0x1b35
xorl %ebp, %ebp
leaq 0x18(%rsp), %r15
leaq 0x20(%rsp), %r12
movq 0x2512(%rip), %r13      # 0x3fd8
leal 0x3(%rbp), %eax
movq (%r14,%rax,8), %rdi
movq %r15, %rsi
movl $0x10, %edx
callq 0x1150
movb %al, (%rbx,%rbp)
movq 0x18(%rsp), %rax
cmpb $0x0, (%rax)
je 0x1b23
movl $0xb, %edx
movq %r13, %rdi
leaq 0x585(%rip), %rsi       # 0x207b
callq 0x1100
movq (%r13), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x10d0
movsbl %al, %esi
movq %r13, %rdi
callq 0x1050
movq %rax, %rdi
callq 0x1080
incq %rbp
movq %r12, %rdi
callq 0x1314
movl %eax, %eax
cmpq %rax, %rbp
jb 0x1ac6
leaq 0x20(%rsp), %r15
movq %r15, %rdi
callq 0x1314
leaq 0x53e(%rip), %rdx       # 0x2087
movq %rbx, %rdi
movl %eax, %esi
callq 0x19f4
movq %r15, %rdi
movq %rbx, %rsi
callq 0x131a
movq 0x8(%r14), %rdi
leaq 0x524(%rip), %rsi       # 0x208d
callq 0x1110
movq %rax, %rbx
movq 0x10(%r14), %rdi
leaq 0x513(%rip), %rsi       # 0x208f
callq 0x1110
movq %rax, (%rsp)
xorl %r14d, %r14d
movq %rbx, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x10f0
movq %rbx, %rdi
callq 0x1030
movq %rax, %r15
movq %rbx, %rdi
callq 0x1070
xorl %edi, %edi
callq 0x10b0
movq %rax, 0x8(%rsp)
movq %r15, 0x10(%rsp)
testq %r15, %r15
jle 0x1c9c
leaq 0x20(%rsp), %r13
xorl %ebp, %ebp
movq %r13, %rdi
callq 0x130e
movl %eax, %edi
callq 0x1040
movq %rax, %r15
movq %rbx, %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq 0x10f0
movq %r13, %rdi
callq 0x130e
movl %eax, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %rbx, %rcx
callq 0x1160
movq %rax, %r12
movq %rbx, %rdi
callq 0x1030
movq %r13, %rdi
callq 0x130e
movl %eax, %eax
cmpq %rax, %r12
je 0x1c45
movq %r13, %rdi
callq 0x130e
cmpl %r12d, %eax
jbe 0x1c45
movl %r12d, %r12d
movb $0x0, (%r15,%r12)
incq %r12
movq %r13, %rdi
callq 0x130e
movl %eax, %eax
cmpq %rax, %r12
jb 0x1c2e
movq %r13, %rdi
movq %r15, %rsi
callq 0x1708
movq (%rsp), %r12
movq %r12, %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq 0x10f0
movq %r13, %rdi
callq 0x130e
movl %eax, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %r12, %rcx
callq 0x11a0
movq %r15, %rdi
callq 0x1120
movq %r13, %rdi
callq 0x130e
addl %eax, %r14d
movslq %r14d, %rbp
cmpq %rbp, 0x10(%rsp)
jg 0x1bcb
movq %rbx, %rdi
callq 0x10e0
movq (%rsp), %rdi
callq 0x10e0
xorl %edi, %edi
callq 0x10b0
movq %rax, %rbx
movq 0x231a(%rip), %r14      # 0x3fd8
leaq 0x3cd(%rip), %rsi       # 0x2092
movl $0xe, %edx
movq %r14, %rdi
callq 0x1100
subq 0x8(%rsp), %rbx
imulq $0x3e8, %rbx, %rsi      # imm = 0x3E8
movq %r14, %rdi
callq 0x10c0
movq %rax, %rbx
leaq 0x3b1(%rip), %rsi       # 0x20a1
movl $0x5, %edx
movq %rax, %rdi
callq 0x1100
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x10d0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1050
movq %rax, %rdi
callq 0x1080
xorl %eax, %eax
addq $0x1078, %rsp           # imm = 0x1078
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq