// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/elmd1001v2/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[]={0x0a,0,0,0x01,0x80,0,0,0}; 
	
	block L, W, Delta_0, Delta_1, Delta_2, blk, result, CS;
	int i; u8 zeroes[16], ozs[16], blen = 16, Is_final = 0, Is_complete =1;
	unsigned long long tlen, blk_ctr=0, blk_ctr1=0;	
	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 < 24)
		return -1;

	*mlen = clen - 24; 
	
	key_schedule(k);
	
	/* =========== Generate the Masks ========== */
	AES(10, ENCRYPT, L, zeroes, &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<32){
	 	process_block(Delta_2, Delta_1, result, blk, W, 0, DECRYPT, Is_final, CIPHERTEXT); 
		store_bytes(m, result, 8, 8+(clen-25)); 
	 }
	 else {
		process_block(Delta_2, Delta_1, result, blk, W, 1, DECRYPT, Is_final, CIPHERTEXT);
		store_bytes(m, result, 8, 15);  m +=8;
	 }
	 xor_block(CS, CS, result); store_bytes(nsec, result, 0, 7); 
	 clen -= 16; c+=16;

	 if(clen < 16){ 
		if(result[clen] != 0x80) return -1; 
		for(i=clen+1; i<16; i++) {if(result[i]!=0) return -1;} 
	 }

	/* ============= Process Successive Ciphertext Blocks ============== */
	while(clen > 16){
	   
	   load_block(blk, c, zeroes, 16, 0);  
	   if(clen < 32){ 
	   	process_block(Delta_2, Delta_1, result, blk, W, 0, DECRYPT, Is_final, CIPHERTEXT); 
		xor_block(result, result, CS);
	   	store_bytes(m, result, 0, clen - 17);  
	   }
	   else{ 
	        process_block(Delta_2, Delta_1, result, blk, W, 1, DECRYPT, Is_final, CIPHERTEXT); 
		if(clen == 32) {xor_block(result, result, CS);}
		store_bytes(m, result, 0, 15); 
	   } 

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

	   
	   if(clen < 16){ 
		if(result[clen] != 0x80) return -1; 
		for(i=clen+1; i<16; i++) {if(result[i]!=0) return -1;} 
	   }
	   else
		   m +=16; 
	}

	/* ==========  Process checksum block  ============= */
	Is_final = 1;
	process_block(Delta_1, Delta_2, result, CS, W, 1, ENCRYPT, Is_final, MESSAGE); /* 2nd result was CS */
	for(i=0; i<clen; i++) {if(result[i]!=c[i]) return -1;} 

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %r9, %r13
movq %r8, %r14
movq %rdx, %r12
movq %rdi, %r15
movq 0x100(%rsp), %rdi
movabsq $0x800100000a, %rax     # imm = 0x800100000A
movq %rax, 0x88(%rsp)
xorl %eax, %eax
cmpq $0x10, %rax
je 0x28fa
movb $0x0, 0x50(%rsp,%rax)
incq %rax
jmp 0x28ea
pushq $-0x1
popq %rax
cmpq $0x18, %r14
jb 0x2c6f
movq %rcx, 0x18(%rsp)
leaq -0x18(%r14), %rax
movq %rax, (%rsi)
callq 0x2516
movq 0x46b1(%rip), %rdx      # 0x6fd0
leaq 0x50(%rsp), %rdi
leaq 0xa0(%rsp), %rbp
movq %rbp, %rsi
callq 0x1050
leaq 0x90(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x2540
movq %rbx, %rdi
movq %rbx, %rsi
callq 0x257d
leaq 0x40(%rsp), %rdi
movq %rbp, %rsi
callq 0x257d
leaq 0x30(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x2540
movq %rbx, %rdi
movq %rbx, %rsi
callq 0x2540
movq %rbx, %rdi
movq %rbx, %rsi
callq 0x257d
movq 0xf8(%rsp), %rdx
movq 0xf0(%rsp), %r9
xorl %eax, %eax
cmpq $0x10, %rax
je 0x29a7
movb $0x0, 0x20(%rsp,%rax)
incq %rax
jmp 0x2997
leaq 0x20(%rsp), %rdi
leaq 0x90(%rsp), %rsi
leaq 0x88(%rsp), %rcx
movq %r13, %r8
callq 0x25c9
leaq 0x60(%rsp), %rdi
leaq 0x50(%rsp), %rbx
pushq $0x10
popq %rbp
movq %rbx, %rsi
movq %rbx, %rdx
movl %ebp, %ecx
xorl %r8d, %r8d
callq 0x26b4
leaq 0x70(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq %rbx, %rdx
movl %ebp, %ecx
xorl %r8d, %r8d
callq 0x26b4
cmpq $0x1f, %r14
ja 0x2a52
subq $0x20, %rsp
movl $0x2, 0x10(%rsp)
andl $0x0, 0x8(%rsp)
andl $0x0, (%rsp)
leaq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rsi
leaq 0x20(%rsp), %rbx
leaq 0x90(%rsp), %rcx
leaq 0x40(%rsp), %r8
movq %rbx, %rdx
xorl %r9d, %r9d
callq 0x2715
addq $0x20, %rsp
leal -0x11(%r14), %ecx
pushq $0x8
popq %rdx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x2881
jmp 0x2a9a
subq $0x8, %rsp
leaq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rsi
leaq 0x8(%rsp), %rbx
leaq 0x78(%rsp), %rcx
leaq 0x28(%rsp), %r8
pushq $0x1
popq %r9
movq %rbx, %rdx
pushq $0x2
pushq $0x0
pushq $0x0
callq 0x2715
addq $0x20, %rsp
pushq $0x8
popq %rdx
pushq $0xf
popq %rcx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x2881
addq $0x8, %r15
leaq 0x60(%rsp), %rdi
movq %rsp, %rdx
movq %rdi, %rsi
callq 0x289a
xorl %eax, %eax
cmpq $0x8, %rax
je 0x2abe
movb (%rsp,%rax), %cl
movb %cl, (%r12,%rax)
incq %rax
jmp 0x2aac
leaq -0x10(%r14), %rbx
cmpq $0xf, %rbx
ja 0x2aee
xorl %eax, %eax
cmpb (%rsp,%rbx), %al
jno 0x2c6c
leal 0x11(%r14), %eax
andl $0x1f, %eax
cmpl $0xf, %eax
jg 0x2aee
cmpb $0x0, (%rsp,%rax)
leaq 0x1(%rax), %rax
je 0x2ada
jmp 0x2c6c
addl $-0x1f, %r14d
leaq 0x70(%rsp), %r13
movq %rsp, %r12
leaq 0x60(%rsp), %rbp
cmpq $0x11, %rbx
jb 0x2c20
movq 0x18(%rsp), %rsi
addq $0x10, %rsi
movq %r13, %rdi
movq %rsi, 0x18(%rsp)
leaq 0x50(%rsp), %rdx
pushq $0x10
popq %rcx
xorl %r8d, %r8d
callq 0x26b4
subq $0x8, %rsp
cmpq $0x1f, %rbx
ja 0x2bbf
leaq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rsi
movq %r12, %rdx
movq %r13, %rcx
leaq 0x28(%rsp), %r8
xorl %r9d, %r9d
pushq $0x2
pushq $0x0
pushq $0x0
callq 0x2715
addq $0x20, %rsp
movq %r12, %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x289a
leal -0x11(%rbx), %ecx
movq %r15, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x2881
movq %rbp, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0x289a
addq $-0x10, %rbx
cmpq $0xf, %rbx
ja 0x2c13
xorl %eax, %eax
cmpb (%rsp,%rbx), %al
jno 0x2c6c
movl %r14d, %eax
leaq (%rsp,%rax), %rcx
movl %r14d, %eax
cmpl $0xf, %eax
jg 0x2c17
incl %eax
cmpb $0x0, (%rcx)
leaq 0x1(%rcx), %rcx
je 0x2baa
jmp 0x2c6c
leaq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rsi
movq %r12, %rdx
movq %r13, %rcx
leaq 0x28(%rsp), %r8
pushq $0x1
popq %r9
pushq $0x2
pushq $0x0
pushq $0x0
callq 0x2715
addq $0x20, %rsp
cmpq $0x20, %rbx
jne 0x2bfb
movq %r12, %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x289a
xorl %eax, %eax
cmpq $0x10, %rax
je 0x2b7d
movb (%rsp,%rax), %cl
movb %cl, (%r15,%rax)
incq %rax
jmp 0x2bfd
addq $0x10, %r15
addl $-0x10, %r14d
jmp 0x2aff
subq $0x8, %rsp
leaq 0x48(%rsp), %rdi
leaq 0x38(%rsp), %rsi
leaq 0x8(%rsp), %rdx
leaq 0x68(%rsp), %rcx
leaq 0x28(%rsp), %r8
pushq $0x1
popq %r9
pushq %r9
pushq %r9
pushq %r9
callq 0x2715
addq $0x20, %rsp
xorl %eax, %eax
movq 0x18(%rsp), %rsi
cmpq %rax, %rbx
je 0x2c81
movb (%rsp,%rax), %cl
leaq 0x1(%rax), %rdx
cmpb 0x10(%rsi,%rax), %cl
movq %rdx, %rax
je 0x2c57
pushq $-0x1
popq %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
jmp 0x2c6f