// File: /workspace/github_c_makefile_star2_O1/mjosaarinen[P]brutus/crypto_aead/aes256otrsv3/ref/OTR.c
// Function: DFunc
int DFunc(
		  const uint8 *nonce,
		  uint32 nonce_len,
		  const uint8 *ciphertext,
		  uint32 ci_len,
		  uint32 t_len,
		  uint8 *plaintext)
{
	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(ci_len != 0){
		last = ci_len % DBLOCK;
		if(last == 0) last = DBLOCK;
		ell = (ci_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(Ls,ciphertext+j,tmp);
		Encryption(KeyLen, tmp, tmp);
		xor(tmp,ciphertext+(j+BLOCK),plaintext+j);
		xor(L,plaintext+j,tmp);
		Encryption(KeyLen, tmp, tmp);
		xor(tmp,ciphertext+j,plaintext+(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,ciphertext+j,plaintext+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*/
		ozp(last-BLOCK,ciphertext+(j+BLOCK),tmp); //tmp = ozp(C[m])
		xor(Sum,tmp,Sum);
		xor(Ls,tmp,tmp);
		Encryption(KeyLen,tmp,tmp);
		xor(tmp,ciphertext+j,plaintext+j);
		xor(L,plaintext+j,tmp);
		Encryption(KeyLen,tmp,Z);
		xorp(last-BLOCK,Z,ciphertext+(j+BLOCK),plaintext+(j+BLOCK));
		xor(Sum,Z,Sum);
		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, %rbp
movq %rdx, %r14
vpxor %xmm0, %xmm0, %xmm0
vmovdqa %xmm0, 0x20(%rsp)
movq $0x0, (%rsp)
movl $0x0, %ebx
movl $0x0, %r13d
testl %ecx, %ecx
je 0x1800
movl %ecx, %eax
andl $0x1f, %eax
movl $0x20, %r13d
cmovnel %eax, %r13d
subl %r13d, %ecx
shrl $0x5, %ecx
movl %ecx, %ebx
movl %r8d, %edx
callq 0x10f0
testl %ebx, %ebx
je 0x1973
movl %ebx, 0x34(%rsp)
movq %r13, 0x58(%rsp)
leaq 0x10(%r14), %r13
xorl %ecx, %ecx
movq 0x37d2(%rip), %rdx      # 0x4ff8
movw $0x7fff, %ax            # imm = 0x7FFF
kmovd %eax, %k1
kmovw %k1, 0xe(%rsp)
movq %r14, 0x38(%rsp)
movq %r14, %rbx
movq %rbp, %r12
xorl %r15d, %r15d
movq %rcx, (%rsp)
movq %rbp, %r14
movq 0x38(%rsp), %rax
vmovdqu (%rax,%rcx), %xmm0
vpxor (%rdx), %xmm0, %xmm0
vmovdqa %xmm0, 0x10(%rsp)
movl $0x100, %edi            # imm = 0x100
leaq 0x10(%rsp), %rdx
movq %rdx, %rsi
callq 0x1100
movq (%rsp), %rdx
movq %rdx, %rbp
orq $0x10, %rbp
xorl %eax, %eax
movb (%r13,%rax), %cl
xorb 0x10(%rsp,%rax), %cl
movb %cl, (%r12,%rax)
incq %rax
cmpq $0x10, %rax
jne 0x187c
vmovdqu (%r14,%rdx), %xmm0
movq 0x3701(%rip), %rax      # 0x4fa0
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(%r12,%rax)
incq %rax
cmpq $0x10, %rax
jne 0x18bd
vmovups (%r14,%rbp), %xmm0
movq %r14, %rbp
vxorps 0x20(%rsp), %xmm0, %xmm0
vmovaps %xmm0, 0x20(%rsp)
movq 0x370a(%rip), %rdx      # 0x4ff8
vmovdqa (%rdx), %xmm0
movq 0x36a7(%rip), %rax      # 0x4fa0
vpxor (%rax), %xmm0, %xmm1
vmovdqa %xmm1, (%rax)
kmovw 0xe(%rsp), %k1
vmovdqu8 0x1(%rdx), %xmm1 {%k1} {z}
movzbl (%rdx), %eax
vpsrlw $0x7, %xmm1, %xmm1
vpaddw %xmm0, %xmm0, %xmm0
vpternlogd $0xe4, 0x16d8(%rip){1to4}, %xmm1, %xmm0 # 0x3000
vmovdqu8 %xmm0, (%rdx) {%k1}
shrl $0x7, %eax
movb 0xf(%rdx), %cl
addb %cl, %cl
leaq 0x16c7(%rip), %rsi      # 0x3004
xorb (%rax,%rsi), %cl
movb %cl, 0xf(%rdx)
incl %r15d
movq (%rsp), %rcx
addq $0x20, %rcx
addq $0x20, %r12
addq $0x20, %r13
addq $0x20, %rbx
cmpl 0x34(%rsp), %r15d
jne 0x1842
movq %rcx, (%rsp)
movq 0x58(%rsp), %r13
movq 0x38(%rsp), %r14
cmpl $0x10, %r13d
ja 0x19ee
movq 0x3620(%rip), %r15      # 0x4fa0
leaq 0x40(%rsp), %rdx
movl $0x100, %edi            # imm = 0x100
movq %r15, %rsi
callq 0x1100
movl (%rsp), %eax
addq %rax, %rbp
movl %r13d, %r12d
testl %r13d, %r13d
je 0x19b9
addq %rax, %r14
xorl %eax, %eax
movb (%r14,%rax), %cl
xorb 0x40(%rsp,%rax), %cl
movb %cl, (%rbp,%rax)
incq %rax
cmpq %rax, %r12
jne 0x19a5
vpxor %xmm0, %xmm0, %xmm0
leaq 0x10(%rsp), %rdi
vmovdqa %xmm0, (%rdi)
movq %rbp, %rsi
movq %r12, %rdx
callq 0x1110
cmpl $0x10, %r13d
je 0x19dd
movb $-0x80, 0x10(%rsp,%r12)
vmovaps 0x20(%rsp), %xmm0
vxorps 0x10(%rsp), %xmm0, %xmm0
jmp 0x1ac7
leal -0x10(%r13), %r12d
movl (%rsp), %ebx
leaq (%r14,%rbx), %rsi
addq $0x10, %rsi
vpxor %xmm0, %xmm0, %xmm0
leaq 0x10(%rsp), %rdi
vmovdqa %xmm0, (%rdi)
movq %r12, %rdx
callq 0x1110
cmpl $0xf, %r12d
ja 0x1a1e
movb $-0x80, 0x10(%rsp,%r12)
leaq 0x10(%rsp), %rdx
vmovdqa (%rdx), %xmm0
vpxor 0x20(%rsp), %xmm0, %xmm1
vmovdqa %xmm1, 0x20(%rsp)
movq 0x35be(%rip), %r15      # 0x4ff8
vpxor (%r15), %xmm0, %xmm0
vmovdqa %xmm0, (%rdx)
movl $0x100, %edi            # imm = 0x100
movq %rdx, %rsi
callq 0x1100
leaq (%r14,%rbx), %rax
addq %rbp, %rbx
xorl %ecx, %ecx
movb (%rax,%rcx), %dl
xorb 0x10(%rsp,%rcx), %dl
movb %dl, (%rbx,%rcx)
incq %rcx
cmpq $0x10, %rcx
jne 0x1a59
vmovups (%rbx), %xmm0
movq 0x3529(%rip), %rax      # 0x4fa0
vxorps (%rax), %xmm0, %xmm0
leaq 0x10(%rsp), %rsi
vmovaps %xmm0, (%rsi)
leaq 0x40(%rsp), %rdx
movl $0x100, %edi            # imm = 0x100
callq 0x1100
testq %r12, %r12
je 0x1abb
movq (%rsp), %rax
addl $0x10, %eax
addq %rax, %rbp
addq %rax, %r14
xorl %eax, %eax
movb (%r14,%rax), %cl
xorb 0x40(%rsp,%rax), %cl
movb %cl, (%rbp,%rax)
incq %rax
cmpq %rax, %r12
jne 0x1aa7
vmovaps 0x40(%rsp), %xmm0
vxorps 0x20(%rsp), %xmm0, %xmm0
vmovaps %xmm0, 0x20(%rsp)
cmpl $0x20, %r13d
je 0x1ad9
cmpl $0x10, %r13d
jne 0x1ae6
movq %r15, %rdi
movq %r15, %rsi
callq 0x16f3
jmp 0x1b04
leaq 0x60(%rsp), %rbx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x1e6e
vmovaps (%rbx), %xmm0
vxorps (%r15), %xmm0, %xmm0
vmovaps %xmm0, (%r15)
vmovaps (%r15), %xmm0
leaq 0x20(%rsp), %rsi
vxorps (%rsi), %xmm0, %xmm0
vmovaps %xmm0, (%rsi)
movq 0x34c3(%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