// File: /workspace/github_c_makefile_star2_O2/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 $0x58, %rsp
movq %r9, %r15
movq %rdx, %r12
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x20(%rsp)
testl %ecx, %ecx
je 0x16ca
movl %ecx, %ebp
movl %ecx, %eax
andl $0x1f, %eax
pushq $0x20
popq %rcx
cmovnel %eax, %ecx
movq %rcx, 0x8(%rsp)
subl %ecx, %ebp
shrl $0x5, %ebp
jmp 0x16d5
xorl %ebp, %ebp
movq $0x0, 0x8(%rsp)
movl %r8d, %edx
callq 0x10f0
addq $0x10, %r12
movq 0x3910(%rip), %r14      # 0x4ff8
leaq 0x10(%rsp), %r13
movq %r14, (%rsp)
subl $0x1, %ebp
jb 0x17a9
leaq -0x10(%r12), %rbx
movq %r14, %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x12bf
movl $0x100, %edi            # imm = 0x100
movq %r13, %rsi
movq %r13, %rdx
callq 0x1100
movq %r13, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x12bf
movq %r12, 0x38(%rsp)
movq 0x3869(%rip), %r12      # 0x4fa0
movq %r12, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x12bf
movl $0x100, %edi            # imm = 0x100
movq %r13, %rsi
movq %r13, %rdx
callq 0x1100
leaq 0x10(%r15), %r14
movq %r13, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x12bf
leaq 0x20(%rsp), %rdi
movq %r14, %rsi
movq 0x3882(%rip), %r14      # 0x4ff8
movq %rdi, %rdx
callq 0x12bf
movq %r12, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq 0x38(%rsp), %r12
callq 0x12bf
movq %r14, %rdi
movq %r14, %rsi
callq 0x12d6
addq $0x20, %r15
addq $0x20, %r12
jmp 0x16f1
movq 0x8(%rsp), %r14
cmpl $0x10, %r14d
ja 0x1823
movq 0x37e5(%rip), %rsi      # 0x4fa0
leaq 0x40(%rsp), %rbx
movl $0x100, %edi            # imm = 0x100
movq %rsi, (%rsp)
movq %rbx, %rdx
callq 0x1100
addq $-0x10, %r12
movl %r14d, %edi
movq %rbx, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x15f7
vxorps %xmm0, %xmm0, %xmm0
leaq 0x10(%rsp), %rdi
vmovaps %xmm0, (%rdi)
movl %r14d, %ebx
movq %r15, %rsi
movq %rbx, %rdx
callq 0x1110
cmpl $0x10, %r14d
je 0x180c
movb $-0x80, 0x10(%rsp,%rbx)
leaq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rdx
movq %rdx, %rsi
callq 0x12bf
jmp 0x18e1
leal -0x10(%r14), %ebx
vxorps %xmm0, %xmm0, %xmm0
leaq 0x10(%rsp), %rdi
vmovaps %xmm0, (%rdi)
movq %r12, %rsi
movq %rbx, %rdx
callq 0x1110
cmpl $0xf, %ebx
ja 0x1849
movb $-0x80, 0x10(%rsp,%rbx)
leaq 0x10(%rsp), %rbp
leaq 0x20(%rsp), %r13
movq %r13, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x12bf
movq 0x3790(%rip), %rdi      # 0x4ff8
movq %rbp, %rsi
movq %rbp, %rdx
callq 0x12bf
movl $0x100, %edi            # imm = 0x100
movq %rbp, %rsi
movq %rbp, %rdx
callq 0x1100
leaq -0x10(%r12), %rsi
movq %rbp, %rdi
movq %r15, %rdx
callq 0x12bf
movq 0x3706(%rip), %rdi      # 0x4fa0
movq %r15, %rsi
movq %rbp, %rdx
callq 0x12bf
leaq 0x40(%rsp), %r14
movl $0x100, %edi            # imm = 0x100
movq %rbp, %rsi
movq %r14, %rdx
callq 0x1100
addq $0x10, %r15
movl %ebx, %edi
movq %r14, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x15f7
movq %r13, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x12bf
movq 0x8(%rsp), %r14
cmpl $0x20, %r14d
je 0x18ed
cmpl $0x10, %r14d
jne 0x18fe
movq (%rsp), %r14
movq %r14, %rdi
movq %r14, %rsi
callq 0x1612
jmp 0x190d
movq (%rsp), %r14
movq %r14, %rdi
movq %r14, %rsi
callq 0x165d
leaq 0x20(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x12bf
movq 0x36b9(%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