// File: /workspace/github_c_makefile_star2_O3/mjosaarinen[P]brutus/crypto_aead/twine80n6t4clocv2/ref/twine.c
// Function: Keyschedule128
void Keyschedule128(const uint8 sk[128/4])
{
	int	i;
	uint8	tmp[128/4];
	uint8	wk[5];

	//Copy the key 
#if 1
	for(i=0; i<(128/4); i+=2)
	{
		tmp[i] = sk[i/2] >> 4;
		tmp[i+1] = sk[i/2] & 0x0f;
	}
#else
	tmp[0] = sk[0] >> 4;
	tmp[1] = sk[0] & 0x0f;
	tmp[2] = sk[1] >> 4;
	tmp[3] = sk[1] & 0x0f;
	tmp[4] = sk[2] >> 4;
	tmp[5] = sk[2] & 0x0f;
	tmp[6] = sk[3] >> 4;
	tmp[7] = sk[3] & 0x0f;
	tmp[8] = sk[4] >> 4;
	tmp[9] = sk[4] & 0x0f;
	tmp[10] = sk[5] >> 4;
	tmp[11] = sk[5] & 0x0f;
	tmp[12] = sk[6] >> 4;
	tmp[13] = sk[6] & 0x0f;
	tmp[14] = sk[7] >> 4;
	tmp[15] = sk[7] & 0x0f;
	tmp[16] = sk[8] >> 4;
	tmp[17] = sk[8] & 0x0f;
	tmp[18] = sk[9] >> 4;
	tmp[19] = sk[9] & 0x0f;
	tmp[20] = sk[10] >> 4;
	tmp[21] = sk[10] & 0x0f;
	tmp[22] = sk[11] >> 4;
	tmp[23] = sk[11] & 0x0f;
	tmp[24] = sk[12] >> 4;
	tmp[25] = sk[12] & 0x0f;
	tmp[26] = sk[13] >> 4;
	tmp[27] = sk[13] & 0x0f;
	tmp[28] = sk[14] >> 4;
	tmp[29] = sk[14] & 0x0f;
	tmp[30] = sk[15] >> 4;
	tmp[31] = sk[15] & 0x0f;
#endif
        
	//Extended key (ek) generation
	for(i=0; ; i+=2)
	{
		//ek for two rounds
		ek[i][0] = tmp[2];
		ek[i][1] = tmp[3];
		ek[i][2] = tmp[12];
		ek[i][3] = tmp[15];
		ek[i][4] = tmp[17];
		ek[i][5] = tmp[18];
		ek[i][6] = tmp[28];
		ek[i][7] = tmp[31];

		ek[i+1][0] = tmp[6];
		ek[i+1][1] = tmp[7] ^ CON_H[i];
		ek[i+1][2] = tmp[16];
		ek[i+1][3] = tmp[19] ^ CON_L[i];
		ek[i+1][4] = tmp[21];
		ek[i+1][5] = tmp[22];
		ek[i+1][6] = tmp[1] ^ S[tmp[0]];
		ek[i+1][7] = tmp[0];
	
		//to the last two rounds
		if(i == (ROUND-2))
			break;

		//rotations for two rounds 
		wk[3] = tmp[4] ^ S[tmp[16]];
		wk[0] = tmp[5] ^ S[wk[3]];
		wk[1] = tmp[6];
		wk[2] = tmp[7] ^ CON_H[i];

		tmp[4] = tmp[12];
		tmp[5] = tmp[13];
		tmp[6] = tmp[14];
		tmp[7] = tmp[15];

		tmp[12] = tmp[20];
		tmp[13] = tmp[21];
		tmp[14] = tmp[22];
		tmp[15] = tmp[23] ^ S[tmp[30]] ^ CON_L[i+1];

		wk[4] = tmp[20];
		tmp[20] = tmp[28];
		tmp[21] = tmp[29];
		tmp[22] = tmp[30];
		tmp[23] = tmp[31];

		tmp[28] = wk[0];
		tmp[29] = wk[1];
		tmp[30] = wk[2];
		tmp[31] = wk[3];


		wk[3] = tmp[0];
		wk[0] = ek[i+1][6];
		wk[1] = tmp[2];
		wk[2] = tmp[3];

		tmp[0] = tmp[8] ^ S[wk[4]];
		tmp[1] = tmp[9];
		tmp[2] = tmp[10];
		tmp[3] = tmp[11] ^ CON_H[i+1];

		tmp[8] = tmp[16];
		tmp[9] = tmp[17];
		tmp[10] = tmp[18];
		tmp[11] = tmp[19] ^ CON_L[i];

		tmp[16] = tmp[24];
		tmp[17] = tmp[25];
		tmp[18] = tmp[26];
		tmp[19] = tmp[27] ^ S[wk[2]];

		tmp[24] = wk[0];
		tmp[25] = wk[1];
		tmp[26] = wk[2];
		tmp[27] = wk[3];
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
vmovdqu (%rdi), %xmm1
vpsrlw $0x4, %xmm1, %xmm2
vpbroadcastb 0xf51(%rip), %xmm3 # 0x3023
vpand %xmm3, %xmm2, %xmm0
vpand %xmm3, %xmm1, %xmm1
vpextrb $0x1, %xmm2, %r10d
vpextrb $0x1, %xmm1, %eax
movl %eax, -0x54(%rsp)
vpextrb $0x6, %xmm0, %r8d
vpextrb $0x7, %xmm1, %edx
vpextrb $0x8, %xmm1, %eax
movl %eax, -0x48(%rsp)
vpextrb $0x9, %xmm2, %r14d
vpextrb $0xe, %xmm0, %eax
movl %eax, -0x5c(%rsp)
vpextrb $0xf, %xmm1, %eax
movl %eax, -0x4c(%rsp)
vpextrb $0x3, %xmm2, %ebp
vpextrb $0x3, %xmm1, %eax
movl %eax, -0x20(%rsp)
vpextrb $0x8, %xmm0, %r11d
vpextrb $0x9, %xmm1, %r15d
vpextrb $0xa, %xmm1, %edi
vpextrb $0xb, %xmm2, %eax
movl %eax, -0x50(%rsp)
vmovd %xmm1, %ebx
vpextrb $0x2, %xmm0, %eax
movl %eax, -0x40(%rsp)
vpextrb $0x2, %xmm1, %eax
movl %eax, -0x44(%rsp)
vpextrb $0x6, %xmm1, %eax
movl %eax, -0x60(%rsp)
vpextrb $0x7, %xmm2, %eax
movl %eax, -0x64(%rsp)
vpextrb $0xa, %xmm0, %eax
movl %eax, -0x18(%rsp)
vpextrb $0xb, %xmm1, %eax
movl %eax, -0x34(%rsp)
vpextrb $0xf, %xmm2, %r12d
vpextrb $0xe, %xmm1, %eax
movl %eax, -0x24(%rsp)
vpextrb $0x4, %xmm0, %eax
movl %eax, -0x3c(%rsp)
vpextrb $0x4, %xmm1, %eax
movl %eax, -0x68(%rsp)
vpextrb $0x5, %xmm2, %eax
movl %eax, -0x28(%rsp)
vpextrb $0x5, %xmm1, %eax
movl %eax, -0x30(%rsp)
vpextrb $0xc, %xmm0, %esi
vpextrb $0xc, %xmm1, %eax
movl %eax, -0x2c(%rsp)
vpextrb $0xd, %xmm2, %eax
movl %eax, -0x1c(%rsp)
vpextrb $0xd, %xmm1, %eax
movl %eax, -0x38(%rsp)
vmovd %xmm0, -0x58(%rsp)
movl $0x1, %r13d
movq 0x2ddb(%rip), %rcx      # 0x4fd0
movb %sil, -0x69(%rsp)
movl %r12d, %r9d
movb %r10b, -0x8(%rcx,%r13,8)
movl -0x54(%rsp), %esi
movb %sil, -0x7(%rcx,%r13,8)
movb %r8b, -0x6(%rcx,%r13,8)
movl %edx, -0x8(%rsp)
movb %dl, -0x5(%rcx,%r13,8)
movl -0x48(%rsp), %edx
movb %dl, -0x4(%rcx,%r13,8)
movb %r14b, -0x3(%rcx,%r13,8)
movl -0x5c(%rsp), %edx
movb %dl, -0x2(%rcx,%r13,8)
movl -0x4c(%rsp), %edx
movb %dl, -0x1(%rcx,%r13,8)
movl %ebp, -0x4(%rsp)
movb %bpl, (%rcx,%r13,8)
movl -0x20(%rsp), %r12d
movq 0x2dab(%rip), %rdx      # 0x4ff8
xorb -0x1(%rdx,%r13), %r12b
movb %r12b, 0x1(%rcx,%r13,8)
movb %r11b, 0x2(%rcx,%r13,8)
movq 0x2d5d(%rip), %rdx      # 0x4fc0
xorb -0x1(%rdx,%r13), %r15b
movb %r15b, 0x3(%rcx,%r13,8)
movb %dil, 0x4(%rcx,%r13,8)
movl -0x50(%rsp), %edx
movb %dl, 0x5(%rcx,%r13,8)
movzbl -0x58(%rsp), %edx
movl %ebx, %esi
movq 0x2d67(%rip), %rax      # 0x4ff0
xorb (%rax,%rdx), %sil
movb %sil, 0x6(%rcx,%r13,8)
movb %dl, 0x7(%rcx,%r13,8)
movl -0x60(%rsp), %eax
movb %al, -0x6b(%rsp)
movl -0x64(%rsp), %eax
movl %eax, %ebp
movl -0x18(%rsp), %eax
movl %eax, %ebx
movl -0x24(%rsp), %eax
movb %al, -0x6d(%rsp)
movl -0x28(%rsp), %eax
movl %eax, %edx
movl -0x2c(%rsp), %eax
movb %al, -0x6a(%rsp)
movl -0x1c(%rsp), %eax
movb %al, -0x6c(%rsp)
movl -0x68(%rsp), %eax
movl %eax, -0xc(%rsp)
cmpq $0x23, %r13
je 0x2445
movl %r8d, -0x10(%rsp)
movl -0x40(%rsp), %eax
movl %eax, %r8d
movl %edi, -0x60(%rsp)
movl %r11d, -0x14(%rsp)
movzbl %r11b, %eax
movb %dl, -0x6e(%rsp)
movq 0x2cf1(%rip), %rdi      # 0x4ff0
xorb (%rdi,%rax), %r8b
movl %r10d, -0x64(%rsp)
movl -0x44(%rsp), %eax
movl %eax, %r10d
movzbl %r8b, %eax
xorb (%rdi,%rax), %r10b
movb %r9b, -0x6f(%rsp)
movzbl %r9b, %eax
movb (%rdi,%rax), %al
movq 0x2c96(%rip), %rdx      # 0x4fc0
xorb (%rdx,%r13), %al
xorb -0x34(%rsp), %al
movl %r14d, -0x68(%rsp)
movl -0x3c(%rsp), %edx
movl %edx, %r14d
movzbl %bl, %edx
xorb (%rdi,%rdx), %r14b
movl -0x38(%rsp), %edx
movl -0x54(%rsp), %r9d
movzbl %r9b, %r11d
xorb (%rdi,%r11), %dl
movl -0x30(%rsp), %r11d
movl -0x58(%rsp), %edi
movl %edi, -0x38(%rsp)
movl %r9d, -0x1c(%rsp)
movl %r15d, -0x30(%rsp)
movl -0x4c(%rsp), %r15d
movl %r15d, -0x34(%rsp)
movb %bpl, -0x70(%rsp)
movl -0x5c(%rsp), %ebp
movl %ebp, -0x18(%rsp)
movl -0x8(%rsp), %r15d
movl %r15d, -0x20(%rsp)
movq 0x2c63(%rip), %r9       # 0x4ff8
xorb (%r9,%r13), %r11b
addq $0x2, %r13
movl -0x64(%rsp), %edi
movl %edi, -0x2c(%rsp)
movl -0x68(%rsp), %edi
movl %edi, -0x28(%rsp)
movl -0x48(%rsp), %r15d
movl %r15d, -0x68(%rsp)
movl -0x14(%rsp), %edi
movl %edi, -0x3c(%rsp)
movl -0x4(%rsp), %edi
movl %edi, -0x24(%rsp)
movl -0x50(%rsp), %edi
movl %edi, -0x64(%rsp)
movl -0x60(%rsp), %edi
movl %edi, -0x60(%rsp)
movl -0x10(%rsp), %edi
movl %edi, -0x40(%rsp)
movl %r14d, %edi
movl %edi, -0x58(%rsp)
movl %edx, %r15d
movl %r8d, %edx
movl %edx, -0x4c(%rsp)
movl %r11d, %edx
movl %edx, -0x54(%rsp)
movl %r10d, %edx
movl %edx, -0x5c(%rsp)
movb -0x6b(%rsp), %dl
movl %edx, -0x44(%rsp)
movb -0x6f(%rsp), %dl
movl %edx, -0x50(%rsp)
movb -0x6d(%rsp), %dl
movl %edx, %edi
movb -0x69(%rsp), %dl
movl %edx, %r11d
movb -0x70(%rsp), %dl
movl %edx, %ebp
movb -0x6c(%rsp), %dl
movl %edx, %r14d
movb -0x6a(%rsp), %dl
movl %edx, -0x48(%rsp)
movl %eax, %edx
movl %ebx, %r8d
movb -0x6e(%rsp), %al
movl %eax, %r10d
movl -0xc(%rsp), %ebx
jmp 0x21f5
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq