// File: /workspace/github_cpp_makefile_star2_O3/lmclaramunt[P]AES-Crypto/Cipher.cpp
// Function: Cipher::generateKey(unsigned char*, int)
void Cipher::generateKey(unsigned char* buff, int _Nk) {
    unsigned int rand[_Nk], x=0;
    bool* key2 = new bool[32*_Nk];
    
    std::random_device rd("/dev/urandom");
    std::fill_n(key2, 32*_Nk, 0);

    /* Generate Nk 32 bits INTs */
    for (int i=0; i<_Nk; i++)
        rand[i] = rd();

    /* Saving the binary sequence */
    for (int i=31; i>=0; i--) {
        for (int j=0; j<_Nk; j++) {
            if((rand[j] & 1<<i) == 1<<i)
                key2[32*j+31-i]=1;
        }
    }
    for(int i=0; i<4*_Nk; i++) {
        unsigned char ch = 0;
        for (int j=0; j<8; j++)
            if(key2[8*i+j])
                ch += 1<<(7-j);
        buff[x++] += ch;
    }
    
    /* Local variables cleanup as it can leave traces in memory */
    std::fill_n(key2, 32*_Nk, 0);
    std::fill_n(rand, _Nk, 0);
    delete[] key2, rand;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x13c8, %rsp           # imm = 0x13C8
movq %rsi, -0x40(%rbp)
movl %edx, %ebx
movq %rsp, %r14
leaq 0xf(,%rbx,4), %rax
andq $-0x10, %rax
subq %rax, %r14
movq %r14, %rsp
movq %rdx, -0x30(%rbp)
movl %edx, %r13d
shll $0x5, %r13d
movslq %r13d, %rdi
callq 0x2030
movq %rax, %r15
leaq -0x50(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x2b4f(%rip), %rsi      # 0x7776
leaq 0x2b54(%rip), %rdx      # 0x7782
leaq -0x60(%rbp), %rdi
callq 0x3144
leaq -0x13e8(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x23b0
movq -0x60(%rbp), %rdi
cmpq %r12, %rdi
je 0x4c5c
movq -0x50(%rbp), %rsi
incq %rsi
callq 0x2250
movl %r13d, %eax
movq %rax, -0x38(%rbp)
cmpl $0x0, -0x30(%rbp)
jle 0x4c95
xorl %r12d, %r12d
movq %r15, %rdi
xorl %esi, %esi
movq -0x38(%rbp), %rdx
callq 0x2140
leaq -0x13e8(%rbp), %r13
movq %r13, %rdi
callq 0x22f0
movl %eax, (%r14,%r12,4)
incq %r12
cmpq %r12, %rbx
jne 0x4c81
movl $0x1f, %ecx
xorl %eax, %eax
cmpl $0x0, -0x30(%rbp)
jle 0x4ccc
movl $0x1, %edx
shll %cl, %edx
movl %eax, %esi
xorl %edi, %edi
testl %edx, (%r14,%rdi,4)
je 0x4cbb
movslq %esi, %r8
movb $0x1, (%r15,%r8)
incq %rdi
addl $0x20, %esi
cmpq %rdi, %rbx
jne 0x4cad
testl %ecx, %ecx
jne 0x4cd0
jmp 0x4cd6
testl %ecx, %ecx
je 0x4d48
decl %ecx
incl %eax
jmp 0x4c9c
movq -0x30(%rbp), %rax
testl %eax, %eax
jle 0x4d6d
leal (,%rax,4), %ecx
cmpl $0x2, %ecx
movl $0x1, %eax
cmovgel %ecx, %eax
xorl %ecx, %ecx
movq 0x2732(%rip), %xmm0     # 0x7430
movdqa 0x273a(%rip), %xmm1     # 0x7440
pxor %xmm2, %xmm2
movq 0x273e(%rip), %xmm3     # 0x7450
movq -0x40(%rbp), %rsi
movq (%r15,%rcx,8), %xmm4
psllw $0x7, %xmm4
pand %xmm0, %xmm4
pand %xmm1, %xmm4
pxor %xmm5, %xmm5
pcmpgtb %xmm4, %xmm5
pand %xmm3, %xmm5
psadbw %xmm2, %xmm5
movd %xmm5, %edx
addb %dl, (%rsi,%rcx)
incq %rcx
cmpq %rcx, %rax
jne 0x4d16
cmpl $0x0, -0x30(%rbp)
jle 0x4d6d
movq %r15, %rdi
xorl %esi, %esi
movq -0x38(%rbp), %rdx
callq 0x2140
shlq $0x2, %rbx
movq %r14, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x2140
movq %r15, %rdi
callq 0x22b0
leaq -0x13e8(%rbp), %rdi
callq 0x2210
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4dc8
movq %rax, %rbx
movq -0x60(%rbp), %rdi
cmpq %r12, %rdi
je 0x4dc0
movq -0x50(%rbp), %rsi
incq %rsi
callq 0x2250
jmp 0x4dc0
movq %rax, %rbx
jmp 0x4dc0
movq %rax, %rbx
leaq -0x13e8(%rbp), %rdi
callq 0x2210
movq %rbx, %rdi
callq 0x2400
movq %rax, %rdi
callq 0x364c