// File: /workspace/github_c_makefile_star2_O3/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 $0xf8, %rsp
movq %r8, %r13
movq %rcx, %r12
movabsq $0x80807f000a, %rax     # imm = 0x80807F000A
movq %rax, 0xb8(%rsp)
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0xc0(%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 0x30f6
movq %r9, %rbp
movq %rdx, 0x60(%rsp)
movq %rdi, 0x28(%rsp)
movq %rsi, 0xb0(%rsp)
movq 0x140(%rsp), %rbx
movq 0x53ce(%rip), %r14      # 0x7fd0
movq %rbx, %rdi
movl $0x80, %esi
movq %r14, %rdx
callq 0x1090
movq 0x53c7(%rip), %rdx      # 0x7fe0
movq %rbx, %rdi
movl $0x80, %esi
callq 0x1040
leaq 0xc0(%rsp), %rdi
leaq 0xe0(%rsp), %r15
movq %r15, %rsi
movq %r14, %rdx
callq 0x1060
leaq 0xd0(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x2618
movq %r14, %rdi
movq %r14, %rsi
callq 0x2686
leaq 0x80(%rsp), %rdi
movq %r15, %rsi
callq 0x2686
leaq 0x30(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2618
movq %rbx, %rdi
movq %rbx, %rsi
callq 0x2618
movq %rbx, %rdi
movq %rbx, %rsi
callq 0x2686
leaq 0x40(%rsp), %rdi
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, (%rdi)
leaq 0xb8(%rsp), %rcx
movq %r14, %rsi
movq 0x138(%rsp), %rdx
movq %rbp, %r8
movq 0x130(%rsp), %r9
callq 0x2700
leaq 0xc0(%rsp), %rax
vmovaps (%rax), %xmm0
vmovaps %xmm0, 0x90(%rsp)
vmovups (%r12), %xmm0
vmovaps %xmm0, 0x70(%rsp)
cmpq $0x21, %r13
jne 0x2d56
cmpb $0x1, 0x20(%r12)
jne 0x2d56
subq $0x8, %rsp
xorl %ebx, %ebx
leaq 0x38(%rsp), %rdi
leaq 0x88(%rsp), %rsi
leaq 0x18(%rsp), %rdx
leaq 0x78(%rsp), %rcx
leaq 0x48(%rsp), %r8
xorl %r9d, %r9d
pushq $0x2
pushq %rbx
pushq %rbx
callq 0x28a6
addq $0x20, %rsp
movl $0x7, %ebp
movl $0x10, %eax
movq 0x28(%rsp), %r15
leal 0x8(%rbp), %ecx
cmpb 0x10(%rsp,%rcx), %bl
jo 0x30e0
decq %rbp
decl %eax
cmpl $0x9, %eax
jae 0x2d35
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x30f6
subq $0x8, %rsp
xorl %eax, %eax
leaq 0x38(%rsp), %rdi
leaq 0x88(%rsp), %rsi
leaq 0x18(%rsp), %rbx
leaq 0x78(%rsp), %rcx
leaq 0x48(%rsp), %r8
movq %rbx, %rdx
movl $0x1, %r9d
pushq $0x2
pushq %rax
movq $0x0, 0x70(%rsp)
pushq %rax
callq 0x28a6
addq $0x20, %rsp
movq 0x8(%rbx), %rax
movq 0x28(%rsp), %r15
movq %rax, (%r15)
addq $0x8, %r15
movl $0x8, %ebp
vmovaps 0x10(%rsp), %xmm0
vxorps 0x90(%rsp), %xmm0, %xmm0
vmovaps %xmm0, 0x90(%rsp)
movq 0x10(%rsp), %rax
movq 0x60(%rsp), %rcx
movq %rax, (%rcx)
addq $-0x10, %r13
addq $0x10, %r12
cmpq $0x12, %r13
jb 0x307f
movl $0x1, %eax
movq %rax, 0x20(%rsp)
vmovaps 0x90(%rsp), %xmm0
vmovaps %xmm0, 0x60(%rsp)
xorl %ebx, %ebx
movq $0x0, 0xa0(%rsp)
xorl %eax, %eax
movb (%r12,%rax), %cl
movb %cl, 0x70(%rsp,%rax)
incq %rax
cmpq $0x10, %rax
jne 0x2e0c
cmpq $0x21, %r13
jne 0x2eb8
cmpb $0x1, 0x20(%r12)
jne 0x2eed
subq $0x8, %rsp
leaq 0x38(%rsp), %rdi
leaq 0x88(%rsp), %rsi
leaq 0x18(%rsp), %rdx
leaq 0x78(%rsp), %rcx
leaq 0x48(%rsp), %r8
xorl %r9d, %r9d
pushq $0x2
pushq %rbx
pushq %rbx
callq 0x28a6
addq $0x20, %rsp
vmovaps 0x60(%rsp), %xmm0
vxorps 0x10(%rsp), %xmm0, %xmm0
vmovaps %xmm0, 0x10(%rsp)
movl $0xf, %eax
movl %eax, %ecx
cmpb 0x10(%rsp,%rcx), %bl
jo 0x2e99
leal -0x1(%rax), %ecx
cmpl $0x1, %eax
movl %ecx, %eax
ja 0x2e7a
xorl %eax, %eax
cmpl $0x0, 0x58(%rsp)
je 0x2d4c
movl %eax, %ebx
movq %rbx, %rax
cmpq $0xf, %rax
je 0x3054
cmpb $0x0, 0x11(%rsp,%rax)
leaq 0x1(%rax), %rax
je 0x2e9e
jmp 0x2d4c
subq $0x8, %rsp
leaq 0x38(%rsp), %rdi
leaq 0x88(%rsp), %rsi
leaq 0x18(%rsp), %rdx
leaq 0x78(%rsp), %rcx
leaq 0x48(%rsp), %r8
movl $0x1, %r9d
pushq $0x2
pushq %rbx
pushq %rbx
callq 0x28a6
addq $0x20, %rsp
jmp 0x2f32
subq $0x8, %rsp
leaq 0x38(%rsp), %rdi
leaq 0x88(%rsp), %rsi
leaq 0x18(%rsp), %rdx
leaq 0x78(%rsp), %rcx
leaq 0x48(%rsp), %r8
movl $0x1, %r9d
pushq $0x2
pushq %rbx
pushq %rbx
callq 0x28a6
addq $0x20, %rsp
vmovaps 0x60(%rsp), %xmm0
vxorps 0x10(%rsp), %xmm0, %xmm0
vmovaps %xmm0, 0x10(%rsp)
vmovaps 0x10(%rsp), %xmm0
movq %r15, 0x28(%rsp)
vmovups %xmm0, (%r15)
movl $0x10, %edx
movq 0x20(%rsp), %rcx
vmovaps 0x60(%rsp), %xmm0
vxorps 0x10(%rsp), %xmm0, %xmm0
vmovaps %xmm0, 0x60(%rsp)
vmovaps %xmm0, 0x90(%rsp)
leaq -0x10(%r13), %rax
xorl %r15d, %r15d
cmpq $0x11, %rax
setne %sil
leaq 0x10(%r12), %r14
incq %rcx
cmpq $0x31, %rax
jb 0x3023
cmpq $0x7f, %rcx
jne 0x3023
cmpq $0x1f, 0xa0(%rsp)
ja 0x3023
leaq 0x40(%rsp), %rdi
leaq 0x10(%rsp), %rbx
movb %sil, 0xf(%rsp)
movq %rbx, %rsi
movq %rdx, 0xa8(%rsp)
movq 0x501e(%rip), %rdx      # 0x7fe0
callq 0x1050
leaq 0x30(%rsp), %rdi
movq %rbx, %rsi
movq %rbx, %rdx
movl $0x1, %ecx
callq 0x2a55
movb 0xf(%rsp), %sil
movq 0xa8(%rsp), %rdx
xorl %eax, %eax
movb (%r14,%rax), %cl
cmpb 0x10(%rsp,%rax), %cl
jne 0x2d4c
incq %rax
cmpq $0x10, %rax
jne 0x2feb
addq $0x20, %r12
addq $-0x20, %r13
incq 0xa0(%rsp)
movq %r13, %rax
movq %r12, %r14
movq $0x0, 0x20(%rsp)
jmp 0x3028
movq %rcx, 0x20(%rsp)
addq %rdx, %rbp
movb %sil, %r15b
shll $0x4, %r15d
movq 0x28(%rsp), %rcx
addq %r15, %rcx
movq %rcx, %r15
movq %r14, %r12
movq %rax, %r13
cmpq $0x11, %rax
movl $0x0, %ebx
ja 0x2e0a
jmp 0x3082
testl %ebx, %ebx
jle 0x3068
movq %r15, %rdi
leaq 0x10(%rsp), %rsi
movq %rbx, %rdx
callq 0x1080
movq %r15, 0x28(%rsp)
movslq %ebx, %rdx
movl $0x1, %eax
movq %rax, 0x58(%rsp)
jmp 0x2f47
movq %r12, %r14
subq $0x8, %rsp
leaq 0x38(%rsp), %rdi
leaq 0x88(%rsp), %rsi
leaq 0x18(%rsp), %rdx
leaq 0x48(%rsp), %r8
movq %r14, %rcx
movl $0x1, %r9d
pushq $0x2
pushq $0x1
pushq $0x0
callq 0x28a6
addq $0x20, %rsp
xorl %eax, %eax
movq 0xb0(%rsp), %rdx
movb 0x90(%rsp,%rax), %cl
cmpb 0x10(%rsp,%rax), %cl
jne 0x2d4c
incq %rax
cmpq $0x10, %rax
jne 0x30bf
movq %rbp, (%rdx)
xorl %eax, %eax
jmp 0x30f6
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq $0xf, %rcx
je 0x3108
cmpb $0x0, 0x11(%rsp,%rcx)
leaq 0x1(%rcx), %rcx
je 0x30e5
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, %eax
movq %rax, 0x58(%rsp)
testl %ebp, %ebp
je 0x2dac
leaq 0x18(%rsp), %rsi
movq %r15, %rdi
movq %rbp, %rdx
callq 0x1080
jmp 0x2dac
nop