// File: /workspace/github_cpp_makefile_star2_O2/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 $0x1088, %rsp           # imm = 0x1088
movq %rsi, %r15
movl %edi, %ebx
leaq 0x30(%rsp), %rdi
callq 0x12dc
cmpl $0x4, %ebx
jg 0x1970
movq 0x2674(%rip), %rdi      # 0x3fd8
leaq 0x6e0(%rip), %rsi       # 0x204b
jmp 0x1b8f
leaq 0x30(%rsp), %rbx
movq %rbx, %rdi
callq 0x12f0
movl %eax, %edi
callq 0x1040
movq %rax, %r14
xorl %r13d, %r13d
leaq 0x28(%rsp), %r12
pushq $0x10
popq %rbp
movq %rbx, %rdi
callq 0x12f0
movl %eax, %eax
cmpq %rax, %r13
jae 0x19e1
leal 0x3(%r13), %eax
movq (%r15,%rax,8), %rdi
movq %r12, %rsi
movl %ebp, %edx
callq 0x1130
movb %al, (%r14,%r13)
movq 0x28(%rsp), %rax
cmpb $0x0, (%rax)
je 0x19dc
movq 0x2610(%rip), %rdi      # 0x3fd8
leaq 0x6ac(%rip), %rsi       # 0x207b
callq 0x10c0
movq %rax, %rdi
callq 0x1070
incq %r13
jmp 0x1992
leaq 0x30(%rsp), %rbx
movq %rbx, %rdi
callq 0x12f0
leaq 0x692(%rip), %rdx       # 0x2087
movq %r14, %rdi
movl %eax, %esi
callq 0x18e4
movq %rbx, %rdi
movq %r14, %rsi
callq 0x12f4
movq 0x8(%r15), %rdi
leaq 0x678(%rip), %rsi       # 0x208d
callq 0x10f0
movq %rax, %r14
movq 0x10(%r15), %rdi
leaq 0x667(%rip), %rsi       # 0x208f
callq 0x10f0
movq %rax, 0x10(%rsp)
xorl %r13d, %r13d
pushq $0x2
popq %rdx
movq %r14, %rdi
xorl %esi, %esi
callq 0x10e0
movq %r14, %rdi
callq 0x1030
movq %rax, 0x20(%rsp)
movq %r14, 0x8(%rsp)
movq %r14, %rdi
callq 0x1060
xorl %edi, %edi
callq 0x10a0
movq %rax, 0x18(%rsp)
pushq $0x1
popq %r12
movslq %r13d, %r13
cmpq %r13, 0x20(%rsp)
jle 0x1b40
movq %rbx, %rdi
callq 0x12ec
movl %eax, %edi
callq 0x1040
movq %rax, %rbp
movq 0x8(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x10e0
movq %rbx, %rdi
callq 0x12ec
movl %eax, %esi
movq %rbp, %rdi
movq %r12, %r15
movq %r12, %rdx
movq %r14, %rcx
callq 0x1140
movq %rax, %r12
movq %r14, %rdi
callq 0x1030
movq %rbx, %rdi
callq 0x12ec
movl %eax, %eax
cmpq %rax, %r12
jne 0x1b23
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x1656
movq 0x10(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x10e0
movq %rbx, %rdi
callq 0x12ec
movl %eax, %esi
movq %rbp, %rdi
movq %r15, %r12
movq %r15, %rdx
movq %r14, %rcx
callq 0x1180
movq %rbp, %rdi
callq 0x1100
movq %rbx, %rdi
callq 0x12ec
addl %eax, %r13d
jmp 0x1a6c
movl %r12d, %r12d
movq %rbx, %rdi
callq 0x12ec
movl %eax, %eax
cmpq %rax, %r12
jae 0x1ad3
movb $0x0, (%rbp,%r12)
incq %r12
jmp 0x1b26
movq 0x8(%rsp), %rdi
callq 0x10d0
movq 0x10(%rsp), %rdi
callq 0x10d0
xorl %edi, %edi
callq 0x10a0
movq %rax, %rbx
movq 0x2473(%rip), %rdi      # 0x3fd8
leaq 0x526(%rip), %rsi       # 0x2092
callq 0x10c0
subq 0x18(%rsp), %rbx
imulq $0x3e8, %rbx, %rsi      # imm = 0x3E8
movq %rax, %rdi
callq 0x10b0
leaq 0x515(%rip), %rsi       # 0x20a1
movq %rax, %rdi
callq 0x10c0
movq %rax, %rdi
callq 0x1070
xorl %eax, %eax
addq $0x1088, %rsp           # imm = 0x1088
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq