// File: /workspace/github_c_makefile_star2_O1/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, %rbp
movq %rdx, %rbx
vpxor %xmm0, %xmm0, %xmm0
vmovdqa %xmm0, 0x20(%rsp)
xorl %r14d, %r14d
movl $0x0, %r12d
movl $0x0, %r13d
testl %ecx, %ecx
je 0x17e4
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
je 0x1960
movq %r13, 0x70(%rsp)
leaq 0x10(%rbx), %r13
xorl %r14d, %r14d
movq 0x3790(%rip), %rdx      # 0x4f98
movw $0x7fff, %ax            # imm = 0x7FFF
kmovd %eax, %k1
kmovw %k1, 0x1e(%rsp)
movq %rbx, %rax
movq %rbp, %rbx
movq %rax, 0x50(%rsp)
movq %rax, %rbp
movq %rbx, 0x40(%rsp)
xorl %r15d, %r15d
movl %r12d, 0x4c(%rsp)
movq %r14, 0x58(%rsp)
movq 0x378b(%rip), %rax      # 0x4fc8
vmovdqa (%rax), %xmm0
movq 0x50(%rsp), %rax
vmovdqu (%rax,%r14), %xmm1
movq %rdx, %r14
vpternlogq $0x96, (%rdx), %xmm0, %xmm1
vmovdqa %xmm1, (%rsp)
movl $0x100, %edi            # imm = 0x100
movq %rsp, %rdx
movq %rdx, %rsi
callq 0x1100
movq 0x58(%rsp), %rdx
movq %rdx, %r12
orq $0x10, %r12
xorl %eax, %eax
movb (%r13,%rax), %cl
xorb (%rsp,%rax), %cl
movb %cl, (%rbx,%rax)
incq %rax
cmpq $0x10, %rax
jne 0x1879
movq 0x40(%rsp), %rax
vmovdqu (%rax,%rdx), %xmm0
vpxor (%r14), %xmm0, %xmm0
vmovdqa %xmm0, (%rsp)
movl $0x100, %edi            # imm = 0x100
movq %rsp, %rdx
movq %rdx, %rsi
callq 0x1100
xorl %eax, %eax
movb (%rbp,%rax), %cl
xorb (%rsp,%rax), %cl
movb %cl, 0x10(%rbx,%rax)
incq %rax
cmpq $0x10, %rax
jne 0x18b3
movq 0x40(%rsp), %rax
vmovups (%rax,%r12), %xmm0
vxorps 0x20(%rsp), %xmm0, %xmm0
vmovaps %xmm0, 0x20(%rsp)
movq %r14, %rdx
vmovdqa (%r14), %xmm0
kmovw 0x1e(%rsp), %k1
vmovdqu8 0x1(%r14), %xmm1 {%k1} {z}
movzbl (%r14), %eax
vpsrlw $0x7, %xmm1, %xmm1
vpaddw %xmm0, %xmm0, %xmm0
vpternlogd $0xe4, 0x16f2(%rip){1to4}, %xmm1, %xmm0 # 0x3000
vmovdqu8 %xmm0, (%r14) {%k1}
shrl $0x7, %eax
movb 0xf(%r14), %cl
addb %cl, %cl
leaq 0x16e0(%rip), %rsi      # 0x3004
xorb (%rax,%rsi), %cl
movb %cl, 0xf(%r14)
incl %r15d
movq 0x58(%rsp), %r14
addq $0x20, %r14
addq $0x20, %rbx
addq $0x20, %r13
addq $0x20, %rbp
movl 0x4c(%rsp), %r12d
cmpl %r12d, %r15d
jne 0x1831
movq 0x70(%rsp), %r13
movq 0x40(%rsp), %rbp
movq 0x50(%rsp), %rbx
cmpl $0x11, %r13d
jae 0x19eb
movq 0x3627(%rip), %r15      # 0x4f98
leaq 0x60(%rsp), %rdx
movl $0x100, %edi            # imm = 0x100
movq %r15, %rsi
callq 0x1100
movl %r14d, %eax
addq %rax, %rbp
movl %r13d, %r12d
testl %r13d, %r13d
je 0x19a9
addq %rax, %rbx
xorl %eax, %eax
movb (%rbx,%rax), %cl
xorb 0x60(%rsp,%rax), %cl
movb %cl, (%rbp,%rax)
incq %rax
cmpq %rax, %r12
jne 0x1996
vpxor %xmm0, %xmm0, %xmm0
movq %rsp, %rdi
vmovdqa %xmm0, (%rdi)
movq %rbp, %rsi
movq %r12, %rdx
callq 0x1110
cmpl $0x10, %r13d
je 0x19ca
movb $-0x80, (%rsp,%r12)
vmovaps 0x20(%rsp), %xmm0
vxorps (%rsp), %xmm0, %xmm0
vmovaps %xmm0, 0x20(%rsp)
vmovaps (%r15), %xmm0
vmovaps %xmm0, 0x30(%rsp)
jmp 0x1ad4
movq 0x35d6(%rip), %rax      # 0x4fc8
vmovaps (%rax), %xmm0
movq 0x359b(%rip), %rcx      # 0x4f98
vxorps (%rcx), %xmm0, %xmm0
vmovaps %xmm0, 0x30(%rsp)
leal -0x10(%r13), %r15d
movl %r14d, %r12d
leaq 0x10(%rbx,%r12), %rsi
vxorps %xmm0, %xmm0, %xmm0
movq %rsp, %rdi
vmovaps %xmm0, (%rdi)
movq %r15, %rdx
callq 0x1110
cmpl $0x10, %r15d
jae 0x1a31
movb $-0x80, (%rsp,%r15)
movq %rsp, %rdx
vmovdqa (%rdx), %xmm0
vpxor 0x20(%rsp), %xmm0, %xmm1
vmovdqa %xmm1, 0x20(%rsp)
vpxor 0x30(%rsp), %xmm0, %xmm0
vmovdqa %xmm0, (%rdx)
movl $0x100, %edi            # imm = 0x100
movq %rdx, %rsi
callq 0x1100
leaq (%rbx,%r12), %rax
addq %rbp, %r12
xorl %ecx, %ecx
movb (%rax,%rcx), %dl
xorb (%rsp,%rcx), %dl
movb %dl, (%r12,%rcx)
incq %rcx
cmpq $0x10, %rcx
jne 0x1a64
vmovups (%r12), %xmm0
movq 0x3514(%rip), %rax      # 0x4f98
vxorps (%rax), %xmm0, %xmm0
movq %rsp, %rsi
vmovaps %xmm0, (%rsi)
leaq 0x60(%rsp), %rdx
movl $0x100, %edi            # imm = 0x100
callq 0x1100
testq %r15, %r15
je 0x1ac2
addl $0x10, %r14d
addq %r14, %rbp
addq %r14, %rbx
xorl %eax, %eax
movb (%rbx,%rax), %cl
xorb 0x60(%rsp,%rax), %cl
movb %cl, (%rbp,%rax)
incq %rax
cmpq %rax, %r15
jne 0x1aaf
vmovaps 0x60(%rsp), %xmm0
vxorps 0x20(%rsp), %xmm0, %xmm0
vmovaps %xmm0, 0x20(%rsp)
movzbl 0x30(%rsp), %eax
vmovdqa 0x30(%rsp), %xmm0
movw $0x7fff, %cx            # imm = 0x7FFF
kmovd %ecx, %k1
vmovdqu8 0x31(%rsp), %xmm1 {%k1} {z}
vpsrlw $0x7, %xmm1, %xmm1
vpaddw %xmm0, %xmm0, %xmm0
vpternlogd $0xe4, 0x14fa(%rip){1to4}, %xmm1, %xmm0 # 0x3000
vmovdqu8 %xmm0, (%rsp) {%k1}
shrl $0x7, %eax
movb 0x3f(%rsp), %cl
addb %cl, %cl
leaq 0x14e7(%rip), %rdx      # 0x3004
xorb (%rax,%rdx), %cl
movb %cl, 0xf(%rsp)
vmovaps (%rsp), %xmm0
vxorps 0x30(%rsp), %xmm0, %xmm0
vmovaps %xmm0, 0x30(%rsp)
vxorps 0x20(%rsp), %xmm0, %xmm0
vmovaps %xmm0, 0x20(%rsp)
cmpl $0x20, %r13d
je 0x1b4d
cmpl $0x10, %r13d
jne 0x1b64
movq 0x3474(%rip), %rax      # 0x4fc8
vmovaps (%rax), %xmm0
vxorps 0x20(%rsp), %xmm0, %xmm0
vmovaps %xmm0, 0x20(%rsp)
movq 0x3475(%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