// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/aes128poetv1aes4/ref/poet.c
// Function: encrypt_final
void encrypt_final(struct poet_ctx *ctx, const uint8_t *plaintext,
		   uint64_t plen, uint8_t *ciphertext, uint8_t tag[BLOCKLEN])
{
#ifdef DEBUG
  int i;
#endif
  uint64_t offset=0;
  uint64_t len;
  block s;
  block tmp;
  block tmp2;
  while( plen > BLOCKLEN )
    {
      encrypt_block(ctx,  (plaintext+offset), (ciphertext+offset));
      plen   -= BLOCKLEN;
      offset += BLOCKLEN;
    }

  /* Encrypt length of message */
  ctx->mlen+=(plen*8);
  memset(s,0,BLOCKLEN);
  len =  TO_LITTLE_ENDIAN_64(ctx->mlen);
  memcpy(s, &len,8);
  AES_encrypt(s, s ,&(ctx->aes_enc));

  /* Last message block must be padded if necesscary */
  memcpy(tmp,plaintext+offset,plen);
  memcpy(tmp+plen,ctx->tau,BLOCKLEN-plen);


  /* Process last block + tag generation */

  TOP_HASH;
  xor_block(tmp,s,tmp);
  xor_block(ctx->x,tmp,ctx->x);

  AES_encrypt(ctx->x, tmp, &(ctx->aes_enc));

  BOTTOM_HASH;
  xor_block(tmp2, tmp,ctx->y);
  memcpy(ctx->y, tmp, BLOCKLEN);
  xor_block(tmp,s,tmp2);


#ifdef DEBUG
  puts("S"); for(i=0;i<16;i++) printf("%02x ",s[i]); puts("");
  puts("X"); for(i=0;i<16;i++) printf("%02x ",ctx->x[i]); puts("");
  puts("Y"); for(i=0;i<16;i++) printf("%02x ",ctx->y[i]); puts("");
#endif

  /* Do tag splitting if needed */
  memcpy(ciphertext+offset,tmp,plen);
  memcpy(tag,tmp+plen,BLOCKLEN-plen);


  /* Generate tag */
  TOP_HASH;
  xor_block(ctx->x, ctx->tau, ctx->x);
  xor_block(ctx->x, ctx->tm, ctx->x);

  AES_encrypt(ctx->x, tmp, &(ctx->aes_enc));

  BOTTOM_HASH;
  xor_block(tmp, ctx->y, tmp);
  xor_block(tmp, ctx->tm, tmp);

  memcpy(tag+(BLOCKLEN-plen),tmp,plen);

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %r8, 0x48(%rsp)
movq %rcx, %r13
movq %rdx, %r14
movq %rsi, %rbp
movq %rdi, %r15
leaq (,%rdx,8), %rbx
negq %rbx
xorl %r12d, %r12d
movq %rdx, 0x8(%rsp)
cmpq $0x11, %r14
jb 0x3dd4
leaq (%r12,%rbp), %rsi
leaq (%r12,%r13), %rdx
movq %r15, %rdi
callq 0x10a0
addq $-0x10, %r14
addq $0x10, %r12
subq $-0x80, %rbx
jmp 0x3db0
movq 0x450(%r15), %rax
subq %rbx, %rax
movq %rax, 0x450(%r15)
vxorps %xmm0, %xmm0, %xmm0
leaq 0x50(%rsp), %rdi
vmovaps %xmm0, (%rdi)
movq %rax, (%rdi)
movq %rdi, %rsi
movq %r15, %rdx
callq 0x1050
addq %r12, %rbp
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x1110
leaq (%rsp,%r14), %rdi
addq $0x10, %rdi
movq %rdi, 0x38(%rsp)
leaq 0x440(%r15), %rsi
movq %rsi, 0x40(%rsp)
movq %r12, %rax
subq 0x8(%rsp), %rax
movq %rax, 0x8(%rsp)
leaq 0x10(%rax), %rdx
movq %rdx, 0x30(%rsp)
callq 0x1110
leaq 0x420(%r15), %rbp
leaq 0x1e8(%r15), %rdx
movq %rdx, 0x28(%rsp)
movq %rbp, %rdi
movq %rbp, %rsi
callq 0x10f0
movq %rbx, %rdi
leaq 0x50(%rsp), %rsi
movq %rbx, %rdx
callq 0x1100
movq %rbp, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x1100
movq %rbp, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x1050
leaq 0x430(%r15), %rbx
leaq 0x2dc(%r15), %rdx
movq %rdx, 0x20(%rsp)
movq %rbx, %rdi
movq %rbx, %rsi
callq 0x10f0
leaq 0x60(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movq %rbx, %rdx
callq 0x1100
leaq 0x10(%rsp), %rdi
vmovaps (%rdi), %xmm0
vmovups %xmm0, 0x430(%r15)
leaq 0x50(%rsp), %rsi
leaq 0x60(%rsp), %rdx
callq 0x1100
addq %r12, %r13
movq %r13, %rdi
leaq 0x10(%rsp), %rsi
movq %r14, %rdx
callq 0x1110
movq 0x48(%rsp), %r12
movq %r12, %rdi
movq 0x38(%rsp), %rsi
movq 0x30(%rsp), %rdx
callq 0x1110
movq %rbp, %rdi
movq %rbp, %rsi
movq 0x28(%rsp), %rdx
callq 0x10f0
movq %rbp, %rdi
movq 0x40(%rsp), %rsi
movq %rbp, %rdx
callq 0x1100
leaq 0x3e0(%r15), %r13
movq %rbp, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x1100
movq %rbp, %rdi
leaq 0x10(%rsp), %rbp
movq %rbp, %rsi
movq %r15, %rdx
callq 0x1050
movq %rbx, %rdi
movq %rbx, %rsi
movq 0x20(%rsp), %rdx
callq 0x10f0
movq %rbp, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x1100
movq %rbp, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x1100
movq 0x8(%rsp), %rax
leaq (%rax,%r12), %rdi
addq $0x10, %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x1110
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq