// File: /workspace/github_c_makefile_star2_O1/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 $0x78, %rsp
movq %r9, 0x20(%rsp)
movq %rdx, 0x28(%rsp)
vpxor %xmm0, %xmm0, %xmm0
vmovdqa %xmm0, 0x30(%rsp)
xorl %r14d, %r14d
movl $0x0, %r12d
movl $0x0, %r13d
testl %ecx, %ecx
je 0x13b0
movl %ecx, %eax
andl $0x1f, %eax
movl $0x20, %r13d
cmovnel %eax, %r13d
subl %r13d, %ecx
shrl $0x5, %ecx
movl %ecx, %r12d
movl %r8d, %edx
callq 0x10f0
testl %r12d, %r12d
movq 0x28(%rsp), %rdx
je 0x1517
movl %r13d, 0x48(%rsp)
leaq 0x10(%rdx), %rbp
xorl %r14d, %r14d
movq 0x3bc7(%rip), %rsi      # 0x4fa0
movw $0x7fff, %ax            # imm = 0x7FFF
kmovd %eax, %k1
kmovw %k1, 0xe(%rsp)
movq %rdx, %rbx
movq 0x20(%rsp), %r13
xorl %r15d, %r15d
movl %r12d, 0x4c(%rsp)
vmovdqu (%rdx,%r14), %xmm0
vpxor (%rsi), %xmm0, %xmm0
vmovdqa %xmm0, 0x10(%rsp)
movl $0x100, %edi            # imm = 0x100
leaq 0x10(%rsp), %rdx
movq %rdx, %rsi
callq 0x1100
movq %r14, %r12
orq $0x10, %r12
xorl %eax, %eax
movb (%rbp,%rax), %cl
xorb 0x10(%rsp,%rax), %cl
movb %cl, (%r13,%rax)
incq %rax
cmpq $0x10, %rax
jne 0x1422
movq 0x20(%rsp), %rax
vmovdqu (%rax,%r14), %xmm0
movq 0x3bae(%rip), %rax      # 0x4ff8
vpxor (%rax), %xmm0, %xmm0
vmovdqa %xmm0, 0x10(%rsp)
movl $0x100, %edi            # imm = 0x100
leaq 0x10(%rsp), %rdx
movq %rdx, %rsi
callq 0x1100
xorl %eax, %eax
movb (%rbx,%rax), %cl
xorb 0x10(%rsp,%rax), %cl
movb %cl, 0x10(%r13,%rax)
incq %rax
cmpq $0x10, %rax
jne 0x1468
movq 0x28(%rsp), %rdx
vmovups (%rdx,%r12), %xmm0
vxorps 0x30(%rsp), %xmm0, %xmm0
vmovaps %xmm0, 0x30(%rsp)
movq 0x3b5d(%rip), %rdi      # 0x4ff8
vmovdqa (%rdi), %xmm0
movq 0x3afa(%rip), %rsi      # 0x4fa0
vpxor (%rsi), %xmm0, %xmm1
vmovdqa %xmm1, (%rsi)
kmovw 0xe(%rsp), %k1
vmovdqu8 0x1(%rdi), %xmm1 {%k1} {z}
movzbl (%rdi), %eax
vpsrlw $0x7, %xmm1, %xmm1
vpaddw %xmm0, %xmm0, %xmm0
vpternlogd $0xe4, 0x1b2b(%rip){1to4}, %xmm1, %xmm0 # 0x3000
vmovdqu8 %xmm0, (%rdi) {%k1}
shrl $0x7, %eax
movb 0xf(%rdi), %cl
addb %cl, %cl
leaq 0x1b1a(%rip), %r8       # 0x3004
xorb (%rax,%r8), %cl
movb %cl, 0xf(%rdi)
incl %r15d
addq $0x20, %r14
addq $0x20, %r13
addq $0x20, %rbp
addq $0x20, %rbx
movl 0x4c(%rsp), %r12d
cmpl %r12d, %r15d
jne 0x13f7
movl 0x48(%rsp), %r13d
cmpl $0x10, %r13d
ja 0x15a1
movq 0x3a78(%rip), %r15      # 0x4fa0
leaq 0x50(%rsp), %rdx
movl $0x100, %edi            # imm = 0x100
movq %r15, %rsi
callq 0x1100
movq 0x28(%rsp), %rsi
movl %r14d, %eax
addq %rax, %rsi
movl %r13d, %r12d
testl %r13d, %r13d
movq 0x20(%rsp), %rdx
je 0x1569
addq %rax, %rdx
xorl %eax, %eax
movb (%rsi,%rax), %cl
xorb 0x50(%rsp,%rax), %cl
movb %cl, (%rdx,%rax)
incq %rax
cmpq %rax, %r12
jne 0x1557
vpxor %xmm0, %xmm0, %xmm0
leaq 0x10(%rsp), %rdi
vmovdqa %xmm0, (%rdi)
movq %r12, %rdx
callq 0x1110
cmpl $0x10, %r13d
je 0x158a
movb $-0x80, 0x10(%rsp,%r12)
vmovdqa 0x30(%rsp), %xmm0
vpxor 0x10(%rsp), %xmm0, %xmm0
vmovdqa %xmm0, 0x30(%rsp)
jmp 0x1688
movl %r14d, %ebx
vmovups (%rdx,%rbx), %xmm0
movq 0x39f0(%rip), %rax      # 0x4fa0
vxorps (%rax), %xmm0, %xmm0
leaq 0x10(%rsp), %rsi
vmovaps %xmm0, (%rsi)
leaq 0x50(%rsp), %rdx
movl $0x100, %edi            # imm = 0x100
callq 0x1100
movl %r13d, %ebp
movq 0x20(%rsp), %rax
leaq (%rax,%rbx), %rsi
addq $0x10, %rsi
addl $-0x10, %ebp
movl %ebp, %r15d
je 0x1605
addl $0x10, %r14d
addq %r14, %rax
addq 0x28(%rsp), %r14
xorl %ecx, %ecx
movb (%r14,%rcx), %dl
xorb 0x50(%rsp,%rcx), %dl
movb %dl, (%rax,%rcx)
incq %rcx
cmpq %rcx, %r15
jne 0x15f2
vxorps %xmm0, %xmm0, %xmm0
leaq 0x10(%rsp), %rdi
vmovaps %xmm0, (%rdi)
movq %r15, %rdx
callq 0x1110
cmpl $0xf, %ebp
ja 0x1625
movb $-0x80, 0x10(%rsp,%r15)
vmovdqa 0x50(%rsp), %xmm0
leaq 0x10(%rsp), %rdx
vmovdqa (%rdx), %xmm1
vpternlogq $0x96, 0x30(%rsp), %xmm1, %xmm0
vmovdqa %xmm0, 0x30(%rsp)
movq 0x28(%rsp), %r14
addq %rbx, %r14
movq 0x39a6(%rip), %r15      # 0x4ff8
vpxor (%r15), %xmm1, %xmm0
vmovdqa %xmm0, (%rdx)
movl $0x100, %edi            # imm = 0x100
movq %rdx, %rsi
callq 0x1100
movq %r14, %rdx
movq 0x20(%rsp), %rsi
addq %rbx, %rsi
xorl %eax, %eax
movb (%rdx,%rax), %cl
xorb 0x10(%rsp,%rax), %cl
movb %cl, (%rsi,%rax)
incq %rax
cmpq $0x10, %rax
jne 0x1675
cmpl $0x20, %r13d
je 0x1694
cmpl $0x10, %r13d
jne 0x16a1
movq %r15, %rdi
movq %r15, %rsi
callq 0x16f3
jmp 0x16bf
leaq 0x60(%rsp), %rbx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x1e6e
vmovdqa (%rbx), %xmm0
vpxor (%r15), %xmm0, %xmm0
vmovdqa %xmm0, (%r15)
vmovaps (%r15), %xmm0
leaq 0x30(%rsp), %rsi
vxorps (%rsi), %xmm0, %xmm0
vmovaps %xmm0, (%rsi)
movq 0x3908(%rip), %rdx      # 0x4fe0
movl $0x100, %edi            # imm = 0x100
callq 0x1100
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq