// File: /workspace/github_c_makefile_star2_O2/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, (%rsp)
movq %r8, 0x8(%rsp)
movq %rcx, %rbx
movq %rdx, %r12
movq %rdi, %rbp
movq 0x110(%rsp), %rdi
movabsq $0x80817f000a, %rax     # imm = 0x80817F000A
movq %rax, 0x78(%rsp)
xorl %eax, %eax
cmpq $0x10, %rax
je 0x2269
movb $0x0, 0x90(%rsp,%rax)
incq %rax
jmp 0x2256
pushq $0x1
popq %rax
cmpq $0x10, %rax
je 0x227c
movb $0x0, 0x40(%rsp,%rax)
incq %rax
jmp 0x226c
movb $-0x80, 0x40(%rsp)
leaq 0x7(%rbx), %rax
xorl %r15d, %r15d
movl $0x7f0, %ecx            # imm = 0x7F0
xorl %edx, %edx
divq %rcx
cmpq $0x20, %rax
pushq $0x20
popq %rcx
cmovbq %rax, %rcx
shll $0x4, %ecx
leaq (%rbx,%rcx), %rax
addq $0x18, %rax
movq %rax, (%rsi)
callq 0x25c7
movq 0x5d19(%rip), %rdx      # 0x7fd0
leaq 0x90(%rsp), %rdi
leaq 0xb0(%rsp), %r13
movq %r13, %rsi
callq 0x1050
leaq 0xa0(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x25f1
movq %r14, %rdi
movq %r14, %rsi
callq 0x262e
leaq 0x60(%rsp), %rdi
movq %r13, %rsi
callq 0x262e
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x25f1
movq %r14, %rdi
movq %r14, %rsi
callq 0x25f1
movq %r14, %rdi
movq %r14, %rsi
callq 0x262e
cmpq $0x10, %r15
je 0x2331
movb $0x0, 0x20(%rsp,%r15)
incq %r15
jmp 0x2320
leaq 0x20(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
leaq 0x78(%rsp), %rcx
movq 0x108(%rsp), %rdx
movq 0x8(%rsp), %r8
movq (%rsp), %r9
callq 0x267a
xorl %r13d, %r13d
cmpq $0x8, %rbx
setae %r13b
leal 0x8(%rbx), %eax
cmpq $0x9, %rbx
movzbl %al, %eax
pushq $0x10
popq %rcx
cmovbl %eax, %ecx
movq %rcx, (%rsp)
leal -0x8(%rcx), %eax
movzbl %al, %r8d
leaq 0x50(%rsp), %r14
pushq $0x8
popq %rcx
movq %r14, %rdi
movq 0x100(%rsp), %rsi
movq %r12, %r15
movq %r12, %rdx
callq 0x2765
leaq 0x80(%rsp), %rdi
movq %r14, %rsi
callq 0x27b2
subq $0x8, %rsp
pushq $0x1
popq %rax
leaq 0x68(%rsp), %rdi
leaq 0x38(%rsp), %rsi
leaq 0x18(%rsp), %rdx
movq %r14, %rcx
leaq 0x28(%rsp), %r8
movl %r13d, %r9d
pushq %rax
pushq $0x0
pushq %rax
callq 0x27c6
addq $0x20, %rsp
xorl %eax, %eax
cmpq $0x10, %rax
je 0x23f0
movb 0x10(%rsp,%rax), %cl
movb %cl, (%rbp,%rax)
incq %rax
jmp 0x23dd
addq $0x10, %rbp
leaq -0x8(%rbx), %r12
xorl %ecx, %ecx
xorl %r14d, %r14d
cmpq $0x8, %rbx
setae %r14b
leaq (%r15,%r14,8), %r13
movl $0x0, %eax
movq %rax, 0x8(%rsp)
cmovbq %rcx, %r12
pushq $0x1
popq %r15
movq %rbp, %rbx
testq %r12, %r12
je 0x256d
cmpq $0x10, %r12
jb 0x2473
leaq 0x50(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rsi
leaq 0x40(%rsp), %rdx
pushq $0x10
popq %rcx
xorl %r8d, %r8d
callq 0x2765
cmpq $0x10, %r12
leaq 0x80(%rsp), %rsi
movq %rsi, %rdi
cmoveq %rbp, %rdi
movq %rbp, %rdx
callq 0x294b
addq $-0x10, %r12
addq $0x10, %r13
movb $0x10, %al
movq %rax, (%rsp)
jmp 0x24af
movl %r12d, %eax
movq %rax, (%rsp)
movzbl %r12b, %ecx
xorl %r12d, %r12d
leaq 0x50(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rsi
leaq 0x40(%rsp), %rdx
xorl %r8d, %r8d
callq 0x2765
movq %rbp, %rdi
leaq 0x80(%rsp), %rsi
movq %rbp, %rdx
callq 0x294b
xorl %r14d, %r14d
subq $0x8, %rsp
movzbl %r14b, %r9d
leaq 0x68(%rsp), %rdi
leaq 0x38(%rsp), %rsi
leaq 0x18(%rsp), %rdx
movq %rbp, %rcx
leaq 0x28(%rsp), %r8
pushq $0x1
popq %rax
pushq %rax
pushq $0x0
pushq %rax
callq 0x27c6
addq $0x20, %rsp
xorl %eax, %eax
cmpq $0x10, %rax
je 0x24f2
movb 0x10(%rsp,%rax), %cl
movb %cl, (%rbx,%rax)
incq %rax
jmp 0x24e0
leaq 0x10(%rbx), %rbp
incq %r15
cmpq $0x7f, %r15
jne 0x241b
cmpq $0x1f, 0x8(%rsp)
ja 0x241b
testq %r12, %r12
je 0x241b
leaq 0x20(%rsp), %rdi
leaq 0x10(%rsp), %r15
movq %r15, %rsi
movq 0x5ab4(%rip), %rdx      # 0x7fe0
callq 0x1040
leaq 0x30(%rsp), %rdi
movq %r15, %rsi
movq %r15, %rdx
pushq $0x1
popq %rcx
callq 0x2962
xorl %eax, %eax
cmpq $0x10, %rax
je 0x2559
movb 0x10(%rsp,%rax), %cl
movb %cl, (%rbp,%rax)
incq %rax
jmp 0x2546
addq $0x20, %rbx
incq 0x8(%rsp)
xorl %r15d, %r15d
movq %rbx, %rbp
jmp 0x241b
subq $0x8, %rsp
leaq 0x68(%rsp), %rdi
leaq 0x38(%rsp), %rsi
leaq 0x18(%rsp), %r15
leaq 0x58(%rsp), %rcx
leaq 0x28(%rsp), %r8
movq %r15, %rdx
pushq $0x1
popq %r9
pushq %r9
pushq %r9
pushq %r9
callq 0x27c6
addq $0x20, %rsp
movzbl (%rsp), %ecx
decl %ecx
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x2932
xorl %eax, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq