// File: /workspace/github_c_makefile_star2_O0/fritzw[P]cube-utils/blowfish.c
// Function: blowfish_key_setup
void blowfish_key_setup(const BYTE user_key[], BLOWFISH_KEY *keystruct, size_t len)
{
    BYTE block[8];
    int idx,idx2;

    // Copy over the constant init array vals (so the originals aren't destroyed).
    memcpy(keystruct->p,p_perm,sizeof(WORD) * 18);
    memcpy(keystruct->s,s_perm,sizeof(WORD) * 1024);

    // Combine the key with the P box. Assume key is standard 448 bits (56 bytes) or less.
    for (idx = 0, idx2 = 0; idx < 18; ++idx, idx2 += 4)
        keystruct->p[idx] ^= (user_key[idx2 % len] << 24) | (user_key[(idx2+1) % len] << 16)
            | (user_key[(idx2+2) % len] << 8) | (user_key[(idx2+3) % len]);
    // Re-calculate the P box.
    memset(block, 0, 8);
    for (idx = 0; idx < 18; idx += 2) {
        blowfish_encrypt(block,block,keystruct, 0);
        keystruct->p[idx] = (block[0] << 24) | (block[1] << 16) | (block[2] << 8) | block[3];
        keystruct->p[idx+1]=(block[4] << 24) | (block[5] << 16) | (block[6] << 8) | block[7];
    }
    // Recalculate the S-boxes.
    for (idx = 0; idx < 4; ++idx) {
        for (idx2 = 0; idx2 < 256; idx2 += 2) {
            blowfish_encrypt(block,block,keystruct, 0);
            keystruct->s[idx][idx2] = (block[0] << 24) | (block[1] << 16) |
                (block[2] << 8) | block[3];
            keystruct->s[idx][idx2+1] = (block[4] << 24) | (block[5] << 16) |
                (block[6] << 8) | block[7];
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x1371(%rip), %rsi      # 0x4230
movl $0x48, %edx
callq 0x10c0
movq -0x10(%rbp), %rdi
addq $0x48, %rdi
leaq 0x13a8(%rip), %rsi      # 0x4280
movl $0x1000, %edx           # imm = 0x1000
callq 0x10c0
movl $0x0, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
cmpl $0x12, -0x24(%rbp)
jge 0x2fab
movq -0x8(%rbp), %rax
movq %rax, -0x48(%rbp)
movslq -0x28(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x18(%rbp)
movq -0x48(%rbp), %rax
movzbl (%rax,%rdx), %eax
shll $0x18, %eax
movl %eax, -0x40(%rbp)
movq -0x8(%rbp), %rcx
movl -0x28(%rbp), %eax
addl $0x1, %eax
cltq
xorl %edx, %edx
divq -0x18(%rbp)
movl -0x40(%rbp), %eax
movzbl (%rcx,%rdx), %ecx
shll $0x10, %ecx
orl %ecx, %eax
movl %eax, -0x3c(%rbp)
movq -0x8(%rbp), %rcx
movl -0x28(%rbp), %eax
addl $0x2, %eax
cltq
xorl %edx, %edx
divq -0x18(%rbp)
movl -0x3c(%rbp), %eax
movzbl (%rcx,%rdx), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movl %eax, -0x2c(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x38(%rbp)
movl -0x28(%rbp), %eax
addl $0x3, %eax
cltq
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x18(%rbp)
movq -0x38(%rbp), %rax
movq %rdx, %rcx
movl -0x2c(%rbp), %edx
movzbl (%rax,%rcx), %eax
orl %eax, %edx
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
xorl (%rax,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
movl -0x28(%rbp), %eax
addl $0x4, %eax
movl %eax, -0x28(%rbp)
jmp 0x2ef0
leaq -0x20(%rbp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x10a0
movl $0x0, -0x24(%rbp)
cmpl $0x12, -0x24(%rbp)
jge 0x303f
leaq -0x20(%rbp), %rdi
leaq -0x20(%rbp), %rsi
movq -0x10(%rbp), %rdx
xorl %ecx, %ecx
callq 0x1840
movzbl -0x20(%rbp), %edx
shll $0x18, %edx
movzbl -0x1f(%rbp), %eax
shll $0x10, %eax
orl %eax, %edx
movzbl -0x1e(%rbp), %eax
shll $0x8, %eax
orl %eax, %edx
movzbl -0x1d(%rbp), %eax
orl %eax, %edx
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movzbl -0x1c(%rbp), %edx
shll $0x18, %edx
movzbl -0x1b(%rbp), %eax
shll $0x10, %eax
orl %eax, %edx
movzbl -0x1a(%rbp), %eax
shll $0x8, %eax
orl %eax, %edx
movzbl -0x19(%rbp), %eax
orl %eax, %edx
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
movl -0x24(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x24(%rbp)
jmp 0x2fc2
movl $0x0, -0x24(%rbp)
cmpl $0x4, -0x24(%rbp)
jge 0x310c
movl $0x0, -0x28(%rbp)
cmpl $0x100, -0x28(%rbp)     # imm = 0x100
jge 0x30fc
leaq -0x20(%rbp), %rdi
leaq -0x20(%rbp), %rsi
movq -0x10(%rbp), %rdx
xorl %ecx, %ecx
callq 0x1840
movzbl -0x20(%rbp), %edx
shll $0x18, %edx
movzbl -0x1f(%rbp), %eax
shll $0x10, %eax
orl %eax, %edx
movzbl -0x1e(%rbp), %eax
shll $0x8, %eax
orl %eax, %edx
movzbl -0x1d(%rbp), %eax
orl %eax, %edx
movq -0x10(%rbp), %rax
addq $0x48, %rax
movslq -0x24(%rbp), %rcx
shlq $0xa, %rcx
addq %rcx, %rax
movslq -0x28(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movzbl -0x1c(%rbp), %edx
shll $0x18, %edx
movzbl -0x1b(%rbp), %eax
shll $0x10, %eax
orl %eax, %edx
movzbl -0x1a(%rbp), %eax
shll $0x8, %eax
orl %eax, %edx
movzbl -0x19(%rbp), %eax
orl %eax, %edx
movq -0x10(%rbp), %rax
addq $0x48, %rax
movslq -0x24(%rbp), %rcx
shlq $0xa, %rcx
addq %rcx, %rax
movl -0x28(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
movl -0x28(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x28(%rbp)
jmp 0x3057
jmp 0x30fe
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x3046
addq $0x50, %rsp
popq %rbp
retq