// File: /workspace/github_c_makefile_star2_O1/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 $0x148, %rsp            # imm = 0x148
movq %r9, %rbx
movq %r8, %r14
movl %ecx, %ebp
movl %edx, %r15d
movq %rdi, %r8
cmpl $0x30, %esi
jne 0x2187
vmovups (%r8), %ymm0
vmovups 0x10(%r8), %ymm1
vmovups %ymm1, 0x60(%rsp)
vmovups %ymm0, 0x50(%rsp)
jmp 0x219d
movl %esi, %r9d
leaq 0x50(%rsp), %rdi
movl $0x30, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x1070
vmovaps 0x50(%rsp), %xmm0
vmovaps 0x60(%rsp), %xmm1
vmovaps 0x70(%rsp), %xmm2
vmovaps %xmm0, 0x40(%rsp)
vmovaps %xmm1, 0x30(%rsp)
vmovaps %xmm2, 0x80(%rsp)
cmpl $-0x1, %r15d
je 0x2211
leaq 0x60(%rsp), %rcx
vxorps %xmm0, %xmm0, %xmm0
leaq 0xa0(%rsp), %rax
vmovaps %xmm0, -0x10(%rax)
cmpl $0x2, %r15d
jne 0x22d4
vmovaps 0x10(%rcx), %xmm0
vmovaps %xmm0, (%rax)
vmovaps 0x50(%rsp), %xmm0
vmovaps %xmm0, 0x10(%rax)
vmovaps (%rcx), %xmm0
vmovaps %xmm0, 0x20(%rax)
vmovaps 0x10(%rcx), %xmm0
jmp 0x22f1
vxorps %xmm0, %xmm0, %xmm0
leaq 0xa0(%rsp), %rax
vmovaps %xmm0, -0x10(%rax)
vmovups 0x50(%rsp), %ymm0
vmovups 0x60(%rsp), %ymm1
vmovups %ymm0, (%rax)
vmovups %ymm1, 0x10(%rax)
movl $0x4, %ecx
movl 0x90(%rsp,%rcx,4), %edx
movbel %edx, 0x90(%rsp,%rcx,4)
incq %rcx
cmpq $0x10, %rcx
jne 0x223c
vmovups (%rax), %ymm0
vmovups 0x10(%rax), %ymm1
leaq 0x90(%rsp), %r15
vmovups %ymm1, 0x50(%r15)
vmovups %ymm0, 0x40(%r15)
vmovups (%rax), %ymm0
vmovups 0x10(%rax), %ymm1
vmovups %ymm0, 0x70(%r15)
vmovups %ymm1, 0x80(%r15)
vmovaps (%rax), %xmm0
vmovaps %xmm0, 0xa0(%r15)
leaq 0x30(%rsp), %rsi
leaq 0x10(%rsp), %r12
movl %ebp, %edi
movq %r12, %rdx
vzeroupper
callq 0x24b7
vmovups (%r14), %xmm0
vxorps (%r12), %xmm0, %xmm0
vmovaps %xmm0, (%r12)
movq %r15, %rdi
movl $0x63, %esi
movq %r12, %rdx
movl $0xa, %ecx
jmp 0x23bf
vmovaps (%rcx), %xmm1
vmovaps %xmm1, (%rax)
vmovaps 0x50(%rsp), %xmm1
vmovaps %xmm1, 0x10(%rax)
vmovaps 0x10(%rcx), %xmm1
vmovaps %xmm1, 0x20(%rax)
vmovaps %xmm0, 0x30(%rax)
movl $0x4, %eax
movl 0x90(%rsp,%rax,4), %ecx
movbel %ecx, 0x90(%rsp,%rax,4)
incq %rax
cmpq $0x14, %rax
jne 0x22fb
testl %r15d, %r15d
je 0x2384
cmpl $0x2, %r15d
ja 0x23e0
decl %ebp
movl %ebp, %edi
andl $0x7, %edi
leaq 0x40(%rsp), %r15
movq %rsp, %rdx
movq %r15, %rsi
vzeroupper
callq 0x24b7
shrl $0x3, %ebp
addl $0x3, %ebp
movl $0x2, %edi
movq %r15, %rsi
movq %r15, %rdx
callq 0x24b7
decl %ebp
jne 0x2343
vmovaps 0x40(%rsp), %xmm0
vxorps (%rsp), %xmm0, %xmm0
vmovaps %xmm0, (%rsp)
vxorps (%r14), %xmm0, %xmm0
leaq 0x10(%rsp), %rdx
vmovaps %xmm0, (%rdx)
leaq 0x90(%rsp), %rdi
movl $0x63, %esi
jmp 0x23ba
leaq 0x40(%rsp), %rsi
leaq 0x10(%rsp), %r15
movl %ebp, %edi
movq %r15, %rdx
vzeroupper
callq 0x24b7
vmovups (%r14), %xmm0
vxorps (%r15), %xmm0, %xmm0
vmovaps %xmm0, (%r15)
leaq 0x90(%rsp), %rdi
movl $0x63, %esi
movq %r15, %rdx
movl $0x4, %ecx
callq 0x1100
vmovaps 0x10(%rsp), %xmm0
vmovups %xmm0, (%rbx)
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
setle %r12b
testl %ebp, %ebp
setne %r13b
addb $-0x3, %r15b
movl $0x1, %eax
shlxl %r15d, %eax, %edi
leaq 0x80(%rsp), %rsi
movq %rsp, %rdx
vzeroupper
callq 0x24b7
orb %r12b, %r13b
jne 0x241d
vmovaps (%rsp), %xmm0
vmovaps %xmm0, 0x20(%rsp)
jmp 0x2479
decl %ebp
movl %ebp, %edi
andl $0x7, %edi
leaq 0x30(%rsp), %r15
leaq 0x10(%rsp), %r12
movq %r15, %rsi
movq %r12, %rdx
callq 0x24b7
vmovaps (%r12), %xmm0
vxorps (%rsp), %xmm0, %xmm0
vmovaps %xmm0, (%rsp)
shrl $0x3, %ebp
addl $0x3, %ebp
movl $0x2, %edi
movq %r15, %rsi
movq %r15, %rdx
callq 0x24b7
decl %ebp
jne 0x244f
vmovaps 0x30(%rsp), %xmm0
vxorps (%rsp), %xmm0, %xmm0
vmovaps %xmm0, 0x20(%rsp)
vmovaps %xmm0, (%rsp)
vxorps (%r14), %xmm0, %xmm0
leaq 0x10(%rsp), %r14
vmovaps %xmm0, (%r14)
leaq 0x90(%rsp), %rdi
movl $0x63, %esi
movq %r14, %rdx
movl $0x4, %ecx
callq 0x1100
vmovaps 0x20(%rsp), %xmm0
vxorps (%r14), %xmm0, %xmm0
vmovaps %xmm0, (%r14)
jmp 0x23c4