// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/aezv4/ref/encrypt.c
// Function: E
static void E(byte *K, unsigned kbytes, int j, unsigned i,
                                                byte src[16], byte dst[16]) {
    byte extracted_key[3*16], buf[16], delta[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 (j == -1) {
        u32 aes_key[4*11];
        memset(aes_key,0,16);                                  /* 0        */
        memcpy((byte*)aes_key+ 16, extracted_key, 48);         /* I J L    */
        correct_key(aes_key+4,3*16);
        memcpy((byte*)aes_key+ 64, (byte*)aes_key+16, 48);     /* I J L    */
        memcpy((byte*)aes_key+112, (byte*)aes_key+16, 48);     /* I J L    */
        memcpy((byte*)aes_key+160, (byte*)aes_key+16, 16);     /* I        */
        mult_block(i,J,buf); xor_bytes(buf,src,16,buf);
        rijndaelEncryptRound(aes_key, 99, buf, 10); /*incl final MixColumns*/
    } else {
        u32 aes4_key[4*5];
	    memset(aes4_key,0,16);
        if (j==2) {
        	memcpy((byte*)aes4_key+16, L, 16);
        	memcpy((byte*)aes4_key+32, I, 16);
        	memcpy((byte*)aes4_key+48, J, 16);
        	memcpy((byte*)aes4_key+64, L, 16);
        } else {
        	memcpy((byte*)aes4_key+16, J, 16);
        	memcpy((byte*)aes4_key+32, I, 16);
        	memcpy((byte*)aes4_key+48, L, 16);
	        memset((byte*)aes4_key+64,0,16);
        }
        correct_key(aes4_key+4,4*16);
        if (j==0) {
        	mult_block(i,I,buf); xor_bytes(buf, src, 16, buf);
        	rijndaelEncryptRound(aes4_key, 99, buf, 4);
        } else if (j==1 || j==2) {
        	mult_block((i-1)%8,I,delta);
        	for (i=3+(i-1)/8; i>0; i--) mult_block(2,I,I);
        	xor_bytes(delta, I, 16, delta);
        	xor_bytes(delta, src, 16, buf);
        	rijndaelEncryptRound(aes4_key, 99, buf, 4);
        } else if (j>=3 && i==0) {
        	mult_block(1<<(j-3),L,delta); xor_bytes(delta, src, 16, buf);
        	rijndaelEncryptRound(aes4_key, 99, buf, 4);
        	xor_bytes(buf, delta, 16, buf);
        } else {
        	mult_block(1<<(j-3),L,delta);
        	mult_block((i-1)%8,J,buf); xor_bytes(delta, buf, 16, delta);
        	for (i=3+(i-1)/8; i>0; i--) mult_block(2,J,J);
        	xor_bytes(delta, J, 16, delta);
        	xor_bytes(src, delta, 16, buf);
        	rijndaelEncryptRound(aes4_key, 99, buf, 4);
        	xor_bytes(buf, delta, 16, buf);
        }
    }
    memcpy(dst, buf, 16);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movq %r9, %rbx
movq %r8, %r14
movl %ecx, %ebp
movl %edx, %r15d
movq %rdi, %r8
cmpl $0x30, %esi
jne 0x1ec1
vmovups (%r8), %ymm0
vmovups 0x10(%r8), %ymm1
vmovups %ymm1, 0x50(%rsp)
vmovups %ymm0, 0x40(%rsp)
jmp 0x1ed5
movl %esi, %r9d
leaq 0x40(%rsp), %rdi
pushq $0x30
popq %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x1070
vmovaps 0x40(%rsp), %xmm0
vmovaps 0x50(%rsp), %xmm1
vmovaps 0x60(%rsp), %xmm2
vmovaps %xmm0, 0x30(%rsp)
vmovaps %xmm1, 0x20(%rsp)
vmovaps %xmm2, 0x70(%rsp)
cmpl $-0x1, %r15d
je 0x1f46
leaq 0x50(%rsp), %rcx
vxorps %xmm0, %xmm0, %xmm0
leaq 0x90(%rsp), %rax
vmovaps %xmm0, -0x10(%rax)
cmpl $0x2, %r15d
jne 0x1fe6
vmovaps 0x10(%rcx), %xmm0
vmovaps %xmm0, (%rax)
vmovaps 0x40(%rsp), %xmm0
vmovaps %xmm0, 0x10(%rax)
vmovaps (%rcx), %xmm0
vmovaps %xmm0, 0x20(%rax)
vmovaps 0x10(%rcx), %xmm0
jmp 0x2003
vxorps %xmm0, %xmm0, %xmm0
leaq 0x90(%rsp), %r15
vmovaps %xmm0, -0x10(%r15)
vmovups 0x40(%rsp), %ymm0
vmovups 0x50(%rsp), %ymm1
vmovups %ymm0, (%r15)
vmovups %ymm1, 0x10(%r15)
pushq $0x30
popq %rsi
movq %r15, %rdi
vzeroupper
callq 0x21fa
vmovups (%r15), %ymm0
vmovups 0x10(%r15), %ymm1
vmovups %ymm1, 0x40(%r15)
vmovups %ymm0, 0x30(%r15)
vmovups %ymm0, 0x60(%r15)
vmovups %ymm1, 0x70(%r15)
vmovaps (%r15), %xmm0
vmovaps %xmm0, 0x90(%r15)
leaq 0x20(%rsp), %rsi
movq %rsp, %r15
movl %ebp, %edi
movq %r15, %rdx
vzeroupper
callq 0x2212
pushq $0x10
popq %rdx
movq %r15, %rdi
movq %r14, %rsi
movq %r15, %rcx
callq 0x21da
leaq 0x80(%rsp), %rdi
pushq $0x63
popq %rsi
pushq $0xa
jmp 0x2092
vmovaps (%rcx), %xmm1
vmovaps %xmm1, (%rax)
vmovaps 0x40(%rsp), %xmm1
vmovaps %xmm1, 0x10(%rax)
vmovaps 0x10(%rcx), %xmm1
vmovaps %xmm1, 0x20(%rax)
vmovaps %xmm0, 0x30(%rax)
leaq 0x90(%rsp), %rdi
pushq $0x40
popq %rsi
vzeroupper
callq 0x21fa
testl %r15d, %r15d
je 0x2062
cmpl $0x2, %r15d
ja 0x2098
decl %ebp
movl %ebp, %edi
andl $0x7, %edi
leaq 0x30(%rsp), %r15
leaq 0x10(%rsp), %rdx
movq %r15, %rsi
callq 0x2212
shrl $0x3, %ebp
addl $0x3, %ebp
pushq $0x2
popq %r12
subl $0x1, %ebp
jb 0x20d8
movl %r12d, %edi
movq %r15, %rsi
movq %r15, %rdx
callq 0x2212
jmp 0x2049
leaq 0x30(%rsp), %rsi
movq %rsp, %r15
movl %ebp, %edi
movq %r15, %rdx
callq 0x2212
pushq $0x10
popq %rdx
movq %r15, %rdi
movq %r14, %rsi
movq %r15, %rcx
callq 0x21da
leaq 0x80(%rsp), %rdi
pushq $0x63
popq %rsi
pushq $0x4
popq %rcx
movq %r15, %rdx
jmp 0x2116
setle %r12b
testl %ebp, %ebp
setne %r13b
addb $-0x3, %r15b
pushq $0x1
popq %rax
shlxl %r15d, %eax, %edi
leaq 0x70(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0x2212
orb %r12b, %r13b
jne 0x2136
leaq 0x10(%rsp), %r15
pushq $0x10
popq %rbp
movq %rsp, %r12
movq %r15, %rdi
movq %r14, %rsi
jmp 0x21a5
leaq 0x30(%rsp), %rsi
pushq $0x10
popq %rbp
leaq 0x10(%rsp), %r15
movq %r15, %rdi
movl %ebp, %edx
movq %r15, %rcx
callq 0x21da
movq %rsp, %r12
movq %r15, %rdi
movq %r14, %rsi
movl %ebp, %edx
movq %r12, %rcx
callq 0x21da
leaq 0x80(%rsp), %rdi
pushq $0x63
popq %rsi
pushq $0x4
popq %rcx
movq %r12, %rdx
callq 0x1100
vmovaps (%rsp), %xmm0
vmovups %xmm0, (%rbx)
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
decl %ebp
movl %ebp, %edi
andl $0x7, %edi
leaq 0x20(%rsp), %r15
movq %rsp, %r12
movq %r15, %rsi
movq %r12, %rdx
callq 0x2212
pushq $0x10
popq %rdx
leaq 0x10(%rsp), %rcx
movq %rcx, %rdi
movq %r12, %rsi
callq 0x21da
shrl $0x3, %ebp
addl $0x3, %ebp
pushq $0x2
popq %r12
subl $0x1, %ebp
jb 0x2182
movl %r12d, %edi
movq %r15, %rsi
movq %r15, %rdx
callq 0x2212
jmp 0x216d
leaq 0x20(%rsp), %rsi
pushq $0x10
popq %rbp
leaq 0x10(%rsp), %r15
movq %r15, %rdi
movl %ebp, %edx
movq %r15, %rcx
callq 0x21da
movq %rsp, %r12
movq %r14, %rdi
movq %r15, %rsi
movl %ebp, %edx
movq %r12, %rcx
callq 0x21da
leaq 0x80(%rsp), %rdi
pushq $0x63
popq %rsi
pushq $0x4
popq %rcx
movq %r12, %rdx
callq 0x1100
movq %r12, %rdi
movq %r15, %rsi
movl %ebp, %edx
movq %r12, %rcx
callq 0x21da
jmp 0x211b