// File: /workspace/github_cpp_makefile_star2_O1/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, %r12d
movq %rsp, %r14
leaq 0xf(,%r12,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), %rbx
movq %rbx, -0x10(%rbx)
leaq 0x2aff(%rip), %rsi      # 0x7716
leaq 0x2b04(%rip), %rdx      # 0x7722
leaq -0x60(%rbp), %rdi
callq 0x314c
leaq -0x13e8(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x23b0
movq -0x60(%rbp), %rdi
cmpq %rbx, %rdi
je 0x4c4c
movq -0x50(%rbp), %rsi
incq %rsi
callq 0x2250
movl %r13d, %ebx
cmpl $0x0, -0x30(%rbp)
jle 0x4c62
movq %r15, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x2140
movq %rbx, -0x38(%rbp)
cmpl $0x0, -0x30(%rbp)
jle 0x4c89
xorl %ebx, %ebx
leaq -0x13e8(%rbp), %r13
movq %r13, %rdi
callq 0x22f0
movl %eax, (%r14,%rbx,4)
incq %rbx
cmpq %rbx, %r12
jne 0x4c75
movl $0x1f, %ecx
xorl %eax, %eax
cmpl $0x0, -0x30(%rbp)
jle 0x4cba
movl $0x1, %edx
shll %cl, %edx
movl %eax, %esi
xorl %edi, %edi
testl %edx, (%r14,%rdi,4)
je 0x4caf
movslq %esi, %r8
movb $0x1, (%r15,%r8)
incq %rdi
addl $0x20, %esi
cmpq %rdi, %r12
jne 0x4ca1
incl %eax
subl $0x1, %ecx
jae 0x4c90
cmpl $0x0, -0x30(%rbp)
jle 0x4d23
movq -0x30(%rbp), %rax
leal (,%rax,4), %eax
cmpl $0x2, %eax
movl $0x1, %ecx
cmovll %ecx, %eax
xorl %edx, %edx
movq %r15, %rsi
xorl %edi, %edi
movl $0x7, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
movl $0x1, %r10d
shll %cl, %r10d
cmpb $0x0, (%rsi,%r8)
cmovel %edx, %r10d
addb %r10b, %r9b
incq %r8
decl %ecx
cmpq $0x8, %r8
jne 0x4cef
movq -0x40(%rbp), %rcx
addb %r9b, (%rcx,%rdi)
incq %rdi
addq $0x8, %rsi
cmpq %rax, %rdi
jne 0x4ce4
cmpl $0x0, -0x30(%rbp)
jle 0x4d37
movq %r15, %rdi
xorl %esi, %esi
movq -0x38(%rbp), %rdx
callq 0x2140
cmpl $0x0, -0x30(%rbp)
jle 0x4d4f
leaq (,%r12,4), %rdx
movq %r14, %rdi
xorl %esi, %esi
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 0x4dad
movq %rbx, %rcx
movq %rax, %rbx
movq -0x60(%rbp), %rdi
cmpq %rcx, %rdi
je 0x4da5
movq -0x50(%rbp), %rsi
incq %rsi
callq 0x2250
jmp 0x4da5
movq %rax, %rbx
jmp 0x4da5
movq %rax, %rbx
leaq -0x13e8(%rbp), %rdi
callq 0x2210
movq %rbx, %rdi
callq 0x2400
movq %rax, %rdi
callq 0x3642
nop