// File: /workspace/github_c_makefile_star2_O0/mjosaarinen[P]brutus/crypto_aead/elmd1001v2/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,0,0x01,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 tlen, 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;

	*mlen = clen - 24; 
	
	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)); 
	 }
	 else {
		process_block(Delta_2, Delta_1, result, blk, W, 1, DECRYPT, Is_final, CIPHERTEXT);
		store_bytes(m, result, 8, 15);  m +=8;
	 }
	 xor_block(CS, CS, result); store_bytes(nsec, result, 0, 7); 
	 clen -= 16; c+=16;

	 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);  
	   }
	   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); 
	   } 

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

	   
	   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; 
	}

	/* ==========  Process checksum block  ============= */
	Is_final = 1;
	process_block(Delta_1, Delta_2, result, CS, W, 1, ENCRYPT, Is_final, MESSAGE); /* 2nd result was CS */
	for(i=0; i<clen; i++) {if(result[i]!=c[i]) return -1;} 

	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 0x2efa(%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 0x4285
movslq 0x6c(%rsp), %rax
movb $0x0, 0x50(%rsp,%rax)
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x4267
movl $0x1, 0x6c(%rsp)
cmpl $0x10, 0x6c(%rsp)
jge 0x42ab
movslq 0x6c(%rsp), %rax
movb $0x0, 0x40(%rsp,%rax)
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x428d
movb $-0x80, 0x40(%rsp)
cmpq $0x18, 0x108(%rsp)
jae 0x42cb
movl $0xffffffff, 0x134(%rsp) # imm = 0xFFFFFFFF
jmp 0x48e4
movq 0x108(%rsp), %rcx
subq $0x18, %rcx
movq 0x120(%rsp), %rax
movq %rcx, (%rax)
movq 0x150(%rsp), %rdi
callq 0x3a80
leaq 0xe0(%rsp), %rdx
leaq 0x50(%rsp), %rcx
movl $0xa, %edi
movl $0x1, %esi
movq 0x4cc3(%rip), %r8       # 0x8fd0
callq 0x3ac0
leaq 0xc0(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x3b60
leaq 0xc0(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x3ba0
leaq 0xb0(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x3ba0
leaq 0xa0(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x3b60
leaq 0xa0(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x3b60
leaq 0xa0(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x3ba0
movl $0x0, 0x6c(%rsp)
cmpl $0x10, 0x6c(%rsp)
jge 0x43b9
movslq 0x6c(%rsp), %rax
movb $0x0, 0xd0(%rsp,%rax)
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x4398
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 0x3c60
leaq 0x70(%rsp), %rdi
leaq 0x50(%rsp), %rsi
leaq 0x50(%rsp), %rdx
movl $0x10, %ecx
xorl %r8d, %r8d
callq 0x3de0
leaq 0x90(%rsp), %rdi
movq 0x110(%rsp), %rsi
leaq 0x50(%rsp), %rdx
movl $0x10, %ecx
xorl %r8d, %r8d
callq 0x3de0
cmpq $0x20, 0x108(%rsp)
jae 0x44b0
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 0x3f30
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 0x4120
jmp 0x4530
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 0x3f30
movq 0x128(%rsp), %rdi
leaq 0x80(%rsp), %rsi
movl $0x8, %edx
movl $0xf, %ecx
callq 0x4120
movq 0x128(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x128(%rsp)
leaq 0x70(%rsp), %rdi
leaq 0x70(%rsp), %rsi
leaq 0x80(%rsp), %rdx
callq 0x4180
movq 0x118(%rsp), %rdi
leaq 0x80(%rsp), %rsi
xorl %edx, %edx
movl $0x7, %ecx
callq 0x4120
movq 0x108(%rsp), %rax
subq $0x10, %rax
movq %rax, 0x108(%rsp)
movq 0x110(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x110(%rsp)
cmpq $0x10, 0x108(%rsp)
jae 0x4607
movq 0x108(%rsp), %rax
movzbl 0x80(%rsp,%rax), %eax
cmpl $0x80, %eax
je 0x45bd
movl $0xffffffff, 0x134(%rsp) # imm = 0xFFFFFFFF
jmp 0x48e4
movq 0x108(%rsp), %rax
addq $0x1, %rax
movl %eax, 0x6c(%rsp)
cmpl $0x10, 0x6c(%rsp)
jge 0x4605
movslq 0x6c(%rsp), %rax
movzbl 0x80(%rsp,%rax), %eax
cmpl $0x0, %eax
je 0x45f6
movl $0xffffffff, 0x134(%rsp) # imm = 0xFFFFFFFF
jmp 0x48e4
jmp 0x45f8
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x45cd
jmp 0x4607
jmp 0x4609
cmpq $0x10, 0x108(%rsp)
jbe 0x4837
leaq 0x90(%rsp), %rdi
movq 0x110(%rsp), %rsi
leaq 0x50(%rsp), %rdx
movl $0x10, %ecx
xorl %r8d, %r8d
callq 0x3de0
cmpq $0x20, 0x108(%rsp)
jae 0x46d5
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 0x3f30
leaq 0x80(%rsp), %rdi
leaq 0x80(%rsp), %rsi
leaq 0x70(%rsp), %rdx
callq 0x4180
movq 0x128(%rsp), %rdi
leaq 0x80(%rsp), %rsi
movq 0x108(%rsp), %rax
subq $0x11, %rax
movl %eax, %ecx
xorl %edx, %edx
callq 0x4120
jmp 0x4763
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 0x3f30
cmpq $0x20, 0x108(%rsp)
jne 0x4747
leaq 0x80(%rsp), %rdi
leaq 0x80(%rsp), %rsi
leaq 0x70(%rsp), %rdx
callq 0x4180
movq 0x128(%rsp), %rdi
leaq 0x80(%rsp), %rsi
xorl %edx, %edx
movl $0xf, %ecx
callq 0x4120
leaq 0x70(%rsp), %rdi
leaq 0x70(%rsp), %rsi
leaq 0x80(%rsp), %rdx
callq 0x4180
movq 0x108(%rsp), %rax
subq $0x10, %rax
movq %rax, 0x108(%rsp)
movq 0x110(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x110(%rsp)
cmpq $0x10, 0x108(%rsp)
jae 0x481e
movq 0x108(%rsp), %rax
movzbl 0x80(%rsp,%rax), %eax
cmpl $0x80, %eax
je 0x47d4
movl $0xffffffff, 0x134(%rsp) # imm = 0xFFFFFFFF
jmp 0x48e4
movq 0x108(%rsp), %rax
addq $0x1, %rax
movl %eax, 0x6c(%rsp)
cmpl $0x10, 0x6c(%rsp)
jge 0x481c
movslq 0x6c(%rsp), %rax
movzbl 0x80(%rsp,%rax), %eax
cmpl $0x0, %eax
je 0x480d
movl $0xffffffff, 0x134(%rsp) # imm = 0xFFFFFFFF
jmp 0x48e4
jmp 0x480f
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x47e4
jmp 0x4832
movq 0x128(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x128(%rsp)
jmp 0x4609
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 0x3f30
movl $0x0, 0x6c(%rsp)
movslq 0x6c(%rsp), %rax
cmpq 0x108(%rsp), %rax
jae 0x48d9
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 0x48ca
movl $0xffffffff, 0x134(%rsp) # imm = 0xFFFFFFFF
jmp 0x48e4
jmp 0x48cc
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x488c
movl $0x0, 0x134(%rsp)
movl 0x134(%rsp), %eax
addq $0x138, %rsp            # imm = 0x138
retq
nopw %cs:(%rax,%rax)