// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x128, %rsp            # imm = 0x128
movq 0x140(%rsp), %rax
movq 0x138(%rsp), %rax
movq 0x130(%rsp), %rax
movq %rdi, 0x118(%rsp)
movq %rsi, 0x110(%rsp)
movq %rdx, 0x108(%rsp)
movq %rcx, 0x100(%rsp)
movq %r8, 0xf8(%rsp)
movq %r9, 0xf0(%rsp)
movq 0x2e3a(%rip), %rax      # 0x7130
movq %rax, 0xe8(%rsp)
movl $0x0, 0x54(%rsp)
movb $0x10, 0x2f(%rsp)
movb $0x0, 0x2e(%rsp)
movb $0x1, 0x2d(%rsp)
movl $0x0, 0x5c(%rsp)
cmpl $0x10, 0x5c(%rsp)
jge 0x433b
movslq 0x5c(%rsp), %rax
movb $0x0, 0x40(%rsp,%rax)
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x431d
movl $0x1, 0x5c(%rsp)
cmpl $0x10, 0x5c(%rsp)
jge 0x4361
movslq 0x5c(%rsp), %rax
movb $0x0, 0x30(%rsp,%rax)
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x4343
movb $-0x80, 0x30(%rsp)
movq 0xf8(%rsp), %rax
andq $0xf, %rax
cmpq $0x1, %rax
jne 0x4383
cmpq $0x21, 0xf8(%rsp)
jae 0x4393
movl $0xffffffff, 0x124(%rsp) # imm = 0xFFFFFFFF
jmp 0x4a27
movq 0x140(%rsp), %rdi
callq 0x3b40
leaq 0x80(%rsp), %rdx
leaq 0x40(%rsp), %rcx
movl $0x6, %edi
movl $0x1, %esi
movq 0x4c12(%rip), %r8       # 0x8fd0
callq 0x3b80
leaq 0xd0(%rsp), %rdx
leaq 0x80(%rsp), %rcx
movl $0x6, %edi
movl $0x1, %esi
movq 0x4bec(%rip), %r8       # 0x8fd0
callq 0x3b80
leaq 0xb0(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x3c20
leaq 0xb0(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x3c60
leaq 0xa0(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x3c60
leaq 0x90(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x3c20
leaq 0x90(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x3c20
leaq 0x90(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x3c60
movl $0x0, 0x5c(%rsp)
cmpl $0x10, 0x5c(%rsp)
jge 0x4490
movslq 0x5c(%rsp), %rax
movb $0x0, 0xc0(%rsp,%rax)
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x446f
leaq 0xc0(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
movq 0x138(%rsp), %rdx
leaq 0xe8(%rsp), %rcx
movq 0xf0(%rsp), %r8
movq 0x130(%rsp), %r9
callq 0x3d20
leaq 0x60(%rsp), %rdi
leaq 0x40(%rsp), %rsi
leaq 0x40(%rsp), %rdx
movl $0x10, %ecx
xorl %r8d, %r8d
callq 0x3ea0
leaq 0x80(%rsp), %rdi
movq 0x100(%rsp), %rsi
leaq 0x40(%rsp), %rdx
movl $0x10, %ecx
xorl %r8d, %r8d
callq 0x3ea0
cmpq $0x21, 0xf8(%rsp)
jne 0x4639
movq 0x100(%rsp), %rax
movq 0xf8(%rsp), %rcx
subq $0x1, %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x1, %eax
jne 0x4639
leaq 0x90(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
leaq 0x70(%rsp), %rdx
leaq 0x80(%rsp), %rcx
leaq 0xc0(%rsp), %r8
xorl %r9d, %r9d
movl $0x0, (%rsp)
movzbl 0x2e(%rsp), %eax
movl %eax, 0x8(%rsp)
movl $0x2, 0x10(%rsp)
callq 0x3ff0
movl $0xf, 0x5c(%rsp)
cmpl $0x7, 0x5c(%rsp)
jle 0x45b1
movslq 0x5c(%rsp), %rax
movzbl 0x70(%rsp,%rax), %eax
cmpl $0x80, %eax
jne 0x45a2
movl $0x1, 0x54(%rsp)
jmp 0x45b1
jmp 0x45a4
movl 0x5c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x4580
cmpl $0x0, 0x54(%rsp)
jne 0x45c8
movl $0xffffffff, 0x124(%rsp) # imm = 0xFFFFFFFF
jmp 0x4a27
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x58(%rsp)
cmpl $0x10, 0x58(%rsp)
jge 0x4608
movslq 0x58(%rsp), %rax
movzbl 0x70(%rsp,%rax), %eax
cmpl $0x0, %eax
je 0x45f9
movl $0xffffffff, 0x124(%rsp) # imm = 0xFFFFFFFF
jmp 0x4a27
jmp 0x45fb
movl 0x58(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x58(%rsp)
jmp 0x45d3
movq 0x118(%rsp), %rdi
leaq 0x70(%rsp), %rsi
movl 0x5c(%rsp), %ecx
subl $0x1, %ecx
movl $0x8, %edx
callq 0x41e0
movl 0x5c(%rsp), %eax
subl $0x8, %eax
cltq
movq %rax, 0x20(%rsp)
jmp 0x46bc
leaq 0x90(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
leaq 0x70(%rsp), %rdx
leaq 0x80(%rsp), %rcx
leaq 0xc0(%rsp), %r8
movl $0x1, %r9d
xorl %eax, %eax
movl $0x0, (%rsp)
movzbl 0x2e(%rsp), %eax
movl %eax, 0x8(%rsp)
movl $0x2, 0x10(%rsp)
callq 0x3ff0
movq 0x118(%rsp), %rdi
leaq 0x70(%rsp), %rsi
movl $0x8, %edx
movl $0xf, %ecx
callq 0x41e0
movq 0x118(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x118(%rsp)
movq $0x8, 0x20(%rsp)
leaq 0x60(%rsp), %rdi
leaq 0x60(%rsp), %rsi
leaq 0x70(%rsp), %rdx
callq 0x4240
movq 0x108(%rsp), %rdi
leaq 0x70(%rsp), %rsi
xorl %edx, %edx
movl $0x7, %ecx
callq 0x41e0
movq 0xf8(%rsp), %rax
subq $0x10, %rax
movq %rax, 0xf8(%rsp)
movq 0x100(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x100(%rsp)
cmpq $0x11, 0xf8(%rsp)
jbe 0x497a
leaq 0x80(%rsp), %rdi
movq 0x100(%rsp), %rsi
leaq 0x40(%rsp), %rdx
movl $0x10, %ecx
xorl %r8d, %r8d
callq 0x3ea0
cmpq $0x21, 0xf8(%rsp)
jne 0x488a
movq 0x100(%rsp), %rax
movq 0xf8(%rsp), %rcx
subq $0x1, %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x1, %eax
jne 0x488a
leaq 0x90(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
leaq 0x70(%rsp), %rdx
leaq 0x80(%rsp), %rcx
leaq 0xc0(%rsp), %r8
xorl %r9d, %r9d
movl $0x0, (%rsp)
movzbl 0x2e(%rsp), %eax
movl %eax, 0x8(%rsp)
movl $0x2, 0x10(%rsp)
callq 0x3ff0
leaq 0x70(%rsp), %rdi
leaq 0x70(%rsp), %rsi
leaq 0x60(%rsp), %rdx
callq 0x4240
movl $0xf, 0x5c(%rsp)
cmpl $0x0, 0x5c(%rsp)
jle 0x4804
movslq 0x5c(%rsp), %rax
movzbl 0x70(%rsp,%rax), %eax
cmpl $0x80, %eax
jne 0x47f5
movl $0x1, 0x54(%rsp)
jmp 0x4804
jmp 0x47f7
movl 0x5c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x47d3
cmpl $0x0, 0x54(%rsp)
jne 0x481b
movl $0xffffffff, 0x124(%rsp) # imm = 0xFFFFFFFF
jmp 0x4a27
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x58(%rsp)
cmpl $0x10, 0x58(%rsp)
jge 0x485b
movslq 0x58(%rsp), %rax
movzbl 0x70(%rsp,%rax), %eax
cmpl $0x0, %eax
je 0x484c
movl $0xffffffff, 0x124(%rsp) # imm = 0xFFFFFFFF
jmp 0x4a27
jmp 0x484e
movl 0x58(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x58(%rsp)
jmp 0x4826
movq 0x118(%rsp), %rdi
leaq 0x70(%rsp), %rsi
movl 0x5c(%rsp), %ecx
subl $0x1, %ecx
xorl %edx, %edx
callq 0x41e0
movslq 0x5c(%rsp), %rax
addq 0x20(%rsp), %rax
movq %rax, 0x20(%rsp)
jmp 0x491a
leaq 0x90(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
leaq 0x70(%rsp), %rdx
leaq 0x80(%rsp), %rcx
leaq 0xc0(%rsp), %r8
movl $0x1, %r9d
xorl %eax, %eax
movl $0x0, (%rsp)
movzbl 0x2e(%rsp), %eax
movl %eax, 0x8(%rsp)
movl $0x2, 0x10(%rsp)
callq 0x3ff0
cmpq $0x21, 0xf8(%rsp)
jne 0x48f3
leaq 0x70(%rsp), %rdi
leaq 0x70(%rsp), %rsi
leaq 0x60(%rsp), %rdx
callq 0x4240
movq 0x118(%rsp), %rdi
leaq 0x70(%rsp), %rsi
xorl %edx, %edx
movl $0xf, %ecx
callq 0x41e0
movq 0x20(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x20(%rsp)
leaq 0x60(%rsp), %rdi
leaq 0x60(%rsp), %rsi
leaq 0x70(%rsp), %rdx
callq 0x4240
movq 0xf8(%rsp), %rax
subq $0x10, %rax
movq %rax, 0xf8(%rsp)
movq 0x100(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x100(%rsp)
cmpq $0x11, 0xf8(%rsp)
je 0x4975
movq 0x118(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x118(%rsp)
jmp 0x4711
movb $0x1, 0x2e(%rsp)
leaq 0x90(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
leaq 0x70(%rsp), %rdx
movq 0x100(%rsp), %rcx
leaq 0xc0(%rsp), %r8
movl $0x1, %r9d
xorl %eax, %eax
movl $0x0, (%rsp)
movzbl 0x2e(%rsp), %eax
movl %eax, 0x8(%rsp)
movl $0x2, 0x10(%rsp)
callq 0x3ff0
movl $0x0, 0x5c(%rsp)
cmpl $0x10, 0x5c(%rsp)
jge 0x4a0c
movslq 0x5c(%rsp), %rax
movzbl 0x60(%rsp,%rax), %eax
movslq 0x5c(%rsp), %rcx
movzbl 0x70(%rsp,%rcx), %ecx
cmpl %ecx, %eax
je 0x49fd
movl $0xffffffff, 0x124(%rsp) # imm = 0xFFFFFFFF
jmp 0x4a27
jmp 0x49ff
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x49d1
movq 0x20(%rsp), %rcx
movq 0x110(%rsp), %rax
movq %rcx, (%rax)
movl $0x0, 0x124(%rsp)
movl 0x124(%rsp), %eax
addq $0x128, %rsp            # imm = 0x128
retq
nopw %cs:(%rax,%rax)