// File: /workspace/github_c_makefile_star2_O0/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:
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 0x3b2a(%rip), %rax      # 0x7130
movq %rax, 0xe0(%rsp)
movb $0x10, 0x2f(%rsp)
movb $0x0, 0x2e(%rsp)
movb $0x1, 0x2d(%rsp)
movl $0x0, 0x5c(%rsp)
cmpl $0x10, 0x5c(%rsp)
jge 0x3643
movslq 0x5c(%rsp), %rax
movb $0x0, 0x40(%rsp,%rax)
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x3625
movl $0x1, 0x5c(%rsp)
cmpl $0x10, 0x5c(%rsp)
jge 0x3669
movslq 0x5c(%rsp), %rax
movb $0x0, 0x30(%rsp,%rax)
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x364b
movb $-0x80, 0x30(%rsp)
movq 0xf8(%rsp), %rax
addq $0x8, %rax
subq $0x1, %rax
shrq $0x4, %rax
addq $0x1, %rax
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rcx
addq $0x1, %rcx
shlq $0x4, %rcx
addq $0x1, %rcx
movq 0x108(%rsp), %rax
movq %rcx, (%rax)
movq 0xf8(%rsp), %rax
addq $0x8, %rax
andq $0xf, %rax
cmpq $0x0, %rax
jne 0x36da
movq 0x110(%rsp), %rax
movq 0x108(%rsp), %rcx
movq (%rcx), %rcx
subq $0x1, %rcx
movb $0x0, (%rax,%rcx)
jmp 0x36f5
movq 0x110(%rsp), %rax
movq 0x108(%rsp), %rcx
movq (%rcx), %rcx
subq $0x1, %rcx
movb $0x1, (%rax,%rcx)
movq 0x130(%rsp), %rdi
callq 0x3b40
leaq 0x80(%rsp), %rdx
leaq 0x40(%rsp), %rcx
movl $0x6, %edi
movl $0x1, %esi
movq 0x58b0(%rip), %r8       # 0x8fd0
callq 0x3b80
leaq 0xd0(%rsp), %rdx
leaq 0x80(%rsp), %rcx
movl $0x6, %edi
movl $0x1, %esi
movq 0x588a(%rip), %r8       # 0x8fd0
callq 0x3b80
leaq 0xb0(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x3c20
leaq 0xb0(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x3c60
leaq 0xa0(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x3c60
leaq 0x90(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x3c20
leaq 0x90(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x3c20
leaq 0x90(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x3c60
movl $0x0, 0x5c(%rsp)
cmpl $0x10, 0x5c(%rsp)
jge 0x37f2
movslq 0x5c(%rsp), %rax
movb $0x0, 0xc0(%rsp,%rax)
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x37d1
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 0x3d20
cmpq $0x8, 0xf8(%rsp)
jae 0x3837
movb $0x0, 0x2d(%rsp)
cmpq $0x8, 0xf8(%rsp)
ja 0x3852
movq 0xf8(%rsp), %rax
addq $0x8, %rax
movb %al, 0x2f(%rsp)
leaq 0x80(%rsp), %rdi
movq 0x120(%rsp), %rsi
movq 0x100(%rsp), %rdx
movzbl 0x2f(%rsp), %eax
subl $0x8, %eax
movl $0x8, %ecx
movzbl %al, %r8d
callq 0x3ea0
leaq 0x60(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x3fa0
leaq 0xa0(%rsp), %rdi
leaq 0x90(%rsp), %rsi
leaq 0x70(%rsp), %rdx
leaq 0x80(%rsp), %rcx
leaq 0xc0(%rsp), %r8
movb 0x2d(%rsp), %al
movzbl %al, %r9d
movl $0x1, (%rsp)
movzbl 0x2e(%rsp), %eax
movl %eax, 0x8(%rsp)
movl $0x1, 0x10(%rsp)
callq 0x3ff0
movq 0x110(%rsp), %rdi
leaq 0x70(%rsp), %rsi
xorl %edx, %edx
movl $0xf, %ecx
callq 0x41e0
movq 0x110(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x110(%rsp)
cmpq $0x8, 0xf8(%rsp)
jb 0x393e
movq 0xf8(%rsp), %rax
subq $0x8, %rax
movq %rax, 0xf8(%rsp)
movq 0x100(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x100(%rsp)
jmp 0x394a
movq $0x0, 0xf8(%rsp)
jmp 0x394c
cmpq $0x0, 0xf8(%rsp)
jbe 0x3ace
cmpq $0x10, 0xf8(%rsp)
jb 0x39f9
leaq 0x80(%rsp), %rdi
movq 0x100(%rsp), %rsi
leaq 0x30(%rsp), %rdx
movl $0x10, %ecx
xorl %r8d, %r8d
callq 0x3ea0
cmpq $0x10, 0xf8(%rsp)
jne 0x39b3
leaq 0x80(%rsp), %rdi
leaq 0x80(%rsp), %rsi
leaq 0x60(%rsp), %rdx
callq 0x4240
jmp 0x39ca
leaq 0x60(%rsp), %rdi
leaq 0x60(%rsp), %rsi
leaq 0x80(%rsp), %rdx
callq 0x4240
movb $0x10, 0x2f(%rsp)
movq 0xf8(%rsp), %rax
subq $0x10, %rax
movq %rax, 0xf8(%rsp)
movq 0x100(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x100(%rsp)
jmp 0x3a52
movb $0x0, 0x2d(%rsp)
movq 0xf8(%rsp), %rax
movb %al, 0x2f(%rsp)
movq $0x0, 0xf8(%rsp)
leaq 0x80(%rsp), %rdi
movq 0x100(%rsp), %rsi
leaq 0x30(%rsp), %rdx
xorl %r8d, %r8d
movzbl 0x2f(%rsp), %ecx
callq 0x3ea0
leaq 0x80(%rsp), %rdi
leaq 0x60(%rsp), %rsi
leaq 0x80(%rsp), %rdx
callq 0x4240
leaq 0xa0(%rsp), %rdi
leaq 0x90(%rsp), %rsi
leaq 0x70(%rsp), %rdx
leaq 0x80(%rsp), %rcx
leaq 0xc0(%rsp), %r8
movb 0x2d(%rsp), %al
movzbl %al, %r9d
movl $0x1, (%rsp)
movzbl 0x2e(%rsp), %eax
movl %eax, 0x8(%rsp)
movl $0x1, 0x10(%rsp)
callq 0x3ff0
movq 0x110(%rsp), %rdi
leaq 0x70(%rsp), %rsi
xorl %edx, %edx
movl $0xf, %ecx
callq 0x41e0
movq 0x110(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x110(%rsp)
jmp 0x394c
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 0x3ff0
movq 0x110(%rsp), %rdi
leaq 0x70(%rsp), %rsi
xorl %edx, %edx
movl $0xf, %ecx
callq 0x41e0
xorl %eax, %eax
addq $0x118, %rsp            # imm = 0x118
retq
nopl (%rax,%rax)