// File: /workspace/github_c_makefile_star2_O1/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 $0x58, %rsp
movq %r8, 0x38(%rsp)
movq %rcx, %r12
movq %rdx, %rbx
movq %rsi, %rbp
movq %rdi, %r15
xorl %r13d, %r13d
cmpq $0x11, %rdx
jb 0x3e69
movq %rbp, %rsi
addq %r13, %rsi
leaq (%r12,%r13), %rdx
movq %r15, %rdi
callq 0x10a0
addq $-0x10, %rbx
addq $0x10, %r13
cmpq $0x10, %rbx
ja 0x3e49
leaq (,%rbx,8), %rax
addq 0x450(%r15), %rax
movq %rax, 0x450(%r15)
vxorps %xmm0, %xmm0, %xmm0
leaq 0x40(%rsp), %rdi
vmovaps %xmm0, (%rdi)
movq %rax, (%rdi)
movq %rdi, %rsi
movq %r15, %rdx
callq 0x1050
addq %r13, %rbp
movq %rsp, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x1100
leaq (%rsp,%rbx), %rdi
movq %rdi, 0x30(%rsp)
leaq 0x440(%r15), %rsi
movl $0x10, %ebp
subq %rbx, %rbp
movq %rbp, %rdx
callq 0x1100
leaq 0x420(%r15), %r14
leaq 0x1e8(%r15), %rdx
movq %rdx, 0x28(%rsp)
movq %r14, %rdi
movq %r14, %rsi
callq 0x10f0
movq %rsp, %rsi
vmovaps (%rsi), %xmm0
leaq 0x40(%rsp), %rax
vxorps (%rax), %xmm0, %xmm0
vmovaps %xmm0, (%rsi)
vxorps 0x420(%r15), %xmm0, %xmm0
vmovups %xmm0, 0x420(%r15)
movq %r14, %rdi
movq %r15, %rdx
callq 0x1050
leaq 0x430(%r15), %rdi
movq %rdi, 0x18(%rsp)
leaq 0x2dc(%r15), %rdx
movq %rdx, 0x20(%rsp)
movq %rdi, %rsi
callq 0x10f0
movq %rsp, %rsi
vmovdqa (%rsi), %xmm0
vmovdqu 0x430(%r15), %xmm1
vmovdqu %xmm0, 0x430(%r15)
leaq 0x40(%rsp), %rax
vpternlogq $0x96, (%rax), %xmm0, %xmm1
vmovdqa %xmm1, (%rsi)
addq %r13, %r12
movq %r12, %rdi
movq %rsi, %r13
movq %rbx, %rdx
callq 0x1100
movq 0x38(%rsp), %r12
movq %r12, %rdi
movq 0x30(%rsp), %rsi
movq %rbp, %rdx
callq 0x1100
movq %r14, %rdi
movq %r14, %rsi
movq 0x28(%rsp), %rdx
callq 0x10f0
vmovdqu 0x3e0(%r15), %xmm0
vmovdqu 0x420(%r15), %xmm1
vpternlogq $0x96, 0x440(%r15), %xmm0, %xmm1
vmovdqu %xmm1, 0x420(%r15)
movq %r14, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x1050
movq 0x18(%rsp), %rdi
movq %rdi, %rsi
movq 0x20(%rsp), %rdx
callq 0x10f0
vmovaps (%r13), %xmm0
vxorps 0x430(%r15), %xmm0, %xmm0
vmovaps %xmm0, (%r13)
vxorps 0x3e0(%r15), %xmm0, %xmm0
vmovaps %xmm0, (%r13)
addq %r12, %rbp
movq %rbp, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x1100
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq