// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/acorn128v2/opt/encrypt.c
// Function: encrypt_8bits
void encrypt_8bits(unsigned long long *state, unsigned int plaintextword, unsigned int *ciphertextword, unsigned int *ks, unsigned int ca, unsigned int cb)
{
    unsigned long long f;
    unsigned long long word_244, word_23, word_160, word_111, word_66, word_196, word_0, word_107, word_230;
    unsigned long long word_12,word_154,word_235,word_61,word_193;


    //f  = state[0] ^ (state[107] ^ 1) ^ maj(state[244], state[23], state[160]) ^ ch(state[230], state[111], state[66]) ^ (ca & state[196]) ^ (cb & (*ks));
    word_12  = state[0] >> 12;
    word_235 = state[5] >> 5;
    word_244 = state[5] >> 14;
    word_23  = state[0] >> 23;
    word_160 = state[3] >> 6;
    word_111 = state[2] >> 4;
    word_66  = state[1] >> 5;
    word_196 = state[4] >> 3;

	state[6] ^= (state[5] ^ word_235) & 0xff;
	state[5] ^= (state[4] ^ word_196) & 0xff;
	state[4] ^= (state[3] ^ word_160) & 0xff;
	state[3] ^= (state[2] ^ word_111) & 0xff;
	state[2] ^= (state[1] ^ word_66)  & 0xff;
	state[1] ^= (state[0] ^ word_23)  & 0xff;

	*ks = word_12 ^ state[3] ^ maj(word_235, state[1], state[4]);
    *ks &= 0xff;
    //f  = state[0] ^ (state[107] ^ 1) ^ maj(state[244], state[23], state[160]) ^ ch(state[230], state[111], state[66]) ^ state[196];
	f = state[0] ^ (state[2] ^ 0xffffffff) ^ maj(word_244, word_23, word_160) ^ ch32(state[5], word_111, word_66) ^ (word_196 & ca) ^ (cb & *ks);
    f  = (f ^ plaintextword) & 0xff;
    state[6] =  state[6] ^ (f << 4);

    state[0] = (state[0] >> 8) | ((state[1] & 0xff) << (29+24));   //32-(64-61) = 29
    state[1] = (state[1] >> 8) | ((state[2] & 0xff) << (14+24));   //32-(64-46) = 14
    state[2] = (state[2] >> 8) | ((state[3] & 0xff) << (15+24));   //32-(64-47) = 15
    state[3] = (state[3] >> 8) | ((state[4] & 0xff) << (7+24));    //32-(64-39) = 7
    state[4] = (state[4] >> 8) | ((state[5] & 0xff) << (5+24));    //32-(64-37) = 5
    state[5] = (state[5] >> 8) | ((state[6] & 0xff) << (27+24));    //32-(64-59) = 27
    state[6] =  state[6] >> 8;

    *ciphertextword = plaintextword ^ *ks;
    return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movl %r9d, -0x4(%rsp)
movq %rcx, %r9
vmovdqu (%rdi), %ymm0
movq (%rdi), %r11
movq 0x20(%rdi), %r10
movq %r11, %rcx
shrq $0xc, %rcx
vpsrlvq 0xcf7(%rip), %ymm0, %ymm2 # 0x2000
vpxor %ymm0, %ymm2, %ymm4
vmovq %xmm2, %rbx
vmovdqu 0x20(%rdi), %xmm1
valignq $0x1, %ymm0, %ymm1, %ymm3 # ymm3 = ymm0[1,2,3],ymm1[0]
vpternlogq $0x78, 0xdd7(%rip){1to4}, %ymm4, %ymm3 # 0x2100
vextracti128 $0x1, %ymm3, %xmm4
vpextrq $0x1, %xmm4, %r13
vmovq %xmm4, %r14
vextracti128 $0x1, %ymm2, %xmm4
vpextrq $0x1, %xmm4, %r15
movl %r15d, %r12d
xorl %ebx, %r12d
andl %ebx, %r15d
movq %r10, %rbx
shrq $0x3, %rbx
xorl %ebx, %r10d
andl %ebx, %r8d
vmovq %xmm3, %rbx
movl %r13d, %ebp
xorl %ebx, %ebp
andl %ebx, %r13d
movq 0x28(%rdi), %rax
xorl %r13d, %ecx
movq %rax, %rbx
shrq $0x5, %rbx
andl %ebx, %ebp
xorl %eax, %ebx
xorl %ebp, %ecx
movl $0xff, %ebp
andl %ebp, %ebx
xorq 0x30(%rdi), %rbx
xorl %ecx, %r14d
movl %r14d, %ecx
andl %ebp, %ecx
movl %ecx, (%r9)
andl %ebp, %r10d
xorq %rax, %r10
shrq $0xe, %rax
andl %r12d, %eax
vmovq %xmm4, %r9
xorl %esi, %r15d
xorl %r15d, %eax
vpextrq $0x1, %xmm2, %r15
andl %r10d, %r9d
andnl %r15d, %r10d, %r15d
orl %r9d, %r15d
xorl %r8d, %r15d
xorl %eax, %r15d
andl -0x4(%rsp), %r14d
vpextrq $0x1, %xmm3, %rax
xorl %r15d, %eax
xorl %eax, %r11d
xorl %r14d, %r11d
andl %ebp, %r11d
shll $0x4, %r11d
xorq %rbx, %r11
vmovq %r10, %xmm2
vpsrlq $0x8, %ymm0, %ymm0
vpsllvq 0xc8d(%rip), %ymm3, %ymm3 # 0x2080
xorq $0xff0, %r11            # imm = 0xFF0
vpternlogq $0xec, 0xc9b(%rip), %ymm0, %ymm3 # 0x20a0
vmovdqu %ymm3, (%rdi)
vpsrlq $0x8, %xmm1, %xmm0
vmovq %r11, %xmm1
vpunpcklqdq %xmm1, %xmm2, %xmm1 # xmm1 = xmm2[0],xmm1[0]
vpsllvq 0xcc0(%rip), %xmm1, %xmm1 # 0x20e0
vpternlogq $0xec, 0xcc5(%rip), %xmm0, %xmm1 # 0x20f0
vmovdqu %xmm1, 0x20(%rdi)
shrq $0x8, %r11
movq %r11, 0x30(%rdi)
xorl %esi, %ecx
movl %ecx, (%rdx)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
vzeroupper
retq