// File: /workspace/github_c_makefile_star2_O1/mjosaarinen[P]brutus/crypto_aead/elmd101270v2/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,0x7f,0x80,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, 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 % 16) != 1)||(clen < 33))
		return -1;
	
	key_schedule(k);
	
	/* =========== Generate the Masks ========== */
	AES(10, ENCRYPT, L, 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; blk_ctr ++; 

	/* ============= 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); 
		i = -1;
		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; blk_ctr ++; 

	   if(clen != 17)
		   m +=16; 
	
	   if(blk_ctr == IT_GAP && blk_ctr1 < IT_MAX && clen > 48){ 
		AES(10, DECRYPT, result, W, &aes_key2); 
		mask(Delta_2, result, result, 1); 
		for(i=0; i<16; i++) { if(c[i] != result[i]) {return -1; } }
		c +=16; clen -= 16; blk_ctr =0; blk_ctr1++;
	   }
	   
	}

	/* ==========  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 $0xe8, %rsp
movq %r8, %r13
movq %rcx, %r12
movabsq $0x80807f000a, %rax     # imm = 0x80807F000A
movq %rax, 0xa8(%rsp)
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0xb0(%rsp)
movl %r13d, %eax
andl $0xf, %eax
cmpl $0x1, %eax
setne %al
cmpq $0x21, %r8
setb %cl
orb %al, %cl
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jne 0x30b2
movq %r9, %rbp
movq %rdx, 0x50(%rsp)
movq %rdi, 0x10(%rsp)
movq %rsi, 0xa0(%rsp)
movq 0x130(%rsp), %rbx
movq 0x53f3(%rip), %r15      # 0x7fd0
movq %rbx, %rdi
movl $0x80, %esi
movq %r15, %rdx
callq 0x1090
movq 0x53ec(%rip), %rdx      # 0x7fe0
movq %rbx, %rdi
movl $0x80, %esi
callq 0x1040
leaq 0xb0(%rsp), %rdi
leaq 0xd0(%rsp), %r14
movq %r14, %rsi
movq %r15, %rdx
callq 0x1060
leaq 0xc0(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x261a
movq %rbx, %rdi
movq %rbx, %rsi
callq 0x267f
leaq 0x70(%rsp), %rdi
movq %r14, %rsi
callq 0x267f
leaq 0x20(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x261a
movq %r15, %rdi
movq %r15, %rsi
callq 0x261a
movq %r15, %rdi
movq %r15, %rsi
callq 0x267f
leaq 0x30(%rsp), %rdi
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, (%rdi)
leaq 0xa8(%rsp), %rcx
movq %rbx, %rsi
movq 0x128(%rsp), %rdx
movq %rbp, %r8
movq 0x120(%rsp), %r9
callq 0x26f9
leaq 0xb0(%rsp), %rax
vmovaps (%rax), %xmm0
vmovaps %xmm0, 0x80(%rsp)
vmovups (%r12), %xmm0
vmovaps %xmm0, 0x60(%rsp)
cmpq $0x21, %r13
jne 0x2d25
cmpb $0x1, 0x20(%r12)
jne 0x2d25
subq $0x8, %rsp
xorl %ebx, %ebx
leaq 0x28(%rsp), %rdi
leaq 0x78(%rsp), %rsi
leaq 0x8(%rsp), %rdx
leaq 0x68(%rsp), %rcx
leaq 0x38(%rsp), %r8
xorl %r9d, %r9d
pushq $0x2
pushq %rbx
pushq %rbx
callq 0x2878
addq $0x20, %rsp
movl $0x7, %ebp
movl $0x10, %eax
movq 0x10(%rsp), %r15
leal 0x8(%rbp), %ecx
cmpb (%rsp,%rcx), %bl
jo 0x3099
decq %rbp
decl %eax
cmpl $0x9, %eax
jae 0x2d0a
jmp 0x30ad
subq $0x8, %rsp
xorl %eax, %eax
leaq 0x28(%rsp), %rdi
leaq 0x78(%rsp), %rsi
leaq 0x8(%rsp), %rbx
leaq 0x68(%rsp), %rcx
leaq 0x38(%rsp), %r8
movq %rbx, %rdx
movl $0x1, %r9d
pushq $0x2
pushq %rax
movq $0x0, 0x60(%rsp)
pushq %rax
callq 0x2878
addq $0x20, %rsp
movq 0x8(%rbx), %rax
movq 0x10(%rsp), %r15
movq %rax, (%r15)
addq $0x8, %r15
movl $0x8, %ebp
vmovaps (%rsp), %xmm0
vxorps 0x80(%rsp), %xmm0, %xmm0
vmovaps %xmm0, 0x80(%rsp)
movq (%rsp), %rax
movq 0x50(%rsp), %rcx
movq %rax, (%rcx)
addq $-0x10, %r13
addq $0x10, %r12
cmpq $0x12, %r13
jb 0x3040
movl $0x1, %edx
vmovaps 0x80(%rsp), %xmm0
vmovaps %xmm0, 0x50(%rsp)
leaq 0x60(%rsp), %rbx
movq $0x0, 0x90(%rsp)
xorl %eax, %eax
movb (%r12,%rax), %cl
movb %cl, 0x60(%rsp,%rax)
incq %rax
cmpq $0x10, %rax
jne 0x2dd4
cmpq $0x21, %r13
movl $0x0, %r14d
movq %rdx, 0x18(%rsp)
jne 0x2e64
cmpb $0x1, 0x20(%r12)
jne 0x2e96
subq $0x8, %rsp
leaq 0x28(%rsp), %rdi
leaq 0x78(%rsp), %rsi
leaq 0x8(%rsp), %rdx
movq %rbx, %rcx
leaq 0x38(%rsp), %r8
xorl %r9d, %r9d
pushq $0x2
pushq %r14
pushq %r14
callq 0x2878
addq $0x20, %rsp
vmovaps 0x50(%rsp), %xmm0
vxorps (%rsp), %xmm0, %xmm0
vmovaps %xmm0, (%rsp)
movl $0x10, %ebx
leal -0x1(%rbx), %eax
cmpb (%rsp,%rax), %r14b
jo 0x2feb
movl %eax, %ebx
cmpl $0x1, %eax
ja 0x2e44
xorl %ebx, %ebx
movq 0x48(%rsp), %rax
jmp 0x2ff2
subq $0x8, %rsp
leaq 0x28(%rsp), %rdi
leaq 0x78(%rsp), %rsi
leaq 0x8(%rsp), %rdx
movq %rbx, %rcx
leaq 0x38(%rsp), %r8
movl $0x1, %r9d
pushq $0x2
pushq %r14
pushq %r14
callq 0x2878
addq $0x20, %rsp
jmp 0x2ed6
subq $0x8, %rsp
leaq 0x28(%rsp), %rdi
leaq 0x78(%rsp), %rsi
leaq 0x8(%rsp), %rdx
movq %rbx, %rcx
leaq 0x38(%rsp), %r8
movl $0x1, %r9d
pushq $0x2
pushq %r14
pushq %r14
callq 0x2878
addq $0x20, %rsp
vmovaps 0x50(%rsp), %xmm0
vxorps (%rsp), %xmm0, %xmm0
vmovaps %xmm0, (%rsp)
vmovaps (%rsp), %xmm0
movq %r15, 0x10(%rsp)
vmovups %xmm0, (%r15)
movl $0x10, %ecx
movq 0x18(%rsp), %rdx
vmovaps 0x50(%rsp), %xmm0
vxorps (%rsp), %xmm0, %xmm0
vmovaps %xmm0, 0x50(%rsp)
vmovaps %xmm0, 0x80(%rsp)
leaq -0x10(%r13), %rax
xorl %r15d, %r15d
cmpq $0x11, %rax
setne %sil
leaq 0x10(%r12), %r14
incq %rdx
cmpq $0x31, %rax
jb 0x2fc4
cmpq $0x7f, %rdx
jne 0x2fc4
cmpq $0x1f, 0x90(%rsp)
ja 0x2fc4
movq %rcx, 0x98(%rsp)
leaq 0x30(%rsp), %rdi
movq %rsp, %rbx
movq %r13, 0x18(%rsp)
movl %esi, %r13d
movq %rbx, %rsi
movq 0x507b(%rip), %rdx      # 0x7fe0
callq 0x1050
leaq 0x20(%rsp), %rdi
movq %rbx, %rsi
movq %rbx, %rdx
leaq 0x60(%rsp), %rbx
movl $0x1, %ecx
callq 0x2a40
movl %r13d, %esi
movq 0x18(%rsp), %rdx
xorl %eax, %eax
movb (%r14,%rax), %cl
cmpb (%rsp,%rax), %cl
jne 0x30ad
incq %rax
cmpq $0x10, %rax
jne 0x2f8e
addq $0x20, %r12
addq $-0x20, %rdx
incq 0x90(%rsp)
movq %rdx, %rax
movq %r12, %r14
xorl %edx, %edx
movq 0x98(%rsp), %rcx
addq %rcx, %rbp
movb %sil, %r15b
shll $0x4, %r15d
movq 0x10(%rsp), %rcx
addq %r15, %rcx
movq %rcx, %r15
movq %r14, %r12
movq %rax, %r13
cmpq $0x11, %rax
ja 0x2dd2
jmp 0x3043
decl %ebx
movl $0x1, %eax
testl %eax, %eax
je 0x30ad
movl %ebx, %edx
movq %rdx, %rax
cmpq $0xf, %rax
je 0x3015
cmpb $0x0, 0x1(%rsp,%rax)
leaq 0x1(%rax), %rax
je 0x2fff
jmp 0x30ad
testl %ebx, %ebx
jle 0x3024
movq %r15, %rdi
movq %rsp, %rsi
callq 0x1080
movq %r15, 0x10(%rsp)
movslq %ebx, %rcx
movl $0x1, %eax
movq %rax, 0x48(%rsp)
leaq 0x60(%rsp), %rbx
jmp 0x2eea
movq %r12, %r14
subq $0x8, %rsp
leaq 0x28(%rsp), %rdi
leaq 0x78(%rsp), %rsi
leaq 0x8(%rsp), %rdx
leaq 0x38(%rsp), %r8
movq %r14, %rcx
movl $0x1, %r9d
pushq $0x2
pushq $0x1
pushq $0x0
callq 0x2878
addq $0x20, %rsp
xorl %eax, %eax
movq 0xa0(%rsp), %rdx
movb 0x80(%rsp,%rax), %cl
cmpb (%rsp,%rax), %cl
jne 0x30ad
incq %rax
cmpq $0x10, %rax
jne 0x307d
movq %rbp, (%rdx)
xorl %eax, %eax
jmp 0x30b2
leal 0x8(%rbp), %eax
cmpq $0xf, %rax
je 0x30c4
cmpb $0x0, 0x1(%rsp,%rax)
leaq 0x1(%rax), %rax
je 0x309c
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, %eax
movq %rax, 0x48(%rsp)
testl %ebp, %ebp
je 0x2d78
leaq 0x8(%rsp), %rsi
movq %r15, %rdi
movq %rbp, %rdx
callq 0x1080
jmp 0x2d78
nop