// File: /workspace/github_c_makefile_star2_O2/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), %eax
leaq -0x17(%rsp), %r8
movw %ax, 0x7(%r8)
movq (%rsi), %rax
vmovq 0xcb9(%rip), %xmm0      # 0x2000
movq %rax, -0x1(%r8)
leaq 0xcbe(%rip), %rsi       # 0x2010
vpbroadcastb 0xcc5(%rip), %xmm1 # 0x2020
vpbroadcastd 0xcc0(%rip), %ymm2 # 0x2024
movw $0x3f, %r9w
movw $0x1ff, %ax             # imm = 0x1FF
kmovd %eax, %k1
vpbroadcastd 0xcae(%rip), %xmm3 # 0x2028
movl $0x3f, %r10d
xorl %r11d, %r11d
movq -0x16(%rsp), %rax
xorq (%rdi), %rax
vmovq %rax, %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, %rax
vpextrq $0x1, %xmm5, %rcx
vextracti128 $0x1, %ymm5, %xmm5
vmovq %xmm5, %rdx
vpextrq $0x1, %xmm5, %rbx
vmovq %xmm6, %r14
vpextrq $0x1, %xmm6, %r15
vextracti128 $0x1, %ymm6, %xmm5
vmovq %xmm5, %r12
vpextrq $0x1, %xmm5, %r13
movzbl (%rax,%rsi), %eax
vmovd %eax, %xmm5
vpinsrb $0x1, (%rcx,%rsi), %xmm5, %xmm5
vpinsrb $0x2, (%rdx,%rsi), %xmm5, %xmm5
vpinsrb $0x3, (%rbx,%rsi), %xmm5, %xmm5
vpinsrb $0x4, (%r14,%rsi), %xmm5, %xmm5
vpinsrb $0x5, (%r15,%rsi), %xmm5, %xmm5
vpinsrb $0x6, (%r12,%rsi), %xmm5, %xmm5
vpinsrb $0x7, (%r13,%rsi), %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, %rax
vpextrq $0x1, %xmm4, %rcx
vextracti128 $0x1, %ymm4, %xmm4
vpextrq $0x1, %xmm4, %rdx
vmovq %xmm4, %rbx
vmovq %xmm6, %r14
vpextrq $0x1, %xmm6, %r15
vextracti128 $0x1, %ymm6, %xmm4
vmovq %xmm4, %r12
vpextrq $0x1, %xmm4, %r13
movzbl (%rax,%rsi), %eax
vmovd %eax, %xmm4
vpinsrb $0x1, (%rcx,%rsi), %xmm4, %xmm4
vpinsrb $0x2, (%rbx,%rsi), %xmm4, %xmm4
vpinsrb $0x3, (%rdx,%rsi), %xmm4, %xmm4
vpinsrb $0x4, (%r14,%rsi), %xmm4, %xmm4
vpinsrb $0x5, (%r15,%rsi), %xmm4, %xmm4
vpinsrb $0x6, (%r12,%rsi), %xmm4, %xmm4
vpinsrb $0x7, (%r13,%rsi), %xmm4, %xmm4
vpsllw $0x4, %xmm5, %xmm5
vpternlogq $0xf8, %xmm1, %xmm5, %xmm4
vpshufb %xmm0, %xmm4, %xmm4
vmovq %xmm4, (%rdi)
xorl %eax, %eax
cmpq $0x8, %rax
je 0x14ce
movb $0x0, -0x8(%rsp,%rax)
incq %rax
jmp 0x14be
xorl %ecx, %ecx
xorl %ebx, %ebx
movl %ecx, %eax
xorl %edx, %edx
divw %r9w
cmpw $0x40, %bx
je 0x151f
cmpw $0x3f, %bx
cmovel %r10d, %edx
movl %ebx, %eax
shrl $0x3, %eax
movzwl %dx, %r14d
shrl $0x3, %r14d
andl $0x1f, %eax
movzbl (%rdi,%rax), %eax
movl %ebx, %ebp
andl $0x7, %ebp
xorl %r15d, %r15d
btl %ebp, %eax
setb %r15b
andb $0x7, %dl
shlxl %edx, %r15d, %eax
orb %al, -0x8(%rsp,%r14)
incl %ebx
addl $0x10, %ecx
jmp 0x14d2
xorl %eax, %eax
cmpq $0x8, %rax
je 0x1533
movb -0x8(%rsp,%rax), %cl
movb %cl, (%rdi,%rax)
incq %rax
jmp 0x1521
movzwl -0x18(%rsp), %eax
movq -0x16(%rsp), %rcx
movq %rcx, -0x18(%rsp)
movw %ax, -0x10(%rsp)
vmovd %ecx, %xmm4
vpbroadcastb %xmm4, %xmm4
vmovdqu8 (%r8), %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), %al
shlb $0x5, %cl
shrb $0x3, %al
orb %cl, %al
movzbl %al, %eax
movl %eax, %ecx
shrl $0x4, %ecx
movb (%rcx,%rsi), %cl
shlb $0x4, %cl
andb $0xf, %al
orb %cl, %al
movb %al, -0xf(%rsp)
testb $0x1, %r11b
jne 0x15a5
xorb $-0x80, -0x17(%rsp)
incl %r11d
movl %r11d, %eax
shrl %eax
movzbl -0x16(%rsp), %ecx
movl %ecx, %edx
andl $0xf, %edx
xorl %eax, %edx
andl $-0x10, %ecx
orl %edx, %ecx
movb %cl, -0x16(%rsp)
cmpl $0x1f, %r11d
jne 0x1383
movq -0x16(%rsp), %rax
xorq %rax, (%rdi)
xorl %eax, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
vzeroupper
retq