// File: /workspace/github_c_makefile_star2_O2/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 $0x858, %rsp            # imm = 0x858
movq %r8, 0x8(%rsp)
movq %rcx, %r12
movq %rdx, 0x18(%rsp)
movq %rsi, 0x38(%rsp)
movq %rdi, 0x10(%rsp)
movq 0x8a0(%rsp), %rdi
movq 0x898(%rsp), %rsi
vxorps %xmm0, %xmm0, %xmm0
vmovups %ymm0, 0x130(%rsp)
vmovups %ymm0, 0x110(%rsp)
vmovups %ymm0, 0xf0(%rsp)
vmovups %ymm0, 0xd0(%rsp)
vmovups %ymm0, 0xb0(%rsp)
vmovups %ymm0, 0x90(%rsp)
vmovups %ymm0, 0x70(%rsp)
vmovups %ymm0, 0x50(%rsp)
leal 0xf(%rcx), %eax
andl $0xf0, %eax
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x20(%rsp)
testq %r9, %r9
movq %rcx, (%rsp)
je 0x2824
movq %r9, %rbx
movq %rax, 0x30(%rsp)
pushq $0x2
popq %rdx
leaq 0x350(%rsp), %r14
movq %r14, %rcx
vzeroupper
callq 0x10c0
leaq (%rsp,%rbx), %r12
addq $0x150, %r12            # imm = 0x150
xorl %r13d, %r13d
leaq 0x250(%rsp), %r15
leaq 0x100(%r13), %rbp
cmpq %rbx, %rbp
jae 0x2742
movq 0x8(%rsp), %rax
leaq (%rax,%r13), %rdi
movq %r15, %rsi
callq 0x10f0
movq %r15, %rdi
movq %r14, %rsi
callq 0x1110
movq %r14, %rdi
movq %r13, %rsi
callq 0x1050
movq %r15, %rdi
xorl %esi, %esi
leaq 0x20(%rsp), %rdx
pushq $0x10
popq %rcx
callq 0x1040
addq $-0x100, %r12
movq %rbp, %r13
jmp 0x26f1
vxorps %xmm0, %xmm0, %xmm0
leaq 0x150(%rsp), %rdi
vmovups %ymm0, 0xe0(%rdi)
vmovups %ymm0, 0xc0(%rdi)
vmovups %ymm0, 0xa0(%rdi)
vmovups %ymm0, 0x80(%rdi)
vmovups %ymm0, 0x60(%rdi)
vmovups %ymm0, 0x40(%rdi)
vmovups %ymm0, 0x20(%rdi)
vmovups %ymm0, (%rdi)
movq 0x8(%rsp), %rsi
addq %r13, %rsi
movq %rbx, %r14
subq %r13, %r14
movq %r14, %rdx
vzeroupper
callq 0x1130
leaq -0x1(%r14), %rsi
shrq $0x4, %rsi
leaq 0x350(%rsp), %rdi
pushq $0xf
popq %rdx
testb $0xf, %bl
je 0x27c1
pushq $0x6
popq %rcx
callq 0x1060
movb $-0x80, (%r12)
jmp 0x27c9
pushq $0x4
popq %rcx
callq 0x1060
movq (%rsp), %r12
leaq 0x150(%rsp), %rdi
leaq 0x250(%rsp), %rbx
movq %rbx, %rsi
callq 0x10f0
leaq 0x350(%rsp), %rsi
movq %rbx, %rdi
callq 0x1110
addq $0xf, %r14
shrq $0x4, %r14
leaq 0x20(%rsp), %rdx
movq %rbx, %rdi
xorl %esi, %esi
movl %r14d, %ecx
callq 0x1040
movq 0x898(%rsp), %rsi
movq 0x8a0(%rsp), %rdi
movq 0x30(%rsp), %rax
leaq (%rsp,%rax), %r13
addq $0x50, %r13
xorl %ebx, %ebx
leaq 0x350(%rsp), %rcx
xorl %edx, %edx
vzeroupper
callq 0x10c0
xorl %ebp, %ebp
testq %r12, %r12
je 0x29f8
leaq 0x250(%rsp), %rbx
pushq $0x10
popq %r14
leaq 0x350(%rsp), %r15
leaq 0x100(%rbp), %r12
cmpq (%rsp), %r12
movq 0x18(%rsp), %rcx
ja 0x28ae
leaq (%rcx,%rbp), %rdi
movq %rbx, %rsi
movq %r13, %rdx
movl %r14d, %ecx
callq 0x1120
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1110
movq %r15, %rdi
movq %rbp, %rsi
callq 0x1050
addq 0x10(%rsp), %rbp
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x1070
movq %r12, %rbp
jmp 0x285f
leaq 0xf0(%rbp), %rax
movq (%rsp), %r12
cmpq %r12, %rax
jae 0x29a2
leaq (%rcx,%rbp), %r15
leaq 0x150(%rsp), %rbx
movl $0xf0, %r14d
movl $0xf0, %edx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1130
movq %rbx, %rdi
leaq (%rbx,%r14), %rax
movq %rax, 0x8(%rsp)
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0xf0(%rbx)
movl %r12d, %ebx
andl $0xf, %ebx
leal (,%rbx,8), %eax
movb %al, 0xff(%rdi)
leaq 0x250(%rsp), %r12
pushq $0xf
popq %rcx
movq %r12, %rsi
movq %r13, %rdx
callq 0x1120
addq %r14, %r15
movq (%rsp), %rdx
subq %rbp, %rdx
addq $-0xf0, %rdx
movq %r15, %rdi
movq %r13, %rsi
callq 0x1100
leaq 0x350(%rsp), %r15
movq %r12, %rdi
movq %r15, %rsi
callq 0x1110
movq %r15, %rdi
movq %rbp, %rsi
callq 0x1050
movq %r12, %rdi
movq (%rsp), %r12
leaq 0x150(%rsp), %r14
movq %r14, %rsi
callq 0x1070
addq 0x10(%rsp), %rbp
movl $0xf0, %edx
movq %rbp, %rdi
movq %r14, %rsi
callq 0x1130
leaq 0x40(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %rbx, %rdx
callq 0x1130
movq %r12, %rbp
jmp 0x29f8
leaq (%rcx,%rbp), %rsi
movq %r12, %r14
subq %rbp, %r14
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rdx
callq 0x1130
movq %rbx, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x1100
movq %r12, %rbx
andq $0xf, %rbx
je 0x29f6
movq %r14, %rax
orq $0xf, %rax
andq $-0x10, %r14
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x50(%rsp,%r14)
leal (,%rbx,8), %ecx
movb %cl, 0x50(%rsp,%rax)
jmp 0x29f8
xorl %ebx, %ebx
movq %r12, %rax
subq %rbp, %rax
movq %rax, 0x8(%rsp)
leaq 0xf(%rax), %r14
shrq $0x4, %r14
movl %r12d, %eax
shlb $0x4, %al
incb %al
movzbl %al, %ecx
leaq 0x350(%rsp), %r15
pushq $0xf
popq %rdx
movq %r15, %rdi
movl %r14d, %esi
callq 0x1060
movl %r12d, %ecx
shrl $0x4, %ecx
movl $0xff, %eax
andl %eax, %ecx
pushq $0xe
popq %rdx
movq %r15, %rdi
movl %r14d, %esi
callq 0x1060
movl %r12d, %ecx
shrl $0xc, %ecx
movl $0xff, %eax
andl %eax, %ecx
pushq $0xd
popq %rdx
movq %r15, %rdi
movl %r14d, %esi
callq 0x1060
movl %r12d, %ecx
shrl $0x14, %ecx
movl $0xff, %eax
andl %eax, %ecx
pushq $0xc
popq %rdx
movq %r15, %rdi
movl %r14d, %esi
callq 0x1060
movq %r12, %rcx
shrq $0x1c, %rcx
movl $0xff, %eax
andl %eax, %ecx
pushq $0xb
popq %rdx
movq %r15, %rdi
movq %r15, %r12
movl %r14d, %esi
callq 0x1060
leaq 0x50(%rsp), %r14
leaq 0x250(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x10f0
movq %r15, %rdi
movq %r12, %rsi
callq 0x1110
movq %r15, %rdi
movq %r14, %rsi
callq 0x1070
movq 0x10(%rsp), %r14
addq %r14, %rbp
movq 0x8(%rsp), %r12
movq %r12, %r15
andq $-0x10, %r15
movq %rbp, %rdi
leaq 0x50(%rsp), %rsi
movq %r15, %rdx
callq 0x1130
leaq (%rsp,%r15), %rsi
addq $0x50, %rsi
movq (%rsp), %r15
andl $0xf, %r12d
leaq 0x40(%rsp), %rdi
movq %r12, %rdx
callq 0x1130
vmovaps (%r13), %xmm0
vxorps 0x20(%rsp), %xmm0, %xmm0
vmovaps %xmm0, (%r13)
vmovups %xmm0, (%r14,%r15)
movq %r15, %rax
andq $-0x10, %rax
addq %rax, %r14
movq 0x18(%rsp), %rdx
addq %rax, %rdx
xorl %eax, %eax
cmpl %ebx, %eax
jae 0x2b4b
movb 0x40(%rsp,%rax), %cl
xorb (%rdx,%rax), %cl
movb %cl, (%r14,%rax)
incq %rax
jmp 0x2b37
addq $0x10, %r15
movq 0x38(%rsp), %rax
movq %r15, (%rax)
xorl %eax, %eax
addq $0x858, %rsp            # imm = 0x858
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq