// File: /workspace/github_c_makefile_star2_O0/mjosaarinen[P]brutus/crypto_aead/elmd101271v2/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,0x81,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 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 < 24)
		return -1;
	
	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)); outputmlen = clen - 24;
	 }
	 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 ++; 

	 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); outputmlen += clen-16 ;
	   }
	   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); outputmlen += 16;
	   } 

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

	   
	   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; 

	   
	   if(blk_ctr == IT_GAP && blk_ctr1 < IT_MAX && clen > 32){ 
		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_1, Delta_2, result, CS, W, 1, ENCRYPT, Is_final, MESSAGE); 
	for(i=0; i<clen; i++) {if(result[i]!=c[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 0x2d6a(%rip), %rax      # 0x7130
movq %rax, 0xf8(%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 0x4415
movslq 0x6c(%rsp), %rax
movb $0x0, 0x50(%rsp,%rax)
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x43f7
movl $0x1, 0x6c(%rsp)
cmpl $0x10, 0x6c(%rsp)
jge 0x443b
movslq 0x6c(%rsp), %rax
movb $0x0, 0x40(%rsp,%rax)
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x441d
movb $-0x80, 0x40(%rsp)
cmpq $0x18, 0x108(%rsp)
jae 0x445b
movl $0xffffffff, 0x134(%rsp) # imm = 0xFFFFFFFF
jmp 0x4bc6
movq 0x150(%rsp), %rdi
callq 0x3bb0
leaq 0xe0(%rsp), %rdx
leaq 0x50(%rsp), %rcx
movl $0xa, %edi
movl $0x1, %esi
movq 0x4b4a(%rip), %r8       # 0x8fd0
callq 0x3bf0
leaq 0xc0(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x3c90
leaq 0xc0(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x3cd0
leaq 0xb0(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x3cd0
leaq 0xa0(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x3c90
leaq 0xa0(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x3c90
leaq 0xa0(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x3cd0
movl $0x0, 0x6c(%rsp)
cmpl $0x10, 0x6c(%rsp)
jge 0x4532
movslq 0x6c(%rsp), %rax
movb $0x0, 0xd0(%rsp,%rax)
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x4511
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 0x3d90
leaq 0x70(%rsp), %rdi
leaq 0x50(%rsp), %rsi
leaq 0x50(%rsp), %rdx
movl $0x10, %ecx
xorl %r8d, %r8d
callq 0x3f10
leaq 0x90(%rsp), %rdi
movq 0x110(%rsp), %rsi
leaq 0x50(%rsp), %rdx
movl $0x10, %ecx
xorl %r8d, %r8d
callq 0x3f10
cmpq $0x20, 0x108(%rsp)
jae 0x463e
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 0x4060
movq 0x128(%rsp), %rdi
leaq 0x80(%rsp), %rsi
movq 0x108(%rsp), %rax
subq $0x19, %rax
addq $0x8, %rax
movl %eax, %ecx
movl $0x8, %edx
callq 0x4250
movq 0x108(%rsp), %rax
subq $0x18, %rax
movq %rax, 0x30(%rsp)
jmp 0x46c7
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 0x4060
movq 0x128(%rsp), %rdi
leaq 0x80(%rsp), %rsi
movl $0x8, %edx
movl $0xf, %ecx
callq 0x4250
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 0x42b0
movq 0x118(%rsp), %rdi
leaq 0x80(%rsp), %rsi
xorl %edx, %edx
movl $0x7, %ecx
callq 0x4250
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 $0x10, 0x108(%rsp)
jae 0x47ac
movq 0x108(%rsp), %rax
movzbl 0x80(%rsp,%rax), %eax
cmpl $0x80, %eax
je 0x4762
movl $0xffffffff, 0x134(%rsp) # imm = 0xFFFFFFFF
jmp 0x4bc6
movq 0x108(%rsp), %rax
addq $0x1, %rax
movl %eax, 0x6c(%rsp)
cmpl $0x10, 0x6c(%rsp)
jge 0x47aa
movslq 0x6c(%rsp), %rax
movzbl 0x80(%rsp,%rax), %eax
cmpl $0x0, %eax
je 0x479b
movl $0xffffffff, 0x134(%rsp) # imm = 0xFFFFFFFF
jmp 0x4bc6
jmp 0x479d
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x4772
jmp 0x47ac
jmp 0x47ae
cmpq $0x10, 0x108(%rsp)
jbe 0x4b09
leaq 0x90(%rsp), %rdi
movq 0x110(%rsp), %rsi
leaq 0x50(%rsp), %rdx
movl $0x10, %ecx
xorl %r8d, %r8d
callq 0x3f10
cmpq $0x20, 0x108(%rsp)
jae 0x4890
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 0x4060
leaq 0x80(%rsp), %rdi
leaq 0x80(%rsp), %rsi
leaq 0x70(%rsp), %rdx
callq 0x42b0
movq 0x128(%rsp), %rdi
leaq 0x80(%rsp), %rsi
movq 0x108(%rsp), %rax
subq $0x11, %rax
movl %eax, %ecx
xorl %edx, %edx
callq 0x4250
movq 0x108(%rsp), %rax
subq $0x10, %rax
addq 0x30(%rsp), %rax
movq %rax, 0x30(%rsp)
jmp 0x492c
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 0x4060
cmpq $0x20, 0x108(%rsp)
jne 0x4902
leaq 0x80(%rsp), %rdi
leaq 0x80(%rsp), %rsi
leaq 0x70(%rsp), %rdx
callq 0x42b0
movq 0x128(%rsp), %rdi
leaq 0x80(%rsp), %rsi
xorl %edx, %edx
movl $0xf, %ecx
callq 0x4250
movq 0x30(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x30(%rsp)
leaq 0x70(%rsp), %rdi
leaq 0x70(%rsp), %rsi
leaq 0x80(%rsp), %rdx
callq 0x42b0
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 $0x10, 0x108(%rsp)
jae 0x49f5
movq 0x108(%rsp), %rax
movzbl 0x80(%rsp,%rax), %eax
cmpl $0x80, %eax
je 0x49ab
movl $0xffffffff, 0x134(%rsp) # imm = 0xFFFFFFFF
jmp 0x4bc6
movq 0x108(%rsp), %rax
addq $0x1, %rax
movl %eax, 0x6c(%rsp)
cmpl $0x10, 0x6c(%rsp)
jge 0x49f3
movslq 0x6c(%rsp), %rax
movzbl 0x80(%rsp,%rax), %eax
cmpl $0x0, %eax
je 0x49e4
movl $0xffffffff, 0x134(%rsp) # imm = 0xFFFFFFFF
jmp 0x4bc6
jmp 0x49e6
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x49bb
jmp 0x4a09
movq 0x128(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x128(%rsp)
cmpq $0x7f, 0x28(%rsp)
jne 0x4b04
cmpq $0x20, 0x20(%rsp)
jae 0x4b04
cmpq $0x20, 0x108(%rsp)
jbe 0x4b04
leaq 0x80(%rsp), %rdx
leaq 0xd0(%rsp), %rcx
movl $0xa, %edi
xorl %esi, %esi
movq 0x4592(%rip), %r8       # 0x8fe0
callq 0x3bf0
leaq 0xa0(%rsp), %rdi
leaq 0x80(%rsp), %rsi
leaq 0x80(%rsp), %rdx
movl $0x1, %ecx
callq 0x4310
movl $0x0, 0x6c(%rsp)
cmpl $0x10, 0x6c(%rsp)
jge 0x4ac5
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 0x4ab6
movl $0xffffffff, 0x134(%rsp) # imm = 0xFFFFFFFF
jmp 0x4bc6
jmp 0x4ab8
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x4a7d
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 0x47ae
movb $0x1, 0x3e(%rsp)
leaq 0xb0(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
leaq 0x80(%rsp), %rdx
leaq 0x70(%rsp), %rcx
leaq 0xd0(%rsp), %r8
movl $0x1, %r9d
movl $0x1, (%rsp)
movzbl 0x3e(%rsp), %eax
movl %eax, 0x8(%rsp)
movl $0x1, 0x10(%rsp)
callq 0x4060
movl $0x0, 0x6c(%rsp)
movslq 0x6c(%rsp), %rax
cmpq 0x108(%rsp), %rax
jae 0x4bab
movslq 0x6c(%rsp), %rax
movzbl 0x80(%rsp,%rax), %eax
movq 0x110(%rsp), %rcx
movslq 0x6c(%rsp), %rdx
movzbl (%rcx,%rdx), %ecx
cmpl %ecx, %eax
je 0x4b9c
movl $0xffffffff, 0x134(%rsp) # imm = 0xFFFFFFFF
jmp 0x4bc6
jmp 0x4b9e
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x4b5e
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
nopw %cs:(%rax,%rax)