// File: /workspace/github_c_makefile_star2_O3/mjosaarinen[P]brutus/crypto_aead/elmd600v2/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[]={0x06,0,0,0,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 cnt;
	for(i=0; i<16; i++)	{zeroes[i]=0x00;}   	
	for(i=1; i<16; i++)	{ozs[i]=0x00;} ozs[0] = 0x80; 	


	cnt = (8+mlen-1)/16 + 1;
	*clen = (cnt +1)* 16 + 1;

	if((mlen+8)%16 == 0) 
		c[*clen-1] = 0x00; 
	else 
		c[*clen-1] = 0x01; 

	key_schedule(k);

	/* ==========  Generate the Masks =========== */
	AES(6, ENCRYPT, blk, 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 Successive 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;
	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, blk, CS); }
			else 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;
		
	}

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %r9, 0x20(%rsp)
movq %r8, 0x18(%rsp)
movq %rcx, %r15
movq %rdx, %r14
movq %rdi, %rbx
movq 0x100(%rsp), %r12
movabsq $0x8000000006, %rax     # imm = 0x8000000006
leaq 0x78(%rsp), %rcx
movq %rax, (%rcx)
vxorps %xmm0, %xmm0, %xmm0
leaq 0x80(%rsp), %rax
vmovaps %xmm0, (%rax)
leaq 0x8(%r15), %rbp
leaq 0x7(%r15), %rax
andq $-0x10, %rax
leaq 0x21(%rax), %rcx
movq %rcx, (%rsi)
testb $0xf, %bpl
setne 0x20(%rdi,%rax)
movq 0x5cdb(%rip), %r13      # 0x7fd0
movq %r12, %rdi
movl $0x80, %esi
movq %r13, %rdx
callq 0x10a0
movq 0x5cd4(%rip), %rdx      # 0x7fe0
movq %r12, %rdi
movl $0x80, %esi
callq 0x1040
movq %rsp, %r12
leaq 0x80(%rsp), %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x1060
leaq 0xa0(%rsp), %r13
movq %r12, %rdi
movq %r13, %rsi
movq 0x5c8c(%rip), %rdx      # 0x7fd0
callq 0x1060
leaq 0x90(%rsp), %r12
movq %r12, %rdi
movq %r13, %rsi
callq 0x25b1
movq %r12, %rdi
movq %r12, %rsi
callq 0x261f
leaq 0x50(%rsp), %rdi
movq %r13, %rsi
callq 0x261f
leaq 0x40(%rsp), %r12
movq %r12, %rdi
movq %r13, %rsi
callq 0x25b1
movq %r12, %rdi
movq %r12, %rsi
callq 0x25b1
movq %r12, %rdi
movq %r12, %rsi
callq 0x261f
leaq 0x60(%rsp), %rdi
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, (%rdi)
leaq 0x90(%rsp), %rsi
movq 0xf8(%rsp), %rdx
leaq 0x78(%rsp), %rcx
movq 0x18(%rsp), %r8
movq 0x20(%rsp), %r9
callq 0x2699
xorl %r12d, %r12d
cmpq $0x8, %r15
setae %r12b
addb $-0x8, %bpl
cmpq $0x9, %r15
movzbl %bpl, %eax
movl $0x8, %ecx
cmovael %ecx, %eax
movzbl %al, %r8d
movq %rsp, %r13
movq %r13, %rdi
movq 0xf0(%rsp), %rsi
movq %r14, %rdx
callq 0x27df
vmovaps (%r13), %xmm0
movq %r13, %rcx
vmovaps %xmm0, 0x20(%rsp)
subq $0x8, %rsp
movl $0x1, %eax
leaq 0x38(%rsp), %r13
leaq 0x58(%rsp), %rdi
leaq 0x48(%rsp), %rsi
movq %r13, %rdx
leaq 0x68(%rsp), %r8
movl %r12d, %r9d
pushq %rax
pushq $0x0
pushq %rax
callq 0x283f
addq $0x20, %rsp
vmovaps (%r13), %xmm0
vmovups %xmm0, (%rbx)
cmpq $0x9, %r15
jb 0x2556
addq $-0x8, %r15
addq $0x8, %r14
leaq 0x10(%rbx), %rax
movq %rax, 0x18(%rsp)
xorl %r13d, %r13d
xorl %eax, %eax
cmpq $0xf, %r15
jbe 0x249b
movb (%r14,%rax), %cl
movb %cl, (%rsp,%rax)
incq %rax
cmpq $0x10, %rax
jne 0x2473
cmpq $0x10, %r15
jne 0x24e3
vmovaps 0x20(%rsp), %xmm0
vxorps (%rsp), %xmm0, %xmm0
vmovaps %xmm0, (%rsp)
jmp 0x24f4
movb (%r14,%rax), %cl
movb %cl, (%rsp,%rax)
incq %rax
cmpq %rax, %r15
jne 0x249b
movb $-0x80, (%rsp,%r15)
cmpq $0xf, %r15
je 0x24cb
leaq (%rsp,%r15), %rdi
incq %rdi
movl $0xf, %edx
subq %r15, %rdx
xorl %esi, %esi
callq 0x1030
vmovaps 0x20(%rsp), %xmm0
vxorps (%rsp), %xmm0, %xmm0
vmovaps %xmm0, (%rsp)
xorl %r15d, %r15d
xorl %r12d, %r12d
jmp 0x24fc
vmovaps 0x20(%rsp), %xmm0
vxorps (%rsp), %xmm0, %xmm0
vmovaps %xmm0, 0x20(%rsp)
addq $-0x10, %r15
addq $0x10, %r14
movq %r13, %rbp
shlq $0x4, %rbp
addq 0x18(%rsp), %rbp
addq $0x10, %rbx
subq $0x8, %rsp
movzbl %r12b, %r9d
leaq 0x58(%rsp), %rdi
leaq 0x48(%rsp), %rsi
leaq 0x38(%rsp), %rdx
leaq 0x8(%rsp), %rcx
leaq 0x68(%rsp), %r8
movl $0x1, %eax
pushq %rax
pushq $0x0
pushq %rax
callq 0x283f
addq $0x20, %rsp
vmovaps 0x30(%rsp), %xmm0
vmovups %xmm0, (%rbp)
incq %r13
testq %r15, %r15
jne 0x246b
subq $0x8, %rsp
leaq 0x58(%rsp), %rdi
leaq 0x48(%rsp), %rsi
leaq 0x38(%rsp), %rdx
leaq 0x8(%rsp), %rcx
leaq 0x68(%rsp), %r8
movl $0x1, %r9d
movl $0x1, %eax
pushq %rax
pushq %rax
pushq %rax
callq 0x283f
addq $0x20, %rsp
xorl %eax, %eax
movb 0x30(%rsp,%rax), %cl
movb %cl, 0x10(%rbx,%rax)
incq %rax
cmpq $0x10, %rax
jne 0x258c
xorl %eax, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq