// File: /workspace/github_c_makefile_star2_O0/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:
movq %rdi, -0x8(%rsp)
movq %rsi, -0x10(%rsp)
movq -0x10(%rsp), %rax
movq (%rax), %rcx
movq %rcx, -0x1a(%rsp)
movw 0x8(%rax), %ax
movw %ax, -0x12(%rsp)
movq 0x1b69(%rip), %rax      # 0x3000
movq %rax, -0x38(%rsp)
movq 0x1b65(%rip), %rax      # 0x3008
movq %rax, -0x30(%rsp)
movb $0x0, -0x39(%rsp)
movb $0x0, -0x3a(%rsp)
movb $0x0, -0x3b(%rsp)
movb $0x0, -0x3c(%rsp)
movb $0x0, -0x3d(%rsp)
movb $0x0, -0x3e(%rsp)
movb $0x0, -0x47(%rsp)
movb $0x0, -0x39(%rsp)
movq -0x8(%rsp), %rax
movzbl -0x39(%rsp), %ecx
movzbl (%rax,%rcx), %eax
movzbl -0x39(%rsp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movzbl -0x1a(%rsp,%rcx), %ecx
xorl %ecx, %eax
movb %al, %dl
movq -0x8(%rsp), %rax
movzbl -0x39(%rsp), %ecx
movb %dl, (%rax,%rcx)
movb -0x39(%rsp), %al
addb $0x1, %al
movb %al, -0x39(%rsp)
movzbl -0x39(%rsp), %eax
cmpl $0x7, %eax
jle 0x14d0
jmp 0x1515
movb -0x39(%rsp), %al
addb $-0x1, %al
movb %al, -0x39(%rsp)
movq -0x8(%rsp), %rax
movzbl -0x39(%rsp), %ecx
movzbl (%rax,%rcx), %eax
sarl $0x4, %eax
cltq
movzbl -0x38(%rsp,%rax), %eax
shll $0x4, %eax
movq -0x8(%rsp), %rcx
movzbl -0x39(%rsp), %edx
movzbl (%rcx,%rdx), %ecx
andl $0xf, %ecx
movslq %ecx, %rcx
movzbl -0x38(%rsp,%rcx), %ecx
orl %ecx, %eax
movb %al, %dl
movq -0x8(%rsp), %rax
movzbl -0x39(%rsp), %ecx
movb %dl, (%rax,%rcx)
movzbl -0x39(%rsp), %eax
cmpl $0x0, %eax
jg 0x1515
movb $0x0, -0x39(%rsp)
movzbl -0x39(%rsp), %eax
cmpl $0x8, %eax
jge 0x1593
movzbl -0x39(%rsp), %eax
movb $0x0, -0x46(%rsp,%rax)
movb -0x39(%rsp), %al
addb $0x1, %al
movb %al, -0x39(%rsp)
jmp 0x1573
movb $0x0, -0x39(%rsp)
movzbl -0x39(%rsp), %eax
cmpl $0x40, %eax
jge 0x1651
movzbl -0x39(%rsp), %eax
shll $0x4, %eax
movl $0x3f, %ecx
cltd
idivl %ecx
movb %dl, %al
movb %al, -0x3a(%rsp)
movzbl -0x39(%rsp), %eax
cmpl $0x3f, %eax
jne 0x15cb
movb $0x3f, -0x3a(%rsp)
movzbl -0x39(%rsp), %eax
movl $0x8, %ecx
cltd
idivl %ecx
movb %al, -0x3b(%rsp)
movzbl -0x39(%rsp), %eax
movl $0x8, %ecx
cltd
idivl %ecx
movb %dl, %al
movb %al, -0x3c(%rsp)
movzbl -0x3a(%rsp), %eax
movl $0x8, %ecx
cltd
idivl %ecx
movb %al, -0x3d(%rsp)
movzbl -0x3a(%rsp), %eax
movl $0x8, %ecx
cltd
idivl %ecx
movb %dl, %al
movb %al, -0x3e(%rsp)
movq -0x8(%rsp), %rax
movzbl -0x3b(%rsp), %ecx
movzbl (%rax,%rcx), %edx
movzbl -0x3c(%rsp), %ecx
sarl %cl, %edx
andl $0x1, %edx
movzbl -0x3e(%rsp), %ecx
shll %cl, %edx
movzbl -0x3d(%rsp), %eax
movzbl -0x46(%rsp,%rax), %ecx
orl %edx, %ecx
movb %cl, -0x46(%rsp,%rax)
movb -0x39(%rsp), %al
addb $0x1, %al
movb %al, -0x39(%rsp)
jmp 0x1598
movb $0x0, -0x39(%rsp)
movzbl -0x39(%rsp), %eax
cmpl $0x7, %eax
jg 0x1682
movzbl -0x39(%rsp), %eax
movb -0x46(%rsp,%rax), %dl
movq -0x8(%rsp), %rax
movzbl -0x39(%rsp), %ecx
movb %dl, (%rax,%rcx)
movb -0x39(%rsp), %al
addb $0x1, %al
movb %al, -0x39(%rsp)
jmp 0x1656
movb -0x1a(%rsp), %al
movb %al, -0x48(%rsp)
movb -0x19(%rsp), %al
movb %al, -0x49(%rsp)
movb $0x0, -0x39(%rsp)
movzbl -0x39(%rsp), %eax
addl $0x2, %eax
cltq
movb -0x1a(%rsp,%rax), %cl
movzbl -0x39(%rsp), %eax
movb %cl, -0x1a(%rsp,%rax)
movb -0x39(%rsp), %al
addb $0x1, %al
movb %al, -0x39(%rsp)
movzbl -0x39(%rsp), %eax
cmpl $0x8, %eax
jl 0x1697
movb -0x48(%rsp), %al
movb %al, -0x12(%rsp)
movb -0x49(%rsp), %al
movb %al, -0x11(%rsp)
movb $0x0, -0x39(%rsp)
movzbl -0x1a(%rsp), %eax
andl $0x7, %eax
movb %al, -0x48(%rsp)
movzbl -0x39(%rsp), %eax
movzbl -0x1a(%rsp,%rax), %eax
sarl $0x3, %eax
movzbl -0x39(%rsp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movzbl -0x1a(%rsp,%rcx), %ecx
shll $0x5, %ecx
orl %ecx, %eax
movb %al, %cl
movzbl -0x39(%rsp), %eax
movb %cl, -0x1a(%rsp,%rax)
movb -0x39(%rsp), %al
addb $0x1, %al
movb %al, -0x39(%rsp)
movzbl -0x39(%rsp), %eax
cmpl $0x9, %eax
jl 0x16e3
movzbl -0x11(%rsp), %eax
sarl $0x3, %eax
movzbl -0x48(%rsp), %ecx
shll $0x5, %ecx
orl %ecx, %eax
movb %al, -0x11(%rsp)
movzbl -0x11(%rsp), %eax
sarl $0x4, %eax
cltq
movzbl -0x38(%rsp,%rax), %eax
shll $0x4, %eax
movzbl -0x11(%rsp), %ecx
andl $0xf, %ecx
orl %ecx, %eax
movb %al, -0x11(%rsp)
movzbl -0x47(%rsp), %eax
addl $0x1, %eax
movl $0x2, %ecx
cltd
idivl %ecx
cmpl $0x1, %edx
jne 0x177d
movzbl -0x19(%rsp), %eax
xorl $0x80, %eax
movb %al, -0x19(%rsp)
movzbl -0x47(%rsp), %eax
addl $0x1, %eax
sarl %eax
movzbl -0x18(%rsp), %ecx
andl $0xf, %ecx
xorl %ecx, %eax
movzbl -0x18(%rsp), %ecx
andl $0xf0, %ecx
orl %ecx, %eax
movb %al, -0x18(%rsp)
movb -0x47(%rsp), %al
addb $0x1, %al
movb %al, -0x47(%rsp)
movzbl -0x47(%rsp), %eax
cmpl $0x1f, %eax
jl 0x14cb
movb $0x0, -0x39(%rsp)
movq -0x8(%rsp), %rax
movzbl -0x39(%rsp), %ecx
movzbl (%rax,%rcx), %eax
movzbl -0x39(%rsp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movzbl -0x1a(%rsp,%rcx), %ecx
xorl %ecx, %eax
movb %al, %dl
movq -0x8(%rsp), %rax
movzbl -0x39(%rsp), %ecx
movb %dl, (%rax,%rcx)
movb -0x39(%rsp), %al
addb $0x1, %al
movb %al, -0x39(%rsp)
movzbl -0x39(%rsp), %eax
cmpl $0x7, %eax
jle 0x17bf
xorl %eax, %eax
retq
nopw %cs:(%rax,%rax)
nop