// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/aes256otrsv2/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], LdL[BLOCK], Llast[BLOCK], Z[BLOCK];
	uint8 Sum[BLOCK]={0};

	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(L,delta,LdL);
		xor(LdL,ciphertext+j,tmp);
		Encryption(KeyLen, tmp, tmp);
		xor(tmp,plaintext+j,ciphertext+(j+BLOCK));
		xor(Sum,plaintext+(j+BLOCK),Sum);
		mul2(L,L); // L <- 2L
	}
	/* 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);
		memcpy(Llast,L,BLOCK); //Llast=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));
		xor(L,delta,Llast); // Llast = L xor delta
		ozp(last-BLOCK,ciphertext+(j+BLOCK),tmp);
		xor(Sum,tmp,Sum);
		xor(Sum,Z,Sum);
		xor(Llast,tmp,tmp);
		Encryption(KeyLen,tmp,tmp);
		xor(tmp,plaintext+j,ciphertext+j);
	}
	/* TE generation */
	mul2(Llast,tmp);
	xor(Llast,tmp,Llast); //Llast = 3Llast
	xor(Sum,Llast,Sum);	//Sum = 3Llast xor Sum
	if(last == BLOCK || last == DBLOCK){//last = 16 or 32
		xor(Sum,delta,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, %r12
movq %rdx, %r15
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x20(%rsp)
testl %ecx, %ecx
je 0x1376
movl %ecx, %ebp
movl %ecx, %eax
andl $0x1f, %eax
pushq $0x20
popq %rcx
cmovnel %eax, %ecx
movq %rcx, 0x38(%rsp)
subl %ecx, %ebp
shrl $0x5, %ebp
jmp 0x1381
xorl %ebp, %ebp
movq $0x0, 0x38(%rsp)
movl %r8d, %edx
callq 0x10f0
addq $0x10, %r12
subl $0x1, %ebp
jb 0x144c
movq 0x3bfb(%rip), %r13      # 0x4f98
movq %r13, %rdi
movq %r15, %rsi
movl %ebp, 0x1c(%rsp)
movq %rsp, %rbp
movq %rbp, %rdx
callq 0x1324
movl $0x100, %edi            # imm = 0x100
movq %rbp, %rsi
movq %rbp, %rdx
callq 0x1100
leaq 0x10(%r15), %rbx
leaq -0x10(%r12), %r14
movq %rbp, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x1324
movq %r13, %rdi
movq 0x3be5(%rip), %rsi      # 0x4fc8
leaq 0x60(%rsp), %r13
movq %r13, %rdx
callq 0x1324
movq %r13, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x1324
movl $0x100, %edi            # imm = 0x100
movq %rbp, %rsi
movq %rbp, %rdx
callq 0x1100
movq %rbp, %rdi
movl 0x1c(%rsp), %ebp
movq %r15, %rsi
movq %r12, %rdx
callq 0x1324
leaq 0x20(%rsp), %rdi
movq %rbx, %rsi
movq %rdi, %rdx
callq 0x1324
movq 0x3b61(%rip), %rdi      # 0x4f98
movq %rdi, %rsi
callq 0x125f
addq $0x20, %r15
addq $0x20, %r12
jmp 0x138d
movq 0x38(%rsp), %rbp
cmpl $0x10, %ebp
ja 0x14cb
movq 0x3b3b(%rip), %rbx      # 0x4f98
leaq 0x50(%rsp), %r14
movl $0x100, %edi            # imm = 0x100
movq %rbx, %rsi
movq %r14, %rdx
callq 0x1100
addq $-0x10, %r12
movl %ebp, %edi
movq %r14, %rsi
movq %r15, %r14
movq %r15, %rdx
movq %r12, %rcx
callq 0x1612
vxorps %xmm0, %xmm0, %xmm0
movq %rsp, %rdi
vmovaps %xmm0, (%rdi)
movl %ebp, %r15d
movq %r14, %rsi
movq %r15, %rdx
callq 0x1110
cmpl $0x10, %ebp
je 0x14ac
movb $-0x80, (%rsp,%r15)
movq %rsp, %rdi
leaq 0x20(%rsp), %rdx
movq %rdx, %rsi
callq 0x1324
vmovaps (%rbx), %xmm0
vmovaps %xmm0, 0x40(%rsp)
jmp 0x159c
movq 0x3ac6(%rip), %rdi      # 0x4f98
movq %rsp, %r14
movq %r15, %rsi
movq %r14, %rdx
callq 0x1324
leaq 0x50(%rsp), %r13
movl $0x100, %edi            # imm = 0x100
movq %r14, %rsi
movq %r13, %rdx
callq 0x1100
leal -0x10(%rbp), %r14d
leaq 0x10(%r15), %rdx
movl %r14d, %edi
movq %r13, %rsi
movq %r12, %rcx
callq 0x1612
movq 0x3ab6(%rip), %rsi      # 0x4fc8
leaq 0x40(%rsp), %rdx
movq 0x3a7a(%rip), %rdi      # 0x4f98
callq 0x1324
vxorps %xmm0, %xmm0, %xmm0
movq %rsp, %rdi
vmovaps %xmm0, (%rdi)
movq %r12, %rsi
movq %r14, %rdx
callq 0x1110
cmpl $0xf, %r14d
ja 0x1544
movb $-0x80, (%rsp,%r14)
movq %rsp, %rbx
leaq 0x20(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x1324
leaq 0x50(%rsp), %rsi
movq %r14, %rdi
movq %r14, %rdx
callq 0x1324
leaq 0x40(%rsp), %rdi
movq %rbx, %rsi
movq %rbx, %rdx
callq 0x1324
movl $0x100, %edi            # imm = 0x100
movq %rbx, %rsi
movq %rbx, %rdx
callq 0x1100
addq $-0x10, %r12
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x1324
leaq 0x40(%rsp), %rbx
movq %rsp, %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x125f
movq %rbx, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x1324
leaq 0x20(%rsp), %rdi
movq %rbx, %rsi
movq %rdi, %rdx
callq 0x1324
cmpl $0x20, %ebp
je 0x15d7
cmpl $0x10, %ebp
jne 0x15eb
movq 0x39ea(%rip), %rsi      # 0x4fc8
leaq 0x20(%rsp), %rdi
movq %rdi, %rdx
callq 0x1324
movq 0x39ee(%rip), %rdx      # 0x4fe0
leaq 0x20(%rsp), %rsi
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