// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x128, %rsp            # imm = 0x128
movq 0x140(%rsp), %rax
movq 0x138(%rsp), %rax
movq 0x130(%rsp), %rax
movq %rdi, 0x120(%rsp)
movq %rsi, 0x118(%rsp)
movq %rdx, 0x110(%rsp)
movq %rcx, 0x108(%rsp)
movq %r8, 0x100(%rsp)
movq %r9, 0xf8(%rsp)
leaq 0xb0(%rsp), %rdi
movq 0x140(%rsp), %rsi
xorl %eax, %eax
movl %eax, %edx
movl $0x20, %ecx
callq 0x2180
leaq 0xa0(%rsp), %rdi
movq 0x138(%rsp), %rsi
xorl %eax, %eax
movl %eax, %edx
movl $0x10, %ecx
callq 0x2180
leaq 0xd0(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x2080
movl $0x0, 0x6c(%rsp)
cmpl $0x2, 0x6c(%rsp)
jge 0x23a0
movl $0x0, 0x68(%rsp)
cmpl $0x8, 0x68(%rsp)
jge 0x2386
movl 0x6c(%rsp), %eax
shll $0x3, %eax
addl 0x68(%rsp), %eax
cltq
movzbl 0xa0(%rsp,%rax), %edx
movslq 0x68(%rsp), %rax
movzbl 0xd0(%rsp,%rax), %ecx
xorl %edx, %ecx
movb %cl, 0xd0(%rsp,%rax)
movl 0x68(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x68(%rsp)
jmp 0x2347
leaq 0xd0(%rsp), %rdi
callq 0x2050
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x2338
cmpq $0x0, 0xf8(%rsp)
je 0x25cd
cmpq $0x5, 0xf8(%rsp)
jbe 0x2457
movq $0x0, 0x60(%rsp)
movq 0x60(%rsp), %rax
movq 0xf8(%rsp), %rcx
subq $0x5, %rcx
cmpq %rcx, %rax
jae 0x2455
leaq 0x78(%rsp), %rdi
movq 0x100(%rsp), %rsi
movq 0x60(%rsp), %rdx
movl $0x8, %ecx
callq 0x2180
movl $0x0, 0x5c(%rsp)
cmpl $0x8, 0x5c(%rsp)
jge 0x2435
movslq 0x5c(%rsp), %rax
movzbl 0x78(%rsp,%rax), %edx
movslq 0x5c(%rsp), %rax
movzbl 0xd0(%rsp,%rax), %ecx
xorl %edx, %ecx
movb %cl, 0xd0(%rsp,%rax)
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x2401
leaq 0xd0(%rsp), %rdi
callq 0x2050
movq 0x60(%rsp), %rax
addq $0x5, %rax
movq %rax, 0x60(%rsp)
jmp 0x23c7
jmp 0x2457
movq 0xf8(%rsp), %rax
movl $0x5, %ecx
xorl %edx, %edx
divq %rcx
movl %edx, %eax
movl %eax, 0x58(%rsp)
cmpl $0x0, 0x58(%rsp)
jne 0x24ec
leaq 0x78(%rsp), %rdi
movq 0x100(%rsp), %rsi
movq 0xf8(%rsp), %rdx
subq $0x5, %rdx
movl $0x8, %ecx
callq 0x2180
movl $0x0, 0x54(%rsp)
cmpl $0x8, 0x54(%rsp)
jge 0x24d5
movslq 0x54(%rsp), %rax
movzbl 0x78(%rsp,%rax), %edx
movslq 0x54(%rsp), %rax
movzbl 0xd0(%rsp,%rax), %ecx
xorl %edx, %ecx
movb %cl, 0xd0(%rsp,%rax)
movl 0x54(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x54(%rsp)
jmp 0x24a1
movzbl 0xd8(%rsp), %eax
xorl $0x10, %eax
movb %al, 0xd8(%rsp)
jmp 0x25c0
movl $0x0, 0x48(%rsp)
movl 0x48(%rsp), %eax
cmpl 0x58(%rsp), %eax
jge 0x2537
movq 0x100(%rsp), %rax
movq 0xf8(%rsp), %rcx
movslq 0x58(%rsp), %rdx
subq %rdx, %rcx
movslq 0x48(%rsp), %rdx
addq %rdx, %rcx
movb (%rax,%rcx), %cl
movslq 0x48(%rsp), %rax
movb %cl, 0x4f(%rsp,%rax)
movl 0x48(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x48(%rsp)
jmp 0x24f4
movslq 0x58(%rsp), %rax
movb $-0x80, 0x4f(%rsp,%rax)
movl 0x58(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
cmpl $0x5, 0x44(%rsp)
jge 0x256a
movslq 0x44(%rsp), %rax
movb $0x0, 0x4f(%rsp,%rax)
movl 0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
jmp 0x254c
leaq 0x78(%rsp), %rdi
leaq 0x4f(%rsp), %rsi
xorl %eax, %eax
movl %eax, %edx
movl $0x8, %ecx
callq 0x2180
movl $0x0, 0x40(%rsp)
cmpl $0x8, 0x40(%rsp)
jge 0x25be
movslq 0x40(%rsp), %rax
movzbl 0x78(%rsp,%rax), %edx
movslq 0x40(%rsp), %rax
movzbl 0xd0(%rsp,%rax), %ecx
xorl %edx, %ecx
movb %cl, 0xd0(%rsp,%rax)
movl 0x40(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x40(%rsp)
jmp 0x258a
jmp 0x25c0
leaq 0xd0(%rsp), %rdi
callq 0x2050
movzbl 0xf7(%rsp), %eax
xorl $0x1, %eax
movb %al, 0xf7(%rsp)
cmpq $0x5, 0x108(%rsp)
jbe 0x26d8
movq $0x0, 0x38(%rsp)
movq 0x38(%rsp), %rax
movq 0x108(%rsp), %rcx
subq $0x5, %rcx
cmpq %rcx, %rax
jae 0x26d6
leaq 0x78(%rsp), %rdi
movq 0x110(%rsp), %rsi
movq 0x38(%rsp), %rdx
movl $0x8, %ecx
callq 0x2180
movl $0x0, 0x34(%rsp)
cmpl $0x8, 0x34(%rsp)
jge 0x2669
movslq 0x34(%rsp), %rax
movzbl 0x78(%rsp,%rax), %edx
movslq 0x34(%rsp), %rax
movzbl 0xd0(%rsp,%rax), %ecx
xorl %edx, %ecx
movb %cl, 0xd0(%rsp,%rax)
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x34(%rsp)
jmp 0x2635
leaq 0xd0(%rsp), %rdi
callq 0x2050
movl $0x0, 0x30(%rsp)
cmpl $0x8, 0x30(%rsp)
jge 0x26a7
movslq 0x30(%rsp), %rax
movb 0xd0(%rsp,%rax), %cl
movslq 0x30(%rsp), %rax
movb %cl, 0x70(%rsp,%rax)
movl 0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30(%rsp)
jmp 0x267e
movq 0x120(%rsp), %rdi
leaq 0x70(%rsp), %rsi
movq 0x38(%rsp), %rdx
movl $0x5, %ecx
callq 0x2060
movq 0x38(%rsp), %rax
addq $0x5, %rax
movq %rax, 0x38(%rsp)
jmp 0x25f7
jmp 0x26d8
movq 0x108(%rsp), %rax
movl $0x5, %ecx
xorl %edx, %edx
divq %rcx
movl %edx, %eax
movl %eax, 0x2c(%rsp)
cmpl $0x0, 0x2c(%rsp)
jne 0x277c
cmpq $0x0, 0x108(%rsp)
je 0x277c
leaq 0x78(%rsp), %rdi
movq 0x110(%rsp), %rsi
movq 0x108(%rsp), %rdx
subq $0x5, %rdx
movl $0x8, %ecx
callq 0x2180
movl $0x0, 0x28(%rsp)
cmpl $0x8, 0x28(%rsp)
jge 0x2765
movslq 0x28(%rsp), %rax
movzbl 0x78(%rsp,%rax), %edx
movslq 0x28(%rsp), %rax
movzbl 0xd0(%rsp,%rax), %ecx
xorl %edx, %ecx
movb %cl, 0xd0(%rsp,%rax)
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0x2731
movzbl 0xd8(%rsp), %eax
xorl $0x10, %eax
movb %al, 0xd8(%rsp)
jmp 0x2850
movl $0x0, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
cmpl 0x2c(%rsp), %eax
jge 0x27c7
movq 0x110(%rsp), %rax
movq 0x108(%rsp), %rcx
movslq 0x2c(%rsp), %rdx
subq %rdx, %rcx
movslq 0x1c(%rsp), %rdx
addq %rdx, %rcx
movb (%rax,%rcx), %cl
movslq 0x1c(%rsp), %rax
movb %cl, 0x23(%rsp,%rax)
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x2784
movslq 0x2c(%rsp), %rax
movb $-0x80, 0x23(%rsp,%rax)
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x18(%rsp)
cmpl $0x5, 0x18(%rsp)
jge 0x27fa
movslq 0x18(%rsp), %rax
movb $0x0, 0x23(%rsp,%rax)
movl 0x18(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x18(%rsp)
jmp 0x27dc
leaq 0x78(%rsp), %rdi
leaq 0x23(%rsp), %rsi
xorl %eax, %eax
movl %eax, %edx
movl $0x8, %ecx
callq 0x2180
movl $0x0, 0x14(%rsp)
cmpl $0x8, 0x14(%rsp)
jge 0x284e
movslq 0x14(%rsp), %rax
movzbl 0x78(%rsp,%rax), %edx
movslq 0x14(%rsp), %rax
movzbl 0xd0(%rsp,%rax), %ecx
xorl %edx, %ecx
movb %cl, 0xd0(%rsp,%rax)
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0x281a
jmp 0x2850
leaq 0xd0(%rsp), %rdi
callq 0x2050
movl $0x0, 0x10(%rsp)
cmpl $0x8, 0x10(%rsp)
jge 0x288e
movslq 0x10(%rsp), %rax
movb 0xd0(%rsp,%rax), %cl
movslq 0x10(%rsp), %rax
movb %cl, 0x70(%rsp,%rax)
movl 0x10(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x10(%rsp)
jmp 0x2865
cmpq $0x5, 0x108(%rsp)
jae 0x28b6
movq 0x120(%rsp), %rdi
leaq 0x70(%rsp), %rsi
xorl %eax, %eax
movl %eax, %edx
movl $0x5, %ecx
callq 0x2060
jmp 0x28d9
movq 0x120(%rsp), %rdi
leaq 0x70(%rsp), %rsi
movq 0x108(%rsp), %rdx
subq $0x5, %rdx
movl $0x5, %ecx
callq 0x2060
movl $0x0, 0xc(%rsp)
cmpl $0x20, 0xc(%rsp)
jge 0x2923
movl 0xc(%rsp), %eax
addl $0x8, %eax
cltq
movzbl 0xd0(%rsp,%rax), %eax
movslq 0xc(%rsp), %rcx
movzbl 0xb0(%rsp,%rcx), %ecx
xorl %ecx, %eax
movb %al, %cl
movslq 0xc(%rsp), %rax
movb %cl, 0x80(%rsp,%rax)
movl 0xc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc(%rsp)
jmp 0x28e1
cmpq $0x0, 0x108(%rsp)
jbe 0x2969
cmpq $0x5, 0x108(%rsp)
jae 0x2969
movq 0x120(%rsp), %rdi
leaq 0x80(%rsp), %rsi
movl $0x5, %edx
movl $0x14, %ecx
callq 0x2060
movq 0x118(%rsp), %rax
movq $0x19, (%rax)
jmp 0x29a2
movq 0x120(%rsp), %rdi
leaq 0x80(%rsp), %rsi
movq 0x108(%rsp), %rdx
movl $0x14, %ecx
callq 0x2060
movq 0x108(%rsp), %rcx
addq $0x14, %rcx
movq 0x118(%rsp), %rax
movq %rcx, (%rax)
xorl %eax, %eax
addq $0x128, %rsp            # imm = 0x128
retq
nopl (%rax)