// File: /workspace/github_c_makefile_star2_O1/mjosaarinen[P]brutus/crypto_aead/scream12v3/ref/scream_cipher.c
// Function: LS_decrypt
void LS_decrypt(const crypto_uint8 input[16], const crypto_uint8 key[16], const crypto_uint8 tweak[16],
		crypto_uint8 output[16]) {
  crypto_uint16 data[8];
  crypto_uint16 k[8];
  crypto_uint16 t[8];
  int i,j;

  for (i=0; i<8; i++) {
    data[i] = ((crypto_uint16)input[2*i+1]<<8) + input[2*i];
    k[i] = ((crypto_uint16)  key[2*i+1]<<8) +   key[2*i];
    t[i] = ((crypto_uint16)tweak[2*i+1]<<8) + tweak[2*i];
  }

  /* ---------------- Step Function ---------------- */
  for (i=nSteps-1; i>=0; i--) {
    /* ----------- Key + Tweak Addition ---------- */
    for (j=0; j<8; j++) data[j] ^= k[j];
    switch(i%3) {
    case     0:
      for (j=0; j<4; j++) data[j] ^= t[j] ^ t[j+4];
      for (j=4; j<8; j++) data[j] ^= t[j-4];
      break;
    case     1:
      for (j=0; j<4; j++) data[j] ^= t[j+4];
      for (j=4; j<8; j++) data[j] ^= t[j] ^ t[j-4];
      break;
    case     2:
      for (j=0; j<8; j++) data[j] ^= t[j] ;
      break;
    }

    /* ---------------- Second Round -------------- */
    /* LBox layer (tables) */
    for (j=0; j<8; j++)
      data[j] = LBoxInv2[data[j]>>8] ^ LBoxInv1[data[j]&0xff];
    /* Round constant */
    data[0] ^= ((2199*(2*i+1)) % 65536);
    /* SBox layer (bitsliced) */
    SBOX_Inv(data);
        
    /* ---------------- First Round ---------------- */
    /* LBox layer (tables) */
    for (j=0; j<8; j++)
      data[j] = LBoxInv2[data[j]>>8] ^ LBoxInv1[data[j]&0xff];
    /* First round constant */
    data[0] ^= ((2199*(2*i)) % 65536);
    /* SBox layer (bitsliced) */
    SBOX_Inv(data);
  }

  /* Final key + tweak addition */
  for (j=0; j<8; j++) data[j] ^= k[j] ^ t[j];
    
  for (i=0; i<8; i++) {
    output[2*i]   = data[i];
    output[2*i+1] = data[i]>>8;
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rcx, -0x8(%rsp)
vmovdqu (%rdi), %xmm5
vpbroadcastw 0x198c(%rip), %xmm2 # 0x3002
vmovdqu (%rsi), %xmm0
vpternlogd $0xb8, %xmm5, %xmm2, %xmm5
vpternlogd $0xb8, %xmm0, %xmm2, %xmm0
vmovdqu (%rdx), %xmm1
vpternlogd $0xb8, %xmm1, %xmm2, %xmm1
vpshufd $0xee, %xmm1, %xmm2     # xmm2 = xmm1[2,3,2,3]
vpxor %xmm1, %xmm2, %xmm3
movl $0xb, %eax
movl $0xce28, %r14d          # imm = 0xCE28
vpbroadcastd 0x2160(%rip), %ymm4 # 0x3810
leaq 0x1d59(%rip), %rsi      # 0x3410
leaq 0x1f52(%rip), %rdi      # 0x3610
movq 0x3933(%rip), %r11      # 0x4ff8
movl %eax, %ecx
movl $0xaaaaaaab, %edx       # imm = 0xAAAAAAAB
imulq %rdx, %rcx
shrq $0x21, %rcx
leal (%rcx,%rcx,2), %edx
vpxor %xmm0, %xmm5, %xmm5
movl %eax, %ecx
subl %edx, %ecx
je 0x16fc
cmpl $0x1, %ecx
jne 0x170c
vpxor %xmm2, %xmm5, %xmm6
vpshufd $0xee, %xmm5, %xmm5     # xmm5 = xmm5[2,3,2,3]
vpternlogq $0x96, %xmm2, %xmm1, %xmm5
vpunpcklqdq %xmm5, %xmm6, %xmm5 # xmm5 = xmm6[0],xmm5[0]
jmp 0x1710
vpunpcklqdq %xmm1, %xmm5, %xmm6 # xmm6 = xmm5[0],xmm1[0]
vpblendd $0xc, %xmm5, %xmm3, %xmm5 # xmm5 = xmm3[0,1],xmm5[2,3]
vpxor %xmm6, %xmm5, %xmm5
jmp 0x1710
vpxor %xmm1, %xmm5, %xmm5
vpmovzxwd %xmm5, %ymm5    # ymm5 = xmm5[0],zero,xmm5[1],zero,xmm5[2],zero,xmm5[3],zero,xmm5[4],zero,xmm5[5],zero,xmm5[6],zero,xmm5[7],zero
vpsrld $0x8, %ymm5, %ymm6
vextracti128 $0x1, %ymm6, %xmm7
vpmovzxdq %xmm7, %ymm7    # ymm7 = xmm7[0],zero,xmm7[1],zero,xmm7[2],zero,xmm7[3],zero
vpmovzxdq %xmm6, %ymm6    # ymm6 = xmm6[0],zero,xmm6[1],zero,xmm6[2],zero,xmm6[3],zero
vpextrd $0x2, %xmm6, %ecx
vmovd %xmm6, %edx
vextracti128 $0x1, %ymm6, %xmm6
vmovd %xmm6, %r8d
vpextrd $0x2, %xmm6, %r9d
vmovd %xmm7, %ebx
vpextrd $0x2, %xmm7, %r15d
vextracti128 $0x1, %ymm7, %xmm6
vmovd %xmm6, %r12d
vpextrd $0x2, %xmm6, %r13d
movzwl (%rsi,%rdx,2), %edx
vmovd %edx, %xmm6
vpinsrw $0x1, (%rsi,%rcx,2), %xmm6, %xmm6
vpinsrw $0x2, (%rsi,%r8,2), %xmm6, %xmm6
vpinsrw $0x3, (%rsi,%r9,2), %xmm6, %xmm6
vpinsrw $0x4, (%rsi,%rbx,2), %xmm6, %xmm6
vpinsrw $0x5, (%rsi,%r15,2), %xmm6, %xmm6
vpinsrw $0x6, (%rsi,%r12,2), %xmm6, %xmm6
vpinsrw $0x7, (%rsi,%r13,2), %xmm6, %xmm6
vpand %ymm4, %ymm5, %ymm5
vextracti128 $0x1, %ymm5, %xmm7
vpmovzxdq %xmm7, %ymm7    # ymm7 = xmm7[0],zero,xmm7[1],zero,xmm7[2],zero,xmm7[3],zero
vpmovzxdq %xmm5, %ymm5    # ymm5 = xmm5[0],zero,xmm5[1],zero,xmm5[2],zero,xmm5[3],zero
vmovd %xmm5, %ecx
vpextrd $0x2, %xmm5, %edx
vextracti128 $0x1, %ymm5, %xmm5
vmovd %xmm5, %r8d
vpextrd $0x2, %xmm5, %r9d
vmovd %xmm7, %ebx
vpextrd $0x2, %xmm7, %r15d
vextracti128 $0x1, %ymm7, %xmm5
vmovd %xmm5, %r12d
vpextrd $0x2, %xmm5, %r13d
movzwl (%rdi,%rcx,2), %ecx
vmovd %ecx, %xmm5
vpinsrw $0x1, (%rdi,%rdx,2), %xmm5, %xmm5
vpinsrw $0x2, (%rdi,%r8,2), %xmm5, %xmm5
vpinsrw $0x3, (%rdi,%r9,2), %xmm5, %xmm5
vpinsrw $0x4, (%rdi,%rbx,2), %xmm5, %xmm5
vpinsrw $0x5, (%rdi,%r15,2), %xmm5, %xmm5
vpinsrw $0x6, (%rdi,%r12,2), %xmm5, %xmm5
vpinsrw $0x7, (%rdi,%r13,2), %xmm5, %xmm5
vpxor %xmm6, %xmm5, %xmm5
vmovd %xmm5, %ecx
leal -0x112e(%r14), %ebx
addl $0xfffff769, %r14d      # imm = 0xFFFFF769
xorl %ecx, %r14d
vpextrw $0x1, %xmm5, %r12d
vpextrw $0x2, %xmm5, %ebp
movl %ebp, %ecx
andl %r12d, %ecx
xorl %r14d, %ecx
vpextrw $0x3, %xmm5, %r10d
andnl %r10d, %ecx, %r8d
movl %ecx, %r9d
notl %r9d
movl %r10d, %r15d
xorl %r12d, %r15d
xorl %ebp, %r9d
vpextrw $0x5, %xmm5, %r13d
xorl %r9d, %r13d
vpextrw $0x6, %xmm5, %edx
xorl %r8d, %edx
movl %r9d, %r8d
andl %r15d, %r8d
vpextrw $0x7, %xmm5, %ecx
xorl %r8d, %ecx
xorl %r10d, %r9d
xorl %ebp, %r15d
andl %r9d, %r15d
vpextrw $0x4, %xmm5, %r8d
xorl %r15d, %r8d
movl %r8d, %r9d
andl %r13d, %r9d
xorl %edx, %r9d
xorl %r9d, %r14d
movl %edx, %r15d
orl %r13d, %r15d
xorl %ecx, %r15d
xorl %r15d, %ebp
andl %ecx, %r9d
xorl %r8d, %r12d
xorl %r9d, %r12d
andl %r8d, %r15d
xorl %r13d, %r10d
xorl %r15d, %r10d
movl %r12d, %r9d
vmovd %r14d, %xmm5
vpinsrw $0x1, %r12d, %xmm5, %xmm5
andl %ebp, %r12d
xorl %r14d, %r12d
xorl %r10d, %r9d
movl %r12d, %r14d
xorl %ebp, %r14d
xorl %r14d, %r13d
andl %r10d, %r12d
xorl %edx, %r12d
movl %r14d, %edx
andl %r9d, %edx
xorl %r10d, %r14d
xorl %ebp, %r9d
andl %r14d, %r9d
xorl %r8d, %r9d
vpinsrw $0x2, %ebp, %xmm5, %xmm5
vpinsrw $0x3, %r10d, %xmm5, %xmm5
vpinsrw $0x4, %r9d, %xmm5, %xmm5
vpinsrw $0x5, %r13d, %xmm5, %xmm5
vpinsrw $0x6, %r12d, %xmm5, %xmm5
xorl %ecx, %edx
vpinsrw $0x7, %edx, %xmm5, %xmm5
vpmovzxwd %xmm5, %ymm5    # ymm5 = xmm5[0],zero,xmm5[1],zero,xmm5[2],zero,xmm5[3],zero,xmm5[4],zero,xmm5[5],zero,xmm5[6],zero,xmm5[7],zero
vpsrld $0x8, %ymm5, %ymm6
vextracti128 $0x1, %ymm6, %xmm7
vpmovzxdq %xmm7, %ymm7    # ymm7 = xmm7[0],zero,xmm7[1],zero,xmm7[2],zero,xmm7[3],zero
vpmovzxdq %xmm6, %ymm6    # ymm6 = xmm6[0],zero,xmm6[1],zero,xmm6[2],zero,xmm6[3],zero
vmovd %xmm6, %ecx
vpextrd $0x2, %xmm6, %edx
vextracti128 $0x1, %ymm6, %xmm6
vmovd %xmm6, %r8d
vpextrd $0x2, %xmm6, %r9d
vmovd %xmm7, %r10d
vpextrd $0x2, %xmm7, %r14d
vextracti128 $0x1, %ymm7, %xmm6
vmovd %xmm6, %r15d
vpextrd $0x2, %xmm6, %r12d
movzwl (%rsi,%rcx,2), %ecx
vmovd %ecx, %xmm6
vpinsrw $0x1, (%rsi,%rdx,2), %xmm6, %xmm6
vpinsrw $0x2, (%rsi,%r8,2), %xmm6, %xmm6
vpinsrw $0x3, (%rsi,%r9,2), %xmm6, %xmm6
vpinsrw $0x4, (%rsi,%r10,2), %xmm6, %xmm6
vpinsrw $0x5, (%rsi,%r14,2), %xmm6, %xmm6
vpinsrw $0x6, (%rsi,%r15,2), %xmm6, %xmm6
vpinsrw $0x7, (%rsi,%r12,2), %xmm6, %xmm6
vpand %ymm4, %ymm5, %ymm5
vextracti128 $0x1, %ymm5, %xmm7
vpmovzxdq %xmm7, %ymm7    # ymm7 = xmm7[0],zero,xmm7[1],zero,xmm7[2],zero,xmm7[3],zero
vpmovzxdq %xmm5, %ymm5    # ymm5 = xmm5[0],zero,xmm5[1],zero,xmm5[2],zero,xmm5[3],zero
vmovd %xmm5, %ecx
vpextrd $0x2, %xmm5, %edx
vextracti128 $0x1, %ymm5, %xmm5
vpextrd $0x2, %xmm5, %r8d
vmovd %xmm5, %r9d
vmovd %xmm7, %r10d
vpextrd $0x2, %xmm7, %r14d
vextracti128 $0x1, %ymm7, %xmm5
vmovd %xmm5, %r15d
vpextrd $0x2, %xmm5, %r12d
movzwl (%rdi,%rcx,2), %ecx
vmovd %ecx, %xmm5
vpinsrw $0x1, (%rdi,%rdx,2), %xmm5, %xmm5
vpinsrw $0x2, (%rdi,%r9,2), %xmm5, %xmm5
vpinsrw $0x3, (%rdi,%r8,2), %xmm5, %xmm5
vpinsrw $0x4, (%rdi,%r10,2), %xmm5, %xmm5
vpinsrw $0x5, (%rdi,%r14,2), %xmm5, %xmm5
vpinsrw $0x6, (%rdi,%r15,2), %xmm5, %xmm5
vpinsrw $0x7, (%rdi,%r12,2), %xmm5, %xmm5
vpxor %xmm6, %xmm5, %xmm5
vmovd %xmm5, %r15d
xorl %ebx, %r15d
vpextrw $0x1, %xmm5, %r9d
vpextrw $0x2, %xmm5, %ebp
movl %ebp, %ecx
andl %r9d, %ecx
xorl %r15d, %ecx
vpextrw $0x3, %xmm5, %r14d
andnl %r14d, %ecx, %edx
movl %ecx, %r8d
notl %r8d
movl %r14d, %r10d
xorl %r9d, %r10d
xorl %ebp, %r8d
vpextrw $0x5, %xmm5, %r12d
xorl %r8d, %r12d
vpextrw $0x6, %xmm5, %r13d
xorl %edx, %r13d
movl %r8d, %ecx
vpextrw $0x7, %xmm5, %edx
andl %r10d, %ecx
xorl %ecx, %edx
xorl %r14d, %r8d
xorl %ebp, %r10d
vpextrw $0x4, %xmm5, %ecx
andl %r8d, %r10d
xorl %r10d, %ecx
movl %ecx, %r8d
andl %r12d, %r8d
xorl %r13d, %r8d
xorl %r8d, %r15d
andl %edx, %r8d
xorl %ecx, %r8d
movw %r8w, (%r11)
xorl %r9d, %r8d
movl %r13d, %r9d
orl %r12d, %r9d
xorl %edx, %r9d
xorl %r9d, %ebp
andl %ecx, %r9d
xorl %r12d, %r14d
xorl %r9d, %r14d
movl %r8d, %r9d
vmovd %r15d, %xmm5
vpinsrw $0x1, %r8d, %xmm5, %xmm5
andl %ebp, %r8d
xorl %r15d, %r8d
movq 0x3525(%rip), %r10      # 0x4ff0
movw %r8w, (%r10)
xorl %r14d, %r9d
movl %r8d, %r10d
xorl %ebp, %r10d
andl %r14d, %r8d
xorl %r13d, %r8d
movl %r10d, %r15d
andl %r9d, %r15d
xorl %edx, %r15d
movq 0x34f2(%rip), %rdx      # 0x4fe0
movw %r9w, (%rdx)
movq 0x34c7(%rip), %rdx      # 0x4fc0
movw %r10w, (%rdx)
xorl %r10d, %r12d
xorl %r14d, %r10d
xorl %ebp, %r9d
andl %r10d, %r9d
xorl %ecx, %r9d
vpinsrw $0x2, %ebp, %xmm5, %xmm5
vpinsrw $0x3, %r14d, %xmm5, %xmm5
vpinsrw $0x4, %r9d, %xmm5, %xmm5
vpinsrw $0x5, %r12d, %xmm5, %xmm5
vpinsrw $0x6, %r8d, %xmm5, %xmm5
vpinsrw $0x7, %r15d, %xmm5, %xmm5
decl %eax
movl %ebx, %r14d
testl %ebx, %ebx
jne 0x16c5
vpternlogq $0x96, %xmm0, %xmm1, %xmm5
vpsrlw $0x8, %xmm5, %xmm0
vpunpckhwd %xmm0, %xmm5, %xmm1 # xmm1 = xmm5[4],xmm0[4],xmm5[5],xmm0[5],xmm5[6],xmm0[6],xmm5[7],xmm0[7]
vpunpcklwd %xmm0, %xmm5, %xmm0 # xmm0 = xmm5[0],xmm0[0],xmm5[1],xmm0[1],xmm5[2],xmm0[2],xmm5[3],xmm0[3]
vinserti128 $0x1, %xmm1, %ymm0, %ymm0
movq -0x8(%rsp), %rax
vpmovwb %ymm0, (%rax)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
vzeroupper
retq
nop