// File: /workspace/github_c_makefile_star2_O3/mjosaarinen[P]brutus/crypto_aead/elmd600v2/ref/encrypt.c
// Function: crypto_aead_decrypt
int crypto_aead_decrypt(
unsigned char *m,unsigned long long *mlen,
unsigned char *nsec,
const unsigned char *c, unsigned long long clen,
const unsigned char *ad, unsigned long long adlen,
const unsigned char *npub,
const unsigned char *k
)
{
	u8 param[]={0x06,0,0,0,0x80,0,0,0}; 
	
	block L, W, Delta_0, Delta_1, Delta_2, blk, result, CS;
	int i, j, flag=0; u8 zeroes[16], ozs[16], blen = 16, Is_final = 0, Is_complete =1;
	unsigned long long outputmlen;	
	for(i=0; i<16; i++)	{zeroes[i]=0x00;}   		// all zero.
	for(i=1; i<16; i++)	{ozs[i]=0x00;} ozs[0] = 0x80; 	//useful for padding. 
	
	if(((clen % 16) != 1)||(clen < 33))
		return -1;

	key_schedule(k);
	
	/* =========== Generate the Masks ========== */
	AES(6, ENCRYPT, blk, zeroes, &aes_key1);
	AES(6, ENCRYPT, L, blk, &aes_key1);

	mult_3(Delta_0, L); 
	mult_inv2(Delta_0, Delta_0); 

	mult_inv2(Delta_1, L); 

	mult_3(Delta_2, L); 
	mult_3(Delta_2, Delta_2); 
	mult_inv2(Delta_2, Delta_2);

	/* =============  Process Associated Data  ================ */
	for(i=0; i<16; i++)
		W[i]=0x00;
	process_AD(W, Delta_0, npub, param, ad, adlen); 

	
	 load_block(CS, zeroes, zeroes, 16, 0);

	 /* ================ Process Ciphertext Blocks ============ */
	 load_block(blk, c, zeroes, 16, 0);  

	 /* =================== Process 1st Block =================== */
	 if( (clen==33) && (c[clen-1]==0x01) ) {
	 	process_block(Delta_2, Delta_1, result, blk, W, 0, DECRYPT, Is_final, CIPHERTEXT); 
		for(i=15; i>7; i--) { if(result[i]==0x80) { flag=1; break; } }
		if(flag == 0) {return -1; }
		for(j=i+1; j<16; j++) { if(result[j]!=0) {return -1; }}
		store_bytes(m, result, 8, i-1);  
		outputmlen = i-8;
	 }
	 else {
		process_block(Delta_2, Delta_1, result, blk, W, 1, DECRYPT, Is_final, CIPHERTEXT);
		store_bytes(m, result, 8, 15);  m +=8; outputmlen = 8;
	 }
	 xor_block(CS, CS, result); store_bytes(nsec, result, 0, 7); 
	 clen -= 16; c+=16;

	/* ============= Process Successive Ciphertext Blocks ============== */
	while(clen > 17){
	   
	   load_block(blk, c, zeroes, 16, 0);  
	   if((clen == 33) && (c[clen-1]==0x01)) { 
	   	process_block(Delta_2, Delta_1, result, blk, W, 0, DECRYPT, Is_final, CIPHERTEXT); 
		xor_block(result, result, CS);
		for(i=15; i>0; i--) { if(result[i]==0x80) { flag=1; break; } }
		if(flag == 0) {return -1;}
		for(j=i+1; j<16; j++) { if(result[j]!=0) {return -1; }}
	   	store_bytes(m, result, 0, i-1); outputmlen += i;
	   }
	   else{ 
	        process_block(Delta_2, Delta_1, result, blk, W, 1, DECRYPT, Is_final, CIPHERTEXT); 
		if(clen == 33) { xor_block(result, result, CS); }
		store_bytes(m, result, 0, 15); outputmlen += 16;
	   } 

	   xor_block(CS, CS, result);
	   clen -= 16; c+=16;

	   if(clen != 17)
		   m +=16; 
	}
	

	/* ==========  Process checksum block  ============= */
	Is_final = 1;
	process_block(Delta_2, Delta_1, result, c, W, 1, DECRYPT, Is_final, CIPHERTEXT); 
	
	for(i=0; i<16; i++) {if(CS[i]!=result[i]) return -1; } 
	*mlen = outputmlen;
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %r8, %r15
movq %rcx, %r14
movabsq $0x8000000006, %rax     # imm = 0x8000000006
movq %rax, 0x88(%rsp)
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x90(%rsp)
movl %r15d, %eax
andl $0xf, %eax
cmpl $0x1, %eax
setne %al
cmpq $0x21, %r8
setb %cl
orb %al, %cl
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jne 0x2e53
movq %rdi, %r12
movq %rdx, 0x80(%rsp)
movq %rsi, 0x78(%rsp)
movq 0x110(%rsp), %rbx
movq 0x556d(%rip), %r13      # 0x7fd0
movq %rbx, %rdi
movl $0x80, %esi
movq %r13, %rdx
movq %r9, 0x30(%rsp)
callq 0x10a0
movq 0x5561(%rip), %rdx      # 0x7fe0
movq %rbx, %rdi
movl $0x80, %esi
callq 0x1040
leaq 0x90(%rsp), %rdi
leaq 0x20(%rsp), %rbp
movq %rbp, %rsi
movq %r13, %rdx
callq 0x1060
leaq 0xb0(%rsp), %rbx
movq %rbp, %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x1060
leaq 0xa0(%rsp), %rbp
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x25b1
movq %rbp, %rdi
movq %rbp, %rsi
callq 0x261f
leaq 0x50(%rsp), %rdi
movq %rbx, %rsi
callq 0x261f
leaq 0x40(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
callq 0x25b1
movq %r13, %rdi
movq %r13, %rsi
callq 0x25b1
movq %r13, %rdi
movq %r13, %rsi
callq 0x261f
leaq 0x60(%rsp), %rdi
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, (%rdi)
leaq 0x88(%rsp), %rcx
movq %rbp, %rsi
movq 0x108(%rsp), %rdx
movq 0x30(%rsp), %r8
movq 0x100(%rsp), %r9
callq 0x2699
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x10(%rsp)
vmovups (%r14), %xmm0
leaq 0x20(%rsp), %rax
vmovaps %xmm0, (%rax)
cmpq $0x21, %r15
jne 0x2bed
cmpb $0x1, 0x20(%r14)
jne 0x2bed
subq $0x8, %rsp
xorl %ebx, %ebx
leaq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rsi
leaq 0x8(%rsp), %rdx
leaq 0x28(%rsp), %rcx
leaq 0x68(%rsp), %r8
xorl %r9d, %r9d
pushq $0x2
pushq %rbx
pushq %rbx
callq 0x283f
addq $0x20, %rsp
movl $0x7, %r13d
movl $0x10, %eax
leal 0x8(%r13), %ecx
cmpb (%rsp,%rcx), %bl
jo 0x2bcb
decq %r13
decl %eax
cmpl $0x9, %eax
jae 0x2ba4
jmp 0x2dfb
cmpb $0x0, 0x1(%rsp,%rcx)
leaq 0x1(%rcx), %rcx
jne 0x2dfb
cmpq $0xf, %rcx
jne 0x2bbc
movl $0x1, %ebp
testl %r13d, %r13d
je 0x2c34
leaq 0x8(%rsp), %rsi
movq %r12, %rdi
movq %r13, %rdx
callq 0x10b0
jmp 0x2c34
subq $0x8, %rsp
xorl %ebp, %ebp
leaq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rsi
leaq 0x8(%rsp), %rbx
leaq 0x28(%rsp), %rcx
leaq 0x68(%rsp), %r8
movq %rbx, %rdx
movl $0x1, %r9d
pushq $0x2
pushq %rbp
pushq %rbp
callq 0x283f
addq $0x20, %rsp
movq 0x8(%rbx), %rax
movq %rax, (%r12)
addq $0x8, %r12
movl $0x8, %r13d
vmovaps (%rsp), %xmm0
vxorps 0x10(%rsp), %xmm0, %xmm0
vmovaps %xmm0, 0x10(%rsp)
movq (%rsp), %rax
movq 0x80(%rsp), %rcx
movq %rax, (%rcx)
addq $-0x10, %r15
leaq 0x10(%r14), %rcx
cmpq $0x12, %r15
jb 0x2e08
movq %rbp, %rbx
vmovaps 0x10(%rsp), %xmm1
movq %rcx, %rbp
vmovups (%rcx), %xmm0
vmovaps %xmm0, 0x20(%rsp)
cmpq $0x21, %r15
vmovaps %xmm1, 0x30(%rsp)
jne 0x2d19
cmpb $0x1, 0x30(%r14)
jne 0x2d53
subq $0x8, %rsp
leaq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rsi
leaq 0x8(%rsp), %rdx
leaq 0x28(%rsp), %rcx
leaq 0x68(%rsp), %r8
xorl %r9d, %r9d
pushq $0x2
xorl %r14d, %r14d
pushq %r14
pushq %r14
callq 0x283f
addq $0x20, %rsp
vmovaps 0x30(%rsp), %xmm1
vxorps (%rsp), %xmm1, %xmm0
vmovaps %xmm0, (%rsp)
movl $0xf, %eax
movl %eax, %ecx
cmpb (%rsp,%rcx), %r14b
jo 0x2cfa
leal -0x1(%rax), %ecx
cmpl $0x1, %eax
movl %ecx, %eax
ja 0x2cde
xorl %eax, %eax
testl %ebx, %ebx
je 0x2df5
movl %eax, %ebx
movq %rbx, %rax
cmpq $0xf, %rax
je 0x2dd3
cmpb $0x0, 0x1(%rsp,%rax)
leaq 0x1(%rax), %rax
je 0x2cff
jmp 0x2df5
subq $0x8, %rsp
leaq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rsi
leaq 0x8(%rsp), %rdx
leaq 0x28(%rsp), %rcx
leaq 0x68(%rsp), %r8
movl $0x1, %r9d
pushq $0x2
xorl %eax, %eax
pushq %rax
pushq %rax
callq 0x283f
vmovaps 0x50(%rsp), %xmm1
addq $0x20, %rsp
jmp 0x2d95
subq $0x8, %rsp
leaq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rsi
leaq 0x8(%rsp), %rdx
leaq 0x28(%rsp), %rcx
leaq 0x68(%rsp), %r8
movl $0x1, %r9d
pushq $0x2
xorl %eax, %eax
pushq %rax
pushq %rax
callq 0x283f
addq $0x20, %rsp
vmovaps 0x30(%rsp), %xmm1
vxorps (%rsp), %xmm1, %xmm0
vmovaps %xmm0, (%rsp)
vmovaps (%rsp), %xmm0
vmovups %xmm0, (%r12)
movl $0x10, %eax
addq %rax, %r13
addq $-0x10, %r15
xorl %eax, %eax
cmpq $0x11, %r15
setne %al
shll $0x4, %eax
addq %rax, %r12
leaq 0x10(%rbp), %rcx
vxorps (%rsp), %xmm1, %xmm1
movq %rbp, %r14
cmpq $0x11, %r15
ja 0x2c6f
jmp 0x2e02
testl %ebx, %ebx
jle 0x2deb
movq %r12, %rdi
movq %rsp, %rsi
movq %rbx, %rdx
callq 0x10b0
vmovaps 0x30(%rsp), %xmm1
movslq %ebx, %rax
movl $0x1, %ebx
jmp 0x2da5
vmovaps %xmm1, 0x10(%rsp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x2e53
vmovaps %xmm1, 0x10(%rsp)
subq $0x8, %rsp
leaq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rsi
leaq 0x8(%rsp), %rdx
leaq 0x68(%rsp), %r8
movl $0x1, %r9d
pushq $0x2
pushq $0x1
pushq $0x0
callq 0x283f
addq $0x20, %rsp
xorl %eax, %eax
movb 0x10(%rsp,%rax), %cl
cmpb (%rsp,%rax), %cl
jne 0x2dfb
incq %rax
cmpq $0x10, %rax
jne 0x2e37
movq 0x78(%rsp), %rax
movq %r13, (%rax)
xorl %eax, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq