// File: /workspace/github_c_makefile_star2_O3/fritzw[P]cube-utils/cube-encoder.c
// Function: main
int main(int argc, char **argv) {
    char cubepro_key[] = "221BBakerMycroft";
    char cubex_key[] = "kWd$qG*25Xmgf-Sg";
    char *userkey = cubepro_key;
    char *extension = ".cubepro";
    BLOWFISH_KEY key;
    char *infilename, *outfilename;
    int i;
	char *executable;

    /* TODO:
	This whole code is a mess that was hacked together quickly. Clean it up!
    Implement flag to enable CubeX encoding?
    Implement decoding of .cubepro/.cubex files?
    Implement legacy mode for compatibility with CodeX:
    argv[0] [CUBEPRO | CUBEX] [ENCODE | DECODE | RECODE] inputfile outputfile
    */
    
	for (executable = argv[0]; *executable; ++executable) {
		*executable = tolower(*executable);
	}
	executable = argv[0];
    if (ends_with(executable, "cubex-encoder") ||
		ends_with(executable, "cubex-encoder.exe")) {
        userkey = cubex_key;
        extension = ".cubex";
    } else if (ends_with(executable, "cube3-encoder") ||
		ends_with(executable, "cube3-encoder.exe")) {
        extension = ".cube3";
    }

    // "Parse" arguments
    if (argc == 2) {
        infilename = argv[1];
        size_t inlength = strlen(infilename);
        outfilename = malloc(inlength + 10);
        if (outfilename == NULL) {
            perror("Unable to allocate memory for output file name");
            return 4;
        }
        strncpy(outfilename, infilename, inlength);
        if (strncmp(outfilename + inlength - 4, ".bfb", 4) == 0) {
            strcpy(outfilename + inlength - 4, extension);
        } else {
            strcpy(outfilename + inlength, extension);
        }
    } else if (argc == 3) {
        infilename = argv[1];
        outfilename = argv[2];
    } else {
        printf("Usage:\n"
               "    %s inputfile [outputfile]\n"
               "    Default outputfile is inputfile minus the .bfb extension plus %s\n",
               argv[0], extension);
        return 1;
    }


    // Load input file
    FILE *infile = fopen(infilename, "rb");
    if (infile == NULL) {
        perror("Unable to open input file");
        return 2;
    }
    size_t infilesize = fsize(infile);
    if (infilesize < 0) {
        perror("Unable to determine size of input file");
        return 3;
    }
    size_t outfilesize = ((infilesize + 8) / 8) * 8;
    BYTE *data = malloc(outfilesize);
    if (data == NULL) {
        perror("Unable to allocate memory for input file");
        return 4;
    }
    size_t readcount = fread(data, 1, infilesize, infile);
    if (readcount != infilesize) {
        printf("Unable to read the whole input file: %zd != %zd\n", readcount, infilesize);
        return 5;
    }
    fclose(infile);

    // Pad input data
    BYTE pad = outfilesize - infilesize;
    for (i = infilesize; i < outfilesize; i++) {
        data[i] = pad;
    }

    // Encrypt data
    blowfish_key_setup((BYTE*) userkey, &key, strlen(userkey));
    for (i = 0; i < outfilesize; i += 8) {
        blowfish_encrypt(&data[i], &data[i], &key, 1);
    }


    // Save data to output file
    FILE *outfile = fopen(outfilename, "wb+");
    if (outfile == NULL) {
        perror("Unable to open output file");
        return 6;
    }
    size_t writecount = fwrite(data, 1, outfilesize, outfile);
    if (writecount != outfilesize) {
        printf("Unable to write the whole output file: %zd != %zd\n", writecount, outfilesize);
        return 7;
    }
    fclose(outfile);

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1098, %rsp           # imm = 0x1098
movq %rsi, %r14
movl %edi, %ebp
movaps 0x1ddf(%rip), %xmm0     # 0x30a0
movaps %xmm0, 0x10(%rsp)
xorl %eax, %eax
movb %al, 0x20(%rsp)
movdqa 0x1dec(%rip), %xmm0     # 0x30c0
movdqa %xmm0, 0x30(%rsp)
movb %al, 0x40(%rsp)
movq (%rsi), %r15
movb (%r15), %bl
testb %bl, %bl
je 0x130b
callq 0x1110
incq %r15
movq (%rax), %rcx
movsbq %bl, %rdx
movb (%rcx,%rdx,4), %cl
movb %cl, -0x1(%r15)
movb (%r15), %bl
incq %r15
testb %bl, %bl
jne 0x12f0
movq (%r14), %r15
leaq 0x1dc8(%rip), %rsi      # 0x30da
movq %r15, %rdi
callq 0x125c
leaq 0x30(%rsp), %rbx
leaq 0x1dd4(%rip), %r12      # 0x30fa
testl %eax, %eax
jne 0x1373
leaq 0x1db7(%rip), %rsi      # 0x30e8
movq %r15, %rdi
callq 0x125c
testl %eax, %eax
jne 0x1373
leaq 0x1dbd(%rip), %rsi      # 0x3101
movq %r15, %rdi
callq 0x125c
testl %eax, %eax
jne 0x1367
leaq 0x1db8(%rip), %rsi      # 0x310f
movq %r15, %rdi
callq 0x125c
testl %eax, %eax
je 0x1a98
leaq 0x10(%rsp), %rbx
leaq 0x1dae(%rip), %r12      # 0x3121
cmpl $0x3, %ebp
je 0x13d7
cmpl $0x2, %ebp
jne 0x13e1
movq 0x8(%r14), %r15
movq %r15, %rdi
callq 0x1080
movq %rax, %r13
leaq 0xa(%rax), %rdi
callq 0x10c0
testq %rax, %rax
je 0x1aa9
movq %rax, %r14
movq %rax, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x1030
leaq (%r14,%r13), %rbp
addq $-0x4, %rbp
leaq 0x1d99(%rip), %rsi      # 0x3157
movl $0x4, %edx
movq %rbp, %rdi
callq 0x1040
testl %eax, %eax
je 0x13ff
movq %r14, %rdi
addq %r13, %rdi
jmp 0x1402
movq 0x8(%r14), %r15
movq 0x10(%r14), %r14
jmp 0x140a
leaq 0x1d74(%rip), %rdi      # 0x315c
movq %r15, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0x1090
movl $0x1, %eax
jmp 0x1acd
movq %rbp, %rdi
movq %r12, %rsi
callq 0x1050
leaq 0x1db6(%rip), %rsi      # 0x31c7
movq %r15, %rdi
callq 0x10e0
testq %rax, %rax
je 0x1a7c
movq %rax, %rbp
movq %r14, 0x8(%rsp)
movq %rbx, %r14
movq %rax, %rdi
callq 0x121c
movq %rax, %rbx
movq %rax, %r13
andq $-0x8, %r13
leaq 0x8(%r13), %r15
movq %r15, %rdi
callq 0x10c0
testq %rax, %rax
je 0x1a8f
movq %rax, %r12
movl $0x1, %esi
movq %rax, %rdi
movq %rbx, %rdx
movq %rbp, %rcx
callq 0x1060
cmpq %rbx, %rax
jne 0x1a46
movq %rbp, %rdi
callq 0x1070
movslq %ebx, %rdx
cmpq %rdx, %r15
movq %r14, %rbx
movq 0x8(%rsp), %r14
jbe 0x19c2
movl %r15d, %eax
subl %edx, %eax
subq %rdx, %r13
leaq 0x17(%r13), %rcx
andq $-0x10, %rcx
addq $0x7, %r13
movq %r13, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
addq %r12, %rdx
addq $0xf, %rdx
xorl %esi, %esi
movdqa 0x1b72(%rip), %xmm3     # 0x3030
movdqa 0x1b7a(%rip), %xmm4     # 0x3040
movdqa 0x1b82(%rip), %xmm5     # 0x3050
movdqa 0x1b8a(%rip), %xmm6     # 0x3060
movdqa 0x1b92(%rip), %xmm7     # 0x3070
movdqa 0x1b99(%rip), %xmm8     # 0x3080
movdqa 0x1ba0(%rip), %xmm9     # 0x3090
pcmpeqd %xmm10, %xmm10
movq %rsi, %xmm11
pshufd $0x44, %xmm11, %xmm11   # xmm11 = xmm11[0,1,0,1]
movdqa %xmm11, %xmm13
por %xmm8, %xmm13
movdqa %xmm0, %xmm12
pxor %xmm9, %xmm12
pxor %xmm9, %xmm13
movdqa %xmm13, %xmm15
pcmpgtd %xmm12, %xmm15
pshufd $0xa0, %xmm15, %xmm14   # xmm14 = xmm15[0,0,2,2]
pshuflw $0xe8, %xmm14, %xmm1    # xmm1 = xmm14[0,2,2,3,4,5,6,7]
pcmpeqd %xmm12, %xmm13
pshufd $0xf5, %xmm13, %xmm13   # xmm13 = xmm13[1,1,3,3]
pshuflw $0xe8, %xmm13, %xmm2    # xmm2 = xmm13[0,2,2,3,4,5,6,7]
pand %xmm1, %xmm2
pshufd $0xf5, %xmm15, %xmm15   # xmm15 = xmm15[1,1,3,3]
pshuflw $0xe8, %xmm15, %xmm1    # xmm1 = xmm15[0,2,2,3,4,5,6,7]
por %xmm2, %xmm1
pxor %xmm10, %xmm1
packssdw %xmm1, %xmm1
movd %xmm1, %edi
testb $0x1, %dil
je 0x156b
movb %al, -0xf(%rdx,%rsi)
pand %xmm14, %xmm13
por %xmm15, %xmm13
packssdw %xmm13, %xmm13
pxor %xmm10, %xmm13
packssdw %xmm13, %xmm13
packsswb %xmm13, %xmm13
movd %xmm13, %edi
shrl $0x8, %edi
testb $0x1, %dil
je 0x159b
movb %al, -0xe(%rdx,%rsi)
movdqa %xmm11, %xmm1
por %xmm7, %xmm1
pxor %xmm9, %xmm1
movdqa %xmm1, %xmm2
pcmpgtd %xmm12, %xmm2
pshufd $0xa0, %xmm2, %xmm13    # xmm13 = xmm2[0,0,2,2]
pcmpeqd %xmm12, %xmm1
pshufd $0xf5, %xmm1, %xmm14    # xmm14 = xmm1[1,1,3,3]
movdqa %xmm14, %xmm1
pand %xmm13, %xmm1
pshufd $0xf5, %xmm2, %xmm15    # xmm15 = xmm2[1,1,3,3]
por %xmm15, %xmm1
packssdw %xmm1, %xmm1
pxor %xmm10, %xmm1
packssdw %xmm1, %xmm1
packsswb %xmm1, %xmm1
movd %xmm1, %edi
shrl $0x10, %edi
testb $0x1, %dil
je 0x15fa
movb %al, -0xd(%rdx,%rsi)
pshufhw $0x84, %xmm13, %xmm1    # xmm1 = xmm13[0,1,2,3,4,5,4,6]
pshufhw $0x84, %xmm14, %xmm2    # xmm2 = xmm14[0,1,2,3,4,5,4,6]
pand %xmm1, %xmm2
pshufhw $0x84, %xmm15, %xmm1    # xmm1 = xmm15[0,1,2,3,4,5,4,6]
por %xmm2, %xmm1
pxor %xmm10, %xmm1
packssdw %xmm1, %xmm1
packsswb %xmm1, %xmm1
movd %xmm1, %edi
shrl $0x18, %edi
testb $0x1, %dil
je 0x1632
movb %al, -0xc(%rdx,%rsi)
movdqa %xmm11, %xmm1
por %xmm6, %xmm1
pxor %xmm9, %xmm1
movdqa %xmm1, %xmm2
pcmpgtd %xmm12, %xmm2
pshufd $0xa0, %xmm2, %xmm14    # xmm14 = xmm2[0,0,2,2]
pshuflw $0xe8, %xmm14, %xmm15   # xmm15 = xmm14[0,2,2,3,4,5,6,7]
pcmpeqd %xmm12, %xmm1
pshufd $0xf5, %xmm1, %xmm13    # xmm13 = xmm1[1,1,3,3]
pshuflw $0xe8, %xmm13, %xmm1    # xmm1 = xmm13[0,2,2,3,4,5,6,7]
pand %xmm15, %xmm1
pshufd $0xf5, %xmm2, %xmm15    # xmm15 = xmm2[1,1,3,3]
pshuflw $0xe8, %xmm15, %xmm2    # xmm2 = xmm15[0,2,2,3,4,5,6,7]
por %xmm1, %xmm2
pxor %xmm10, %xmm2
packssdw %xmm2, %xmm1
packsswb %xmm1, %xmm1
pextrw $0x2, %xmm1, %edi
testb $0x1, %dil
je 0x1697
movb %al, -0xb(%rdx,%rsi)
pand %xmm14, %xmm13
por %xmm15, %xmm13
packssdw %xmm13, %xmm13
pxor %xmm10, %xmm13
packssdw %xmm13, %xmm1
packsswb %xmm1, %xmm1
pextrw $0x2, %xmm1, %edi
shrl $0x8, %edi
testb $0x1, %dil
je 0x16c6
movb %al, -0xa(%rdx,%rsi)
movdqa %xmm11, %xmm1
por %xmm5, %xmm1
pxor %xmm9, %xmm1
movdqa %xmm1, %xmm2
pcmpgtd %xmm12, %xmm2
pshufd $0xa0, %xmm2, %xmm13    # xmm13 = xmm2[0,0,2,2]
pcmpeqd %xmm12, %xmm1
pshufd $0xf5, %xmm1, %xmm14    # xmm14 = xmm1[1,1,3,3]
movdqa %xmm14, %xmm1
pand %xmm13, %xmm1
pshufd $0xf5, %xmm2, %xmm15    # xmm15 = xmm2[1,1,3,3]
por %xmm15, %xmm1
packssdw %xmm1, %xmm1
pxor %xmm10, %xmm1
packssdw %xmm1, %xmm1
packsswb %xmm1, %xmm1
pextrw $0x3, %xmm1, %edi
testb $0x1, %dil
je 0x1723
movb %al, -0x9(%rdx,%rsi)
pshufhw $0x84, %xmm13, %xmm1    # xmm1 = xmm13[0,1,2,3,4,5,4,6]
pshufhw $0x84, %xmm14, %xmm2    # xmm2 = xmm14[0,1,2,3,4,5,4,6]
pand %xmm1, %xmm2
pshufhw $0x84, %xmm15, %xmm1    # xmm1 = xmm15[0,1,2,3,4,5,4,6]
por %xmm2, %xmm1
pxor %xmm10, %xmm1
packssdw %xmm1, %xmm1
packsswb %xmm1, %xmm1
pextrw $0x3, %xmm1, %edi
shrl $0x8, %edi
testb $0x1, %dil
je 0x175c
movb %al, -0x8(%rdx,%rsi)
movdqa %xmm11, %xmm1
por %xmm4, %xmm1
pxor %xmm9, %xmm1
movdqa %xmm1, %xmm2
pcmpgtd %xmm12, %xmm2
pshufd $0xa0, %xmm2, %xmm14    # xmm14 = xmm2[0,0,2,2]
pshuflw $0xe8, %xmm14, %xmm15   # xmm15 = xmm14[0,2,2,3,4,5,6,7]
pcmpeqd %xmm12, %xmm1
pshufd $0xf5, %xmm1, %xmm13    # xmm13 = xmm1[1,1,3,3]
pshuflw $0xe8, %xmm13, %xmm1    # xmm1 = xmm13[0,2,2,3,4,5,6,7]
pand %xmm15, %xmm1
pshufd $0xf5, %xmm2, %xmm15    # xmm15 = xmm2[1,1,3,3]
pshuflw $0xe8, %xmm15, %xmm2    # xmm2 = xmm15[0,2,2,3,4,5,6,7]
por %xmm1, %xmm2
pxor %xmm10, %xmm2
packssdw %xmm2, %xmm2
packsswb %xmm2, %xmm1
pextrw $0x4, %xmm1, %edi
testb $0x1, %dil
je 0x17c1
movb %al, -0x7(%rdx,%rsi)
pand %xmm14, %xmm13
por %xmm15, %xmm13
packssdw %xmm13, %xmm13
pxor %xmm10, %xmm13
packssdw %xmm13, %xmm13
packsswb %xmm13, %xmm1
pextrw $0x4, %xmm1, %edi
shrl $0x8, %edi
testb $0x1, %dil
je 0x17f1
movb %al, -0x6(%rdx,%rsi)
movdqa %xmm11, %xmm1
por %xmm3, %xmm1
pxor %xmm9, %xmm1
movdqa %xmm1, %xmm2
pcmpgtd %xmm12, %xmm2
pshufd $0xa0, %xmm2, %xmm13    # xmm13 = xmm2[0,0,2,2]
pcmpeqd %xmm12, %xmm1
pshufd $0xf5, %xmm1, %xmm14    # xmm14 = xmm1[1,1,3,3]
movdqa %xmm14, %xmm1
pand %xmm13, %xmm1
pshufd $0xf5, %xmm2, %xmm15    # xmm15 = xmm2[1,1,3,3]
por %xmm15, %xmm1
packssdw %xmm1, %xmm1
pxor %xmm10, %xmm1
packssdw %xmm1, %xmm1
packsswb %xmm1, %xmm1
pextrw $0x5, %xmm1, %edi
testb $0x1, %dil
je 0x184e
movb %al, -0x5(%rdx,%rsi)
pshufhw $0x84, %xmm13, %xmm1    # xmm1 = xmm13[0,1,2,3,4,5,4,6]
pshufhw $0x84, %xmm14, %xmm2    # xmm2 = xmm14[0,1,2,3,4,5,4,6]
pand %xmm1, %xmm2
pshufhw $0x84, %xmm15, %xmm1    # xmm1 = xmm15[0,1,2,3,4,5,4,6]
por %xmm2, %xmm1
pxor %xmm10, %xmm1
packssdw %xmm1, %xmm1
packsswb %xmm1, %xmm1
pextrw $0x5, %xmm1, %edi
shrl $0x8, %edi
testb $0x1, %dil
je 0x1887
movb %al, -0x4(%rdx,%rsi)
movdqa %xmm11, %xmm1
por 0x178c(%rip), %xmm1     # 0x3020
pxor %xmm9, %xmm1
movdqa %xmm1, %xmm2
pcmpgtd %xmm12, %xmm2
pshufd $0xa0, %xmm2, %xmm14    # xmm14 = xmm2[0,0,2,2]
pshuflw $0xe8, %xmm14, %xmm15   # xmm15 = xmm14[0,2,2,3,4,5,6,7]
pcmpeqd %xmm12, %xmm1
pshufd $0xf5, %xmm1, %xmm13    # xmm13 = xmm1[1,1,3,3]
pshuflw $0xe8, %xmm13, %xmm1    # xmm1 = xmm13[0,2,2,3,4,5,6,7]
pand %xmm15, %xmm1
pshufd $0xf5, %xmm2, %xmm15    # xmm15 = xmm2[1,1,3,3]
pshuflw $0xe8, %xmm15, %xmm2    # xmm2 = xmm15[0,2,2,3,4,5,6,7]
por %xmm1, %xmm2
pxor %xmm10, %xmm2
packssdw %xmm2, %xmm1
packsswb %xmm1, %xmm1
pextrw $0x6, %xmm1, %edi
testb $0x1, %dil
je 0x18f0
movb %al, -0x3(%rdx,%rsi)
pand %xmm14, %xmm13
por %xmm15, %xmm13
packssdw %xmm13, %xmm13
pxor %xmm10, %xmm13
packssdw %xmm13, %xmm1
packsswb %xmm1, %xmm1
pextrw $0x6, %xmm1, %edi
shrl $0x8, %edi
testb $0x1, %dil
je 0x191f
movb %al, -0x2(%rdx,%rsi)
por 0x16e8(%rip), %xmm11    # 0x3010
pxor %xmm9, %xmm11
movdqa %xmm11, %xmm1
pcmpgtd %xmm12, %xmm1
pshufd $0xa0, %xmm1, %xmm13    # xmm13 = xmm1[0,0,2,2]
pcmpeqd %xmm12, %xmm11
pshufd $0xf5, %xmm11, %xmm11   # xmm11 = xmm11[1,1,3,3]
movdqa %xmm11, %xmm2
pand %xmm13, %xmm2
pshufd $0xf5, %xmm1, %xmm12    # xmm12 = xmm1[1,1,3,3]
por %xmm12, %xmm2
packssdw %xmm2, %xmm1
pxor %xmm10, %xmm1
packssdw %xmm1, %xmm1
packsswb %xmm1, %xmm1
pextrw $0x7, %xmm1, %edi
testb $0x1, %dil
je 0x197d
movb %al, -0x1(%rdx,%rsi)
pshufhw $0x84, %xmm13, %xmm1    # xmm1 = xmm13[0,1,2,3,4,5,4,6]
pshufhw $0x84, %xmm11, %xmm2    # xmm2 = xmm11[0,1,2,3,4,5,4,6]
pand %xmm1, %xmm2
pshufhw $0x84, %xmm12, %xmm1    # xmm1 = xmm12[0,1,2,3,4,5,4,6]
por %xmm2, %xmm1
pxor %xmm10, %xmm1
packssdw %xmm1, %xmm1
packsswb %xmm1, %xmm1
pextrw $0x7, %xmm1, %edi
shrl $0x8, %edi
testb $0x1, %dil
je 0x19b5
movb %al, (%rdx,%rsi)
addq $0x10, %rsi
cmpq %rsi, %rcx
jne 0x14f5
movq %rbx, %rdi
callq 0x1080
leaq 0x50(%rsp), %rsi
movq %rbx, %rdi
movq %rax, %rdx
callq 0x24a6
testq %r15, %r15
je 0x1a04
xorl %r13d, %r13d
leaq 0x50(%rsp), %rbx
leaq (%r12,%r13), %rdi
movq %rdi, %rsi
movq %rbx, %rdx
movl $0x1, %ecx
callq 0x1ae0
addq $0x8, %r13
cmpq %r13, %r15
ja 0x19e7
leaq 0x1833(%rip), %rsi      # 0x323e
movq %r14, %rdi
callq 0x10e0
testq %rax, %rax
je 0x1abc
movq %rax, %rbx
movl $0x1, %esi
movq %r12, %rdi
movq %r15, %rdx
movq %rax, %rcx
callq 0x1100
cmpq %r15, %rax
jne 0x1a61
movq %rbx, %rdi
callq 0x1070
xorl %eax, %eax
jmp 0x1acd
leaq 0x17c0(%rip), %rdi      # 0x320d
movq %rax, %rsi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x1090
movl $0x5, %eax
jmp 0x1acd
leaq 0x17f5(%rip), %rdi      # 0x325d
movq %rax, %rsi
movq %r15, %rdx
xorl %eax, %eax
callq 0x1090
movl $0x7, %eax
jmp 0x1acd
leaq 0x1747(%rip), %rdi      # 0x31ca
callq 0x10f0
movl $0x2, %eax
jmp 0x1acd
leaq 0x174e(%rip), %rdi      # 0x31e4
jmp 0x1ab0
leaq 0x10(%rsp), %rbx
leaq 0x162d(%rip), %r12      # 0x30d1
jmp 0x1373
leaq 0x1678(%rip), %rdi      # 0x3128
callq 0x10f0
movl $0x4, %eax
jmp 0x1acd
leaq 0x177f(%rip), %rdi      # 0x3242
callq 0x10f0
movl $0x6, %eax
addq $0x1098, %rsp           # imm = 0x1098
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop