// File: /workspace/github_c_makefile_star2_O1/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movabsq $0x80817f000a, %rax     # imm = 0x80817F000A
movq %rax, 0x88(%rsp)
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x90(%rsp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq $0x18, %r8
jb 0x3008
movq %r8, %r12
movq %rcx, %r13
movq %rdi, %r15
movq %rdx, 0x18(%rsp)
movq %rsi, 0x80(%rsp)
movq 0x110(%rsp), %rbx
movq 0x53fa(%rip), %rbp      # 0x7fd0
movq %rbx, %rdi
movl $0x80, %esi
movq %rbp, %rdx
movq %r9, 0x38(%rsp)
callq 0x1090
movq 0x53ee(%rip), %rdx      # 0x7fe0
movq %rbx, %rdi
movl $0x80, %esi
callq 0x1040
leaq 0x90(%rsp), %rdi
leaq 0xb0(%rsp), %rbx
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x1060
leaq 0xa0(%rsp), %rbp
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x2629
movq %rbp, %rdi
movq %rbp, %rsi
callq 0x268e
leaq 0x70(%rsp), %rdi
movq %rbx, %rsi
callq 0x268e
leaq 0x40(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2629
movq %r14, %rdi
movq %r14, %rsi
callq 0x2629
movq %r14, %rdi
movq %r14, %rsi
callq 0x268e
leaq 0x50(%rsp), %rdi
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, (%rdi)
leaq 0x88(%rsp), %rcx
movq %rbp, %rsi
movq 0x108(%rsp), %rdx
movq 0x38(%rsp), %r8
movq 0x100(%rsp), %r9
callq 0x2708
leaq 0x90(%rsp), %rax
vmovaps (%rax), %xmm0
vmovaps %xmm0, 0x20(%rsp)
vmovups (%r13), %xmm0
vmovaps %xmm0, 0x60(%rsp)
subq $0x8, %rsp
cmpq $0x1f, %r12
ja 0x2d12
xorl %eax, %eax
leaq 0x48(%rsp), %rdi
leaq 0x78(%rsp), %rsi
leaq 0x8(%rsp), %rdx
leaq 0x68(%rsp), %rcx
leaq 0x58(%rsp), %r8
xorl %r9d, %r9d
pushq $0x2
pushq %rax
pushq %rax
callq 0x2887
addq $0x20, %rsp
cmpl $0x19, %r12d
jl 0x2d0b
leaq 0x8(%rsp), %rsi
movl $0xffffffe8, %edx       # imm = 0xFFFFFFE8
addl %r12d, %edx
movq %r15, %rdi
callq 0x1080
leaq -0x18(%r12), %rbx
jmp 0x2d53
xorl %eax, %eax
leaq 0x48(%rsp), %rdi
leaq 0x78(%rsp), %rsi
leaq 0x8(%rsp), %rbx
leaq 0x68(%rsp), %rcx
leaq 0x58(%rsp), %r8
movq %rbx, %rdx
movl $0x1, %r9d
pushq $0x2
pushq %rax
pushq %rax
callq 0x2887
addq $0x20, %rsp
movq 0x8(%rbx), %rax
movq %rax, (%r15)
addq $0x8, %r15
movl $0x8, %ebx
vmovaps (%rsp), %xmm0
vxorps 0x20(%rsp), %xmm0, %xmm0
vmovaps %xmm0, 0x20(%rsp)
movq (%rsp), %rax
movq 0x18(%rsp), %rcx
movq %rax, (%rcx)
leaq -0x10(%r12), %rbp
cmpq $0xf, %rbp
ja 0x2db7
xorl %eax, %eax
cmpb (%rsp,%rbp), %al
jno 0x3003
cmpl $0x1e, %r12d
jg 0x2db7
leal 0x11(%r12), %ecx
andl $0x1f, %ecx
addb $0x11, %r12b
movzbl %r12b, %eax
andl $0x1f, %eax
decl %eax
addq %rsp, %rcx
cmpb $0x0, (%rcx)
jne 0x3003
incl %eax
incq %rcx
cmpl $0xe, %eax
jle 0x2da4
addq $0x10, %r13
cmpq $0x11, %rbp
jb 0x2fa2
movl $0x1, %edx
movq $0x0, 0x18(%rsp)
xorl %eax, %eax
movb (%r13,%rax), %cl
movb %cl, 0x60(%rsp,%rax)
incq %rax
cmpq $0x10, %rax
jne 0x2dd5
movq %rdx, %r14
subq $0x8, %rsp
cmpq $0x1f, %rbp
ja 0x2e51
leaq 0x48(%rsp), %rdi
leaq 0x78(%rsp), %rsi
leaq 0x8(%rsp), %rdx
leaq 0x68(%rsp), %rcx
leaq 0x58(%rsp), %r8
xorl %r9d, %r9d
pushq $0x2
xorl %eax, %eax
pushq %rax
xorl %r12d, %r12d
pushq %rax
callq 0x2887
addq $0x20, %rsp
vmovaps 0x20(%rsp), %xmm0
vxorps (%rsp), %xmm0, %xmm0
vmovaps %xmm0, (%rsp)
cmpl $0x11, %ebp
jl 0x2e4b
leal -0x10(%rbp), %eax
xorl %ecx, %ecx
movb (%rsp,%rcx), %dl
movb %dl, (%r15,%rcx)
incq %rcx
cmpq %rcx, %rax
jne 0x2e3c
leaq -0x10(%rbp), %rsi
jmp 0x2eaf
leaq 0x48(%rsp), %rdi
leaq 0x78(%rsp), %rsi
leaq 0x8(%rsp), %rdx
leaq 0x68(%rsp), %rcx
leaq 0x58(%rsp), %r8
movl $0x1, %r9d
pushq $0x2
xorl %eax, %eax
pushq %rax
xorl %r12d, %r12d
pushq %rax
callq 0x2887
addq $0x20, %rsp
cmpq $0x20, %rbp
jne 0x2e98
vmovaps 0x20(%rsp), %xmm0
vxorps (%rsp), %xmm0, %xmm0
vmovaps %xmm0, (%rsp)
xorl %eax, %eax
movb (%rsp,%rax), %cl
movb %cl, (%r15,%rax)
incq %rax
cmpq $0x10, %rax
jne 0x2e9a
movl $0x10, %esi
movq %r14, %rdx
vmovaps (%rsp), %xmm0
vxorps 0x20(%rsp), %xmm0, %xmm0
vmovaps %xmm0, 0x20(%rsp)
leaq -0x10(%rbp), %r14
cmpq $0xf, %r14
ja 0x2f05
cmpb (%rsp,%r14), %r12b
jno 0x3003
cmpl $0x1e, %ebp
jg 0x2f09
leal 0x11(%rbp), %ecx
andl $0x1f, %ecx
leal 0x11(%rbp), %eax
movzbl %al, %eax
andl $0x1f, %eax
decl %eax
addq %rsp, %rcx
cmpb $0x0, (%rcx)
jne 0x3003
incl %eax
incq %rcx
cmpl $0xe, %eax
jle 0x2ef0
jmp 0x2f09
addq $0x10, %r15
leaq 0x10(%r13), %r12
incq %rdx
cmpq $0x21, %r14
jb 0x2f8d
cmpq $0x7f, %rdx
jne 0x2f8d
cmpq $0x1f, 0x18(%rsp)
ja 0x2f8d
movq %rbx, 0x38(%rsp)
leaq 0x50(%rsp), %rdi
movq %rsp, %r14
movq %rsi, %rbx
movq %r14, %rsi
movq 0x50a2(%rip), %rdx      # 0x7fe0
callq 0x1050
leaq 0x40(%rsp), %rdi
movq %r14, %rsi
movq %r14, %rdx
movl $0x1, %ecx
callq 0x2a4f
movq %rbx, %rsi
xorl %eax, %eax
movb (%r12,%rax), %cl
cmpb (%rsp,%rax), %cl
jne 0x3003
incq %rax
cmpq $0x10, %rax
jne 0x2f5d
addq $0x20, %r13
addq $-0x20, %rbp
incq 0x18(%rsp)
movq %rbp, %r14
movq %r13, %r12
xorl %edx, %edx
movq 0x38(%rsp), %rbx
addq %rsi, %rbx
movq %r12, %r13
movq %r14, %rbp
cmpq $0x10, %r14
ja 0x2dd3
jmp 0x2fa8
movq %rbp, %r14
movq %r13, %r12
subq $0x8, %rsp
movl $0x1, %eax
leaq 0x78(%rsp), %rdi
leaq 0x48(%rsp), %rsi
leaq 0x8(%rsp), %rdx
leaq 0x28(%rsp), %rcx
leaq 0x58(%rsp), %r8
movl $0x1, %r9d
pushq %rax
pushq %rax
pushq %rax
callq 0x2887
addq $0x20, %rsp
testq %r14, %r14
je 0x2ff4
xorl %eax, %eax
movb (%rsp,%rax), %cl
cmpb (%r12,%rax), %cl
jne 0x3003
incq %rax
cmpq %rax, %r14
jne 0x2fe3
movq 0x80(%rsp), %rax
movq %rbx, (%rax)
xorl %eax, %eax
jmp 0x3008
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop