// File: /workspace/github_c_makefile_star2_O2/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
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 0x17e0(%rip), %ymm1, %ymm1 # 0x3000
vpmovwb %ymm1, %xmm2
movq %rsi, 0x60(%rsp)
vpbroadcastb 0x17ec(%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 0x3782(%rip), %rax      # 0x4fd8
movl (%rax), %eax
movl %eax, 0x50(%rsp)
vmovd %xmm1, %r9d
vpextrb $0x1, %xmm0, %eax
movl %eax, 0x58(%rsp)
vpextrb $0x2, %xmm2, %r12d
vpextrb $0x3, %xmm0, %ebx
vpextrb $0x4, %xmm2, %eax
movl %eax, 0x10(%rsp)
vpextrb $0x5, %xmm0, %edx
vpextrb $0x6, %xmm2, %ebp
vpextrb $0x7, %xmm0, %ecx
movl %ecx, 0x2c(%rsp)
vpextrb $0x8, %xmm2, %eax
movl %eax, 0x8(%rsp)
vpextrb $0x9, %xmm0, %ecx
movl %ecx, 0x54(%rsp)
vpextrb $0xa, %xmm2, %eax
movl %eax, 0x24(%rsp)
vpextrb $0xb, %xmm0, %eax
movl %eax, 0x18(%rsp)
vpextrb $0xc, %xmm2, %ecx
movl %ecx, 0xc(%rsp)
vpextrb $0xd, %xmm0, %ecx
movl %ecx, 0x28(%rsp)
vpextrb $0xe, %xmm2, %eax
movl %eax, 0x14(%rsp)
vpextrb $0xf, %xmm0, %eax
movl %eax, 0x1c(%rsp)
xorl %r13d, %r13d
movq 0x36df(%rip), %r11      # 0x4fd0
movb (%r11,%r13,8), %cl
xorb %r9b, %cl
movzbl %cl, %ecx
movl 0x58(%rsp), %r15d
movq 0x36e9(%rip), %rax      # 0x4ff0
xorb (%rax,%rcx), %r15b
movl 0x2c(%rsp), %ecx
movl %ecx, %esi
movb 0x1(%r11,%r13,8), %cl
xorb %r12b, %cl
movzbl %cl, %ecx
xorb (%rax,%rcx), %bl
movl %ebx, 0x2c(%rsp)
movl 0x1c(%rsp), %ecx
movl %edx, %r10d
movb 0x2(%r11,%r13,8), %dl
xorb 0x10(%rsp), %dl
movzbl %dl, %edx
xorb (%rax,%rdx), %r10b
movl %r10d, 0x1c(%rsp)
movl 0x28(%rsp), %edx
movl %esi, %r10d
movb 0x3(%r11,%r13,8), %sil
xorb %bpl, %sil
movzbl %sil, %esi
xorb (%rax,%rsi), %r10b
movl %r10d, 0x28(%rsp)
movb 0x4(%r11,%r13,8), %sil
xorb 0x8(%rsp), %sil
movzbl %sil, %esi
movl 0x54(%rsp), %ebx
xorb (%rax,%rsi), %bl
movl %ebx, 0x20(%rsp)
movb 0x5(%r11,%r13,8), %sil
xorb 0x24(%rsp), %sil
movzbl %sil, %esi
movl 0x18(%rsp), %r10d
movl %r10d, %r14d
xorb (%rax,%rsi), %r14b
movb 0x6(%r11,%r13,8), %sil
xorb 0xc(%rsp), %sil
movzbl %sil, %esi
xorb (%rax,%rsi), %dl
movl %edx, %esi
movb 0x7(%r11,%r13,8), %dl
xorb 0x14(%rsp), %dl
movzbl %dl, %edx
xorb (%rax,%rdx), %cl
movl %ecx, 0x18(%rsp)
cmpl $0x0, 0x50(%rsp)
movl %esi, 0x54(%rsp)
movl %r15d, 0x5c(%rsp)
je 0x1a66
movb %r15b, 0x40(%rsp)
movb %r12b, 0x41(%rsp)
movb %r14b, 0x42(%rsp)
movb %bpl, 0x43(%rsp)
movl 0x2c(%rsp), %eax
movb %al, 0x44(%rsp)
movb %r9b, 0x45(%rsp)
movl 0x20(%rsp), %eax
movb %al, 0x46(%rsp)
movl 0x10(%rsp), %eax
movb %al, 0x47(%rsp)
movl 0x28(%rsp), %eax
movb %al, 0x48(%rsp)
movl 0x24(%rsp), %eax
movb %al, 0x49(%rsp)
movb %sil, 0x4a(%rsp)
movl 0x14(%rsp), %eax
movb %al, 0x4b(%rsp)
movl 0x1c(%rsp), %eax
movb %al, 0x4c(%rsp)
movl 0x8(%rsp), %eax
movb %al, 0x4d(%rsp)
movl 0x18(%rsp), %eax
movb %al, 0x4e(%rsp)
movl 0xc(%rsp), %eax
movb %al, 0x4f(%rsp)
leaq 0x16a8(%rip), %rdi      # 0x30e5
movl %r13d, %esi
xorl %eax, %eax
movl %r9d, %ebx
vzeroupper
callq 0x1030
leaq 0x40(%rsp), %rdi
pushq $0x10
popq %rsi
callq 0x10d0
movl 0x54(%rsp), %esi
movl %ebx, %r9d
movl 0x5c(%rsp), %r15d
movq 0x3563(%rip), %rcx      # 0x4fd0
movb 0x8(%rcx,%r13,8), %al
xorb %r15b, %al
movl %r15d, %r11d
movl %r12d, %r15d
movzbl %al, %eax
movq 0x356b(%rip), %rdx      # 0x4ff0
xorb (%rdx,%rax), %r15b
movb 0x9(%rcx,%r13,8), %al
xorb %r14b, %al
movl %r14d, %r10d
movl %ebp, %ebx
movzbl %al, %eax
xorb (%rdx,%rax), %bl
movb 0xa(%rcx,%r13,8), %al
movl 0x2c(%rsp), %r14d
xorb %r14b, %al
movzbl %al, %eax
xorb (%rdx,%rax), %r9b
movb 0xb(%rcx,%r13,8), %al
xorb 0x20(%rsp), %al
movzbl %al, %eax
movl 0x10(%rsp), %edi
xorb (%rdx,%rax), %dil
movl %edi, 0x10(%rsp)
movb 0xc(%rcx,%r13,8), %al
movl %esi, %r12d
movl 0x28(%rsp), %esi
xorb %sil, %al
movzbl %al, %eax
movl 0x24(%rsp), %ebp
xorb (%rdx,%rax), %bpl
movb 0xd(%rcx,%r13,8), %al
movl %r12d, %edi
xorb %r12b, %al
movzbl %al, %eax
movl 0x14(%rsp), %r12d
movl %r12d, %r8d
xorb (%rdx,%rax), %r8b
movb 0xe(%rcx,%r13,8), %al
xorb 0x1c(%rsp), %al
movzbl %al, %eax
movl 0x8(%rsp), %r12d
xorb (%rdx,%rax), %r12b
movl %r12d, 0x8(%rsp)
movb 0xf(%rcx,%r13,8), %al
xorb 0x18(%rsp), %al
movzbl %al, %eax
movl 0xc(%rsp), %ecx
xorb (%rdx,%rax), %cl
movl %ecx, 0xc(%rsp)
cmpq $0x22, %r13
movl %esi, %edx
je 0x1c2a
movb %r15b, 0x30(%rsp)
movb %r8b, 0x32(%rsp)
movl 0xc(%rsp), %eax
movb %al, 0x3e(%rsp)
movb %r9b, 0x3c(%rsp)
movl %r10d, 0x58(%rsp)
movb %r10b, 0x31(%rsp)
movl 0x18(%rsp), %eax
movb %al, 0x3b(%rsp)
movl 0x1c(%rsp), %eax
movb %al, 0x3f(%rsp)
movb %r11b, 0x35(%rsp)
movl 0x20(%rsp), %eax
movb %al, 0x33(%rsp)
movb %dil, 0x39(%rsp)
movb %dl, 0x3d(%rsp)
movb %r14b, 0x37(%rsp)
movb %bl, 0x34(%rsp)
movb %bpl, 0x36(%rsp)
movl 0x8(%rsp), %eax
movb %al, 0x3a(%rsp)
movl 0x10(%rsp), %eax
movb %al, 0x38(%rsp)
movq 0x3433(%rip), %rax      # 0x4fd8
cmpl $0x0, (%rax)
movl %r8d, %r12d
je 0x1be9
leaq 0x1(%r13), %rsi
leaq 0x152d(%rip), %rdi      # 0x30e5
xorl %eax, %eax
movl %r9d, 0x14(%rsp)
vzeroupper
callq 0x1030
leaq 0x30(%rsp), %rdi
pushq $0x10
popq %rsi
callq 0x10d0
movq 0x33fd(%rip), %r11      # 0x4fd8
movl 0x14(%rsp), %r9d
movl (%r11), %eax
movl %eax, 0x50(%rsp)
jmp 0x1bf1
movl $0x0, 0x50(%rsp)
addq $0x2, %r13
movl 0xc(%rsp), %ecx
movl %ecx, 0x14(%rsp)
movl %r9d, 0xc(%rsp)
movl 0x8(%rsp), %edi
movl %edi, 0x24(%rsp)
movl 0x10(%rsp), %r8d
movl %r8d, 0x8(%rsp)
movl %ebx, %eax
movl %eax, 0x10(%rsp)
movl %r15d, %r9d
movl 0x20(%rsp), %ebx
movl 0x5c(%rsp), %edx
jmp 0x18ea
shlb $0x4, %r11b
xorb %r11b, %r15b
shlb $0x4, %r10b
xorb %r10b, %bl
shlb $0x4, %r14b
xorb %r14b, %r9b
movq 0x60(%rsp), %rax
movb %r15b, (%rax)
movb %bl, 0x1(%rax)
movb %r9b, 0x2(%rax)
movl 0x20(%rsp), %ecx
shlb $0x4, %cl
movl 0x10(%rsp), %esi
xorb %cl, %sil
movb %sil, 0x3(%rax)
shlb $0x4, %dl
xorb %dl, %bpl
movb %bpl, 0x4(%rax)
shlb $0x4, %dil
xorb %dil, %r8b
movb %r8b, 0x5(%rax)
movl 0x1c(%rsp), %ecx
shlb $0x4, %cl
movl 0x8(%rsp), %edx
xorb %cl, %dl
movb %dl, 0x6(%rax)
movl 0x18(%rsp), %edx
shlb $0x4, %dl
movl 0xc(%rsp), %ecx
xorb %dl, %cl
movb %cl, 0x7(%rax)
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
vzeroupper
retq