// File: /workspace/github_c_makefile_star2_O3/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, %r15
movq %rdx, 0x70(%rsp)
movq %rsi, 0x78(%rsp)
movq %rdi, 0x60(%rsp)
movq 0xf8(%rsp), %rbx
movq 0x100(%rsp), %rsi
leaq 0x90(%rsp), %r14
movq %r14, %rdi
xorl %edx, %edx
movl $0x20, %ecx
callq 0x2150
leaq 0x80(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
xorl %edx, %edx
movl $0x10, %ecx
callq 0x2150
leaq 0x30(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x20b0
movq (%r13), %rax
xorq %rax, (%rbx)
movq %rbx, %rdi
callq 0x2070
movq 0x8(%r13), %rax
movq %r12, %r14
xorq %rax, (%rbx)
movq %rbx, %rdi
callq 0x2070
testq %r12, %r12
movq %r15, 0x8(%rsp)
je 0x240a
movq %r14, %r12
cmpq $0x6, %r14
jb 0x234a
leaq -0x5(%r12), %r15
xorl %ebx, %ebx
movq %rsp, %r14
leaq 0x30(%rsp), %r13
movq %r14, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
movl $0x8, %ecx
callq 0x2150
movq (%rsp), %rax
xorq %rax, 0x30(%rsp)
movq %r13, %rdi
callq 0x2070
addq $0x5, %rbx
cmpq %r15, %rbx
jb 0x231d
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 0x23d4
movq %r12, %rax
subq %rbx, %rax
addq %rax, %rbp
leaq 0x10(%rsp), %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x2160
movb $-0x80, 0x10(%rsp,%rbx)
cmpq $0x3, %rbx
ja 0x23b3
leal (%r14,%r14,4), %edx
movl %r12d, %eax
subl %edx, %eax
incl %eax
leaq (%rsp,%rax), %rdi
addq $0x10, %rdi
subl %r12d, %edx
addl $0x3, %edx
incq %rdx
xorl %esi, %esi
callq 0x2040
movq %rsp, %rbx
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
xorl %edx, %edx
movl $0x8, %ecx
callq 0x2150
movq (%rbx), %rax
xorq %rax, 0x30(%rsp)
jmp 0x23fb
addq $-0x5, %r12
movq %rsp, %rbx
movq %rbx, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movl $0x8, %ecx
callq 0x2150
movq (%rbx), %rax
xorq %rax, 0x30(%rsp)
xorb $0x10, 0x38(%rsp)
movq 0x8(%rsp), %r15
leaq 0x30(%rsp), %rdi
callq 0x2070
xorb $0x1, 0x57(%rsp)
cmpq $0x6, %r15
movq 0x60(%rsp), %r12
movq 0x70(%rsp), %rbp
jb 0x247f
movq 0x8(%rsp), %rax
leaq -0x5(%rax), %r15
xorl %r13d, %r13d
leaq 0x30(%rsp), %rbx
leaq 0x68(%rsp), %r14
movq %rsp, %rdi
movq %rbp, %rsi
movq %r13, %rdx
movl $0x8, %ecx
callq 0x2150
movq (%rsp), %rax
xorq %rax, 0x30(%rsp)
movq %rbx, %rdi
callq 0x2070
movq 0x30(%rsp), %rax
movq %rax, 0x68(%rsp)
movq %r12, %rdi
movq %r14, %rsi
movq %r13, %rdx
movl $0x5, %ecx
callq 0x2090
addq $0x5, %r13
cmpq %r15, %r13
jb 0x2435
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 0x24d7
leaq -0x5(%r15), %rdx
movq %rsp, %rbx
movq %rbx, %rdi
movq %rbp, %rsi
movl $0x8, %ecx
callq 0x2150
movq (%rbx), %rax
xorq %rax, 0x30(%rsp)
xorb $0x10, 0x38(%rsp)
jmp 0x2549
testq %rbx, %rbx
je 0x2502
movq %r15, %rax
subq %rbx, %rax
addq %rax, %rbp
leaq 0x10(%rsp), %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x2160
movb $-0x80, 0x10(%rsp,%rbx)
cmpq $0x3, %rbx
jbe 0x2507
jmp 0x252a
movb $-0x80, 0x10(%rsp,%rbx)
leal (%r14,%r14,4), %edx
movl %r15d, %eax
subl %edx, %eax
incl %eax
leaq (%rsp,%rax), %rdi
addq $0x10, %rdi
subl %r15d, %edx
addl $0x3, %edx
incq %rdx
xorl %esi, %esi
callq 0x2040
movq %rsp, %rbx
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
xorl %edx, %edx
movl $0x8, %ecx
callq 0x2150
movq (%rbx), %rax
xorq %rax, 0x30(%rsp)
leaq 0x30(%rsp), %rbx
movq %rbx, %rdi
callq 0x2070
movq (%rbx), %rax
leaq 0x68(%rsp), %rsi
movq %rax, (%rsi)
xorl %edx, %edx
movq %r15, %rax
subq $0x5, %rax
cmovaeq %rax, %rdx
movq 0x60(%rsp), %r14
movq %r14, %rdi
movl $0x5, %ecx
callq 0x2090
vmovups 0x90(%rsp), %ymm0
vxorps 0x8(%rbx), %ymm0, %ymm0
vmovups %ymm0, 0x10(%rsp)
leaq -0x1(%r15), %rax
leaq 0x10(%rsp), %rsi
cmpq $0x3, %rax
ja 0x25c0
movl $0x5, %edx
movq %r14, %rdi
movl $0x14, %ecx
vzeroupper
callq 0x2090
movl $0x19, %r15d
jmp 0x25d7
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