// File: /workspace/github_c_makefile_star2_O2/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 $0x58, %rsp
movq %r9, %r12
movq %rdx, %r15
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x30(%rsp)
testl %ecx, %ecx
je 0x137d
movl %ecx, %ebx
movl %ecx, %eax
andl $0x1f, %eax
pushq $0x20
popq %rcx
cmovnel %eax, %ecx
movq %rcx, 0x28(%rsp)
subl %ecx, %ebx
shrl $0x5, %ebx
jmp 0x1388
xorl %ebx, %ebx
movq $0x0, 0x28(%rsp)
movl %r8d, %edx
callq 0x10f0
addq $0x10, %r12
movq 0x3c5d(%rip), %rax      # 0x4ff8
movq %rax, (%rsp)
subl $0x1, %ebx
jb 0x1467
movq 0x3bf1(%rip), %rdi      # 0x4fa0
movq %r15, %rsi
leaq 0x10(%rsp), %rbp
movq %rbp, %rdx
callq 0x12bf
movl $0x100, %edi            # imm = 0x100
movq %rbp, %rsi
movq %rbp, %rdx
callq 0x1100
leaq 0x10(%r15), %r13
leaq -0x10(%r12), %r14
movq %rbp, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x12bf
movl %ebx, 0xc(%rsp)
movq %r15, %rbx
movq %r12, %r15
movq 0x3c01(%rip), %r12      # 0x4ff8
movq %r12, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x12bf
movl $0x100, %edi            # imm = 0x100
movq %rbp, %rsi
movq %rbp, %rdx
callq 0x1100
movq %rbp, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x12bf
leaq 0x30(%rsp), %rdi
movq %r13, %rsi
movq %rdi, %rdx
callq 0x12bf
movq 0x3b66(%rip), %rdi      # 0x4fa0
movq %r12, %rsi
movq %rdi, %rdx
callq 0x12bf
movq %r12, %rdi
movq %r12, %rsi
movq %r15, %r12
movq %rbx, %r15
movl 0xc(%rsp), %ebx
callq 0x12d6
addq $0x20, %r15
addq $0x20, %r12
jmp 0x139f
movq 0x28(%rsp), %rbp
cmpl $0x10, %ebp
ja 0x14d8
movq 0x3b28(%rip), %rsi      # 0x4fa0
leaq 0x40(%rsp), %rbx
movl $0x100, %edi            # imm = 0x100
movq %rsi, (%rsp)
movq %rbx, %rdx
callq 0x1100
addq $-0x10, %r12
movl %ebp, %edi
movq %rbx, %rsi
movq %r15, %rdx
movq %r12, %rcx
callq 0x15f7
vxorps %xmm0, %xmm0, %xmm0
leaq 0x10(%rsp), %rdi
vmovaps %xmm0, (%rdi)
movl %ebp, %ebx
movq %r15, %rsi
movq %rbx, %rdx
callq 0x1110
cmpl $0x10, %ebp
je 0x14c6
movb $-0x80, 0x10(%rsp,%rbx)
leaq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rdx
movq %rdx, %rsi
jmp 0x1590
movq 0x3ac1(%rip), %rdi      # 0x4fa0
leaq 0x10(%rsp), %r14
movq %r15, %rsi
movq %r14, %rdx
callq 0x12bf
leaq 0x40(%rsp), %r13
movl $0x100, %edi            # imm = 0x100
movq %r14, %rsi
movq %r13, %rdx
callq 0x1100
leal -0x10(%rbp), %ebx
leaq 0x10(%r15), %rdx
movl %ebx, %edi
movq %r13, %rsi
movq %r12, %rcx
callq 0x15f7
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, (%r14)
movq %r14, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x1110
cmpl $0xf, %ebx
ja 0x1539
movb $-0x80, 0x10(%rsp,%rbx)
leaq 0x40(%rsp), %rsi
leaq 0x30(%rsp), %rbx
movq %rbx, %rdi
movq %rbx, %rdx
callq 0x12bf
leaq 0x10(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x12bf
movq 0x3a90(%rip), %rdi      # 0x4ff8
movq %r14, %rsi
movq %r14, %rdx
callq 0x12bf
movl $0x100, %edi            # imm = 0x100
movq %r14, %rsi
movq %r14, %rdx
callq 0x1100
addq $-0x10, %r12
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x12bf
cmpl $0x20, %ebp
je 0x159f
cmpl $0x10, %ebp
jne 0x15b0
movq (%rsp), %r14
movq %r14, %rdi
movq %r14, %rsi
callq 0x1612
jmp 0x15bf
movq (%rsp), %r14
movq %r14, %rdi
movq %r14, %rsi
callq 0x165d
leaq 0x30(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x12bf
movq 0x3a07(%rip), %rdx      # 0x4fe0
movl $0x100, %edi            # imm = 0x100
movq %rbx, %rsi
callq 0x1100
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq