// File: /workspace/github_c_makefile_star2_O3/mjosaarinen[P]brutus/crypto_aead/scream10v3/sse/tae.c
// Function: crypto_aead_encrypt
int crypto_aead_encrypt(unsigned char *c,unsigned long long *clen,
                        const unsigned char *m,unsigned long long mlen,
                        const unsigned char *ad,unsigned long long adlen,
                        const unsigned char *nsec,
                        const unsigned char *npub,
                        const unsigned char *k) {
    
  v16qi data[16];
  uint8_t buffer[16*16] = {0};
  v16qi tweakey[16*TWEAKEY_SIZE];
  uint8_t pad[16];

  v16qi *checksum = (v16qi*) &buffer[ 16 * (((mlen+15)/16)%16) ];
  v16qi auth = CV(0);

  // Associated Data
  if (adlen > 0) {
    size_t idx=0;
    tweakey_schedule(k, npub, TWEAK_AD, tweakey);
    
    for (idx=0; idx+256<adlen; idx+=16*16) {
      read128(ad+idx, data);
      encrypt_tweakey(data, tweakey);
      tweakey_increment(tweakey, idx);
      write128_checksum(data, NULL, &auth, 16);
    }

    // Final chunk
    uint8_t buffer2[16*16] = {0};
    memcpy(buffer2, ad+idx, adlen-idx);
    if ((adlen % 16) == 0) {
      tweakey_set(tweakey, (adlen-idx-1)/16, 15, TWEAK_AD_LAST_FULL);
    } else {
      tweakey_set(tweakey, (adlen-idx-1)/16, 15, TWEAK_AD_LAST_PARTIAL);
      buffer2[adlen-idx] = 0x80;
    }
    read128(buffer2, data);
    encrypt_tweakey(data, tweakey);
    write128_checksum(data, NULL, &auth, (adlen-idx+15)/16);
  }

  size_t idx=0;
  tweakey_schedule(k, npub, TWEAK_MESSAGE, tweakey);
  if (mlen > 0) {
    
    for (idx=0; idx+16*16<=mlen; idx+=16*16) {
      read128_with_checksum(m+idx, data, checksum, 16);
      encrypt_tweakey(data, tweakey);
      tweakey_increment(tweakey, idx);
      write128(data, c+idx);
    }

    // Final chunk(s)
    if (mlen > idx+240) {
      /* printf ("## Almost!\n"); */
      // Almost full chunk: one extra chunk for checksum
      uint8_t buffer2[16*16] = {0};
      memcpy(buffer2, m+idx, 240);

      memset(buffer2+256-16, 0, 16);
      buffer2[255] = 8*(mlen%16);
      read128_with_checksum(buffer2, data, checksum, 15);
      update_checksum(m+idx+240, checksum, mlen-idx-240);

      encrypt_tweakey(data, tweakey);
      tweakey_increment(tweakey, idx);
      write128(data, buffer2);
      memcpy(c+idx, buffer2, 240);
      memcpy(pad,   buffer2+240, mlen%16);
      idx = mlen;
    } else {
      // Partial chunk: encrypt length for final block; checksum included
      memcpy(buffer, m+idx, mlen-idx);
      update_checksum(buffer, checksum, mlen-idx);

      if ((mlen % 16) != 0) {
	// Encrypt partial block length
	memset(&buffer[((mlen-idx)|15)-15], 0, 16);
	buffer[(mlen-idx)|15] = 8*(mlen%16);
      }
    }
  }

  // Tag generation
  tweakey_set(tweakey, ((mlen-idx+15)/16), 15, 2*8*mlen+1);
  tweakey_set(tweakey, ((mlen-idx+15)/16), 14, mlen>>4);
  tweakey_set(tweakey, ((mlen-idx+15)/16), 13, mlen>>12);
  tweakey_set(tweakey, ((mlen-idx+15)/16), 12, mlen>>20);
  tweakey_set(tweakey, ((mlen-idx+15)/16), 11, mlen>>28);
  read128(buffer, data);
  encrypt_tweakey(data, tweakey);
  write128(data, buffer);

  memcpy(c+idx, buffer, (mlen-idx)&(~15));
  memcpy(pad,   buffer+((mlen-idx)&(~15)), (mlen-idx)%16);

  *checksum ^= auth;

  memcpy(c+mlen, checksum, CRYPTO_ABYTES);

  unsigned i;
  for (i=0; i < mlen%16; i++)
    c[16*(mlen/16)+i] = m[16*(mlen/16)+i] ^ pad[i];

  *clen = mlen+CRYPTO_ABYTES;

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x878, %rsp            # imm = 0x878
movq %rcx, %rbp
movq %rsi, 0x38(%rsp)
movq %rdi, 0x18(%rsp)
movq 0x8c0(%rsp), %rdi
movq 0x8b8(%rsp), %rsi
vpxor %xmm0, %xmm0, %xmm0
vmovdqu %ymm0, 0x150(%rsp)
vmovdqu %ymm0, 0x130(%rsp)
vmovdqu %ymm0, 0x110(%rsp)
vmovdqu %ymm0, 0xf0(%rsp)
vmovdqu %ymm0, 0xd0(%rsp)
vmovdqu %ymm0, 0xb0(%rsp)
vmovdqu %ymm0, 0x90(%rsp)
vmovdqu %ymm0, 0x70(%rsp)
leal 0xf(%rcx), %eax
andl $0xf0, %eax
vpxor %xmm0, %xmm0, %xmm0
vmovdqa %xmm0, 0x20(%rsp)
testq %r9, %r9
movq %rdx, 0x8(%rsp)
movq %rcx, 0x10(%rsp)
je 0x27c9
movq %r9, %rbx
movq %r8, %r14
movq %rax, (%rsp)
leaq 0x370(%rsp), %rcx
movl $0x2, %edx
vzeroupper
callq 0x10a0
cmpq $0x101, %rbx            # imm = 0x101
jb 0x26d6
xorl %r13d, %r13d
leaq 0x270(%rsp), %rbp
leaq 0x370(%rsp), %r15
leaq 0x20(%rsp), %r12
leaq (%r14,%r13), %rdi
movq %rbp, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x10e0
movq %rbp, %rdi
movq %r15, %rsi
callq 0x10d0
movq %r15, %rdi
movq %r13, %rsi
callq 0x1040
movq %rbp, %rdi
xorl %esi, %esi
movq %r12, %rdx
movl $0x10, %ecx
movl $0x10, %r8d
callq 0x10b0
leaq 0x100(%r13), %rax
addq $0x200, %r13            # imm = 0x200
cmpq %rbx, %r13
movq %rax, %r13
jb 0x2680
jmp 0x26d8
xorl %eax, %eax
vpxor %xmm0, %xmm0, %xmm0
leaq 0x170(%rsp), %rdi
vmovdqu %ymm0, 0xe0(%rdi)
vmovdqu %ymm0, 0xc0(%rdi)
vmovdqu %ymm0, 0xa0(%rdi)
vmovdqu %ymm0, 0x80(%rdi)
vmovdqu %ymm0, 0x60(%rdi)
vmovdqu %ymm0, 0x40(%rdi)
vmovdqu %ymm0, 0x20(%rdi)
vmovdqu %ymm0, (%rdi)
addq %rax, %r14
movq %rbx, %r13
subq %rax, %r13
movq %r14, %rsi
movq %r13, %rdx
vzeroupper
callq 0x10f0
leaq -0x1(%r13), %rsi
shrq $0x4, %rsi
leaq 0x370(%rsp), %rdi
movl $0xf, %edx
testb $0xf, %bl
je 0x275d
movl $0x6, %ecx
callq 0x1050
movb $-0x80, 0x170(%rsp,%r13)
jmp 0x2767
movl $0x4, %ecx
callq 0x1050
movq 0x10(%rsp), %rbp
leaq 0x170(%rsp), %rdi
leaq 0x270(%rsp), %rbx
movq %rbx, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x10e0
leaq 0x370(%rsp), %rsi
movq %rbx, %rdi
callq 0x10d0
addq $0xf, %r13
shrq $0x4, %r13
leaq 0x20(%rsp), %rdx
movq %rbx, %rdi
xorl %esi, %esi
movl %r13d, %ecx
movl %r13d, %r8d
callq 0x10b0
movq 0x8b8(%rsp), %rsi
movq 0x8c0(%rsp), %rdi
movq (%rsp), %rax
addq %rsp, %rax
addq $0x70, %rax
movq %rax, (%rsp)
xorl %ebx, %ebx
leaq 0x370(%rsp), %rcx
xorl %edx, %edx
vzeroupper
callq 0x10a0
testq %rbp, %rbp
je 0x2800
movq %rbp, %r12
cmpq $0x100, %rbp            # imm = 0x100
jae 0x2808
xorl %ecx, %ecx
jmp 0x2881
xorl %r8d, %r8d
jmp 0x2a5d
xorl %ebx, %ebx
leaq 0x270(%rsp), %r14
movq (%rsp), %rbp
movq 0x8(%rsp), %r15
leaq (%r15,%rbx), %rdi
movq %r14, %rsi
movq %rbp, %rdx
movl $0x10, %ecx
callq 0x10e0
movq %r14, %rdi
leaq 0x370(%rsp), %r13
movq %r13, %rsi
callq 0x10d0
movq %r13, %rdi
movq %rbx, %rsi
callq 0x1040
movq 0x18(%rsp), %rax
leaq (%rax,%rbx), %rsi
movq %r14, %rdi
xorl %edx, %edx
movl $0x10, %ecx
movl $0x10, %r8d
callq 0x10b0
leaq 0x100(%rbx), %rcx
addq $0x200, %rbx            # imm = 0x200
cmpq %r12, %rbx
movq %rcx, %rbx
jbe 0x281b
movq %rcx, %rax
orq $0xf0, %rax
cmpq %r12, %rax
movq %r12, %rbp
jae 0x29c4
movq 0x8(%rsp), %rax
leaq (%rax,%rcx), %r14
leaq 0x170(%rsp), %r15
movl $0xf0, %edx
movq %r15, %rdi
movq %r14, %rsi
movq %rcx, %r12
callq 0x10f0
leaq 0x260(%rsp), %rcx
vpxor %xmm0, %xmm0, %xmm0
vmovdqa %xmm0, (%rcx)
movl %ebp, %ebx
andl $0xf, %ebx
leal (,%rbx,8), %eax
movb %al, 0xf(%rcx)
leaq 0x270(%rsp), %rsi
movq %r15, %rdi
movq (%rsp), %r15
movq %r15, %rdx
movl $0xf, %ecx
callq 0x10e0
movq %rbp, %rax
movq %r12, %r13
subq %r12, %rax
addq $-0xf0, %rax
je 0x294c
vmovdqa (%r15), %xmm0
xorl %ecx, %ecx
vmovdqa 0x1bfb(%rip), %xmm1     # 0x4510
movl %ecx, %edx
andl $0xf, %edx
vmovdqa %xmm0, 0x50(%rsp)
movb 0x50(%rsp,%rdx), %sil
xorb 0xf0(%r14,%rcx), %sil
vpbroadcastb %edx, %xmm2
vpcmpeqb %xmm1, %xmm2, %k1
vpbroadcastb %esi, %xmm0 {%k1}
incq %rcx
cmpq %rcx, %rax
jne 0x2915
vmovdqa %xmm0, (%r15)
leaq 0x270(%rsp), %r14
leaq 0x370(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x10d0
movq %r15, %rdi
movq %r13, %rsi
callq 0x1040
leaq 0x170(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
xorl %edx, %edx
movl $0x10, %ecx
movl $0x10, %r8d
callq 0x10b0
addq 0x18(%rsp), %r13
movl $0xf0, %edx
movq %r13, %rdi
movq %r15, %rsi
callq 0x10f0
leaq 0x40(%rsp), %rdi
leaq 0x260(%rsp), %rsi
movq %rbx, %rdx
callq 0x10f0
movq %rbp, %r8
jmp 0x2a5d
movq 0x8(%rsp), %rax
leaq (%rax,%rcx), %rsi
movq %rbp, %rdx
subq %rcx, %rdx
leaq 0x70(%rsp), %rdi
movq %rcx, %rbx
callq 0x10f0
movq %rbx, %r8
movq %rbp, %rax
subq %rbx, %rax
movq (%rsp), %rdi
je 0x2a30
vmovdqa (%rdi), %xmm0
xorl %ecx, %ecx
vmovdqa 0x1b13(%rip), %xmm1     # 0x4510
movl %ecx, %edx
andl $0xf, %edx
vmovdqa %xmm0, 0x60(%rsp)
movb 0x60(%rsp,%rdx), %sil
xorb 0x70(%rsp,%rcx), %sil
vpbroadcastb %edx, %xmm2
vpcmpeqb %xmm1, %xmm2, %k1
vpbroadcastb %esi, %xmm0 {%k1}
vmovdqa %xmm0, (%rdi)
incq %rcx
cmpq %rcx, %rax
jne 0x29fd
movq %rbp, %rbx
andq $0xf, %rbx
je 0x2a5b
movq %rax, %rcx
orq $0xf, %rcx
andq $-0x10, %rax
vpxor %xmm0, %xmm0, %xmm0
vmovdqa %xmm0, 0x70(%rsp,%rax)
leal (,%rbx,8), %eax
movb %al, 0x70(%rsp,%rcx)
jmp 0x2a5d
xorl %ebx, %ebx
movq %r8, 0x30(%rsp)
movq %rbp, %r14
subq %r8, %r14
leaq 0xf(%r14), %r13
shrq $0x4, %r13
movl %ebp, %eax
shlb $0x4, %al
incb %al
movzbl %al, %ecx
leaq 0x370(%rsp), %r12
movq %r12, %rdi
movl %r13d, %esi
movl $0xf, %edx
callq 0x1050
movl %ebp, %ecx
shrl $0x4, %ecx
movl $0xff, %r15d
andl %r15d, %ecx
movq %r12, %rdi
movl %r13d, %esi
movl $0xe, %edx
callq 0x1050
movl %ebp, %ecx
shrl $0xc, %ecx
andl %r15d, %ecx
movq %r12, %rdi
movl %r13d, %esi
movl $0xd, %edx
callq 0x1050
movl %ebp, %ecx
shrl $0x14, %ecx
andl %r15d, %ecx
movq %r12, %rdi
movl %r13d, %esi
movl $0xc, %edx
callq 0x1050
movq %rbp, %rcx
shrq $0x1c, %rcx
andl %r15d, %ecx
movq %r12, %rdi
movl %r13d, %esi
movl $0xb, %edx
callq 0x1050
leaq 0x70(%rsp), %r15
leaq 0x270(%rsp), %r12
movq %r15, %rdi
movq %r12, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x10e0
movq %r12, %rdi
leaq 0x370(%rsp), %rsi
callq 0x10d0
movq %r12, %rdi
movq %r15, %rsi
xorl %edx, %edx
movl $0x10, %ecx
movl $0x10, %r8d
callq 0x10b0
movq 0x18(%rsp), %r13
movq 0x30(%rsp), %rdi
addq %r13, %rdi
movq %r14, %r12
andq $-0x10, %r12
movq %r15, %rsi
movq %r12, %rdx
callq 0x10f0
leaq (%rsp,%r12), %rsi
addq $0x70, %rsi
movq 0x10(%rsp), %r15
andl $0xf, %r14d
leaq 0x40(%rsp), %rdi
movq %r14, %rdx
callq 0x10f0
movq (%rsp), %rax
vmovaps (%rax), %xmm0
vxorps 0x20(%rsp), %xmm0, %xmm0
vmovaps %xmm0, (%rax)
vmovups %xmm0, (%r13,%r15)
testq %rbx, %rbx
movq 0x8(%rsp), %rdx
je 0x2bc1
movq %r15, %rax
andq $-0x10, %rax
addq %rax, %r13
addq %rax, %rdx
xorl %eax, %eax
movb 0x40(%rsp,%rax), %cl
xorb (%rdx,%rax), %cl
movb %cl, (%r13,%rax)
incq %rax
cmpq %rax, %rbx
jne 0x2bad
addq $0x10, %r15
movq 0x38(%rsp), %rax
movq %r15, (%rax)
xorl %eax, %eax
addq $0x878, %rsp            # imm = 0x878
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq