// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/scream12v2/ref/scream_cipher.c
// Function: LS_encrypt
void LS_encrypt(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];
  }
    
  /* Initial key + tweak addition */
  for (j=0; j<8; j++) data[j] ^= k[j] ^ t[j];
    
  /* ---------------- Step Function ---------------- */
  for (i=0; i<nSteps; i++) {
        
    /* ---------------- First Round ---------------- */
    /* SBox layer (bitsliced) */
    SBOX(data);
    /* First round constant */
    data[0] ^= ((27*(2*i)) % 256);
    /* LBox layer (tables) */
    for (j=0; j<8; j++)
      data[j] = LBox2[data[j]>>8] ^ LBox1[data[j]&0xff];
        
    /* ---------------- Second Round -------------- */
    /* SBox layer (bitsliced) */
    SBOX(data);
    /* Round constant */
    data[0] ^= ((27*(2*i+1)) % 256);
    /* LBox layer (tables) */
    for (j=0; j<8; j++)
      data[j] = LBox2[data[j]>>8] ^ LBox1[data[j]&0xff];
        
    /* Tweak schedule */
    for (j=0; j<4; j++) {
      t[j]   ^= t[j+4];
      t[j+4] ^= t[j];
    }
    /* ----------- Key + Tweak Addition ---------- */
    for (j=0; j<8; j++) data[j] ^= k[j] ^ t[j];
  }

  /* ----------- Restore initial tweak --------- */
  for (i=nSteps%3; i<3; i++) {
    for (j=0; j<4; j++) {
      t[j]   ^= t[j+4];
      t[j+4] ^= 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, -0x28(%rsp)
xorl %eax, %eax
cmpq $0x10, %rax
je 0x11b4
movzwl (%rdi,%rax), %ecx
movw %cx, -0x48(%rsp,%rax)
movzwl (%rsi,%rax), %ecx
movw %cx, -0x18(%rsp,%rax)
movzwl (%rdx,%rax), %ecx
movw %cx, -0x38(%rsp,%rax)
addq $0x2, %rax
jmp 0x118d
xorl %eax, %eax
cmpq $0x8, %rax
je 0x11d0
movzwl -0x38(%rsp,%rax,2), %ecx
xorw -0x18(%rsp,%rax,2), %cx
xorw %cx, -0x48(%rsp,%rax,2)
incq %rax
jmp 0x11b6
xorl %ecx, %ecx
leaq 0xe27(%rip), %rsi       # 0x2000
leaq 0x1020(%rip), %rdi      # 0x2200
cmpl $0xc, %ecx
je 0x143a
movzwl -0x46(%rsp), %eax
movl -0x48(%rsp), %edx
movl -0x40(%rsp), %r10d
movl %eax, %r8d
andl %edx, %r8d
xorw -0x44(%rsp), %r8w
xorl %r8d, %eax
movl %r10d, %r9d
andl %edx, %r9d
xorw -0x42(%rsp), %r9w
xorl %r9d, %r8d
movl %r9d, %ebx
andl %eax, %ebx
xorl %edx, %ebx
xorl %eax, %r10d
movl %r8d, %r14d
andl %r10d, %r14d
xorl %eax, %r14d
xorl %ebx, %r14d
movzwl -0x3e(%rsp), %ebp
movl -0x3c(%rsp), %r11d
movzwl -0x3a(%rsp), %r15d
andnl %ebp, %r15d, %r13d
andnl %r11d, %ebp, %r12d
xorl %ebx, %ebp
andnl %r15d, %r11d, %eax
xorl %r14d, %r11d
andnl %ebp, %r10d, %edx
notl %r10d
xorl %ebx, %eax
movw %ax, -0x3e(%rsp)
xorl %r14d, %r13d
movw %r13w, -0x3c(%rsp)
xorl %r8d, %r12d
movw %r12w, -0x3a(%rsp)
movl %r11d, %eax
andl %ebp, %eax
xorl %r15d, %r8d
xorl %eax, %r8d
xorl %r8d, %r11d
xorl %r9d, %edx
andnl %r11d, %edx, %eax
notl %edx
movw %dx, -0x42(%rsp)
xorl %r8d, %edx
movw %dx, -0x44(%rsp)
xorl %ebp, %eax
xorl %r11d, %r10d
movw %r10w, -0x40(%rsp)
andl %edx, %r10d
xorl %r11d, %r10d
xorl %eax, %r10d
movw %r10w, -0x46(%rsp)
imull $0x36, %ecx, %r8d
movzbl %r8b, %edx
xorl %eax, %edx
movw %dx, -0x48(%rsp)
xorl %eax, %eax
cmpq $0x8, %rax
je 0x12e7
movzwl -0x48(%rsp,%rax,2), %edx
movzbl %dl, %r9d
shrl $0x8, %edx
movzwl (%rdi,%r9,2), %r9d
xorw (%rsi,%rdx,2), %r9w
movw %r9w, -0x48(%rsp,%rax,2)
incq %rax
jmp 0x12c0
movq %rcx, -0x20(%rsp)
movzwl -0x46(%rsp), %eax
movl -0x48(%rsp), %edx
movl -0x40(%rsp), %r11d
movl %eax, %r9d
andl %edx, %r9d
xorw -0x44(%rsp), %r9w
xorl %r9d, %eax
movl %r11d, %r10d
andl %edx, %r10d
xorw -0x42(%rsp), %r10w
xorl %r10d, %r9d
movl %r10d, %ebp
andl %eax, %ebp
xorl %edx, %ebp
xorl %eax, %r11d
movl %r9d, %r15d
andl %r11d, %r15d
xorl %eax, %r15d
xorl %ebp, %r15d
movzwl -0x3e(%rsp), %r14d
movl -0x3c(%rsp), %ebx
movzwl -0x3a(%rsp), %r12d
andnl %r14d, %r12d, %edx
andnl %ebx, %r14d, %r13d
xorl %ebp, %r14d
andnl %r12d, %ebx, %ecx
xorl %r15d, %ebx
andnl %r14d, %r11d, %eax
notl %r11d
xorl %ebp, %ecx
movw %cx, -0x3e(%rsp)
xorl %r15d, %edx
movw %dx, -0x3c(%rsp)
xorl %r9d, %r13d
movw %r13w, -0x3a(%rsp)
movl %ebx, %ecx
andl %r14d, %ecx
xorl %r12d, %r9d
xorl %ecx, %r9d
xorl %r9d, %ebx
xorl %r10d, %eax
andnl %ebx, %eax, %ecx
notl %eax
movw %ax, -0x42(%rsp)
xorl %r9d, %eax
movw %ax, -0x44(%rsp)
xorl %r14d, %ecx
xorl %ebx, %r11d
movw %r11w, -0x40(%rsp)
andl %eax, %r11d
xorl %ebx, %r11d
xorl %ecx, %r11d
movw %r11w, -0x46(%rsp)
leal 0x1b(%r8), %eax
movzbl %al, %eax
xorl %ecx, %eax
movw %ax, -0x48(%rsp)
xorl %eax, %eax
cmpq $0x8, %rax
je 0x13e6
movzwl -0x48(%rsp,%rax,2), %ecx
movzbl %cl, %edx
shrl $0x8, %ecx
movzwl (%rdi,%rdx,2), %edx
xorw (%rsi,%rcx,2), %dx
movw %dx, -0x48(%rsp,%rax,2)
incq %rax
jmp 0x13c3
movabsq $-0x8, %rax
testq %rax, %rax
je 0x1412
movzwl -0x30(%rsp,%rax), %ecx
movzwl -0x28(%rsp,%rax), %edx
xorw %cx, %dx
movw %dx, -0x30(%rsp,%rax)
movw %cx, -0x28(%rsp,%rax)
addq $0x2, %rax
jmp 0x13f0
xorl %eax, %eax
cmpq $0x8, %rax
je 0x142e
movzwl -0x38(%rsp,%rax,2), %ecx
xorw -0x18(%rsp,%rax,2), %cx
xorw %cx, -0x48(%rsp,%rax,2)
incq %rax
jmp 0x1414
movq -0x20(%rsp), %rcx
incl %ecx
jmp 0x11e0
xorl %eax, %eax
movabsq $-0x8, %rcx
cmpl $0x3, %eax
je 0x1474
movq %rcx, %rdx
testq %rdx, %rdx
je 0x1470
movzwl -0x30(%rsp,%rdx), %esi
movzwl -0x28(%rsp,%rdx), %edi
xorw %si, %di
movw %di, -0x30(%rsp,%rdx)
movw %si, -0x28(%rsp,%rdx)
addq $0x2, %rdx
jmp 0x144e
incl %eax
jmp 0x1446
xorl %eax, %eax
movq -0x28(%rsp), %rdx
cmpq $0x8, %rax
je 0x148f
movzwl -0x48(%rsp,%rax,2), %ecx
movw %cx, (%rdx,%rax,2)
incq %rax
jmp 0x147b
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq