// File: /workspace/github_c_makefile_star2_O1/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, %r12
movq %rdx, 0x8(%rsp)
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
je 0x2819
movq %r9, %r13
movq %r8, %rbp
movq %rax, 0x10(%rsp)
movq %r12, (%rsp)
leaq 0x370(%rsp), %rcx
movl $0x2, %edx
vzeroupper
callq 0x10a0
cmpq $0x101, %r13            # imm = 0x101
jb 0x2725
xorl %ebx, %ebx
leaq 0x270(%rsp), %r14
leaq 0x370(%rsp), %r15
leaq 0x20(%rsp), %r12
leaq (%rbx,%rbp), %rdi
movq %r14, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x10e0
movq %r14, %rdi
movq %r15, %rsi
callq 0x10d0
movq %r15, %rdi
movq %rbx, %rsi
callq 0x1040
movq %r14, %rdi
xorl %esi, %esi
movq %r12, %rdx
movl $0x10, %ecx
movl $0x10, %r8d
callq 0x10b0
leaq 0x100(%rbx), %rax
addq $0x200, %rbx            # imm = 0x200
cmpq %r13, %rbx
movq %rax, %rbx
jb 0x26cf
jmp 0x2727
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, %rbp
movq %r13, %r14
subq %rax, %r14
movq %rbp, %rsi
movq %r14, %rdx
vzeroupper
callq 0x10f0
leaq -0x1(%r14), %rsi
shrq $0x4, %rsi
leaq 0x370(%rsp), %rdi
movl $0xf, %edx
testb $0xf, %r13b
je 0x27ad
movl $0x6, %ecx
callq 0x1050
movb $-0x80, 0x170(%rsp,%r14)
jmp 0x27b7
movl $0x4, %ecx
callq 0x1050
movq (%rsp), %r12
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, %r14
shrq $0x4, %r14
leaq 0x20(%rsp), %rdx
movq %rbx, %rdi
xorl %esi, %esi
movl %r14d, %ecx
movl %r14d, %r8d
callq 0x10b0
movq 0x8b8(%rsp), %rsi
movq 0x8c0(%rsp), %rdi
movq 0x10(%rsp), %rax
addq %rsp, %rax
addq $0x70, %rax
movq %rax, (%rsp)
leaq 0x370(%rsp), %rcx
xorl %edx, %edx
vzeroupper
callq 0x10a0
testq %r12, %r12
je 0x2857
movq %r12, %rax
cmpq $0x100, %r12            # imm = 0x100
jae 0x285e
xorl %r8d, %r8d
movq 0x8(%rsp), %rcx
movq %rax, %r12
jmp 0x28df
xorl %ebp, %ebp
jmp 0x2aae
xorl %ebx, %ebx
leaq 0x270(%rsp), %r14
leaq 0x370(%rsp), %r15
movq 0x8(%rsp), %rcx
movq 0x18(%rsp), %r13
movq (%rsp), %rbp
movq %rax, %r12
leaq (%rcx,%rbx), %rdi
movq %r14, %rsi
movq %rbp, %rdx
movl $0x10, %ecx
callq 0x10e0
movq %r14, %rdi
movq %r15, %rsi
callq 0x10d0
movq %r15, %rdi
movq %rbx, %rsi
callq 0x1040
leaq (%rbx,%r13), %rsi
movq %r14, %rdi
xorl %edx, %edx
movl $0x10, %ecx
movl $0x10, %r8d
callq 0x10b0
movq 0x8(%rsp), %rcx
leaq 0x100(%rbx), %r8
addq $0x200, %rbx            # imm = 0x200
cmpq %r12, %rbx
movq %r8, %rbx
jbe 0x2881
movq %r8, %rax
orq $0xf0, %rax
cmpq %r12, %rax
jae 0x2a25
leaq (%rcx,%r8), %r14
leaq 0x260(%rsp), %rbp
vpxor %xmm0, %xmm0, %xmm0
vmovdqa %xmm0, (%rbp)
leaq 0x170(%rsp), %rbx
movl $0xf0, %edx
movq %rbx, %rdi
movq %r14, %rsi
movq %r8, %r13
callq 0x10f0
vpxor %xmm0, %xmm0, %xmm0
vmovdqa %xmm0, (%rbp)
movl %r12d, %eax
andl $0xf, %eax
movq %rax, 0x10(%rsp)
leal (,%rax,8), %eax
movb %al, 0xf(%rbp)
leaq 0x270(%rsp), %rsi
movq %rbx, %rdi
movq (%rsp), %rbx
movq %rbx, %rdx
movl $0xf, %ecx
callq 0x10e0
movq %r12, %rax
movq %r13, %r15
subq %r13, %rax
addq $-0xf0, %rax
je 0x29b0
vmovdqa (%rbx), %xmm0
xorl %ecx, %ecx
vmovdqa 0x1b96(%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 0x297a
vmovdqa %xmm0, (%rbx)
leaq 0x270(%rsp), %rbx
leaq 0x370(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x10d0
movq %r14, %rdi
movq %r15, %rsi
callq 0x1040
leaq 0x170(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
movl $0x10, %ecx
movl $0x10, %r8d
callq 0x10b0
addq 0x18(%rsp), %r15
movl $0xf0, %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0x10f0
leaq 0x40(%rsp), %rdi
movq %rbp, %rsi
movq 0x10(%rsp), %rdx
callq 0x10f0
movq %r12, %rbp
jmp 0x2aae
leaq (%rcx,%r8), %rsi
movq %r12, %rdx
subq %r8, %rdx
leaq 0x70(%rsp), %rdi
movq %r8, %rbp
callq 0x10f0
movq %r12, %rax
subq %rbp, %rax
movq (%rsp), %rdi
je 0x2a89
vmovdqa (%rdi), %xmm0
xorl %ecx, %ecx
vmovdqa 0x1aba(%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 0x2a56
movq %r12, %rcx
andq $0xf, %rcx
je 0x2aae
movq %rax, %rdx
orq $0xf, %rdx
andq $-0x10, %rax
vpxor %xmm0, %xmm0, %xmm0
vmovdqa %xmm0, 0x70(%rsp,%rax)
shlb $0x3, %cl
movb %cl, 0x70(%rsp,%rdx)
movq %r12, %r14
subq %rbp, %r14
leaq 0xf(%r14), %rbx
shrq $0x4, %rbx
movl %r12d, %eax
shlb $0x4, %al
incb %al
movzbl %al, %ecx
leaq 0x370(%rsp), %r13
movq %r13, %rdi
movl %ebx, %esi
movl $0xf, %edx
callq 0x1050
movl %r12d, %ecx
shrl $0x4, %ecx
movl $0xff, %r15d
andl %r15d, %ecx
movq %r13, %rdi
movl %ebx, %esi
movl $0xe, %edx
callq 0x1050
movl %r12d, %ecx
shrl $0xc, %ecx
andl %r15d, %ecx
movq %r13, %rdi
movl %ebx, %esi
movl $0xd, %edx
callq 0x1050
movl %r12d, %ecx
shrl $0x14, %ecx
andl %r15d, %ecx
movq %r13, %rdi
movl %ebx, %esi
movl $0xc, %edx
callq 0x1050
movq %r12, %rcx
shrq $0x1c, %rcx
andl %r15d, %ecx
movq %r13, %rdi
movl %ebx, %esi
movl $0xb, %edx
callq 0x1050
leaq 0x70(%rsp), %rbx
leaq 0x270(%rsp), %r15
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x10e0
movq %r15, %rdi
movq %r13, %rsi
callq 0x10d0
movq %r15, %rdi
movq %rbx, %rsi
xorl %edx, %edx
movl $0x10, %ecx
movl $0x10, %r8d
callq 0x10b0
movq 0x18(%rsp), %r13
addq %r13, %rbp
movq %r14, %r15
andq $-0x10, %r15
movq %rbp, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x10f0
leaq (%rsp,%r15), %rsi
addq $0x70, %rsi
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,%r12)
movq %r12, %rax
andq $0xf, %rax
movq 0x8(%rsp), %rsi
je 0x2c05
movq %r13, %rdi
movq %r12, %rcx
andq $-0x10, %rcx
addq %rcx, %rdi
addq %rcx, %rsi
xorl %ecx, %ecx
movb 0x40(%rsp,%rcx), %dl
xorb (%rsi,%rcx), %dl
movb %dl, (%rdi,%rcx)
incq %rcx
cmpq %rcx, %rax
jne 0x2bf3
addq $0x10, %r12
movq 0x38(%rsp), %rax
movq %r12, (%rax)
xorl %eax, %eax
addq $0x878, %rsp            # imm = 0x878
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq