// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/aes256otrsv2/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], LdL[BLOCK], Llast[BLOCK], Z[BLOCK];
	uint8 Sum[BLOCK]={0};

	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(L,delta,LdL);
		xor(LdL,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);
		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,ciphertext+j,plaintext+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,delta,Llast); //Llast = L xor delta
		ozp(last-BLOCK,ciphertext+(j+BLOCK),tmp);
		xor(Sum,tmp,Sum);
		xor(Llast,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);
	}
	/* 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, 0x8(%rsp)
movq %rdx, %r15
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x10(%rsp)
testl %ecx, %ecx
je 0x166a
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 0x1675
xorl %ebp, %ebp
movq $0x0, 0x38(%rsp)
movl %r8d, %edx
callq 0x10f0
addq $0x10, %r15
leaq 0x20(%rsp), %r13
subl $0x1, %ebp
jb 0x174d
movq 0x3902(%rip), %r12      # 0x4f98
movq %r12, %rdi
movq 0x3928(%rip), %rsi      # 0x4fc8
leaq 0x60(%rsp), %r14
movq %r14, %rdx
callq 0x1324
leaq -0x10(%r15), %rbx
movq %r14, %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x1324
movl $0x100, %edi            # imm = 0x100
movq %r13, %rsi
movq %r13, %rdx
callq 0x1100
movq %r13, %rdi
movq %r15, %rsi
movq 0x8(%rsp), %r14
movq %r14, %rdx
callq 0x1324
movq %r12, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x1324
movl $0x100, %edi            # imm = 0x100
movq %r13, %rsi
movq %r13, %rdx
callq 0x1100
movq %r14, %r12
movq %r15, %r14
leaq 0x10(%r12), %r15
movq %r13, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x1324
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
movq %r14, %r15
movq %rdi, %rdx
callq 0x1324
movq 0x3865(%rip), %rdi      # 0x4f98
movq %rdi, %rsi
callq 0x125f
addq $0x20, %r15
addq $0x20, %r12
movq %r12, 0x8(%rsp)
jmp 0x1686
movq 0x38(%rsp), %rbp
cmpl $0x10, %ebp
ja 0x17d6
movq 0x383a(%rip), %rbx      # 0x4f98
movq 0x8(%rsp), %r14
movq %r15, %r12
leaq 0x50(%rsp), %r15
movl $0x100, %edi            # imm = 0x100
movq %rbx, %rsi
movq %r15, %rdx
callq 0x1100
addq $-0x10, %r12
movl %ebp, %edi
movq %r15, %rsi
movq %r12, %rdx
movq %r14, %rcx
callq 0x1612
vxorps %xmm0, %xmm0, %xmm0
leaq 0x20(%rsp), %rdi
vmovaps %xmm0, (%rdi)
movl %ebp, %r15d
movq %r14, %rsi
movq %r15, %rdx
callq 0x1110
cmpl $0x10, %ebp
je 0x17b5
movb $-0x80, 0x20(%rsp,%r15)
leaq 0x20(%rsp), %rdi
leaq 0x10(%rsp), %rdx
movq %rdx, %rsi
callq 0x1324
vmovaps (%rbx), %xmm0
vmovaps %xmm0, 0x40(%rsp)
jmp 0x18a7
movq 0x37bb(%rip), %rdi      # 0x4f98
movq 0x37e4(%rip), %rsi      # 0x4fc8
leaq 0x40(%rsp), %rdx
callq 0x1324
leal -0x10(%rbp), %ebx
vxorps %xmm0, %xmm0, %xmm0
leaq 0x20(%rsp), %rdi
vmovaps %xmm0, (%rdi)
movq %r15, %rsi
movq %rbx, %rdx
callq 0x1110
cmpl $0xf, %ebx
ja 0x1813
movb $-0x80, 0x20(%rsp,%rbx)
leaq 0x20(%rsp), %r13
leaq 0x10(%rsp), %rdi
movq %r13, %rsi
movq %rdi, %rdx
callq 0x1324
leaq 0x40(%rsp), %rdi
movq %r13, %rsi
movq %r13, %rdx
callq 0x1324
movl $0x100, %edi            # imm = 0x100
movq %r13, %rsi
movq %r13, %rdx
callq 0x1100
leaq -0x10(%r15), %rsi
movq %r13, %rdi
movq 0x8(%rsp), %r14
movq %r14, %rdx
callq 0x1324
movq 0x3735(%rip), %rdi      # 0x4f98
movq %r14, %rsi
movq %r13, %rdx
callq 0x1324
leaq 0x50(%rsp), %r12
movl $0x100, %edi            # imm = 0x100
movq %r13, %rsi
movq %r12, %rdx
callq 0x1100
addq $0x10, %r14
movl %ebx, %edi
movq %r12, %rsi
movq %r15, %rdx
movq %r14, %rcx
callq 0x1612
leaq 0x10(%rsp), %rdi
movq %r12, %rsi
movq %rdi, %rdx
callq 0x1324
leaq 0x40(%rsp), %rbx
leaq 0x20(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x125f
movq %rbx, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x1324
leaq 0x10(%rsp), %rdi
movq %rbx, %rsi
movq %rdi, %rdx
callq 0x1324
cmpl $0x20, %ebp
je 0x18e4
cmpl $0x10, %ebp
jne 0x18f8
movq 0x36dd(%rip), %rsi      # 0x4fc8
leaq 0x10(%rsp), %rdi
movq %rdi, %rdx
callq 0x1324
movq 0x36e1(%rip), %rdx      # 0x4fe0
leaq 0x10(%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