// File: /workspace/github_c_makefile_star2_O2/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
xorl %eax, %eax
cmpq $0x10, %rax
je 0x1524
movzwl (%rdi,%rax), %r8d
movw %r8w, -0x38(%rsp,%rax)
movzwl (%rsi,%rax), %r8d
movw %r8w, -0x18(%rsp,%rax)
movzwl (%rdx,%rax), %r8d
movw %r8w, -0x28(%rsp,%rax)
addq $0x2, %rax
jmp 0x14f7
movl $0xb, %esi
leaq 0xed0(%rip), %rbx       # 0x2400
leaq 0x10c9(%rip), %r14      # 0x2600
xorl %eax, %eax
testl %esi, %esi
jns 0x154f
jmp 0x183b
movzwl -0x18(%rsp,%rax,2), %edx
xorw %dx, -0x38(%rsp,%rax,2)
incq %rax
cmpq $0x8, %rax
jne 0x1542
movl %esi, %eax
xorl %edx, %edx
movl $0x3, %edi
divl %edi
xorl %eax, %eax
testl %edx, %edx
je 0x1596
cmpl $0x1, %edx
jne 0x15c0
cmpq $0x4, %rax
je 0x1809
movzwl -0x20(%rsp,%rax,2), %edx
xorw %dx, -0x38(%rsp,%rax,2)
incq %rax
jmp 0x156b
movzwl -0x20(%rsp,%rax,2), %edx
xorw -0x28(%rsp,%rax,2), %dx
xorw %dx, -0x38(%rsp,%rax,2)
incq %rax
cmpq $0x4, %rax
jne 0x1584
xorl %eax, %eax
cmpq $0x4, %rax
je 0x15c6
movzwl -0x28(%rsp,%rax,2), %edx
xorw %dx, -0x30(%rsp,%rax,2)
incq %rax
jmp 0x159e
movzwl -0x28(%rsp,%rax,2), %edx
xorw %dx, -0x38(%rsp,%rax,2)
incq %rax
cmpq $0x8, %rax
jne 0x15b3
xorl %eax, %eax
cmpq $0x8, %rax
je 0x15ec
movzwl -0x38(%rsp,%rax,2), %edx
movzbl %dl, %edi
shrl $0x8, %edx
movzwl (%r14,%rdi,2), %edi
xorw (%rbx,%rdx,2), %di
movw %di, -0x38(%rsp,%rax,2)
incq %rax
jmp 0x15c8
imull $0x112e, %esi, %eax     # imm = 0x112E
movl %eax, %r13d
addl $0x897, %r13d           # imm = 0x897
xorw -0x38(%rsp), %r13w
movzwl -0x36(%rsp), %edx
movl -0x34(%rsp), %ebp
movl %ebp, %r8d
andl %edx, %r8d
xorl %r13d, %r8d
movzwl -0x32(%rsp), %r15d
andnl %r15d, %r8d, %r11d
notl %r8d
movl %r15d, %r12d
xorl %edx, %r12d
xorl %ebp, %r8d
movzwl -0x2e(%rsp), %r9d
xorw %r8w, %r9w
xorw -0x2c(%rsp), %r11w
movl %r8d, %r10d
andl %r12d, %r10d
xorw -0x2a(%rsp), %r10w
xorl %r15d, %r8d
xorl %ebp, %r12d
andl %r8d, %r12d
xorw -0x30(%rsp), %r12w
movl %r12d, %r8d
andl %r9d, %r8d
xorl %r11d, %r8d
xorl %r8d, %r13d
movw %r13w, -0x38(%rsp)
movl %r11d, %edi
orl %r9d, %edi
xorl %r10d, %edi
xorl %edi, %ebp
movw %bp, -0x34(%rsp)
andl %r10d, %r8d
xorl %r12d, %edx
xorl %r8d, %edx
movw %dx, -0x36(%rsp)
andl %r12d, %edi
xorl %r9d, %r15d
xorl %edi, %r15d
movw %r15w, -0x32(%rsp)
movl %edx, %edi
andl %ebp, %edi
xorl %r13d, %edi
xorl %r15d, %edx
movl %edi, %r8d
xorl %ebp, %r8d
xorl %r8d, %r9d
movw %r9w, -0x2e(%rsp)
andl %r15d, %edi
xorl %r11d, %edi
movw %di, -0x2c(%rsp)
movl %r8d, %edi
andl %edx, %edi
xorl %r10d, %edi
movw %di, -0x2a(%rsp)
xorl %r15d, %r8d
xorl %ebp, %edx
andl %r8d, %edx
xorl %r12d, %edx
movw %dx, -0x30(%rsp)
xorl %edx, %edx
cmpq $0x8, %rdx
je 0x16ff
movzwl -0x38(%rsp,%rdx,2), %edi
movzbl %dil, %r8d
shrl $0x8, %edi
movzwl (%r14,%r8,2), %r8d
xorw (%rbx,%rdi,2), %r8w
movw %r8w, -0x38(%rsp,%rdx,2)
incq %rdx
jmp 0x16d8
movzwl -0x38(%rsp), %r15d
xorw %ax, %r15w
movzwl -0x36(%rsp), %r9d
movl -0x34(%rsp), %eax
movl %eax, %edi
andl %r9d, %edi
xorl %r15d, %edi
movzwl -0x32(%rsp), %edx
andnl %edx, %edi, %r12d
notl %edi
movl %edx, %ebp
xorl %r9d, %ebp
xorl %eax, %edi
movzwl -0x2e(%rsp), %r8d
xorw %di, %r8w
xorw -0x2c(%rsp), %r12w
movl %edi, %r13d
andl %ebp, %r13d
xorw -0x2a(%rsp), %r13w
xorl %edx, %edi
xorl %eax, %ebp
andl %edi, %ebp
xorw -0x30(%rsp), %bp
movl %ebp, %edi
andl %r8d, %edi
xorl %r12d, %edi
xorl %edi, %r15d
movw %r15w, -0x38(%rsp)
movl %r12d, %r10d
orl %r8d, %r10d
xorl %r13d, %r10d
xorl %r10d, %eax
movw %ax, -0x34(%rsp)
andl %r13d, %edi
xorl %ebp, %edi
movq 0x2875(%rip), %r11      # 0x3ff8
movw %di, (%r11)
xorl %r9d, %edi
movw %di, -0x36(%rsp)
andl %ebp, %r10d
xorl %r8d, %edx
xorl %r10d, %edx
movw %dx, -0x32(%rsp)
movl %edi, %r9d
andl %eax, %r9d
xorl %r15d, %r9d
movq 0x2843(%rip), %r10      # 0x3ff0
movw %r9w, (%r10)
xorl %edx, %edi
movq 0x2826(%rip), %r10      # 0x3fe0
movw %di, (%r10)
movl %r9d, %r10d
xorl %eax, %r10d
movq 0x27f5(%rip), %r11      # 0x3fc0
movw %r10w, (%r11)
xorl %r10d, %r8d
movw %r8w, -0x2e(%rsp)
andl %edx, %r9d
xorl %r12d, %r9d
movw %r9w, -0x2c(%rsp)
movl %r10d, %r8d
andl %edi, %r8d
xorl %r13d, %r8d
movw %r8w, -0x2a(%rsp)
xorl %edx, %r10d
xorl %eax, %edi
andl %r10d, %edi
xorl %ebp, %edi
movw %di, -0x30(%rsp)
decl %esi
jmp 0x1537
xorl %eax, %eax
cmpq $0x4, %rax
je 0x15c6
movzwl -0x28(%rsp,%rax,2), %edx
xorw -0x20(%rsp,%rax,2), %dx
xorw %dx, -0x30(%rsp,%rax,2)
incq %rax
jmp 0x180b
movzwl -0x28(%rsp,%rax,2), %edx
xorw -0x18(%rsp,%rax,2), %dx
xorw %dx, -0x38(%rsp,%rax,2)
incq %rax
cmpq $0x8, %rax
jne 0x1829
xorl %eax, %eax
cmpq $0x8, %rax
je 0x1857
movzwl -0x38(%rsp,%rax,2), %edx
movw %dx, (%rcx,%rax,2)
incq %rax
jmp 0x1843
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop