// File: 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
movq %rsp, %rbp
subq $0x1110, %rsp           # imm = 0x1110
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x1068(%rbp), %rdi
callq 0x1300
cmpl $0x4, -0x8(%rbp)
jg 0x1e79
movq 0x3186(%rip), %rdi      # 0x4fd8
leaq 0x11f4(%rip), %rsi      # 0x304d
callq 0x1090
movq %rax, %rdi
movq 0x3160(%rip), %rsi      # 0x4fc8
callq 0x10b0
movl $0x0, -0x4(%rbp)
jmp 0x2230
leaq -0x1068(%rbp), %rdi
callq 0x1350
movl %eax, %eax
movl %eax, %edi
callq 0x1040
movq %rax, -0x1070(%rbp)
movl $0x0, -0x1074(%rbp)
movl -0x1074(%rbp), %eax
movl %eax, -0x10dc(%rbp)
leaq -0x1068(%rbp), %rdi
callq 0x1350
movl %eax, %ecx
movl -0x10dc(%rbp), %eax
cmpl %ecx, %eax
jae 0x1f40
movq -0x10(%rbp), %rax
movl -0x1074(%rbp), %ecx
addl $0x3, %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rdi
leaq -0x1080(%rbp), %rsi
movl $0x10, %edx
callq 0x1100
movb %al, %dl
movq -0x1070(%rbp), %rax
movl -0x1074(%rbp), %ecx
movb %dl, (%rax,%rcx)
movq -0x1080(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
je 0x1f2a
movq 0x30c9(%rip), %rdi      # 0x4fd8
leaq 0x1167(%rip), %rsi      # 0x307d
callq 0x1090
movq %rax, %rdi
movq 0x30a3(%rip), %rsi      # 0x4fc8
callq 0x10b0
jmp 0x1f2c
movl -0x1074(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1074(%rbp)
jmp 0x1e9f
movq -0x1070(%rbp), %rax
movq %rax, -0x10e8(%rbp)
leaq -0x1068(%rbp), %rdi
callq 0x1350
movq -0x10e8(%rbp), %rdi
movl %eax, %esi
leaq 0x111f(%rip), %rdx      # 0x3089
callq 0x1db0
movq -0x1070(%rbp), %rsi
leaq -0x1068(%rbp), %rdi
callq 0x1360
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x10fe(%rip), %rsi      # 0x308f
callq 0x10d0
movq %rax, -0x1088(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
leaq 0x10e5(%rip), %rsi      # 0x3091
callq 0x10d0
movq %rax, -0x1090(%rbp)
movq -0x1088(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x10c0
movq -0x1088(%rbp), %rdi
callq 0x1030
movq %rax, -0x1098(%rbp)
movq -0x1088(%rbp), %rdi
callq 0x1060
xorl %eax, %eax
movl %eax, %edi
callq 0x1080
imulq $0x3e8, %rax, %rax      # imm = 0x3E8
movq %rax, -0x10a8(%rbp)
movl $0x0, -0x10ac(%rbp)
movslq -0x10ac(%rbp), %rax
cmpq -0x1098(%rbp), %rax
jge 0x21ba
leaq -0x1068(%rbp), %rdi
callq 0x1340
movl %eax, %eax
movl %eax, %edi
callq 0x1040
movq %rax, -0x10a0(%rbp)
movq -0x1088(%rbp), %rdi
movslq -0x10ac(%rbp), %rsi
xorl %edx, %edx
callq 0x10c0
movq -0x10a0(%rbp), %rax
movq %rax, -0x10f0(%rbp)
leaq -0x1068(%rbp), %rdi
callq 0x1340
movq -0x10f0(%rbp), %rdi
movl %eax, %eax
movl %eax, %esi
movq -0x1088(%rbp), %rcx
movl $0x1, %edx
callq 0x1110
movq %rax, -0x10b8(%rbp)
movq -0x1088(%rbp), %rdi
callq 0x1030
movq %rax, -0x10c0(%rbp)
leaq -0x1068(%rbp), %rdi
callq 0x1340
movl %eax, %eax
subq -0x10b8(%rbp), %rax
movq %rax, -0x10c8(%rbp)
cmpq $0x0, -0x10c8(%rbp)
je 0x211d
movq -0x10b8(%rbp), %rax
movl %eax, -0x10cc(%rbp)
movl -0x10cc(%rbp), %eax
movl %eax, -0x10f4(%rbp)
leaq -0x1068(%rbp), %rdi
callq 0x1340
movl %eax, %ecx
movl -0x10f4(%rbp), %eax
cmpl %ecx, %eax
jae 0x211b
movq -0x10a0(%rbp), %rax
movl -0x10cc(%rbp), %ecx
movb $0x0, (%rax,%rcx)
movl -0x10cc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10cc(%rbp)
jmp 0x20d5
jmp 0x211d
movq -0x10a0(%rbp), %rsi
leaq -0x1068(%rbp), %rdi
callq 0x1930
movq -0x1090(%rbp), %rdi
movslq -0x10ac(%rbp), %rsi
xorl %edx, %edx
callq 0x10c0
movq -0x10a0(%rbp), %rax
movq %rax, -0x1108(%rbp)
leaq -0x1068(%rbp), %rdi
callq 0x1340
movq -0x1108(%rbp), %rdi
movl %eax, %eax
movl %eax, %esi
movq -0x1090(%rbp), %rcx
movl $0x1, %edx
callq 0x1170
movq -0x10a0(%rbp), %rax
movq %rax, -0x1100(%rbp)
cmpq $0x0, %rax
je 0x219b
movq -0x1100(%rbp), %rdi
callq 0x10e0
jmp 0x219d
leaq -0x1068(%rbp), %rdi
callq 0x1340
addl -0x10ac(%rbp), %eax
movl %eax, -0x10ac(%rbp)
jmp 0x200d
movq -0x1088(%rbp), %rdi
callq 0x10a0
movq -0x1090(%rbp), %rdi
callq 0x10a0
xorl %eax, %eax
movl %eax, %edi
callq 0x1080
imulq $0x3e8, %rax, %rax      # imm = 0x3E8
movq %rax, -0x10d8(%rbp)
movq 0x2de8(%rip), %rdi      # 0x4fd8
leaq 0xe9d(%rip), %rsi       # 0x3094
callq 0x1090
movq %rax, %rdi
movq -0x10d8(%rbp), %rsi
subq -0x10a8(%rbp), %rsi
callq 0x1160
movq %rax, %rdi
leaq 0xe87(%rip), %rsi       # 0x30a3
callq 0x1090
movq %rax, %rdi
movq 0x2d9d(%rip), %rsi      # 0x4fc8
callq 0x10b0
movl -0x4(%rbp), %eax
addq $0x1110, %rsp           # imm = 0x1110
popq %rbp
retq