// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/aezv3/ref/aez_ref.c
// Function: E
static void E(byte *K, unsigned kbytes, int i, unsigned j,
                                                byte src[16], byte dst[16]) {
    byte k, extracted_key[3*16], iJ[16], jJ[16], buf[16], I[16], J[16], L[16];

    Extract(K, kbytes, extracted_key);
    memcpy(I,extracted_key,16);
    memcpy(J,extracted_key+16,16);
    memcpy(L,extracted_key+32,16);

    /* Encipher */
    if (i < 0) {
        u32 aes_key[4*11];
        memset(aes_key,0,16);                                  /* 0        */
        memcpy((byte*)aes_key+ 16, I, 16);                     /* I        */
        memcpy((byte*)aes_key+ 32, L, 16);                     /* L        */
        memcpy((byte*)aes_key+ 48, J, 16);                     /* J        */
        correct_key(aes_key,4*16);
        memcpy((byte*)aes_key+ 64, (byte*)aes_key+16, 48);     /* I L J    */
        memcpy((byte*)aes_key+112, (byte*)aes_key+16, 48);     /* I L J    */
        memcpy((byte*)aes_key+160, (byte*)aes_key+16, 16);     /* I        */
        mult_block(j,J,jJ); xor_bytes(jJ,src,16,buf);
        rijndaelEncryptRound(aes_key, 11, buf, 10); /* incl final MixColumns */
    } else {
        u32 aes4_key[4*5];
        unsigned first = (i>=3?0:i);  /* Which key comes first in aes4_key */
        /* Build AES4 key in use */
        memset((byte *)aes4_key, 0, 16);
        for (k=0; k<3; k++)
            memcpy((byte*)aes4_key+16+k*16, extracted_key+(k+first)%3*16, 16);
        if (i==2) memcpy((byte*)aes4_key+64, I, 16);
        else      memset((byte*)aes4_key+64, 0, 16);
        correct_key(aes4_key,5*16);
        /* Build encryption buffer from src and various offsets used */
        memcpy(buf, src, 16);
        if (i == 0) {
            mult_block(j,J,jJ); xor_bytes(buf,jJ,16,buf);
        } else if (i==1 || i==2) {
            mult_block(j%8,J,jJ); xor_bytes(buf,jJ,16,buf);
            for ( ; j>8; j-=8) mult_block(2,L,L);  /* L = 2^((j-1)/8) L */
            xor_bytes(buf, L, 16, buf);
        } else /* i >= 3 */ {
            mult_block((i-2)*8,J,iJ); xor_bytes(buf,iJ,16,buf);
            if (j>0) {
                mult_block(j%8,J,jJ); xor_bytes(buf,jJ,16,buf);
                for ( ; j>8; j-=8) mult_block(2,L,L);  /* L = 2^((j-1)/8) L */
                xor_bytes(buf, L, 16, buf);
            }
        }
        rijndaelEncryptRound(aes4_key, 10, buf, 4);
    }
    memcpy(dst, buf, 16);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1c8, %rsp            # imm = 0x1C8
movq %r9, %rbx
movq %r8, 0x68(%rsp)
movl %ecx, %ebp
movq %rdx, 0x78(%rsp)
movl %esi, 0xc(%rsp)
movq %rdi, 0x38(%rsp)
vxorps %xmm0, %xmm0, %xmm0
vmovups %ymm0, 0xe0(%rsp)
vmovups %ymm0, 0xf0(%rsp)
xorl %eax, %eax
cmpq $0x10, %rax
je 0x17ff
movb %al, 0x110(%rsp,%rax)
incq %rax
jmp 0x17ed
leaq 0x110(%rsp), %rdi
pushq $0x10
popq %rsi
vzeroupper
callq 0x1d44
pushq $0x10
popq %rax
cmpq $0x50, %rax
je 0x1833
vmovaps 0x110(%rsp), %xmm0
vmovaps %xmm0, 0x110(%rsp,%rax)
addq $0x10, %rax
jmp 0x1815
movl %ebp, 0x8(%rsp)
movq %rbx, 0x70(%rsp)
pushq $0x1
popq %rax
movl %eax, 0x4(%rsp)
pushq $0xa
popq %rbp
leaq 0x80(%rsp), %rbx
pushq $0x10
popq %r12
movl 0xc(%rsp), %edx
cmpl $0x10, %edx
jb 0x1979
movl %edx, 0x44(%rsp)
movl 0x4(%rsp), %r15d
movl %r15d, %eax
shrl $0x18, %eax
movl %eax, 0x4c(%rsp)
movl %r15d, %eax
shrl $0x10, %eax
movl %eax, 0x48(%rsp)
shrl $0x8, %r15d
pushq $0x1
popq %r14
cmpq $0x4, %r14
je 0x1963
andq $0x0, 0x80(%rsp)
andl $0x0, 0x88(%rsp)
movb %r14b, 0x87(%rsp)
movl 0x4c(%rsp), %eax
movb %al, 0x8c(%rsp)
movl 0x48(%rsp), %eax
movb %al, 0x8d(%rsp)
movb %r15b, 0x8e(%rsp)
movl 0x4(%rsp), %eax
movb %al, 0x8f(%rsp)
leaq 0x110(%rsp), %rdi
movl %ebp, %esi
movq %rbx, %rdx
pushq $0x4
popq %rcx
callq 0x10c0
movq %rbx, %rdi
movl %r12d, %esi
callq 0x1d44
pushq $0x10
popq %rax
cmpq $0x50, %rax
je 0x1911
vmovaps 0x80(%rsp), %xmm0
vmovaps %xmm0, 0x80(%rsp,%rax)
addq $0x10, %rax
jmp 0x18f3
movq 0x38(%rsp), %rax
vmovups (%rax), %xmm0
vmovaps %xmm0, 0x10(%rsp)
movq %rbx, %rdi
movl %ebp, %esi
movl %ebp, %r13d
leaq 0x10(%rsp), %rbp
movq %rbp, %rdx
pushq $0x4
popq %rcx
callq 0x10c0
movq %r14, %rcx
shlq $0x4, %rcx
leaq 0xd0(%rsp), %rax
addq %rax, %rcx
movq %rcx, %rdi
movq %rbp, %rsi
movl %r13d, %ebp
movl %r12d, %edx
callq 0x1d24
incq %r14
jmp 0x1884
addq $0x10, 0x38(%rsp)
movl 0x44(%rsp), %edx
addl $-0x10, %edx
incl 0x4(%rsp)
jmp 0x1856
cmpl $0x0, 0xc(%rsp)
setne %al
testl %edx, %edx
sete %cl
testb %cl, %al
jne 0x1a5f
movl %edx, %ebx
pushq $0x1
popq %r12
leaq 0x80(%rsp), %r15
pushq $0x10
popq %rbp
pushq $0x10
popq %r13
leaq 0x10(%rsp), %r14
cmpq $0x4, %r12
je 0x1a5f
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x80(%rsp)
movb %r12b, 0x87(%rsp)
leaq 0x110(%rsp), %rdi
pushq $0xa
popq %rsi
movq %r15, %rdx
pushq $0x4
popq %rcx
callq 0x10c0
movq %r15, %rdi
movl %ebp, %esi
callq 0x1d44
movq %r13, %rax
cmpq $0x50, %rax
je 0x1a08
vmovaps 0x80(%rsp), %xmm0
vmovaps %xmm0, 0x80(%rsp,%rax)
addq $0x10, %rax
jmp 0x19ea
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x10(%rsp)
movq %r14, %rdi
movq 0x38(%rsp), %rsi
movq %rbx, %rdx
callq 0x10d0
movb $-0x80, 0x10(%rsp,%rbx)
movq %r15, %rdi
pushq $0xa
popq %rsi
movq %r14, %rdx
pushq $0x4
popq %rcx
callq 0x10c0
movq %r12, %rcx
shlq $0x4, %rcx
leaq 0xd0(%rsp), %rax
addq %rax, %rcx
movq %rcx, %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0x1d24
incq %r12
jmp 0x19a8
vmovaps 0xf0(%rsp), %xmm0
vmovaps 0x100(%rsp), %xmm1
vmovaps %xmm0, 0x50(%rsp)
vmovaps %xmm1, 0xd0(%rsp)
movq 0x78(%rsp), %rbx
testl %ebx, %ebx
js 0x1b10
xorl %eax, %eax
cmpl $0x3, %ebx
movl %ebx, %ecx
cmovael %eax, %ecx
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x110(%rsp)
movl %ecx, %esi
shll $0x4, %esi
pushq $0x10
popq %rdi
pushq $0x3
popq %r8
movq 0x70(%rsp), %r14
movl 0x8(%rsp), %ebp
leaq 0x10(%rsp), %r15
movl %ecx, %eax
xorl %edx, %edx
divl %r8d
cmpq $0x40, %rdi
je 0x1af1
imull $0x30, %eax, %eax
movl %esi, %edx
subl %eax, %edx
movl %edx, %eax
vmovaps 0xe0(%rsp,%rax), %xmm0
vmovaps %xmm0, 0x110(%rsp,%rdi)
addl $0x10, %esi
incl %ecx
addq $0x10, %rdi
jmp 0x1abe
leaq 0x150(%rsp), %rax
cmpl $0x2, %ebx
jne 0x1bca
vmovaps 0xe0(%rsp), %xmm0
jmp 0x1bce
leaq 0xf0(%rsp), %rax
vxorps %xmm0, %xmm0, %xmm0
leaq 0x110(%rsp), %rbx
vmovaps %xmm0, (%rbx)
vmovaps -0x10(%rax), %xmm0
vmovaps %xmm0, 0x10(%rbx)
vmovaps 0x10(%rax), %xmm0
vmovaps %xmm0, 0x20(%rbx)
vmovaps (%rax), %xmm0
vmovaps %xmm0, 0x30(%rbx)
pushq $0x40
popq %rsi
movq %rbx, %rdi
callq 0x1d44
vmovups 0x10(%rbx), %ymm0
vmovups 0x20(%rbx), %ymm1
vmovups %ymm1, 0x50(%rbx)
vmovups %ymm0, 0x40(%rbx)
vmovups %ymm0, 0x70(%rbx)
vmovups %ymm1, 0x80(%rbx)
vmovaps 0x10(%rbx), %xmm0
vmovaps %xmm0, 0xa0(%rbx)
leaq 0x50(%rsp), %rsi
leaq 0x10(%rsp), %r14
movl 0x8(%rsp), %edi
movq %r14, %rdx
vzeroupper
callq 0x1d5c
pushq $0x10
popq %rdx
leaq 0x20(%rsp), %r15
movq %r14, %rdi
movq 0x68(%rsp), %rsi
movq %r15, %rcx
callq 0x1d24
pushq $0xb
popq %rsi
pushq $0xa
popq %rcx
movq %rbx, %rdi
movq %r15, %rdx
callq 0x10c0
movq 0x70(%rsp), %r14
jmp 0x1c84
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, (%rax)
leaq 0x110(%rsp), %rdi
pushq $0x50
popq %rsi
callq 0x1d44
movq 0x68(%rsp), %rax
vmovups (%rax), %xmm0
vmovaps %xmm0, 0x20(%rsp)
testl %ebx, %ebx
je 0x1c4a
cmpl $0x2, %ebx
ja 0x1ca1
movl %ebp, %edi
andl $0x7, %edi
leaq 0x50(%rsp), %rsi
leaq 0x10(%rsp), %rbx
movq %rbx, %rdx
callq 0x1d5c
pushq $0x10
popq %rdx
leaq 0x20(%rsp), %rcx
movq %rcx, %rdi
movq %rbx, %rsi
callq 0x1d24
leaq 0xd0(%rsp), %r15
pushq $0x2
popq %rbx
cmpl $0x9, %ebp
jb 0x1c59
movl %ebx, %edi
movq %r15, %rsi
movq %r15, %rdx
callq 0x1d5c
addl $-0x8, %ebp
jmp 0x1c33
leaq 0x50(%rsp), %rsi
movl %ebp, %edi
movq %r15, %rdx
callq 0x1d5c
pushq $0x10
popq %rdx
leaq 0x20(%rsp), %rcx
movq %rcx, %rdi
movq %r15, %rsi
callq 0x1d24
leaq 0x110(%rsp), %rdi
pushq $0xa
popq %rsi
leaq 0x20(%rsp), %rdx
pushq $0x4
popq %rcx
callq 0x10c0
vmovaps 0x20(%rsp), %xmm0
vmovups %xmm0, (%r14)
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leal -0x10(,%rbx,8), %edi
leaq 0x50(%rsp), %rsi
leaq 0x80(%rsp), %rbx
movq %rbx, %rdx
callq 0x1d5c
pushq $0x10
popq %rdx
leaq 0x20(%rsp), %rcx
movq %rcx, %rdi
movq %rbx, %rsi
callq 0x1d24
testl %ebp, %ebp
je 0x1c6c
movl %ebp, %edi
andl $0x7, %edi
leaq 0x50(%rsp), %rsi
leaq 0x10(%rsp), %rbx
movq %rbx, %rdx
callq 0x1d5c
pushq $0x10
popq %rdx
leaq 0x20(%rsp), %rcx
movq %rcx, %rdi
movq %rbx, %rsi
callq 0x1d24
leaq 0xd0(%rsp), %r15
pushq $0x2
popq %rbx
cmpl $0x9, %ebp
jb 0x1c59
movl %ebx, %edi
movq %r15, %rsi
movq %r15, %rdx
callq 0x1d5c
addl $-0x8, %ebp
jmp 0x1d09