// File: /workspace/github_c_makefile_star2_O1/mjosaarinen[P]brutus/crypto_aead/elmd101271v2/ref/encrypt.c
// Function: crypto_aead_encrypt
int crypto_aead_encrypt(
unsigned char *c,unsigned long long *clen,
const unsigned char *m,unsigned long long mlen,
const unsigned char *ad,unsigned long long adlen,
const unsigned char *nsec,
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; 
	unsigned long long h, blk_ctr=0, blk_ctr1=0 ;
	u8 zeroes[16], ozs[16], blen = 16, Is_final = 0, Is_complete =1;
	for(i=0; i<16; i++)	{zeroes[i]=0x00;}   	
	for(i=1; i<16; i++)	{ozs[i]=0x00;} ozs[0] = 0x80; 	

	h = (mlen+8-1) / (IT_GAP * 16);
	
	if(h > IT_MAX)
		h = IT_MAX;

	*clen = mlen + 24 + h*16 ;  

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


	/* ================  Process Message Blocks ==================== */

	
	/* ====== Process the first Message block, whose first 8 byte is the secret message number ===== */
	
	if(mlen < 8){ Is_complete = 0; }
	if(mlen <= 8) { blen = 8 + mlen; }
	load_block(blk, nsec, m, 8, blen-8); copy_block(CS, blk); 
	process_block(Delta_1, Delta_2, result, blk, W, Is_complete, ENCRYPT, Is_final, MESSAGE);
	
	store_bytes(c, result, 0, 15); c +=16; blk_ctr++; 
	if(mlen >= 8)  {mlen -= 8; m +=8;}
	else mlen = 0;

	/* ============= Process Message blocks ================== */
	while(mlen > 0){
		if(mlen >= 16){
			load_block(blk, m, ozs, 16, 0); 
			if(mlen == 16){xor_block(blk, CS, blk); }
			else xor_block(CS, CS, blk);
			//xor_block(CS, CS, blk); 
			blen = 16; mlen -= 16; m+=16; 
		}
		else 	{Is_complete = 0; blen = mlen; mlen = 0; 
			load_block(blk, m, ozs, blen, 0); xor_block(blk, CS, blk);
			
		}	
		process_block(Delta_1, Delta_2, result, blk, W, Is_complete, ENCRYPT, Is_final, MESSAGE); 
		store_bytes(c, result, 0, 15); c +=16; blk_ctr++;
		
		if(blk_ctr == IT_GAP && blk_ctr1 < IT_MAX && mlen>0) {
			AES(10, DECRYPT, result, W, &aes_key2); 	
			mask(Delta_2, result, result, 1); 
			store_bytes(c, result, 0, 15); c +=16; blk_ctr =0; blk_ctr1++;
		}
	}

	/* ================ Process checksum block ====================== */
	process_block(Delta_1, Delta_2, result, blk, W, 1, ENCRYPT, 1, MESSAGE); 
	store_bytes(c, result, 0, blen-1);

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %r9, %r15
movq %r8, 0x40(%rsp)
movq %rcx, %r14
movq %rdx, %rbx
movq %rdi, 0x18(%rsp)
movq 0x110(%rsp), %r13
movabsq $0x80817f000a, %rax     # imm = 0x80817F000A
leaq 0x88(%rsp), %rcx
movq %rax, (%rcx)
vxorps %xmm0, %xmm0, %xmm0
leaq 0x90(%rsp), %r12
vmovaps %xmm0, (%r12)
leaq 0x7(%r14), %rdx
shrq $0x4, %rdx
movabsq $0x1020408102040811, %rax # imm = 0x1020408102040811
mulxq %rax, %rax, %rax
shrq $0x3, %rax
cmpq $0x20, %rax
movl $0x20, %ecx
cmovbq %rax, %rcx
shll $0x4, %ecx
leaq (%r14,%rcx), %rax
addq $0x18, %rax
movq %rax, (%rsi)
movq 0x5cf2(%rip), %rbp      # 0x7fd0
movq %r13, %rdi
movl $0x80, %esi
movq %rbp, %rdx
callq 0x1090
movq 0x5ceb(%rip), %rdx      # 0x7fe0
movq %r13, %rdi
movl $0x80, %esi
callq 0x1040
leaq 0xb0(%rsp), %r13
movq %r12, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x1060
leaq 0xa0(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rsi
callq 0x2629
movq %rbp, %rdi
movq %rbp, %rsi
callq 0x268e
leaq 0x70(%rsp), %rdi
movq %r13, %rsi
callq 0x268e
leaq 0x50(%rsp), %r12
movq %r12, %rdi
movq %r13, %rsi
callq 0x2629
movq %r12, %rdi
movq %r12, %rsi
callq 0x2629
movq %r12, %rdi
movq %r12, %rsi
callq 0x268e
leaq 0x60(%rsp), %rdi
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, (%rdi)
movq %rbp, %rsi
movq 0x108(%rsp), %rdx
leaq 0x88(%rsp), %rcx
movq 0x40(%rsp), %r8
movq %r15, %r9
callq 0x2708
leal 0x8(%r14), %eax
cmpq $0x9, %r14
movzbl %al, %eax
movl $0x10, %r15d
cmovbl %eax, %r15d
leal -0x8(%r15), %eax
movzbl %al, %r8d
movq %rsp, %r13
movq %r13, %rdi
movq 0x100(%rsp), %rsi
movq %rbx, %rdx
movl $0x8, %ecx
callq 0x281e
vmovaps (%r13), %xmm0
vmovaps %xmm0, 0x40(%rsp)
xorl %r12d, %r12d
cmpq $0x8, %r14
setae %r12b
subq $0x8, %rsp
movl $0x1, %eax
leaq 0x28(%rsp), %rdx
leaq 0x78(%rsp), %rdi
leaq 0x58(%rsp), %rsi
movq %r13, %rcx
leaq 0x68(%rsp), %r8
movl %r12d, %r9d
pushq %rax
pushq $0x0
pushq %rax
callq 0x2887
addq $0x20, %rsp
movq 0x18(%rsp), %rax
leaq 0x10(%rax), %rbp
leaq 0x20(%rsp), %rcx
vmovaps (%rcx), %xmm0
vmovups %xmm0, (%rax)
cmpq $0x9, %r14
jae 0x243e
movq %r15, 0x38(%rsp)
movq %rbp, %r12
jmp 0x25cb
movq %r12, %r15
addq $-0x8, %r14
addq $0x8, %rbx
movl $0x1, %r13d
movq $0x0, 0x18(%rsp)
cmpq $0x10, %r14
jb 0x24a9
xorl %eax, %eax
movb (%rbx,%rax), %cl
movb %cl, (%rsp,%rax)
incq %rax
cmpq $0x10, %rax
jne 0x2460
vmovaps (%rsp), %xmm1
vxorps 0x40(%rsp), %xmm1, %xmm0
vmovaps %xmm0, %xmm2
cmpq $0x10, %r14
movq %r15, %rcx
je 0x248b
vmovaps %xmm1, %xmm2
vmovaps %xmm2, (%rsp)
je 0x2498
vmovaps %xmm0, 0x40(%rsp)
addq $-0x10, %r14
addq $0x10, %rbx
movb $0x10, %al
movq %rax, 0x38(%rsp)
jmp 0x2501
movl %r14d, %eax
movq %rax, 0x38(%rsp)
xorl %eax, %eax
movb (%rbx,%rax), %cl
movb %cl, (%rsp,%rax)
incq %rax
cmpq %rax, %r14
jne 0x24b3
movb $-0x80, (%rsp,%r14)
cmpq $0xf, %r14
je 0x24ec
leaq (%rsp,%r14), %rdi
incq %rdi
movl $0xe, %edx
subl %r14d, %edx
movl $0x0, %eax
cmovbl %eax, %edx
incl %edx
xorl %esi, %esi
callq 0x1030
vmovaps 0x40(%rsp), %xmm0
vxorps (%rsp), %xmm0, %xmm0
vmovaps %xmm0, (%rsp)
xorl %r14d, %r14d
xorl %ecx, %ecx
subq $0x8, %rsp
movq %rcx, %r15
movzbl %cl, %r9d
leaq 0x78(%rsp), %rdi
leaq 0x58(%rsp), %rsi
leaq 0x28(%rsp), %rdx
leaq 0x8(%rsp), %rcx
leaq 0x68(%rsp), %r8
movl $0x1, %eax
pushq %rax
pushq $0x0
pushq %rax
callq 0x2887
addq $0x20, %rsp
xorl %eax, %eax
movb 0x20(%rsp,%rax), %cl
movb %cl, (%rbp,%rax)
incq %rax
cmpq $0x10, %rax
jne 0x2539
leaq 0x10(%rbp), %r12
incq %r13
cmpq $0x7f, %r13
setne %al
cmpq $0x20, 0x18(%rsp)
setae %cl
testq %r14, %r14
sete %dl
orb %cl, %dl
orb %al, %dl
jne 0x25bf
leaq 0x60(%rsp), %rdi
leaq 0x20(%rsp), %r13
movq %r13, %rsi
movq 0x5a5f(%rip), %rdx      # 0x7fe0
callq 0x1050
leaq 0x50(%rsp), %rdi
movq %r13, %rsi
movq %r13, %rdx
movl $0x1, %ecx
callq 0x2a4f
xorl %eax, %eax
movb 0x20(%rsp,%rax), %cl
movb %cl, (%r12,%rax)
incq %rax
cmpq $0x10, %rax
jne 0x259d
addq $0x20, %rbp
incq 0x18(%rsp)
xorl %r13d, %r13d
movq %rbp, %r12
jmp 0x25c2
movq %r12, %rbp
testq %r14, %r14
jne 0x2458
subq $0x8, %rsp
leaq 0x78(%rsp), %rdi
leaq 0x58(%rsp), %rsi
leaq 0x28(%rsp), %rdx
leaq 0x8(%rsp), %rcx
leaq 0x68(%rsp), %r8
movl $0x1, %r9d
movl $0x1, %eax
pushq %rax
pushq %rax
pushq %rax
callq 0x2887
addq $0x20, %rsp
movzbl 0x38(%rsp), %eax
xorl %ecx, %ecx
movb 0x20(%rsp,%rcx), %dl
movb %dl, (%r12,%rcx)
incq %rcx
cmpl %ecx, %eax
jne 0x2606
xorl %eax, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq