// File: /workspace/github_c_makefile_star2_O3/mjosaarinen[P]brutus/crypto_aead/aes256otrsv3/ref/OTR.c
// Function: EFunc
int EFunc(
		  const uint8 *nonce,
		  uint32 nonce_len,
		  const uint8 *plaintext,
		  uint32 pl_len,
		  uint32 t_len,
		  uint8 *ciphertext)
{
	uint32 i,j = 0;
	uint32 ell=0; /*number of 2BLOCK-byte chunks, excl. last one*/
	uint32 last=0; /*number of bytes in the last chunks*/

	uint8 tmp[BLOCK], Z[BLOCK];
	uint8 Sum[BLOCK]={0};
	uint8 *La; 

	if(pl_len != 0){
		last = pl_len % DBLOCK;
		if(last == 0) last = DBLOCK;
		ell = (pl_len-last) / DBLOCK; /* plaintext length = 2BLOCK*ell + last (non-zero)*/
	}

	nEnc(nonce, nonce_len, t_len);

	/* 2-round Feistel for the full chunks */
	for(i = 0; i < ell; i++, j = j+DBLOCK){
		xor(L,plaintext+j,tmp);
		Encryption(KeyLen, tmp, tmp);
		xor(tmp,plaintext+(j+BLOCK),ciphertext+j);
		xor(Ls,ciphertext+j,tmp);
		Encryption(KeyLen, tmp, tmp);
		xor(tmp,plaintext+j,ciphertext+(j+BLOCK));
		xor(Sum,plaintext+(j+BLOCK),Sum);
		xor(L,Ls,L); 
		mul2(Ls,Ls); 
	}
	/* Last chunk */
	if(last <= BLOCK){ 	/*odd block, including the case pl_len = 0 (no plaintext)*/
		Encryption(KeyLen, L, Z);
		xorp(last, Z,plaintext+j,ciphertext+j);
		ozp(last,plaintext+j,tmp);
		xor(tmp,Sum,Sum);
		La = L;
	}
	else{/*even blocks, last > BLOCK always holds. 2-round Feistel with last swap*/
		xor(L,plaintext+j,tmp);
		Encryption(KeyLen,tmp,Z);
		xorp(last-BLOCK,Z,plaintext+(j+BLOCK),ciphertext+(j+BLOCK));
		ozp(last-BLOCK,ciphertext+(j+BLOCK),tmp);
		xor(Sum,Z,Sum);
		xor(Sum,tmp,Sum);
		xor(Ls,tmp,tmp);
		Encryption(KeyLen,tmp,tmp);
		xor(tmp,plaintext+j,ciphertext+j);
		La = Ls;
	}
	/* TE generation */
	if(last == BLOCK || last == DBLOCK){/*[last = 16 or 32] = integral block*/
		mul7(La,La);
	}
	else {/*partial block*/
		mul3twice(La,La);
	}
	xor(Sum, La, Sum);	/*Sum = (3^2 or 7)La xor Sum*/
	Encryption(KeyLen,Sum,TE);
	return SUCCESS;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
vpxor %xmm0, %xmm0, %xmm0
vmovdqa %xmm0, 0x20(%rsp)
testl %ecx, %ecx
je 0x15a0
movq %r9, %r13
movl %ecx, %r15d
movq %rdx, %r12
movl %ecx, %ebx
andl $0x1f, %ebx
movl $0x20, %ebp
cmovnel %ebx, %ebp
subl %ebp, %r15d
movl %r8d, %edx
callq 0x10f0
xorl %r14d, %r14d
cmpl $0x20, %r15d
movl %ebp, 0x1c(%rsp)
jb 0x153c
movq %rbx, 0x60(%rsp)
shrl $0x5, %r15d
movl %r15d, 0x3c(%rsp)
movq %r13, %rbp
leaq 0x10(%r12), %r13
xorl %r14d, %r14d
movq 0x3bb0(%rip), %rdx      # 0x4fa0
movw $0x7fff, %ax            # imm = 0x7FFF
kmovd %eax, %k1
kmovw %k1, 0x1a(%rsp)
movq %r12, %rbx
movq %rbp, 0x58(%rsp)
xorl %r15d, %r15d
movq %r12, 0x68(%rsp)
vmovdqu (%r12,%r14), %xmm0
vpxor (%rdx), %xmm0, %xmm0
vmovdqa %xmm0, (%rsp)
movl $0x100, %edi            # imm = 0x100
movq %rsp, %rdx
movq %rdx, %rsi
callq 0x1100
movq %r14, %r12
orq $0x10, %r12
xorl %eax, %eax
movb (%r13,%rax), %cl
xorb (%rsp,%rax), %cl
movb %cl, (%rbp,%rax)
incq %rax
cmpq $0x10, %rax
jne 0x1436
movq 0x58(%rsp), %rax
vmovdqu (%rax,%r14), %xmm0
movq 0x3b9b(%rip), %rax      # 0x4ff8
vpxor (%rax), %xmm0, %xmm0
vmovdqa %xmm0, (%rsp)
movl $0x100, %edi            # imm = 0x100
movq %rsp, %rdx
movq %rdx, %rsi
callq 0x1100
xorl %eax, %eax
movb (%rbx,%rax), %cl
xorb (%rsp,%rax), %cl
movb %cl, 0x10(%rbp,%rax)
incq %rax
cmpq $0x10, %rax
jne 0x1478
movq 0x68(%rsp), %rax
vmovups (%rax,%r12), %xmm0
movq %rax, %r12
vxorps 0x20(%rsp), %xmm0, %xmm0
vmovaps %xmm0, 0x20(%rsp)
movq 0x3b4c(%rip), %rsi      # 0x4ff8
vmovaps (%rsi), %xmm0
movq 0x3ae9(%rip), %rdx      # 0x4fa0
vxorps (%rdx), %xmm0, %xmm0
vmovaps %xmm0, (%rdx)
movzbl (%rsi), %eax
vmovd %eax, %xmm0
vpbroadcastb %xmm0, %xmm0
kmovw 0x1a(%rsp), %k1
vmovdqu8 0x1(%rsi), %xmm1 {%k1} {z}
vpalignr $0xf, %xmm0, %xmm1, %xmm0 # xmm0 = xmm0[15],xmm1[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14]
vpsrlw $0x7, %xmm1, %xmm1
vpaddw %xmm0, %xmm0, %xmm0
vpternlogd $0xe4, 0x1b0b(%rip){1to4}, %xmm1, %xmm0 # 0x3000
vmovdqu8 %xmm0, (%rsi) {%k1}
shrl $0x7, %eax
movb 0xf(%rsi), %cl
addb %cl, %cl
leaq 0x1b16(%rip), %rdi      # 0x3020
xorb (%rax,%rdi), %cl
movb %cl, 0xf(%rsi)
incl %r15d
addq $0x20, %r14
addq $0x20, %rbp
addq $0x20, %r13
addq $0x20, %rbx
cmpl 0x3c(%rsp), %r15d
jne 0x140e
movq 0x58(%rsp), %r13
movl 0x1c(%rsp), %ebp
movq 0x60(%rsp), %rbx
cmpl $0x11, %ebp
jae 0x15f0
movq 0x3a54(%rip), %rsi      # 0x4fa0
leaq 0x40(%rsp), %rdx
movl $0x100, %edi            # imm = 0x100
callq 0x1100
movl %r14d, %eax
addq %rax, %r12
addq %rax, %r13
movl %ebx, %r15d
xorl %eax, %eax
movb (%r12,%rax), %cl
xorb 0x40(%rsp,%rax), %cl
movb %cl, (%r13,%rax)
incq %rax
cmpq %rax, %r15
jne 0x1569
vpxor %xmm0, %xmm0, %xmm0
movq %rsp, %rdi
vmovdqa %xmm0, (%rdi)
movq %r12, %rsi
movq %r15, %rdx
callq 0x1110
movl $0x10, %edx
cmpl $0x10, %ebx
jne 0x15cc
jmp 0x15d3
movl %r8d, %edx
callq 0x10f0
movq 0x39f1(%rip), %rsi      # 0x4fa0
leaq 0x40(%rsp), %rdx
movl $0x100, %edi            # imm = 0x100
callq 0x1100
vpxor %xmm0, %xmm0, %xmm0
vmovdqa %xmm0, (%rsp)
xorl %ebx, %ebx
xorl %r15d, %r15d
movb $-0x80, (%rsp,%r15)
movl %ebx, %edx
vmovdqa 0x20(%rsp), %xmm0
vpxor (%rsp), %xmm0, %xmm0
vmovdqa %xmm0, 0x20(%rsp)
movq 0x39b5(%rip), %r15      # 0x4fa0
jmp 0x16eb
movl %r14d, %ebx
vmovups (%r12,%rbx), %xmm0
movq 0x39a0(%rip), %rax      # 0x4fa0
vxorps (%rax), %xmm0, %xmm0
movq %rsp, %rsi
vmovaps %xmm0, (%rsi)
leaq 0x40(%rsp), %rdx
movl $0x100, %edi            # imm = 0x100
callq 0x1100
addl $-0x10, %ebp
je 0x1680
movq %r12, %rax
leaq (%rbx,%r13), %r12
addq $0x10, %r12
movl %ebp, %r15d
movq %rax, %r14
addq %rbx, %rax
addq $0x10, %rax
xorl %ecx, %ecx
movb (%rax,%rcx), %dl
xorb 0x40(%rsp,%rcx), %dl
movb %dl, (%r12,%rcx)
incq %rcx
cmpq %rcx, %r15
jne 0x1639
movl $0x10, %eax
subq %r15, %rax
xorl %edx, %edx
cmpl $0x10, %ebp
cmovbq %rax, %rdx
leaq (%rsp,%r15), %rdi
xorl %esi, %esi
callq 0x1030
movq %rsp, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x1110
cmpl $0xf, %ebp
movq %r14, %r12
jbe 0x168c
jmp 0x1691
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, (%rsp)
xorl %r15d, %r15d
movb $-0x80, (%rsp,%r15)
vmovdqa 0x40(%rsp), %xmm0
movq %rsp, %rdx
vmovdqa (%rdx), %xmm1
vpternlogq $0x96, 0x20(%rsp), %xmm1, %xmm0
vmovdqa %xmm0, 0x20(%rsp)
addq %rbx, %r12
movq 0x3941(%rip), %r15      # 0x4ff8
vpxor (%r15), %xmm1, %xmm0
vmovdqa %xmm0, (%rdx)
movl $0x100, %edi            # imm = 0x100
movq %rdx, %rsi
callq 0x1100
addq %rbx, %r13
xorl %eax, %eax
movl 0x1c(%rsp), %edx
movb (%r12,%rax), %cl
xorb (%rsp,%rax), %cl
movb %cl, (%r13,%rax)
incq %rax
cmpq $0x10, %rax
jne 0x16d6
cmpl $0x20, %edx
je 0x16f5
cmpl $0x10, %edx
jne 0x1702
movq %r15, %rdi
movq %r15, %rsi
callq 0x1757
jmp 0x1720
leaq 0x70(%rsp), %rbx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x1f57
vmovdqa (%rbx), %xmm0
vpxor (%r15), %xmm0, %xmm0
vmovdqa %xmm0, (%r15)
vmovaps (%r15), %xmm0
leaq 0x20(%rsp), %rsi
vxorps (%rsi), %xmm0, %xmm0
vmovaps %xmm0, (%rsi)
movq 0x38a7(%rip), %rdx      # 0x4fe0
movl $0x100, %edi            # imm = 0x100
callq 0x1100
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq