// File: /workspace/github_c_makefile_star2_O3/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 %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
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 0xcac(%rip), %xmm0      # 0x2000
vpbroadcastb 0xcc3(%rip), %xmm1 # 0x2020
vpbroadcastd 0xcbe(%rip), %ymm2 # 0x2024
leaq 0xca3(%rip), %rdx       # 0x2010
movl $0xffff, %esi           # imm = 0xFFFF
movl $0x3f, %r8d
movw $0x1ff, %r9w            # imm = 0x1FF
kmovd %r9d, %k1
vpbroadcastd 0xc9d(%rip), %xmm3 # 0x2028
movq -0x16(%rsp), %r9
xorq (%rdi), %r9
vmovq %r9, %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, %r9
vpextrq $0x1, %xmm5, %r10
vextracti128 $0x1, %ymm5, %xmm5
vpextrq $0x1, %xmm5, %r11
vmovq %xmm5, %rbx
vmovq %xmm6, %r14
vpextrq $0x1, %xmm6, %r15
vextracti128 $0x1, %ymm6, %xmm5
vmovq %xmm5, %r12
vpextrq $0x1, %xmm5, %r13
movzbl (%r9,%rdx), %r9d
vmovd %r9d, %xmm5
vpinsrb $0x1, (%r10,%rdx), %xmm5, %xmm5
vpinsrb $0x2, (%rbx,%rdx), %xmm5, %xmm5
vpinsrb $0x3, (%r11,%rdx), %xmm5, %xmm5
vpinsrb $0x4, (%r14,%rdx), %xmm5, %xmm5
vpinsrb $0x5, (%r15,%rdx), %xmm5, %xmm5
vpinsrb $0x6, (%r12,%rdx), %xmm5, %xmm5
vpinsrb $0x7, (%r13,%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, %r9
vpextrq $0x1, %xmm4, %r10
vextracti128 $0x1, %ymm4, %xmm4
vmovq %xmm4, %r11
vpextrq $0x1, %xmm4, %rbx
vmovq %xmm6, %r14
vpextrq $0x1, %xmm6, %r15
vextracti128 $0x1, %ymm6, %xmm4
vpextrq $0x1, %xmm4, %r12
movzbl (%r9,%rdx), %r9d
vmovd %r9d, %xmm6
vpinsrb $0x1, (%r10,%rdx), %xmm6, %xmm6
vpinsrb $0x2, (%r11,%rdx), %xmm6, %xmm6
vpinsrb $0x3, (%rbx,%rdx), %xmm6, %xmm6
vpinsrb $0x4, (%r14,%rdx), %xmm6, %xmm6
vpinsrb $0x5, (%r15,%rdx), %xmm6, %xmm6
vmovq %xmm4, %r9
vpinsrb $0x6, (%r9,%rdx), %xmm6, %xmm4
vpinsrb $0x7, (%r12,%rdx), %xmm4, %xmm4
vpternlogq $0xf8, %xmm1, %xmm5, %xmm4
vpshufb %xmm0, %xmm4, %xmm4
vmovq %xmm4, (%rdi)
movq $0x0, -0x8(%rsp)
xorl %r9d, %r9d
xorl %r10d, %r10d
movzwl %r9w, %r11d
imull $0x411, %r11d, %ebx     # imm = 0x411
shrl $0x10, %ebx
movl %r9d, %r11d
subl %ebx, %r11d
andl %esi, %r11d
shrl %r11d
addl %ebx, %r11d
shrl $0x5, %r11d
movl %r11d, %ebx
shll $0x6, %ebx
subl %ebx, %r11d
addl %r9d, %r11d
cmpw $0x3f, %r10w
cmovel %r8d, %r11d
movl %r10d, %ebx
shrl $0x3, %ebx
movl %r11d, %r14d
andl %esi, %r14d
shrl $0x3, %r14d
andl $0x1f, %ebx
movzbl (%rdi,%rbx), %ebx
movl %r10d, %ebp
andl $0x7, %ebp
xorl %r15d, %r15d
btl %ebp, %ebx
setb %r15b
andb $0x7, %r11b
shlxl %r11d, %r15d, %r11d
orb %r11b, -0x8(%rsp,%r14)
incl %r10d
addl $0x10, %r9d
cmpw $0x40, %r10w
jne 0x14d6
movq -0x8(%rsp), %r9
movq %r9, (%rdi)
movzwl -0x18(%rsp), %r9d
movq -0x16(%rsp), %r10
movq %r10, -0x18(%rsp)
movw %r9w, -0x10(%rsp)
vmovd %r10d, %xmm4
vpbroadcastb %xmm4, %xmm4
vmovdqu8 (%rax), %xmm5 {%k1} {z}
vpalignr $0xf, %xmm4, %xmm5, %xmm4 # xmm4 = xmm4[15],xmm5[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14]
vpsllw $0x5, %xmm5, %xmm5
vpsrlw $0x3, %xmm4, %xmm4
vpternlogd $0xd8, %xmm3, %xmm5, %xmm4
vmovdqu8 %xmm4, -0x18(%rsp) {%k1}
movb -0xf(%rsp), %r9b
shlb $0x5, %r10b
shrb $0x3, %r9b
orb %r10b, %r9b
movzbl %r9b, %r9d
movl %r9d, %r10d
shrl $0x4, %r10d
movb (%r10,%rdx), %r10b
shlb $0x4, %r10b
andb $0xf, %r9b
orb %r10b, %r9b
movb %r9b, -0xf(%rsp)
testb $0x1, %cl
jne 0x15d8
xorb $-0x80, -0x17(%rsp)
incl %ecx
movl %ecx, %r9d
shrl %r9d
movzbl -0x16(%rsp), %r10d
movl %r10d, %r11d
andl $0xf, %r11d
xorl %r9d, %r11d
andl $-0x10, %r10d
orl %r11d, %r10d
movb %r10b, -0x16(%rsp)
cmpl $0x1f, %ecx
jne 0x138b
movq -0x16(%rsp), %rax
xorq %rax, (%rdi)
xorl %eax, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
vzeroupper
retq
nopl (%rax)