// File: /workspace/github_c_makefile_star2_O1/mjosaarinen[P]brutus/crypto_aead/twine80n6t4clocv2/ref/twine.c
// Function: Encode
void Encode(uint8 text[8], uint8 cipher[8])
{
#ifdef _PRINT
	printf("Twine  input: ");
	pstate(text, 8);
#endif
	uint8	i;
	uint8	data1[16], tmp, t_data[16];

	//Set plaintext
	data1[0] = text[0]>>4;
	data1[1] = text[0]&0xf;
	data1[2] = text[1]>>4;
	data1[3] = text[1]&0xf;
	data1[4] = text[2]>>4;
	data1[5] = text[2]&0xf;
	data1[6] = text[3]>>4;
	data1[7] = text[3]&0xf;
	data1[8] = text[4]>>4;
	data1[9] = text[4]&0xf;
	data1[10] = text[5]>>4;
	data1[11] = text[5]&0xf;
	data1[12] = text[6]>>4;
	data1[13] = text[6]&0xf;
	data1[14] = text[7]>>4;
	data1[15] = text[7]&0xf;

	//Round functions
	for(i=0; ; i++)
	{
 		data1[1] ^= S[data1[0] ^ ek[i][0]];
		data1[3] ^= S[data1[2] ^ ek[i][1]];
		data1[5] ^= S[data1[4] ^ ek[i][2]];
		data1[7] ^= S[data1[6] ^ ek[i][3]];
		data1[9] ^= S[data1[8] ^ ek[i][4]];
		data1[11] ^= S[data1[10] ^ ek[i][5]];
		data1[13] ^= S[data1[12] ^ ek[i][6]];
		data1[15] ^= S[data1[14] ^ ek[i][7]];

        if(pswitch){
            t_data[0] = data1[1];
            t_data[1] = data1[2];
            t_data[2] = data1[11];
            t_data[3] = data1[6];
            t_data[4] = data1[3];
            t_data[5] = data1[0];
            t_data[6] = data1[9];
            t_data[7] = data1[4];
            t_data[8] = data1[7];
            t_data[9] = data1[10];
            t_data[10] = data1[13];
            t_data[11] = data1[14];
            t_data[12] = data1[5];
            t_data[13] = data1[8];
            t_data[14] = data1[15];
            t_data[15] = data1[12];
            printf("ROUND %02d: ", i);
            pstate3(t_data, 16);
        }
		i++;
 		data1[2] ^= S[data1[1] ^ ek[i][0]];
		data1[6] ^= S[data1[11] ^ ek[i][1]];
		data1[0] ^= S[data1[3] ^ ek[i][2]];
		data1[4] ^= S[data1[9] ^ ek[i][3]];
		data1[10] ^= S[data1[7] ^ ek[i][4]];
		data1[14] ^= S[data1[13] ^ ek[i][5]];
		data1[8] ^= S[data1[5] ^ ek[i][6]];
		data1[12] ^= S[data1[15] ^ ek[i][7]];

		if(i == (ROUND-1))
			break;

		tmp = data1[0];
		data1[0] = data1[2];
		data1[2] = data1[14];
		data1[14] = data1[12];
		data1[12] = tmp;

		tmp = data1[1];
		data1[1] = data1[11];
		data1[11] = data1[15];
		data1[15] = data1[5];
		data1[5] = tmp;

		tmp = data1[3];
		data1[3] = data1[9];
		data1[9] = data1[13];
		data1[13] = data1[7];
		data1[7] = tmp;

		tmp = data1[4];
		data1[4] = data1[6];
		data1[6] = data1[10];
		data1[10] = data1[8];
		data1[8] = tmp;
        if(pswitch){
            printf("ROUND %02d: ", i);
            pstate3(data1, 16);
        }
	}

	//Set ciphertext
	cipher[0] = (data1[1]<<4) ^ data1[2];
	cipher[1] = (data1[11]<<4) ^ data1[6];
	cipher[2] = (data1[3]<<4) ^ data1[0];
	cipher[3] = (data1[9]<<4) ^ data1[4];
	cipher[4] = (data1[7]<<4) ^ data1[10];
	cipher[5] = (data1[13]<<4) ^ data1[14];
	cipher[6] = (data1[5]<<4) ^ data1[8];
	cipher[7] = (data1[15]<<4) ^ data1[12];
#ifdef _PRINT
	printf("Twine output: ");
	pstate(cipher, 8);
#endif
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, 0x40(%rsp)
vmovq (%rdi), %xmm0
vpunpcklbw %xmm0, %xmm0, %xmm0 # xmm0 = xmm0[0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7]
vpbroadcastb 0x17f8(%rip), %xmm1 # 0x3000
vpsrlw $0x4, %xmm0, %xmm2
vpand %xmm1, %xmm2, %xmm2
vpand %xmm1, %xmm0, %xmm0
movw $0xaaaa, %ax            # imm = 0xAAAA
kmovd %eax, %k1
vmovdqu8 %xmm0, %xmm2 {%k1}
leaq 0x10(%rsp), %rcx
vmovdqa %xmm2, (%rcx)
movb (%rcx), %bpl
movb 0x1(%rcx), %r15b
movb 0x2(%rcx), %r12b
movb 0x3(%rcx), %al
movb %al, 0xf(%rsp)
movb 0x4(%rcx), %r9b
movb 0x5(%rcx), %r8b
movb 0x6(%rcx), %bl
movb 0x7(%rcx), %r11b
movb 0x8(%rcx), %al
movb %al, 0xa(%rsp)
movb 0x9(%rcx), %al
movb %al, 0x6(%rsp)
movb 0xa(%rcx), %al
movb %al, 0x5(%rsp)
movb 0xb(%rcx), %al
movb %al, 0x8(%rsp)
movb 0xc(%rcx), %al
movb %al, 0x9(%rsp)
movb 0xd(%rcx), %dl
movb 0xe(%rcx), %al
movb %al, 0xb(%rsp)
movb 0xf(%rcx), %al
movb %al, 0x7(%rsp)
xorl %r13d, %r13d
movb 0x7(%rsp), %al
movl %edx, %ecx
movl %r11d, %edx
movq 0x373c(%rip), %r14      # 0x4fd0
movb (%r14,%r13,8), %dil
xorb %bpl, %dil
movzbl %dil, %edi
movl %r15d, %r11d
movq 0x3747(%rip), %r10      # 0x4ff0
xorb (%r10,%rdi), %r11b
movl %r8d, %edi
movb %r11b, 0x11(%rsp)
movb 0x1(%r14,%r13,8), %sil
xorb %r12b, %sil
movzbl %sil, %esi
movl %edx, %r8d
movb 0xf(%rsp), %dl
xorb (%r10,%rsi), %dl
movb %dl, 0xd(%rsp)
movb %dl, 0x13(%rsp)
movb 0x2(%r14,%r13,8), %dl
xorb %r9b, %dl
movzbl %dl, %edx
xorb (%r10,%rdx), %dil
movb %dil, 0x7(%rsp)
movb %dil, 0x15(%rsp)
movb 0x3(%r14,%r13,8), %dl
xorb %bl, %dl
movzbl %dl, %edx
xorb (%r10,%rdx), %r8b
movb %r8b, 0xc(%rsp)
movb %r8b, 0x17(%rsp)
movb 0x4(%r14,%r13,8), %dl
xorb 0xa(%rsp), %dl
movzbl %dl, %edx
movb 0x6(%rsp), %sil
xorb (%r10,%rdx), %sil
movb %sil, 0x19(%rsp)
movb 0x5(%r14,%r13,8), %dl
xorb 0x5(%rsp), %dl
movzbl %dl, %edx
movb 0x8(%rsp), %r8b
xorb (%r10,%rdx), %r8b
movb %r8b, 0x1b(%rsp)
movb 0x6(%r14,%r13,8), %dl
xorb 0x9(%rsp), %dl
movzbl %dl, %edx
xorb (%r10,%rdx), %cl
movb %cl, 0x6(%rsp)
movb %cl, 0x1d(%rsp)
movb 0x7(%r14,%r13,8), %cl
movb 0xb(%rsp), %dl
xorb %dl, %cl
movzbl %cl, %ecx
xorb (%r10,%rcx), %al
movb %al, 0x8(%rsp)
movb %al, 0x1f(%rsp)
movq 0x3666(%rip), %rax      # 0x4fd8
cmpl $0x0, (%rax)
movb %sil, 0xf(%rsp)
je 0x1a2b
movb %r11b, 0x30(%rsp)
movb %r12b, 0x31(%rsp)
movb %r8b, 0x32(%rsp)
movb %bl, 0x33(%rsp)
movb 0xd(%rsp), %al
movb %al, 0x34(%rsp)
movb %bpl, 0x35(%rsp)
movb %sil, 0x36(%rsp)
movb %r9b, 0x37(%rsp)
movb 0xc(%rsp), %al
movb %al, 0x38(%rsp)
movb 0x5(%rsp), %al
movb %al, 0x39(%rsp)
movb 0x6(%rsp), %al
movb %al, 0x3a(%rsp)
movb %dl, 0x3b(%rsp)
movb 0x7(%rsp), %al
movb %al, 0x3c(%rsp)
movb 0xa(%rsp), %al
movb %al, 0x3d(%rsp)
movb 0x8(%rsp), %al
movb %al, 0x3e(%rsp)
movb 0x9(%rsp), %al
movb %al, 0x3f(%rsp)
leaq 0x1738(%rip), %rdi      # 0x3125
movl %r13d, %esi
xorl %eax, %eax
movl %r8d, %r14d
movl %r9d, %r15d
movb %bpl, 0xe(%rsp)
movl %r11d, %ebp
callq 0x1030
leaq 0x30(%rsp), %rdi
movl $0x10, %esi
callq 0x10d0
movb 0xb(%rsp), %dl
movl %ebp, %r11d
movb 0xf(%rsp), %sil
movb 0xe(%rsp), %bpl
movl %r15d, %r9d
movl %r14d, %r8d
movq 0x359e(%rip), %r10      # 0x4fd0
movb 0x8(%r10,%r13,8), %al
xorb %r11b, %al
movzbl %al, %eax
movl %r12d, %r14d
movl %r11d, %edi
movq 0x35a6(%rip), %r11      # 0x4ff0
xorb (%r11,%rax), %r14b
movb %r14b, 0x12(%rsp)
movb 0x9(%r10,%r13,8), %al
xorb %r8b, %al
movzbl %al, %eax
movl %ebx, %r15d
xorb (%r11,%rax), %r15b
movb %r15b, 0x16(%rsp)
movb 0xa(%r10,%r13,8), %al
xorb 0xd(%rsp), %al
movzbl %al, %eax
xorb (%r11,%rax), %bpl
movb %bpl, 0x10(%rsp)
movb 0xb(%r10,%r13,8), %al
xorb %sil, %al
movzbl %al, %eax
xorb (%r11,%rax), %r9b
movb %r9b, 0x14(%rsp)
movb 0xc(%r10,%r13,8), %al
xorb 0xc(%rsp), %al
movzbl %al, %eax
movb 0x5(%rsp), %bl
xorb (%r11,%rax), %bl
movb %bl, 0x1a(%rsp)
movb 0xd(%r10,%r13,8), %al
xorb 0x6(%rsp), %al
movzbl %al, %eax
movl %edx, %r12d
xorb (%r11,%rax), %r12b
movb %r12b, 0x1e(%rsp)
movb 0xe(%r10,%r13,8), %al
xorb 0x7(%rsp), %al
movzbl %al, %eax
movb 0xa(%rsp), %dl
xorb (%r11,%rax), %dl
movl %edx, %ecx
movb %dl, 0x18(%rsp)
movb 0xf(%r10,%r13,8), %al
xorb 0x8(%rsp), %al
movzbl %al, %eax
movb 0x9(%rsp), %dl
xorb (%r11,%rax), %dl
movl %edx, %r10d
movb %dl, 0x1c(%rsp)
cmpq $0x22, %r13
je 0x1bf7
movb %r14b, 0x10(%rsp)
movb %r12b, 0x12(%rsp)
movb %r10b, 0x1e(%rsp)
movb %bpl, 0x1c(%rsp)
movb %r8b, 0x2f(%rsp)
movb %r8b, 0x11(%rsp)
movb 0x8(%rsp), %al
movb %al, 0x1b(%rsp)
movb 0x7(%rsp), %al
movb %al, 0x1f(%rsp)
movb %dil, 0x15(%rsp)
movb %sil, 0x13(%rsp)
movb 0x6(%rsp), %al
movb %al, 0x19(%rsp)
movb 0xc(%rsp), %dl
movb %dl, 0x1d(%rsp)
movb 0xd(%rsp), %r11b
movb %r11b, 0x17(%rsp)
movb %r15b, 0x14(%rsp)
movb %bl, 0x16(%rsp)
movb %cl, 0x1a(%rsp)
movb %r9b, 0x18(%rsp)
movq 0x346b(%rip), %rax      # 0x4fd8
cmpl $0x0, (%rax)
movl %edi, %r8d
je 0x1bd0
leaq 0x1(%r13), %rsi
leaq 0x15a5(%rip), %rdi      # 0x3125
xorl %eax, %eax
movb %cl, 0xa(%rsp)
movb %r10b, 0x9(%rsp)
movb %r9b, 0x5(%rsp)
movb %bpl, 0xe(%rsp)
movb %r8b, 0xb(%rsp)
movl %r11d, %ebp
callq 0x1030
leaq 0x10(%rsp), %rdi
movl $0x10, %esi
callq 0x10d0
movl %ebp, %r11d
movb 0xb(%rsp), %r8b
movb 0xc(%rsp), %dl
movb 0xe(%rsp), %bpl
movb 0x5(%rsp), %r9b
movb 0x9(%rsp), %r10b
movb 0xa(%rsp), %cl
addq $0x2, %r13
movb %r10b, 0xb(%rsp)
movb %bpl, 0x9(%rsp)
movb %cl, 0x5(%rsp)
movb %r9b, 0xa(%rsp)
movl %r15d, %r9d
movl %r14d, %ebp
movb 0x2f(%rsp), %r15b
jmp 0x1884
shlb $0x4, %dil
xorb %dil, %r14b
movq 0x40(%rsp), %rax
movb %r14b, (%rax)
shlb $0x4, %r8b
xorb %r8b, %r15b
movb %r15b, 0x1(%rax)
movb 0xd(%rsp), %dl
shlb $0x4, %dl
xorb %dl, %bpl
movb %bpl, 0x2(%rax)
shlb $0x4, %sil
xorb %sil, %r9b
movb %r9b, 0x3(%rax)
movb 0xc(%rsp), %dl
shlb $0x4, %dl
xorb %dl, %bl
movb %bl, 0x4(%rax)
movb 0x6(%rsp), %dl
shlb $0x4, %dl
xorb %dl, %r12b
movb %r12b, 0x5(%rax)
movb 0x7(%rsp), %dl
shlb $0x4, %dl
xorb %dl, %cl
movb %cl, 0x6(%rax)
movb 0x8(%rsp), %cl
shlb $0x4, %cl
xorb %cl, %r10b
movb %r10b, 0x7(%rax)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq