// File: /workspace/github_c_makefile_star2_O3/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 $0x68, %rsp
movq %rsi, 0x60(%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]
vpmovzxbw %xmm0, %ymm1    # ymm1 = xmm0[0],zero,xmm0[1],zero,xmm0[2],zero,xmm0[3],zero,xmm0[4],zero,xmm0[5],zero,xmm0[6],zero,xmm0[7],zero,xmm0[8],zero,xmm0[9],zero,xmm0[10],zero,xmm0[11],zero,xmm0[12],zero,xmm0[13],zero,xmm0[14],zero,xmm0[15],zero
vpsrlvw 0x17de(%rip), %ymm1, %ymm1 # 0x3000
vpmovwb %ymm1, %xmm2
vpbroadcastb 0x17ef(%rip), %xmm3 # 0x3020
vpand %xmm3, %xmm0, %xmm0
movw $0xaaaa, %ax            # imm = 0xAAAA
kmovd %eax, %k1
vpblendmb %xmm0, %xmm2, %xmm3 {%k1}
leaq 0x30(%rsp), %rax
vmovdqa %xmm3, (%rax)
movq 0x3785(%rip), %rax      # 0x4fd8
movl (%rax), %eax
movl %eax, 0x24(%rsp)
vpextrb $0x1, %xmm0, %ebx
vpextrb $0x2, %xmm2, %r10d
vpextrb $0x3, %xmm0, %ecx
movl %ecx, 0x1c(%rsp)
vpextrb $0x4, %xmm2, %ecx
movl %ecx, 0xc(%rsp)
vpextrb $0x5, %xmm0, %edx
vpextrb $0x6, %xmm2, %ebp
vpextrb $0x7, %xmm0, %r12d
vpextrb $0x8, %xmm2, %edi
vpextrb $0x9, %xmm0, %eax
movl %eax, 0x14(%rsp)
vpextrb $0xa, %xmm2, %eax
movl %eax, 0x10(%rsp)
vpextrb $0xb, %xmm0, %eax
movl %eax, 0x20(%rsp)
vpextrb $0xc, %xmm2, %r8d
vpextrb $0xd, %xmm0, %r9d
vpextrb $0xe, %xmm2, %eax
movl %eax, 0x28(%rsp)
vpextrb $0xf, %xmm0, %eax
movl %eax, 0x18(%rsp)
vmovd %xmm1, %r15d
xorl %r13d, %r13d
movq 0x36f2(%rip), %rax      # 0x4fd0
movb (%rax,%r13,8), %cl
xorb %r15b, %cl
movzbl %cl, %ecx
movq 0x3701(%rip), %r14      # 0x4ff0
xorb (%r14,%rcx), %bl
movl %ebx, 0x2c(%rsp)
movl %r12d, %esi
movb 0x1(%rax,%r13,8), %cl
movl %r10d, %r11d
xorb %r10b, %cl
movzbl %cl, %ecx
movl 0x1c(%rsp), %r12d
xorb (%r14,%rcx), %r12b
movl 0x18(%rsp), %ecx
movl %edx, %r10d
movb 0x2(%rax,%r13,8), %dl
xorb 0xc(%rsp), %dl
movzbl %dl, %edx
xorb (%r14,%rdx), %r10b
movl %r10d, 0x18(%rsp)
movl %r9d, %edx
movl %esi, %r9d
movb 0x3(%rax,%r13,8), %sil
xorb %bpl, %sil
movzbl %sil, %esi
xorb (%r14,%rsi), %r9b
movb 0x4(%rax,%r13,8), %sil
xorb %dil, %sil
movzbl %sil, %esi
movl 0x14(%rsp), %r10d
xorb (%r14,%rsi), %r10b
movl %r10d, 0x1c(%rsp)
movb 0x5(%rax,%r13,8), %sil
xorb 0x10(%rsp), %sil
movzbl %sil, %esi
movl 0x20(%rsp), %r10d
movl %r10d, %ebx
xorb (%r14,%rsi), %bl
movb 0x6(%rax,%r13,8), %sil
xorb %r8b, %sil
movzbl %sil, %esi
xorb (%r14,%rsi), %dl
movl %edx, 0x14(%rsp)
movb 0x7(%rax,%r13,8), %dl
movl 0x28(%rsp), %esi
xorb %sil, %dl
movzbl %dl, %edx
movl %ecx, %eax
xorb (%r14,%rdx), %al
movl %eax, 0x20(%rsp)
cmpl $0x0, 0x24(%rsp)
movl %r9d, 0x5c(%rsp)
je 0x1a6e
movl 0x2c(%rsp), %eax
movb %al, 0x40(%rsp)
movb %r11b, 0x41(%rsp)
movb %bl, 0x42(%rsp)
movb %bpl, 0x43(%rsp)
movb %r12b, 0x44(%rsp)
movb %r15b, 0x45(%rsp)
movl 0x1c(%rsp), %eax
movb %al, 0x46(%rsp)
movl 0xc(%rsp), %eax
movb %al, 0x47(%rsp)
movb %r9b, 0x48(%rsp)
movl 0x10(%rsp), %eax
movb %al, 0x49(%rsp)
movl 0x14(%rsp), %eax
movb %al, 0x4a(%rsp)
movb %sil, 0x4b(%rsp)
movl 0x18(%rsp), %eax
movb %al, 0x4c(%rsp)
movb %dil, 0x4d(%rsp)
movl 0x20(%rsp), %eax
movb %al, 0x4e(%rsp)
movb %r8b, 0x4f(%rsp)
movl %edi, 0x50(%rsp)
leaq 0x16c2(%rip), %rdi      # 0x30e5
movl %r13d, %esi
xorl %eax, %eax
movl %r11d, 0x54(%rsp)
movl %r8d, %r14d
movl %ebp, 0x58(%rsp)
movl %r12d, 0x24(%rsp)
vzeroupper
callq 0x1030
leaq 0x40(%rsp), %rdi
movl $0x10, %esi
callq 0x10d0
movl 0x28(%rsp), %esi
movl 0x24(%rsp), %r12d
movl 0x5c(%rsp), %r9d
movl 0x58(%rsp), %ebp
movl %r14d, %r8d
movl 0x54(%rsp), %r11d
movl 0x50(%rsp), %edi
movq 0x355b(%rip), %r10      # 0x4fd0
movb 0x8(%r10,%r13,8), %al
xorb 0x2c(%rsp), %al
movl %r11d, %r14d
movzbl %al, %eax
movq 0x3565(%rip), %r11      # 0x4ff0
xorb (%r11,%rax), %r14b
movb 0x9(%r10,%r13,8), %al
xorb %bl, %al
movzbl %al, %eax
xorb (%r11,%rax), %bpl
movb 0xa(%r10,%r13,8), %al
xorb %r12b, %al
movzbl %al, %eax
xorb (%r11,%rax), %r15b
movb 0xb(%r10,%r13,8), %al
xorb 0x1c(%rsp), %al
movzbl %al, %eax
movl 0xc(%rsp), %ecx
xorb (%r11,%rax), %cl
movl %ecx, 0xc(%rsp)
movb 0xc(%r10,%r13,8), %al
xorb %r9b, %al
movzbl %al, %eax
movl 0x10(%rsp), %edx
xorb (%r11,%rax), %dl
movb 0xd(%r10,%r13,8), %al
xorb 0x14(%rsp), %al
movzbl %al, %eax
xorb (%r11,%rax), %sil
movb 0xe(%r10,%r13,8), %al
xorb 0x18(%rsp), %al
movzbl %al, %eax
xorb (%r11,%rax), %dil
movb 0xf(%r10,%r13,8), %al
xorb 0x20(%rsp), %al
movzbl %al, %eax
xorb (%r11,%rax), %r8b
cmpq $0x22, %r13
je 0x1c13
movb %r14b, 0x30(%rsp)
movl %esi, 0x54(%rsp)
movb %sil, 0x32(%rsp)
movb %r8b, 0x3e(%rsp)
movb %r15b, 0x3c(%rsp)
movb %bl, 0x31(%rsp)
movl 0x20(%rsp), %eax
movb %al, 0x3b(%rsp)
movl 0x18(%rsp), %eax
movb %al, 0x3f(%rsp)
movl 0x2c(%rsp), %ecx
movb %cl, 0x35(%rsp)
movl 0x1c(%rsp), %eax
movb %al, 0x33(%rsp)
movl 0x14(%rsp), %eax
movb %al, 0x39(%rsp)
movb %r9b, 0x3d(%rsp)
movb %r12b, 0x37(%rsp)
movb %bpl, 0x34(%rsp)
movl %edx, 0x58(%rsp)
movb %dl, 0x36(%rsp)
movb %dil, 0x3a(%rsp)
movl 0xc(%rsp), %eax
movb %al, 0x38(%rsp)
movq 0x3458(%rip), %rax      # 0x4fd8
cmpl $0x0, (%rax)
movl %ecx, %edx
movl %r15d, %ecx
je 0x1be0
movl %ecx, 0x28(%rsp)
leaq 0x1(%r13), %rsi
movl %edi, 0x50(%rsp)
leaq 0x1548(%rip), %rdi      # 0x30e5
xorl %eax, %eax
movl %r8d, 0x10(%rsp)
vzeroupper
callq 0x1030
leaq 0x30(%rsp), %rdi
movl $0x10, %esi
callq 0x10d0
movl 0x28(%rsp), %ecx
movl 0x2c(%rsp), %edx
movl 0x5c(%rsp), %r9d
movq 0x3409(%rip), %rax      # 0x4fd8
movl 0x10(%rsp), %r8d
movl 0x50(%rsp), %edi
movl (%rax), %eax
movl %eax, 0x24(%rsp)
jmp 0x1be8
movl $0x0, 0x24(%rsp)
addq $0x2, %r13
movl %r8d, 0x28(%rsp)
movl %ecx, %r8d
movl %edi, 0x10(%rsp)
movl 0xc(%rsp), %edi
movl %ebp, %ecx
movl %ecx, 0xc(%rsp)
movl %r14d, %r15d
movl 0x58(%rsp), %ebp
movl 0x54(%rsp), %r10d
jmp 0x18d7
movl 0x2c(%rsp), %eax
shlb $0x4, %al
xorb %al, %r14b
shlb $0x4, %bl
xorb %bl, %bpl
shlb $0x4, %r12b
xorb %r12b, %r15b
movq 0x60(%rsp), %rax
movb %r14b, (%rax)
movb %bpl, 0x1(%rax)
movb %r15b, 0x2(%rax)
movl 0x1c(%rsp), %r10d
shlb $0x4, %r10b
movl 0xc(%rsp), %ecx
xorb %r10b, %cl
movb %cl, 0x3(%rax)
shlb $0x4, %r9b
xorb %r9b, %dl
movb %dl, 0x4(%rax)
movl 0x14(%rsp), %ecx
shlb $0x4, %cl
xorb %cl, %sil
movb %sil, 0x5(%rax)
movl 0x18(%rsp), %ecx
shlb $0x4, %cl
xorb %cl, %dil
movb %dil, 0x6(%rax)
movl 0x20(%rsp), %ecx
shlb $0x4, %cl
xorb %cl, %r8b
movb %r8b, 0x7(%rax)
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
vzeroupper
retq