// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x138, %rsp            # imm = 0x138
movq 0x150(%rsp), %rax
movq 0x148(%rsp), %rax
movq 0x140(%rsp), %rax
movq %rdi, 0x128(%rsp)
movq %rsi, 0x120(%rsp)
movq %rdx, 0x118(%rsp)
movq %rcx, 0x110(%rsp)
movq %r8, 0x108(%rsp)
movq %r9, 0x100(%rsp)
movq 0x3cda(%rip), %rax      # 0x8130
movq %rax, 0xf8(%rsp)
movl $0x0, 0x64(%rsp)
movb $0x10, 0x3f(%rsp)
movb $0x0, 0x3e(%rsp)
movb $0x1, 0x3d(%rsp)
movq $0x0, 0x28(%rsp)
movq $0x0, 0x20(%rsp)
movl $0x0, 0x6c(%rsp)
cmpl $0x10, 0x6c(%rsp)
jge 0x44ad
movslq 0x6c(%rsp), %rax
movb $0x0, 0x50(%rsp,%rax)
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x448f
movl $0x1, 0x6c(%rsp)
cmpl $0x10, 0x6c(%rsp)
jge 0x44d3
movslq 0x6c(%rsp), %rax
movb $0x0, 0x40(%rsp,%rax)
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x44b5
movb $-0x80, 0x40(%rsp)
movq 0x108(%rsp), %rax
andq $0xf, %rax
cmpq $0x1, %rax
jne 0x44f5
cmpq $0x21, 0x108(%rsp)
jae 0x4505
movl $0xffffffff, 0x134(%rsp) # imm = 0xFFFFFFFF
jmp 0x4cd1
movq 0x150(%rsp), %rdi
callq 0x3c40
leaq 0xe0(%rsp), %rdx
leaq 0x50(%rsp), %rcx
movl $0xa, %edi
movl $0x1, %esi
movq 0x5aa0(%rip), %r8       # 0x9fd0
callq 0x3c80
leaq 0xc0(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x3d20
leaq 0xc0(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x3d60
leaq 0xb0(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x3d60
leaq 0xa0(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x3d20
leaq 0xa0(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x3d20
leaq 0xa0(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x3d60
movl $0x0, 0x6c(%rsp)
cmpl $0x10, 0x6c(%rsp)
jge 0x45dc
movslq 0x6c(%rsp), %rax
movb $0x0, 0xd0(%rsp,%rax)
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x45bb
leaq 0xd0(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
movq 0x148(%rsp), %rdx
leaq 0xf8(%rsp), %rcx
movq 0x100(%rsp), %r8
movq 0x140(%rsp), %r9
callq 0x3e20
leaq 0x70(%rsp), %rdi
leaq 0x50(%rsp), %rsi
leaq 0x50(%rsp), %rdx
movl $0x10, %ecx
xorl %r8d, %r8d
callq 0x3fa0
leaq 0x90(%rsp), %rdi
movq 0x110(%rsp), %rsi
leaq 0x50(%rsp), %rdx
movl $0x10, %ecx
xorl %r8d, %r8d
callq 0x3fa0
cmpq $0x21, 0x108(%rsp)
jne 0x4791
movq 0x110(%rsp), %rax
movq 0x108(%rsp), %rcx
subq $0x1, %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x1, %eax
jne 0x4791
leaq 0xa0(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
leaq 0x80(%rsp), %rdx
leaq 0x90(%rsp), %rcx
leaq 0xd0(%rsp), %r8
xorl %r9d, %r9d
movl $0x0, (%rsp)
movzbl 0x3e(%rsp), %eax
movl %eax, 0x8(%rsp)
movl $0x2, 0x10(%rsp)
callq 0x40f0
movl $0xf, 0x6c(%rsp)
cmpl $0x7, 0x6c(%rsp)
jle 0x4703
movslq 0x6c(%rsp), %rax
movzbl 0x80(%rsp,%rax), %eax
cmpl $0x80, %eax
jne 0x46f4
movl $0x1, 0x64(%rsp)
jmp 0x4703
jmp 0x46f6
movl 0x6c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x46cf
cmpl $0x0, 0x64(%rsp)
jne 0x471a
movl $0xffffffff, 0x134(%rsp) # imm = 0xFFFFFFFF
jmp 0x4cd1
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x68(%rsp)
cmpl $0x10, 0x68(%rsp)
jge 0x475d
movslq 0x68(%rsp), %rax
movzbl 0x80(%rsp,%rax), %eax
cmpl $0x0, %eax
je 0x474e
movl $0xffffffff, 0x134(%rsp) # imm = 0xFFFFFFFF
jmp 0x4cd1
jmp 0x4750
movl 0x68(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x68(%rsp)
jmp 0x4725
movq 0x128(%rsp), %rdi
leaq 0x80(%rsp), %rsi
movl 0x6c(%rsp), %ecx
subl $0x1, %ecx
movl $0x8, %edx
callq 0x42e0
movl 0x6c(%rsp), %eax
subl $0x8, %eax
cltq
movq %rax, 0x30(%rsp)
jmp 0x481a
leaq 0xa0(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
leaq 0x80(%rsp), %rdx
leaq 0x90(%rsp), %rcx
leaq 0xd0(%rsp), %r8
movl $0x1, %r9d
xorl %eax, %eax
movl $0x0, (%rsp)
movzbl 0x3e(%rsp), %eax
movl %eax, 0x8(%rsp)
movl $0x2, 0x10(%rsp)
callq 0x40f0
movq 0x128(%rsp), %rdi
leaq 0x80(%rsp), %rsi
movl $0x8, %edx
movl $0xf, %ecx
callq 0x42e0
movq 0x128(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x128(%rsp)
movq $0x8, 0x30(%rsp)
leaq 0x70(%rsp), %rdi
leaq 0x70(%rsp), %rsi
leaq 0x80(%rsp), %rdx
callq 0x4340
movq 0x118(%rsp), %rdi
leaq 0x80(%rsp), %rsi
xorl %edx, %edx
movl $0x7, %ecx
callq 0x42e0
movq 0x108(%rsp), %rax
subq $0x10, %rax
movq %rax, 0x108(%rsp)
movq 0x110(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x110(%rsp)
movq 0x28(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x28(%rsp)
cmpq $0x11, 0x108(%rsp)
jbe 0x4c1e
leaq 0x90(%rsp), %rdi
movq 0x110(%rsp), %rsi
leaq 0x50(%rsp), %rdx
movl $0x10, %ecx
xorl %r8d, %r8d
callq 0x3fa0
cmpq $0x21, 0x108(%rsp)
jne 0x4a16
movq 0x110(%rsp), %rax
movq 0x108(%rsp), %rcx
subq $0x1, %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x1, %eax
jne 0x4a16
leaq 0xa0(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
leaq 0x80(%rsp), %rdx
leaq 0x90(%rsp), %rcx
leaq 0xd0(%rsp), %r8
xorl %r9d, %r9d
movl $0x0, (%rsp)
movzbl 0x3e(%rsp), %eax
movl %eax, 0x8(%rsp)
movl $0x2, 0x10(%rsp)
callq 0x40f0
movl $0xffffffff, 0x6c(%rsp) # imm = 0xFFFFFFFF
leaq 0x80(%rsp), %rdi
leaq 0x80(%rsp), %rsi
leaq 0x70(%rsp), %rdx
callq 0x4340
movl $0xf, 0x6c(%rsp)
cmpl $0x0, 0x6c(%rsp)
jle 0x498a
movslq 0x6c(%rsp), %rax
movzbl 0x80(%rsp,%rax), %eax
cmpl $0x80, %eax
jne 0x497b
movl $0x1, 0x64(%rsp)
jmp 0x498a
jmp 0x497d
movl 0x6c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x4956
cmpl $0x0, 0x64(%rsp)
jne 0x49a1
movl $0xffffffff, 0x134(%rsp) # imm = 0xFFFFFFFF
jmp 0x4cd1
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x68(%rsp)
cmpl $0x10, 0x68(%rsp)
jge 0x49e4
movslq 0x68(%rsp), %rax
movzbl 0x80(%rsp,%rax), %eax
cmpl $0x0, %eax
je 0x49d5
movl $0xffffffff, 0x134(%rsp) # imm = 0xFFFFFFFF
jmp 0x4cd1
jmp 0x49d7
movl 0x68(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x68(%rsp)
jmp 0x49ac
movq 0x128(%rsp), %rdi
leaq 0x80(%rsp), %rsi
movl 0x6c(%rsp), %ecx
subl $0x1, %ecx
xorl %edx, %edx
callq 0x42e0
movslq 0x6c(%rsp), %rax
addq 0x30(%rsp), %rax
movq %rax, 0x30(%rsp)
jmp 0x4ab2
leaq 0xa0(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
leaq 0x80(%rsp), %rdx
leaq 0x90(%rsp), %rcx
leaq 0xd0(%rsp), %r8
movl $0x1, %r9d
xorl %eax, %eax
movl $0x0, (%rsp)
movzbl 0x3e(%rsp), %eax
movl %eax, 0x8(%rsp)
movl $0x2, 0x10(%rsp)
callq 0x40f0
cmpq $0x21, 0x108(%rsp)
jne 0x4a88
leaq 0x80(%rsp), %rdi
leaq 0x80(%rsp), %rsi
leaq 0x70(%rsp), %rdx
callq 0x4340
movq 0x128(%rsp), %rdi
leaq 0x80(%rsp), %rsi
xorl %edx, %edx
movl $0xf, %ecx
callq 0x42e0
movq 0x30(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x30(%rsp)
leaq 0x70(%rsp), %rdi
leaq 0x70(%rsp), %rsi
leaq 0x80(%rsp), %rdx
callq 0x4340
movq 0x108(%rsp), %rax
subq $0x10, %rax
movq %rax, 0x108(%rsp)
movq 0x110(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x110(%rsp)
movq 0x28(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x28(%rsp)
cmpq $0x11, 0x108(%rsp)
je 0x4b1e
movq 0x128(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x128(%rsp)
cmpq $0x7f, 0x28(%rsp)
jne 0x4c19
cmpq $0x20, 0x20(%rsp)
jae 0x4c19
cmpq $0x30, 0x108(%rsp)
jbe 0x4c19
leaq 0x80(%rsp), %rdx
leaq 0xd0(%rsp), %rcx
movl $0xa, %edi
xorl %esi, %esi
movq 0x547d(%rip), %r8       # 0x9fe0
callq 0x3c80
leaq 0xa0(%rsp), %rdi
leaq 0x80(%rsp), %rsi
leaq 0x80(%rsp), %rdx
movl $0x1, %ecx
callq 0x43a0
movl $0x0, 0x6c(%rsp)
cmpl $0x10, 0x6c(%rsp)
jge 0x4bda
movq 0x110(%rsp), %rax
movslq 0x6c(%rsp), %rcx
movzbl (%rax,%rcx), %eax
movslq 0x6c(%rsp), %rcx
movzbl 0x80(%rsp,%rcx), %ecx
cmpl %ecx, %eax
je 0x4bcb
movl $0xffffffff, 0x134(%rsp) # imm = 0xFFFFFFFF
jmp 0x4cd1
jmp 0x4bcd
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x4b92
movq 0x110(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x110(%rsp)
movq 0x108(%rsp), %rax
subq $0x10, %rax
movq %rax, 0x108(%rsp)
movq $0x0, 0x28(%rsp)
movq 0x20(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x20(%rsp)
jmp 0x4883
movb $0x1, 0x3e(%rsp)
leaq 0xa0(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
leaq 0x80(%rsp), %rdx
movq 0x110(%rsp), %rcx
leaq 0xd0(%rsp), %r8
movl $0x1, %r9d
xorl %eax, %eax
movl $0x0, (%rsp)
movzbl 0x3e(%rsp), %eax
movl %eax, 0x8(%rsp)
movl $0x2, 0x10(%rsp)
callq 0x40f0
movl $0x0, 0x6c(%rsp)
cmpl $0x10, 0x6c(%rsp)
jge 0x4cb6
movslq 0x6c(%rsp), %rax
movzbl 0x70(%rsp,%rax), %eax
movslq 0x6c(%rsp), %rcx
movzbl 0x80(%rsp,%rcx), %ecx
cmpl %ecx, %eax
je 0x4ca7
movl $0xffffffff, 0x134(%rsp) # imm = 0xFFFFFFFF
jmp 0x4cd1
jmp 0x4ca9
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x4c78
movq 0x30(%rsp), %rcx
movq 0x120(%rsp), %rax
movq %rcx, (%rax)
movl $0x0, 0x134(%rsp)
movl 0x134(%rsp), %eax
addq $0x138, %rsp            # imm = 0x138
retq