// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x118, %rsp            # imm = 0x118
movq 0x130(%rsp), %rax
movq 0x128(%rsp), %rax
movq 0x120(%rsp), %rax
movq %rdi, 0x110(%rsp)
movq %rsi, 0x108(%rsp)
movq %rdx, 0x100(%rsp)
movq %rcx, 0xf8(%rsp)
movq %r8, 0xf0(%rsp)
movq %r9, 0xe8(%rsp)
movq 0x3b3a(%rip), %rax      # 0x7130
movq %rax, 0xe0(%rsp)
movq $0x0, 0x48(%rsp)
movq $0x0, 0x40(%rsp)
movb $0x10, 0x1f(%rsp)
movb $0x0, 0x1e(%rsp)
movb $0x1, 0x1d(%rsp)
movl $0x0, 0x5c(%rsp)
cmpl $0x10, 0x5c(%rsp)
jge 0x3645
movslq 0x5c(%rsp), %rax
movb $0x0, 0x30(%rsp,%rax)
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x3627
movl $0x1, 0x5c(%rsp)
cmpl $0x10, 0x5c(%rsp)
jge 0x366b
movslq 0x5c(%rsp), %rax
movb $0x0, 0x20(%rsp,%rax)
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x364d
movb $-0x80, 0x20(%rsp)
movq 0xf8(%rsp), %rax
addq $0x8, %rax
subq $0x1, %rax
movl $0x7f0, %ecx            # imm = 0x7F0
xorl %edx, %edx
divq %rcx
movq %rax, 0x50(%rsp)
cmpq $0x20, 0x50(%rsp)
jbe 0x36a0
movq $0x20, 0x50(%rsp)
movq 0xf8(%rsp), %rcx
addq $0x18, %rcx
movq 0x50(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rcx
movq 0x108(%rsp), %rax
movq %rcx, (%rax)
movq 0x130(%rsp), %rdi
callq 0x3bb0
leaq 0xd0(%rsp), %rdx
leaq 0x30(%rsp), %rcx
movl $0xa, %edi
movl $0x1, %esi
movq 0x58e2(%rip), %r8       # 0x8fd0
callq 0x3bf0
leaq 0xb0(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x3c90
leaq 0xb0(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x3cd0
leaq 0xa0(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x3cd0
leaq 0x90(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x3c90
leaq 0x90(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x3c90
leaq 0x90(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x3cd0
movl $0x0, 0x5c(%rsp)
cmpl $0x10, 0x5c(%rsp)
jge 0x379a
movslq 0x5c(%rsp), %rax
movb $0x0, 0xc0(%rsp,%rax)
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x3779
leaq 0xc0(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
movq 0x128(%rsp), %rdx
leaq 0xe0(%rsp), %rcx
movq 0xf0(%rsp), %r8
movq 0xe8(%rsp), %r9
callq 0x3d90
cmpq $0x8, 0xf8(%rsp)
jae 0x37df
movb $0x0, 0x1d(%rsp)
cmpq $0x8, 0xf8(%rsp)
ja 0x37fa
movq 0xf8(%rsp), %rax
addq $0x8, %rax
movb %al, 0x1f(%rsp)
leaq 0x80(%rsp), %rdi
movq 0x120(%rsp), %rsi
movq 0x100(%rsp), %rdx
movzbl 0x1f(%rsp), %eax
subl $0x8, %eax
movl $0x8, %ecx
movzbl %al, %r8d
callq 0x3f10
leaq 0x60(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x4010
leaq 0xa0(%rsp), %rdi
leaq 0x90(%rsp), %rsi
leaq 0x70(%rsp), %rdx
leaq 0x80(%rsp), %rcx
leaq 0xc0(%rsp), %r8
movb 0x1d(%rsp), %al
movzbl %al, %r9d
movl $0x1, (%rsp)
movzbl 0x1e(%rsp), %eax
movl %eax, 0x8(%rsp)
movl $0x1, 0x10(%rsp)
callq 0x4060
movq 0x110(%rsp), %rdi
leaq 0x70(%rsp), %rsi
xorl %edx, %edx
movl $0xf, %ecx
callq 0x4250
movq 0x110(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x110(%rsp)
movq 0x48(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x48(%rsp)
cmpq $0x8, 0xf8(%rsp)
jb 0x38f4
movq 0xf8(%rsp), %rax
subq $0x8, %rax
movq %rax, 0xf8(%rsp)
movq 0x100(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x100(%rsp)
jmp 0x3900
movq $0x0, 0xf8(%rsp)
jmp 0x3902
cmpq $0x0, 0xf8(%rsp)
jbe 0x3b39
cmpq $0x10, 0xf8(%rsp)
jb 0x39af
leaq 0x80(%rsp), %rdi
movq 0x100(%rsp), %rsi
leaq 0x20(%rsp), %rdx
movl $0x10, %ecx
xorl %r8d, %r8d
callq 0x3f10
cmpq $0x10, 0xf8(%rsp)
jne 0x3969
leaq 0x80(%rsp), %rdi
leaq 0x60(%rsp), %rsi
leaq 0x80(%rsp), %rdx
callq 0x42b0
jmp 0x3980
leaq 0x60(%rsp), %rdi
leaq 0x60(%rsp), %rsi
leaq 0x80(%rsp), %rdx
callq 0x42b0
movb $0x10, 0x1f(%rsp)
movq 0xf8(%rsp), %rax
subq $0x10, %rax
movq %rax, 0xf8(%rsp)
movq 0x100(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x100(%rsp)
jmp 0x3a08
movb $0x0, 0x1d(%rsp)
movq 0xf8(%rsp), %rax
movb %al, 0x1f(%rsp)
movq $0x0, 0xf8(%rsp)
leaq 0x80(%rsp), %rdi
movq 0x100(%rsp), %rsi
leaq 0x20(%rsp), %rdx
xorl %r8d, %r8d
movzbl 0x1f(%rsp), %ecx
callq 0x3f10
leaq 0x80(%rsp), %rdi
leaq 0x60(%rsp), %rsi
leaq 0x80(%rsp), %rdx
callq 0x42b0
leaq 0xa0(%rsp), %rdi
leaq 0x90(%rsp), %rsi
leaq 0x70(%rsp), %rdx
leaq 0x80(%rsp), %rcx
leaq 0xc0(%rsp), %r8
movb 0x1d(%rsp), %al
movzbl %al, %r9d
movl $0x1, (%rsp)
movzbl 0x1e(%rsp), %eax
movl %eax, 0x8(%rsp)
movl $0x1, 0x10(%rsp)
callq 0x4060
movq 0x110(%rsp), %rdi
leaq 0x70(%rsp), %rsi
xorl %edx, %edx
movl $0xf, %ecx
callq 0x4250
movq 0x110(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x110(%rsp)
movq 0x48(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x48(%rsp)
cmpq $0x7f, 0x48(%rsp)
jne 0x3b34
cmpq $0x20, 0x40(%rsp)
jae 0x3b34
cmpq $0x0, 0xf8(%rsp)
jbe 0x3b34
leaq 0x70(%rsp), %rdx
leaq 0xc0(%rsp), %rcx
movl $0xa, %edi
xorl %esi, %esi
movq 0x5511(%rip), %r8       # 0x8fe0
callq 0x3bf0
leaq 0x90(%rsp), %rdi
leaq 0x70(%rsp), %rsi
leaq 0x70(%rsp), %rdx
movl $0x1, %ecx
callq 0x4310
movq 0x110(%rsp), %rdi
leaq 0x70(%rsp), %rsi
xorl %edx, %edx
movl $0xf, %ecx
callq 0x4250
movq 0x110(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x110(%rsp)
movq $0x0, 0x48(%rsp)
movq 0x40(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x40(%rsp)
jmp 0x3902
leaq 0xa0(%rsp), %rdi
leaq 0x90(%rsp), %rsi
leaq 0x70(%rsp), %rdx
leaq 0x80(%rsp), %rcx
leaq 0xc0(%rsp), %r8
movl $0x1, %r9d
movl $0x1, (%rsp)
movl $0x1, 0x8(%rsp)
movl $0x1, 0x10(%rsp)
callq 0x4060
movq 0x110(%rsp), %rdi
leaq 0x70(%rsp), %rsi
movzbl 0x1f(%rsp), %ecx
subl $0x1, %ecx
xorl %edx, %edx
callq 0x4250
xorl %eax, %eax
addq $0x118, %rsp            # imm = 0x118
retq
nopw %cs:(%rax,%rax)