// File: /workspace/github_c_makefile_star2_O1/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 $0xd8, %rsp
movq %r8, %r15
movq %rcx, %r14
movabsq $0x8000000006, %rax     # imm = 0x8000000006
movq %rax, 0x98(%rsp)
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0xa0(%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 0x2e30
movq %rdi, %r12
movq %rdx, 0x90(%rsp)
movq %rsi, 0x88(%rsp)
movq 0x120(%rsp), %rbx
movq 0x5580(%rip), %r13      # 0x7fd0
movq %rbx, %rdi
movl $0x80, %esi
movq %r13, %rdx
movq %r9, 0x40(%rsp)
callq 0x10a0
movq 0x5574(%rip), %rdx      # 0x7fe0
movq %rbx, %rdi
movl $0x80, %esi
callq 0x1040
leaq 0xa0(%rsp), %rdi
leaq 0x20(%rsp), %rbp
movq %rbp, %rsi
movq %r13, %rdx
callq 0x1060
leaq 0xc0(%rsp), %rbx
movq %rbp, %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x1060
leaq 0xb0(%rsp), %rbp
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x25b2
movq %rbp, %rdi
movq %rbp, %rsi
callq 0x2617
leaq 0x60(%rsp), %rdi
movq %rbx, %rsi
callq 0x2617
leaq 0x50(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
callq 0x25b2
movq %r13, %rdi
movq %r13, %rsi
callq 0x25b2
movq %r13, %rdi
movq %r13, %rsi
callq 0x2617
leaq 0x70(%rsp), %rdi
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, (%rdi)
leaq 0x98(%rsp), %rcx
movq %rbp, %rsi
movq 0x118(%rsp), %rdx
movq 0x40(%rsp), %r8
movq 0x110(%rsp), %r9
callq 0x2691
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x10(%rsp)
vmovups (%r14), %xmm0
leaq 0x20(%rsp), %rax
vmovaps %xmm0, (%rax)
cmpq $0x21, %r15
jne 0x2ba5
cmpb $0x1, 0x20(%r14)
jne 0x2ba5
subq $0x8, %rsp
xorl %ebx, %ebx
leaq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rsi
leaq 0x8(%rsp), %rdx
leaq 0x28(%rsp), %rcx
leaq 0x78(%rsp), %r8
xorl %r9d, %r9d
pushq $0x2
pushq %rbx
pushq %rbx
callq 0x2810
addq $0x20, %rsp
movl $0x7, %r13d
movl $0x10, %eax
leal 0x8(%r13), %ecx
cmpb (%rsp,%rcx), %bl
jo 0x2dc5
decq %r13
decl %eax
cmpl $0x9, %eax
jae 0x2b89
jmp 0x2dbe
subq $0x8, %rsp
xorl %eax, %eax
leaq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rsi
leaq 0x8(%rsp), %rbx
leaq 0x28(%rsp), %rcx
leaq 0x78(%rsp), %r8
movq %rbx, %rdx
movl $0x1, %r9d
pushq $0x2
pushq %rax
movq $0x0, 0x50(%rsp)
pushq %rax
callq 0x2810
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 0x90(%rsp), %rcx
movq %rax, (%rcx)
addq $-0x10, %r15
leaq 0x10(%r14), %rcx
cmpq $0x12, %r15
jb 0x2de2
vmovaps 0x10(%rsp), %xmm1
xorl %ebx, %ebx
movq %rcx, %rbp
vmovups (%rcx), %xmm0
vmovaps %xmm0, 0x20(%rsp)
cmpq $0x21, %r15
vmovaps %xmm1, 0x40(%rsp)
jne 0x2cb8
cmpb $0x1, 0x30(%r14)
jne 0x2cf0
subq $0x8, %rsp
leaq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rsi
leaq 0x8(%rsp), %rdx
leaq 0x28(%rsp), %rcx
leaq 0x78(%rsp), %r8
xorl %r9d, %r9d
pushq $0x2
pushq %rbx
pushq %rbx
callq 0x2810
addq $0x20, %rsp
vmovaps 0x40(%rsp), %xmm1
vxorps (%rsp), %xmm1, %xmm0
vmovaps %xmm0, (%rsp)
movl $0x10, %r14d
leal -0x1(%r14), %eax
cmpb (%rsp,%rax), %bl
jo 0x2d6e
movl %eax, %r14d
cmpl $0x1, %eax
ja 0x2c96
xorl %r14d, %r14d
movq 0x38(%rsp), %rax
jmp 0x2d76
subq $0x8, %rsp
leaq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rsi
leaq 0x8(%rsp), %rdx
leaq 0x28(%rsp), %rcx
leaq 0x78(%rsp), %r8
movl $0x1, %r9d
pushq $0x2
pushq %rbx
pushq %rbx
callq 0x2810
vmovaps 0x60(%rsp), %xmm1
addq $0x20, %rsp
jmp 0x2d30
subq $0x8, %rsp
leaq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rsi
leaq 0x8(%rsp), %rdx
leaq 0x28(%rsp), %rcx
leaq 0x78(%rsp), %r8
movl $0x1, %r9d
pushq $0x2
pushq %rbx
pushq %rbx
callq 0x2810
addq $0x20, %rsp
vmovaps 0x40(%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 0x2c2f
jmp 0x2ddc
decl %r14d
movl $0x1, %eax
testl %eax, %eax
je 0x2db8
movl %r14d, %edx
movq %rdx, %rax
cmpq $0xf, %rax
je 0x2d93
cmpb $0x0, 0x1(%rsp,%rax)
leaq 0x1(%rax), %rax
je 0x2d80
jmp 0x2db8
testl %r14d, %r14d
jle 0x2da9
movq %r12, %rdi
movq %rsp, %rsi
callq 0x10b0
vmovaps 0x40(%rsp), %xmm1
movslq %r14d, %rax
movl $0x1, %ecx
movq %rcx, 0x38(%rsp)
jmp 0x2d40
vmovaps %xmm1, 0x10(%rsp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x2e30
leal 0x8(%r13), %eax
cmpq $0xf, %rax
je 0x2e42
cmpb $0x0, 0x1(%rsp,%rax)
leaq 0x1(%rax), %rax
je 0x2dc9
jmp 0x2dbe
vmovaps %xmm1, 0x10(%rsp)
subq $0x8, %rsp
leaq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rsi
leaq 0x8(%rsp), %rdx
leaq 0x78(%rsp), %r8
movl $0x1, %r9d
pushq $0x2
pushq $0x1
pushq $0x0
callq 0x2810
addq $0x20, %rsp
xorl %eax, %eax
movb 0x10(%rsp,%rax), %cl
cmpb (%rsp,%rax), %cl
jne 0x2dbe
incq %rax
cmpq $0x10, %rax
jne 0x2e11
movq 0x88(%rsp), %rax
movq %r13, (%rax)
xorl %eax, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, %eax
movq %rax, 0x38(%rsp)
testl %r13d, %r13d
je 0x2bf5
leaq 0x8(%rsp), %rsi
movq %r12, %rdi
movq %r13, %rdx
callq 0x10b0
jmp 0x2bf5