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

	*clen = mlen + 24; 

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


	/* ================  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, CS, blk); }
			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, blen-1);

	return 0;
}

// Assembly:
subq $0x108, %rsp            # imm = 0x108
movq 0x120(%rsp), %rax
movq 0x118(%rsp), %rax
movq 0x110(%rsp), %rax
movq %rdi, 0x100(%rsp)
movq %rsi, 0xf8(%rsp)
movq %rdx, 0xf0(%rsp)
movq %rcx, 0xe8(%rsp)
movq %r8, 0xe0(%rsp)
movq %r9, 0xd8(%rsp)
movq 0x3b3a(%rip), %rax      # 0x7130
movq %rax, 0xd0(%rsp)
movb $0x10, 0x1f(%rsp)
movb $0x0, 0x1e(%rsp)
movb $0x1, 0x1d(%rsp)
movl $0x0, 0x4c(%rsp)
cmpl $0x10, 0x4c(%rsp)
jge 0x3633
movslq 0x4c(%rsp), %rax
movb $0x0, 0x30(%rsp,%rax)
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0x3615
movl $0x1, 0x4c(%rsp)
cmpl $0x10, 0x4c(%rsp)
jge 0x3659
movslq 0x4c(%rsp), %rax
movb $0x0, 0x20(%rsp,%rax)
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0x363b
movb $-0x80, 0x20(%rsp)
movq 0xe8(%rsp), %rcx
addq $0x18, %rcx
movq 0xf8(%rsp), %rax
movq %rcx, (%rax)
movq 0x120(%rsp), %rdi
callq 0x3a80
leaq 0xc0(%rsp), %rdx
leaq 0x30(%rsp), %rcx
movl $0xa, %edi
movl $0x1, %esi
movq 0x5930(%rip), %r8       # 0x8fd0
callq 0x3ac0
leaq 0xa0(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x3b60
leaq 0xa0(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x3ba0
leaq 0x90(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x3ba0
leaq 0x80(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x3b60
leaq 0x80(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x3b60
leaq 0x80(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x3ba0
movl $0x0, 0x4c(%rsp)
cmpl $0x10, 0x4c(%rsp)
jge 0x374c
movslq 0x4c(%rsp), %rax
movb $0x0, 0xb0(%rsp,%rax)
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0x372b
leaq 0xb0(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
movq 0x118(%rsp), %rdx
leaq 0xd0(%rsp), %rcx
movq 0xe0(%rsp), %r8
movq 0xd8(%rsp), %r9
callq 0x3c60
cmpq $0x8, 0xe8(%rsp)
jae 0x3791
movb $0x0, 0x1d(%rsp)
cmpq $0x8, 0xe8(%rsp)
ja 0x37ac
movq 0xe8(%rsp), %rax
addq $0x8, %rax
movb %al, 0x1f(%rsp)
leaq 0x70(%rsp), %rdi
movq 0x110(%rsp), %rsi
movq 0xf0(%rsp), %rdx
movzbl 0x1f(%rsp), %eax
subl $0x8, %eax
movl $0x8, %ecx
movzbl %al, %r8d
callq 0x3de0
leaq 0x50(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x3ee0
leaq 0x90(%rsp), %rdi
leaq 0x80(%rsp), %rsi
leaq 0x60(%rsp), %rdx
leaq 0x70(%rsp), %rcx
leaq 0xb0(%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 0x3f30
movq 0x100(%rsp), %rdi
leaq 0x60(%rsp), %rsi
xorl %edx, %edx
movl $0xf, %ecx
callq 0x4120
movq 0x100(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x100(%rsp)
cmpq $0x8, 0xe8(%rsp)
jb 0x388f
movq 0xe8(%rsp), %rax
subq $0x8, %rax
movq %rax, 0xe8(%rsp)
movq 0xf0(%rsp), %rax
addq $0x8, %rax
movq %rax, 0xf0(%rsp)
jmp 0x389b
movq $0x0, 0xe8(%rsp)
jmp 0x389d
cmpq $0x0, 0xe8(%rsp)
jbe 0x3a07
cmpq $0x10, 0xe8(%rsp)
jb 0x393e
leaq 0x70(%rsp), %rdi
movq 0xf0(%rsp), %rsi
leaq 0x20(%rsp), %rdx
movl $0x10, %ecx
xorl %r8d, %r8d
callq 0x3de0
cmpq $0x10, 0xe8(%rsp)
jne 0x38fb
leaq 0x70(%rsp), %rdi
leaq 0x50(%rsp), %rsi
leaq 0x70(%rsp), %rdx
callq 0x4180
jmp 0x390f
leaq 0x50(%rsp), %rdi
leaq 0x50(%rsp), %rsi
leaq 0x70(%rsp), %rdx
callq 0x4180
movb $0x10, 0x1f(%rsp)
movq 0xe8(%rsp), %rax
subq $0x10, %rax
movq %rax, 0xe8(%rsp)
movq 0xf0(%rsp), %rax
addq $0x10, %rax
movq %rax, 0xf0(%rsp)
jmp 0x398e
movb $0x0, 0x1d(%rsp)
movq 0xe8(%rsp), %rax
movb %al, 0x1f(%rsp)
movq $0x0, 0xe8(%rsp)
leaq 0x70(%rsp), %rdi
movq 0xf0(%rsp), %rsi
leaq 0x20(%rsp), %rdx
xorl %r8d, %r8d
movzbl 0x1f(%rsp), %ecx
callq 0x3de0
leaq 0x70(%rsp), %rdi
leaq 0x50(%rsp), %rsi
leaq 0x70(%rsp), %rdx
callq 0x4180
leaq 0x90(%rsp), %rdi
leaq 0x80(%rsp), %rsi
leaq 0x60(%rsp), %rdx
leaq 0x70(%rsp), %rcx
leaq 0xb0(%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 0x3f30
movq 0x100(%rsp), %rdi
leaq 0x60(%rsp), %rsi
xorl %edx, %edx
movl $0xf, %ecx
callq 0x4120
movq 0x100(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x100(%rsp)
jmp 0x389d
leaq 0x90(%rsp), %rdi
leaq 0x80(%rsp), %rsi
leaq 0x60(%rsp), %rdx
leaq 0x70(%rsp), %rcx
leaq 0xb0(%rsp), %r8
movl $0x1, %r9d
movl $0x1, (%rsp)
movl $0x1, 0x8(%rsp)
movl $0x1, 0x10(%rsp)
callq 0x3f30
movq 0x100(%rsp), %rdi
leaq 0x60(%rsp), %rsi
movzbl 0x1f(%rsp), %ecx
subl $0x1, %ecx
xorl %edx, %edx
callq 0x4120
xorl %eax, %eax
addq $0x108, %rsp            # imm = 0x108
retq
nopw %cs:(%rax,%rax)