// File: /workspace/github_c_makefile_star2_O1/mjosaarinen[P]brutus/crypto_aead/present80n6t4silcv2/ref/present.c
// Function: present_enc
int present_enc(unsigned char state[8], const unsigned char userkey[10])
{
#ifdef _PRINT
	printf("PRESENT  input: ");
	pstate(state, 8);
#endif
	unsigned char key[10];
	memcpy(key, userkey, 10);
	const unsigned char sBox4[] =	{
		0xc,0x5,0x6,0xb,0x9,0x0,0xa,0xd,0x3,0xe,0xf,0x8,0x4,0x7,0x1,0x2
	};
	//	Counter
	unsigned char i = 0;
	//	pLayer variables
	unsigned char position = 0;
	unsigned char element_source = 0;
	unsigned char bit_source = 0;
	unsigned char element_destination	= 0;
	unsigned char bit_destination	= 0;
	unsigned char temp_pLayer[8];
	//	Key scheduling variables
	unsigned char round;
	unsigned char save1;
	unsigned char save2;
	//	****************** Encryption **************************
	round=0;
	do
	{
		//	****************** addRoundkey *************************
		i=0;
		do
		{
			state[i] = state[i] ^ key[i+2];
			i++;
		}
		while(i<=7);
		//	****************** sBox ********************************
		do
		{
			i--;
			state[i] = sBox4[state[i]>>4]<<4 | sBox4[state[i] & 0xF];
		}
		while(i>0);
		//	****************** pLayer ******************************
		for(i=0;i<8;i++)
		{
			temp_pLayer[i] = 0;
		}
		for(i=0;i<64;i++)
		{
			position = (16*i) % 63;						//Artithmetic calculation of the pLayer
			if(i == 63)									//exception for bit 63
				position = 63;
			element_source		= i / 8;
			bit_source 			= i % 8;
			element_destination	= position / 8;
			bit_destination 	= position % 8;
			temp_pLayer[element_destination] |= ((state[element_source]>>bit_source) & 0x1) << bit_destination;
		}
		for(i=0;i<=7;i++)
		{
			state[i] = temp_pLayer[i];
		}
		//	****************** End pLayer **************************
		//	****************** Key Scheduling **********************
		save1  = key[0];
		save2  = key[1];	
		i = 0;
		do
		{
			key[i] = key[i+2];
			i++;
		}
		while(i<8);
		key[8] = save1;
		key[9] = save2;
		i = 0;
		save1 = key[0] & 7;								//61-bit left shift
		do
		{
			key[i] = key[i] >> 3 | key[i+1] << 5;			
			i++;
		}
		while(i<9);
		key[9] = key[9] >> 3 | save1 << 5;

		key[9] = sBox4[key[9]>>4]<<4 | (key[9] & 0xF);	//S-Box application

		if((round+1) % 2 == 1)							//round counter addition
			key[1] ^= 128;
		key[2] = ((((round+1)>>1) ^ (key[2] & 15)) | (key[2] & 240));
		//	****************** End Key Scheduling ******************
		round++;
	}
	while(round<31);
	//	****************** addRoundkey *************************
	i = 0;
	do										//final key XOR
	{
		state[i] = state[i] ^ key[i+2];
		i++;
	}
	while(i<=7);
#ifdef _PRINT
	printf("PRESENT output: ");
	pstate(state, 8);
#endif
	return 0;		
	//	****************** End addRoundkey *********************
	//	****************** End Encryption  **********************
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
movzwl 0x8(%rsi), %ecx
leaq -0x17(%rsp), %rax
movw %cx, 0x7(%rax)
movq (%rsi), %rcx
movq %rcx, -0x1(%rax)
xorl %ecx, %ecx
vmovq 0xcb1(%rip), %xmm0      # 0x2000
vpbroadcastb 0xcc8(%rip), %xmm1 # 0x2020
leaq 0xcb1(%rip), %rdx       # 0x2010
vpbroadcastd 0xcbc(%rip), %ymm2 # 0x2024
movw $0x1ff, %si             # imm = 0x1FF
kmovd %esi, %k1
vpbroadcastd 0xcaf(%rip), %xmm3 # 0x2028
movq -0x16(%rsp), %rsi
xorq (%rdi), %rsi
vmovq %rsi, %xmm4
vpshufb %xmm0, %xmm4, %xmm4
vpmovzxbd %xmm4, %ymm4    # ymm4 = xmm4[0],zero,zero,zero,xmm4[1],zero,zero,zero,xmm4[2],zero,zero,zero,xmm4[3],zero,zero,zero,xmm4[4],zero,zero,zero,xmm4[5],zero,zero,zero,xmm4[6],zero,zero,zero,xmm4[7],zero,zero,zero
vpsrld $0x4, %ymm4, %ymm5
vextracti128 $0x1, %ymm5, %xmm6
vpmovzxdq %xmm6, %ymm6    # ymm6 = xmm6[0],zero,xmm6[1],zero,xmm6[2],zero,xmm6[3],zero
vpmovzxdq %xmm5, %ymm5    # ymm5 = xmm5[0],zero,xmm5[1],zero,xmm5[2],zero,xmm5[3],zero
vmovq %xmm5, %rsi
vpextrq $0x1, %xmm5, %r8
vextracti128 $0x1, %ymm5, %xmm5
vpextrq $0x1, %xmm5, %r9
vmovq %xmm5, %r10
vmovq %xmm6, %r11
vpextrq $0x1, %xmm6, %rbx
vextracti128 $0x1, %ymm6, %xmm5
vmovq %xmm5, %r14
vpextrq $0x1, %xmm5, %r15
movzbl (%rsi,%rdx), %esi
vmovd %esi, %xmm5
vpinsrb $0x1, (%r8,%rdx), %xmm5, %xmm5
vpinsrb $0x2, (%r10,%rdx), %xmm5, %xmm5
vpinsrb $0x3, (%r9,%rdx), %xmm5, %xmm5
vpinsrb $0x4, (%r11,%rdx), %xmm5, %xmm5
vpinsrb $0x5, (%rbx,%rdx), %xmm5, %xmm5
vpinsrb $0x6, (%r14,%rdx), %xmm5, %xmm5
vpinsrb $0x7, (%r15,%rdx), %xmm5, %xmm5
vpsllw $0x4, %xmm5, %xmm5
vpand %ymm2, %ymm4, %ymm4
vextracti128 $0x1, %ymm4, %xmm6
vpmovzxdq %xmm6, %ymm6    # ymm6 = xmm6[0],zero,xmm6[1],zero,xmm6[2],zero,xmm6[3],zero
vpmovzxdq %xmm4, %ymm4    # ymm4 = xmm4[0],zero,xmm4[1],zero,xmm4[2],zero,xmm4[3],zero
vmovq %xmm4, %rsi
vpextrq $0x1, %xmm4, %r8
vextracti128 $0x1, %ymm4, %xmm4
vmovq %xmm4, %r9
vpextrq $0x1, %xmm4, %r10
vmovq %xmm6, %r11
vpextrq $0x1, %xmm6, %rbx
vextracti128 $0x1, %ymm6, %xmm4
vpextrq $0x1, %xmm4, %r14
movzbl (%rsi,%rdx), %esi
vmovd %esi, %xmm6
vpinsrb $0x1, (%r8,%rdx), %xmm6, %xmm6
vpinsrb $0x2, (%r9,%rdx), %xmm6, %xmm6
vpinsrb $0x3, (%r10,%rdx), %xmm6, %xmm6
vpinsrb $0x4, (%r11,%rdx), %xmm6, %xmm6
vpinsrb $0x5, (%rbx,%rdx), %xmm6, %xmm6
vmovq %xmm4, %rsi
vpinsrb $0x6, (%rsi,%rdx), %xmm6, %xmm4
vpinsrb $0x7, (%r14,%rdx), %xmm4, %xmm4
vpternlogq $0xf8, %xmm1, %xmm5, %xmm4
vpshufb %xmm0, %xmm4, %xmm4
vmovq %xmm4, (%rdi)
movq $0x0, -0x8(%rsp)
xorl %esi, %esi
xorl %r8d, %r8d
movl %esi, %r9d
imulq $0x4104105, %r9, %r10   # imm = 0x4104105
shrq $0x20, %r10
movl %esi, %r9d
subl %r10d, %r9d
shrl %r9d
addl %r10d, %r9d
shrl $0x5, %r9d
movl %r9d, %r10d
shll $0x6, %r10d
subl %r10d, %r9d
addl %esi, %r9d
cmpl $0x3f, %r8d
cmovel %r8d, %r9d
movl %r8d, %r10d
shrl $0x3, %r10d
movl %r9d, %r11d
shrl $0x3, %r11d
andl $0x1f, %r10d
movzbl (%rdi,%r10), %r10d
movl %r8d, %ebx
andb $0x7, %bl
shrxl %ebx, %r10d, %r10d
andl $0x1, %r10d
andb $0x7, %r9b
shlxl %r9d, %r10d, %r9d
orb %r9b, -0x8(%rsp,%r11)
incl %r8d
addl $0x10, %esi
cmpl $0x40, %r8d
jne 0x14be
movq -0x8(%rsp), %rsi
movq %rsi, (%rdi)
movzwl -0x18(%rsp), %esi
movq -0x16(%rsp), %r8
movq %r8, -0x18(%rsp)
movw %si, -0x10(%rsp)
vmovdqu8 -0x18(%rsp), %xmm4 {%k1} {z}
vmovdqu8 (%rax), %xmm5 {%k1} {z}
vpsllw $0x5, %xmm5, %xmm5
vpsrlw $0x3, %xmm4, %xmm4
vpternlogd $0xd8, %xmm3, %xmm5, %xmm4
vmovdqu8 %xmm4, -0x18(%rsp) {%k1}
movb -0xf(%rsp), %sil
shlb $0x5, %r8b
shrb $0x3, %sil
orb %r8b, %sil
movzbl %sil, %esi
movl %esi, %r8d
shrl $0x4, %r8d
movb (%r8,%rdx), %r8b
shlb $0x4, %r8b
andb $0xf, %sil
orb %r8b, %sil
movb %sil, -0xf(%rsp)
testb $0x1, %cl
jne 0x15b3
xorb $-0x80, -0x17(%rsp)
incl %ecx
movl %ecx, %esi
shrl %esi
movzbl -0x16(%rsp), %r8d
movl %r8d, %r9d
andl $0xf, %r9d
xorl %esi, %r9d
andl $-0x10, %r8d
orl %r9d, %r8d
movb %r8b, -0x16(%rsp)
cmpl $0x1f, %ecx
jne 0x1379
movq -0x16(%rsp), %rax
xorq %rax, (%rdi)
xorl %eax, %eax
popq %rbx
popq %r14
popq %r15
vzeroupper
retq
nopl (%rax)