// File: /workspace/github_c_makefile_star2_O1/mjosaarinen[P]brutus/crypto_aead/primatesv1ape80/ref/encrypt.cpp
// 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
 )
 {
    /*...
    ... generating a ciphertext c[0],c[1],...,c[*clen-1]
    ... from a plaintext m[0],m[1],...,m[mlen-1]
    ... and associated data ad[0],ad[1],...,ad[adlen-1]
    ... and secret message number nsec[0],nsec[1],...
    ... and public message number npub[0],npub[1],...
    ... and secret key k[0],k[1],...
    ...*/
	unsigned char V[StateSize]; //the state represented in Elements
	//Temp variables represented as elements
	unsigned char key[KeySizeElements];
	unsigned char nonce[NonceSizeElements];
	unsigned char tag[TagSizeElements];
	unsigned char data[RateSizeElements];
	unsigned char cipher[RateSizeElements];
   
	Bytes2Element(key,k,0,KeySizeElements); //representing k  as elements
	Bytes2Element(nonce,npub,0,NonceSizeElements); //representing npub as elements

	// V <-- O||K
	InitializeState(V,key);
	// Treating the nonce as the first part of the associated data
	for(int i=0; i<(NonceSizeElements/RateSizeElements); i++)
	{
		// V <-- p_1 (N[i]^V_r || V_c)
		for(int j=0; j<RateSizeElements; j++)
			V[j]^=nonce[RateSizeElements*i+j];
		p_1(V);
	}
	if(adlen!=0)
	{ 
		// for i=1 to u-1 (except the last block which needs padding)
		if(adlen>RateSizeBytes)
		{ 
			for(unsigned long long i=0; i<adlen-RateSizeBytes; i=i+RateSizeBytes)
			{
				Bytes2Element(data,ad,i,RateSizeElements); // representing a block of ad as elements
				// V <-- p_1 (A[i]^V_r || V_c)
				for(int j=0; j<RateSizeElements; j++)
					V[j]^=data[j];
				p_1(V);
			}
		}
		// V <-- A[u]^v_r || V_c
		int l = adlen%RateSizeBytes;
		if(l==0){ //do padding with spill over			
			Bytes2Element(data,ad,adlen-RateSizeBytes,RateSizeElements); // representing last block of ad as elements
			for(int i=0; i<RateSizeElements; i++)
				V[i]^=data[i];
			V[RateSizeElements]^=16; //padding spills over to capacity
		}
		else{ //do standard padding
			unsigned char dataP[RateSizeBytes];
			for(int i=0; i< l; i++)
				dataP[i]=ad[adlen-l+i];
			dataP[l]=128; //padding over bytes
			for(int i=l+1; i<RateSizeBytes; i++)
				dataP[i]=0;
			Bytes2Element(data,dataP,0,RateSizeElements); // representing last padded block of ad as elements
			for(int i=0; i<RateSizeElements; i++)
				V[i]^=data[i];
		}
		// V <-- p_1(V)
		p_1(V);	
	}	
	// V <-- V ^ (0..01)
	V[StateSize-1]^=1;
	//for i=1 to w-1 (except the last block which needs padding)
	if(mlen>RateSizeBytes)
	{
		for(unsigned long long i=0; i<mlen-RateSizeBytes; i=i+RateSizeBytes)
		{ 
			Bytes2Element(data,m,i,RateSizeElements); //representing a block of m as elements
			//V <-- p_1 (M[i]^V_r || V_c)
			for(int j=0; j<RateSizeElements; j++)
				V[j]^=data[j];
			p_1(V);
			// C[i] <-- V_r
			for(int j=0; j<RateSizeElements; j++)
				cipher[j]=V[j];
			Element2Bytes(c,cipher,i,RateSizeBytes); //representing a block of c as bytes
		}
	}
	
	//for i=w
	int l = mlen%RateSizeBytes;
	// //V <-- p_1 (M[i]^V_r || V_c)
	if((l==0) && (mlen!=0)){ //do padding with spill over			
		Bytes2Element(data,m,mlen-RateSizeBytes,RateSizeElements); // representing last block of m as elements
		for(int i=0; i<RateSizeElements; i++)
			V[i]^=data[i];
		V[RateSizeElements]^=16; //padding spills over to capacity
	}
	else{ //do standard padding
		unsigned char dataP[RateSizeBytes];
		for(int i=0; i< l; i++)
			dataP[i]=m[mlen-l+i];
		dataP[l]=128; //padding over bytes
		for(int i=l+1; i<RateSizeBytes; i++)
			dataP[i]=0;
		Bytes2Element(data,dataP,0,RateSizeElements);  // representing last block of m as elements
		for(int i=0; i<RateSizeElements; i++)
			V[i]^=data[i];
	}	
	p_1(V);
	// C[i] <-- V_r
	for(int j=0; j<RateSizeElements; j++)
		cipher[j]=V[j];
	if(mlen<RateSizeBytes)
		Element2Bytes(c,cipher,0,RateSizeBytes); //representing last block of c as bytes
	else
		Element2Bytes(c,cipher,mlen-RateSizeBytes,RateSizeBytes); //representing last block of c as bytes

	// T <-- (V_c)_c/2 ^ K
	for(int i=0; i<TagSizeElements; i++)
		tag[i]=V[RateSizeElements+i]^key[i]; 
	if ((mlen>0)&&(mlen<RateSizeBytes)){
		Element2Bytes(c,tag,RateSizeBytes,TagSizeBytes) ;
		*clen=TagSizeBytes+RateSizeBytes;
	}
	else{
		Element2Bytes(c,tag,mlen,TagSizeBytes) ;
		*clen=mlen+TagSizeBytes;
	}
	
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %r9, %r12
movq %r8, %rbp
movq %rcx, %r13
movq %rdx, 0x70(%rsp)
movq %rsi, 0x78(%rsp)
movq %rdi, 0x40(%rsp)
movq 0xf8(%rsp), %rbx
movq 0x100(%rsp), %rsi
xorl %r15d, %r15d
leaq 0x90(%rsp), %r14
movq %r14, %rdi
xorl %edx, %edx
movl $0x20, %ecx
callq 0x2160
leaq 0x80(%rsp), %rdi
movq %rbx, %rsi
xorl %edx, %edx
movl $0x10, %ecx
callq 0x2160
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x20b0
movq 0x80(%rsp,%r15,8), %rax
xorq %rax, 0x10(%rsp)
movq %rbx, %rdi
callq 0x2070
incq %r15
cmpq $0x1, %r15
je 0x22e6
testq %r12, %r12
movq %r13, 0x8(%rsp)
je 0x2418
cmpq $0x6, %r12
jb 0x2358
movq %r12, %r13
addq $-0x5, %r13
je 0x2358
xorl %ebx, %ebx
movq %rsp, %r14
leaq 0x10(%rsp), %r15
movq %r14, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
movl $0x8, %ecx
callq 0x2160
movq (%rsp), %rax
xorq %rax, 0x10(%rsp)
movq %r15, %rdi
callq 0x2070
addq $0x5, %rbx
cmpq %r13, %rbx
jb 0x232b
movq %r12, %rdx
movabsq $-0x3333333333333333, %rax # imm = 0xCCCCCCCCCCCCCCCD
mulxq %rax, %r14, %r14
shrq $0x2, %r14
leaq (%r14,%r14,4), %rax
movq %r12, %rbx
subq %rax, %rbx
je 0x23e2
movq %r12, %rax
subq %rbx, %rax
addq %rax, %rbp
leaq 0x50(%rsp), %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x2170
movb $-0x80, 0x50(%rsp,%rbx)
cmpq $0x3, %rbx
ja 0x23c1
leal (%r14,%r14,4), %edx
movl %r12d, %eax
subl %edx, %eax
incl %eax
leaq (%rsp,%rax), %rdi
addq $0x50, %rdi
subl %r12d, %edx
addl $0x3, %edx
incq %rdx
xorl %esi, %esi
callq 0x2040
movq %rsp, %rbx
leaq 0x50(%rsp), %rsi
movq %rbx, %rdi
xorl %edx, %edx
movl $0x8, %ecx
callq 0x2160
movq (%rbx), %rax
xorq %rax, 0x10(%rsp)
jmp 0x2409
addq $-0x5, %r12
movq %rsp, %rbx
movq %rbx, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movl $0x8, %ecx
callq 0x2160
movq (%rbx), %rax
xorq %rax, 0x10(%rsp)
xorb $0x10, 0x18(%rsp)
movq 0x8(%rsp), %r13
leaq 0x10(%rsp), %rdi
callq 0x2070
xorb $0x1, 0x37(%rsp)
cmpq $0x6, %r13
movq 0x40(%rsp), %r12
movq 0x70(%rsp), %rbp
jb 0x248f
movq 0x8(%rsp), %r15
addq $-0x5, %r15
je 0x248f
xorl %r13d, %r13d
leaq 0x10(%rsp), %rbx
leaq 0x48(%rsp), %r14
movq %rsp, %rdi
movq %rbp, %rsi
movq %r13, %rdx
movl $0x8, %ecx
callq 0x2160
movq (%rsp), %rax
xorq %rax, 0x10(%rsp)
movq %rbx, %rdi
callq 0x2070
movq 0x10(%rsp), %rax
movq %rax, 0x48(%rsp)
movq %r12, %rdi
movq %r14, %rsi
movq %r13, %rdx
movl $0x5, %ecx
callq 0x2090
addq $0x5, %r13
cmpq %r15, %r13
jb 0x2445
movq 0x8(%rsp), %r15
movq %r15, %rdx
movabsq $-0x3333333333333333, %rax # imm = 0xCCCCCCCCCCCCCCCD
mulxq %rax, %r14, %r14
shrq $0x2, %r14
leaq (%r14,%r14,4), %rax
movq %r15, %rbx
subq %rax, %rbx
setne %al
testq %r15, %r15
sete %cl
orb %al, %cl
jne 0x24e7
leaq -0x5(%r15), %rdx
movq %rsp, %rbx
movq %rbx, %rdi
movq %rbp, %rsi
movl $0x8, %ecx
callq 0x2160
movq (%rbx), %rax
xorq %rax, 0x10(%rsp)
xorb $0x10, 0x18(%rsp)
jmp 0x2552
testq %rbx, %rbx
je 0x2505
movq %r15, %rax
subq %rbx, %rax
addq %rax, %rbp
leaq 0x50(%rsp), %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x2170
movb $-0x80, 0x50(%rsp,%rbx)
cmpq $0x3, %rbx
ja 0x2533
leal (%r14,%r14,4), %edx
movl %r15d, %eax
subl %edx, %eax
incl %eax
leaq (%rsp,%rax), %rdi
addq $0x50, %rdi
subl %r15d, %edx
addl $0x3, %edx
incq %rdx
xorl %esi, %esi
callq 0x2040
movq %rsp, %rbx
leaq 0x50(%rsp), %rsi
movq %rbx, %rdi
xorl %edx, %edx
movl $0x8, %ecx
callq 0x2160
movq (%rbx), %rax
xorq %rax, 0x10(%rsp)
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x2070
movq (%rbx), %rax
leaq 0x48(%rsp), %rsi
movq %rax, (%rsi)
xorl %edx, %edx
movq %r15, %rax
subq $0x5, %rax
cmovaeq %rax, %rdx
movq 0x40(%rsp), %r14
movq %r14, %rdi
movl $0x5, %ecx
callq 0x2090
vmovups 0x90(%rsp), %ymm0
vxorps 0x8(%rbx), %ymm0, %ymm0
vmovups %ymm0, 0x50(%rsp)
leaq -0x1(%r15), %rax
leaq 0x50(%rsp), %rsi
cmpq $0x3, %rax
ja 0x25c9
movl $0x5, %edx
movq %r14, %rdi
movl $0x14, %ecx
vzeroupper
callq 0x2090
movl $0x19, %r15d
jmp 0x25e0
movq %r14, %rdi
movq %r15, %rdx
movl $0x14, %ecx
vzeroupper
callq 0x2090
addq $0x14, %r15
movq 0x78(%rsp), %rax
movq %r15, (%rax)
xorl %eax, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq