// File: /workspace/github_c_makefile_star2_O0/mjosaarinen[P]brutus/crypto_aead/primatesv1hanuman80/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 <-- p_1(O||K||N)
	InitializeState(V,key,nonce);
	p_1(V); 
	
	if(adlen!=0)
	{ 
		// for i=1 to u-1
		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_4 (A[i]^V_r || V_c)
				for(int j=0; j<RateSizeElements; j++)
					V[j]^=data[j];
				p_4(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);	
	}	
	//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
			// C[i] <-- M[i]^V_r
			for(int j=0; j<RateSizeElements; j++)
				cipher[j]=V[j]^data[j];
			Element2Bytes(c,cipher,i,RateSizeBytes); //representing a block of c as bytes
			//V <-- p_1 (C[i] || V_c)
			for(int j=0; j<RateSizeElements; j++)
				V[j]=cipher[j];
			p_1(V);
		}
	}
	
	//for i=w
	int l = mlen%RateSizeBytes;
	// C[i] <-- M[i]^V_r
	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++)
			cipher[i]=V[i]^data[i];
		Element2Bytes(c,cipher,mlen-RateSizeBytes,RateSizeBytes); //representing last block of c as bytes
		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++)
			cipher[i]=V[i]^data[i];
		unsigned char ct[RateSizeBytes];
		Element2Bytes(ct,cipher,0,RateSizeBytes); //representing last block of c as bytes
		for(int i=0; i< l; i++)
			c[mlen-l+i]=ct[i]; //storing only l bytes of c
	}	
	//V <-- p_1 (C[i] || V_c)
	for(int j=0; j<RateSizeElements; j++)
		V[j]=cipher[j];
	p_1(V);

	// T <-- (V_c)_c/2 ^ K
	for(int i=0; i<TagSizeElements; i++)
		tag[i]=V[RateSizeElements+i]^key[i]; 
	Element2Bytes(c,tag,mlen,TagSizeBytes) ;
	*clen=mlen+TagSizeBytes;
	
    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)
leaq 0xa0(%rsp), %rdi
movq 0x120(%rsp), %rsi
xorl %eax, %eax
movl %eax, %edx
movl $0x10, %ecx
callq 0x1150
leaq 0x90(%rsp), %rdi
movq 0x118(%rsp), %rsi
xorl %eax, %eax
movl %eax, %edx
movl $0x10, %ecx
callq 0x1150
leaq 0xb0(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
leaq 0x90(%rsp), %rdx
callq 0x10b0
leaq 0xb0(%rsp), %rdi
callq 0x1060
cmpq $0x0, 0xd8(%rsp)
je 0x1532
cmpq $0x5, 0xd8(%rsp)
jbe 0x13bc
movq $0x0, 0x68(%rsp)
movq 0x68(%rsp), %rax
movq 0xd8(%rsp), %rcx
subq $0x5, %rcx
cmpq %rcx, %rax
jae 0x13ba
leaq 0x78(%rsp), %rdi
movq 0xe0(%rsp), %rsi
movq 0x68(%rsp), %rdx
movl $0x8, %ecx
callq 0x1150
movl $0x0, 0x64(%rsp)
cmpl $0x8, 0x64(%rsp)
jge 0x139a
movslq 0x64(%rsp), %rax
movzbl 0x78(%rsp,%rax), %edx
movslq 0x64(%rsp), %rax
movzbl 0xb0(%rsp,%rax), %ecx
xorl %edx, %ecx
movb %cl, 0xb0(%rsp,%rax)
movl 0x64(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x64(%rsp)
jmp 0x1366
leaq 0xb0(%rsp), %rdi
callq 0x1050
movq 0x68(%rsp), %rax
addq $0x5, %rax
movq %rax, 0x68(%rsp)
jmp 0x132c
jmp 0x13bc
movq 0xd8(%rsp), %rax
movl $0x5, %ecx
xorl %edx, %edx
divq %rcx
movl %edx, %eax
movl %eax, 0x60(%rsp)
cmpl $0x0, 0x60(%rsp)
jne 0x1451
leaq 0x78(%rsp), %rdi
movq 0xe0(%rsp), %rsi
movq 0xd8(%rsp), %rdx
subq $0x5, %rdx
movl $0x8, %ecx
callq 0x1150
movl $0x0, 0x5c(%rsp)
cmpl $0x8, 0x5c(%rsp)
jge 0x143a
movslq 0x5c(%rsp), %rax
movzbl 0x78(%rsp,%rax), %edx
movslq 0x5c(%rsp), %rax
movzbl 0xb0(%rsp,%rax), %ecx
xorl %edx, %ecx
movb %cl, 0xb0(%rsp,%rax)
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x1406
movzbl 0xb8(%rsp), %eax
xorl $0x10, %eax
movb %al, 0xb8(%rsp)
jmp 0x1525
movl $0x0, 0x50(%rsp)
movl 0x50(%rsp), %eax
cmpl 0x60(%rsp), %eax
jge 0x149c
movq 0xe0(%rsp), %rax
movq 0xd8(%rsp), %rcx
movslq 0x60(%rsp), %rdx
subq %rdx, %rcx
movslq 0x50(%rsp), %rdx
addq %rdx, %rcx
movb (%rax,%rcx), %cl
movslq 0x50(%rsp), %rax
movb %cl, 0x57(%rsp,%rax)
movl 0x50(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x50(%rsp)
jmp 0x1459
movslq 0x60(%rsp), %rax
movb $-0x80, 0x57(%rsp,%rax)
movl 0x60(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
cmpl $0x5, 0x4c(%rsp)
jge 0x14cf
movslq 0x4c(%rsp), %rax
movb $0x0, 0x57(%rsp,%rax)
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0x14b1
leaq 0x78(%rsp), %rdi
leaq 0x57(%rsp), %rsi
xorl %eax, %eax
movl %eax, %edx
movl $0x8, %ecx
callq 0x1150
movl $0x0, 0x48(%rsp)
cmpl $0x8, 0x48(%rsp)
jge 0x1523
movslq 0x48(%rsp), %rax
movzbl 0x78(%rsp,%rax), %edx
movslq 0x48(%rsp), %rax
movzbl 0xb0(%rsp,%rax), %ecx
xorl %edx, %ecx
movb %cl, 0xb0(%rsp,%rax)
movl 0x48(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x48(%rsp)
jmp 0x14ef
jmp 0x1525
leaq 0xb0(%rsp), %rdi
callq 0x1060
cmpq $0x5, 0xe8(%rsp)
jbe 0x162f
movq $0x0, 0x40(%rsp)
movq 0x40(%rsp), %rax
movq 0xe8(%rsp), %rcx
subq $0x5, %rcx
cmpq %rcx, %rax
jae 0x162d
leaq 0x78(%rsp), %rdi
movq 0xf0(%rsp), %rsi
movq 0x40(%rsp), %rdx
movl $0x8, %ecx
callq 0x1150
movl $0x0, 0x3c(%rsp)
cmpl $0x8, 0x3c(%rsp)
jge 0x15c0
movslq 0x3c(%rsp), %rax
movzbl 0xb0(%rsp,%rax), %eax
movslq 0x3c(%rsp), %rcx
movzbl 0x78(%rsp,%rcx), %ecx
xorl %ecx, %eax
movb %al, %cl
movslq 0x3c(%rsp), %rax
movb %cl, 0x70(%rsp,%rax)
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0x1588
movq 0x100(%rsp), %rdi
leaq 0x70(%rsp), %rsi
movq 0x40(%rsp), %rdx
movl $0x5, %ecx
callq 0x1070
movl $0x0, 0x38(%rsp)
cmpl $0x8, 0x38(%rsp)
jge 0x160d
movslq 0x38(%rsp), %rax
movb 0x70(%rsp,%rax), %cl
movslq 0x38(%rsp), %rax
movb %cl, 0xb0(%rsp,%rax)
movl 0x38(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x38(%rsp)
jmp 0x15e4
leaq 0xb0(%rsp), %rdi
callq 0x1060
movq 0x40(%rsp), %rax
addq $0x5, %rax
movq %rax, 0x40(%rsp)
jmp 0x154a
jmp 0x162f
movq 0xe8(%rsp), %rax
movl $0x5, %ecx
xorl %edx, %edx
divq %rcx
movl %edx, %eax
movl %eax, 0x34(%rsp)
cmpl $0x0, 0x34(%rsp)
jne 0x16fe
cmpq $0x0, 0xe8(%rsp)
je 0x16fe
leaq 0x78(%rsp), %rdi
movq 0xf0(%rsp), %rsi
movq 0xe8(%rsp), %rdx
subq $0x5, %rdx
movl $0x8, %ecx
callq 0x1150
movl $0x0, 0x30(%rsp)
cmpl $0x8, 0x30(%rsp)
jge 0x16c4
movslq 0x30(%rsp), %rax
movzbl 0xb0(%rsp,%rax), %eax
movslq 0x30(%rsp), %rcx
movzbl 0x78(%rsp,%rcx), %ecx
xorl %ecx, %eax
movb %al, %cl
movslq 0x30(%rsp), %rax
movb %cl, 0x70(%rsp,%rax)
movl 0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30(%rsp)
jmp 0x168c
movq 0x100(%rsp), %rdi
leaq 0x70(%rsp), %rsi
movq 0xe8(%rsp), %rdx
subq $0x5, %rdx
movl $0x5, %ecx
callq 0x1070
movzbl 0xb8(%rsp), %eax
xorl $0x10, %eax
movb %al, 0xb8(%rsp)
jmp 0x1839
movl $0x0, 0x24(%rsp)
movl 0x24(%rsp), %eax
cmpl 0x34(%rsp), %eax
jge 0x1749
movq 0xf0(%rsp), %rax
movq 0xe8(%rsp), %rcx
movslq 0x34(%rsp), %rdx
subq %rdx, %rcx
movslq 0x24(%rsp), %rdx
addq %rdx, %rcx
movb (%rax,%rcx), %cl
movslq 0x24(%rsp), %rax
movb %cl, 0x2b(%rsp,%rax)
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
jmp 0x1706
movslq 0x34(%rsp), %rax
movb $-0x80, 0x2b(%rsp,%rax)
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x20(%rsp)
cmpl $0x5, 0x20(%rsp)
jge 0x177c
movslq 0x20(%rsp), %rax
movb $0x0, 0x2b(%rsp,%rax)
movl 0x20(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x20(%rsp)
jmp 0x175e
leaq 0x78(%rsp), %rdi
leaq 0x2b(%rsp), %rsi
xorl %eax, %eax
movl %eax, %edx
movl $0x8, %ecx
callq 0x1150
movl $0x0, 0x1c(%rsp)
cmpl $0x8, 0x1c(%rsp)
jge 0x17d4
movslq 0x1c(%rsp), %rax
movzbl 0xb0(%rsp,%rax), %eax
movslq 0x1c(%rsp), %rcx
movzbl 0x78(%rsp,%rcx), %ecx
xorl %ecx, %eax
movb %al, %cl
movslq 0x1c(%rsp), %rax
movb %cl, 0x70(%rsp,%rax)
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x179c
leaq 0x17(%rsp), %rdi
leaq 0x70(%rsp), %rsi
xorl %eax, %eax
movl %eax, %edx
movl $0x5, %ecx
callq 0x1070
movl $0x0, 0x10(%rsp)
movl 0x10(%rsp), %eax
cmpl 0x34(%rsp), %eax
jge 0x1837
movslq 0x10(%rsp), %rax
movb 0x17(%rsp,%rax), %dl
movq 0x100(%rsp), %rax
movq 0xe8(%rsp), %rcx
movslq 0x34(%rsp), %rsi
subq %rsi, %rcx
movslq 0x10(%rsp), %rsi
addq %rsi, %rcx
movb %dl, (%rax,%rcx)
movl 0x10(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x10(%rsp)
jmp 0x17f4
jmp 0x1839
movl $0x0, 0xc(%rsp)
cmpl $0x8, 0xc(%rsp)
jge 0x186a
movslq 0xc(%rsp), %rax
movb 0x70(%rsp,%rax), %cl
movslq 0xc(%rsp), %rax
movb %cl, 0xb0(%rsp,%rax)
movl 0xc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc(%rsp)
jmp 0x1841
leaq 0xb0(%rsp), %rdi
callq 0x1060
movl $0x0, 0x8(%rsp)
cmpl $0x10, 0x8(%rsp)
jge 0x18c1
movl 0x8(%rsp), %eax
addl $0x8, %eax
cltq
movzbl 0xb0(%rsp,%rax), %eax
movslq 0x8(%rsp), %rcx
movzbl 0xa0(%rsp,%rcx), %ecx
xorl %ecx, %eax
movb %al, %cl
movslq 0x8(%rsp), %rax
movb %cl, 0x80(%rsp,%rax)
movl 0x8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8(%rsp)
jmp 0x187f
movq 0x100(%rsp), %rdi
leaq 0x80(%rsp), %rsi
movq 0xe8(%rsp), %rdx
movl $0xa, %ecx
callq 0x1070
movq 0xe8(%rsp), %rcx
addq $0xa, %rcx
movq 0xf8(%rsp), %rax
movq %rcx, (%rax)
xorl %eax, %eax
addq $0x108, %rsp            # imm = 0x108
retq
nopw %cs:(%rax,%rax)