// File: /workspace/github_c_makefile_star2_O1/mjosaarinen[P]brutus/crypto_aead/aes128poetv1aes4/ni/poet.c
// Function: decrypt_final
int decrypt_final(struct poet_ctx *ctx, const uint8_t *ciphertext,
		   uint64_t clen, const uint8_t tag[BLOCKLEN],
		   uint8_t *plaintext)
{
#ifdef DEBUG
  int i;
#endif
  uint64_t offset=0;
  block s;
  block tmp;
  block tmp2;
  int alpha;
  int beta;
  uint64_t len;

  while( clen > BLOCKLEN )
    {
      decrypt_block(ctx, ciphertext+offset, plaintext+offset);
      clen   -= BLOCKLEN;
      offset += BLOCKLEN;
    }

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


  /* Last ciphertext block must be padded if necesscary */
  memcpy(tmp,ciphertext+offset,clen);
  memcpy(tmp+clen,tag,BLOCKLEN-clen);


  /* Process last block + tag generation */
  BOTTOM_HASH;
  xor_block(tmp,s,tmp);
  xor_block(ctx->y, tmp,ctx->y);

  AESNI_decrypt(ctx->y, tmp, ctx->aes_dec);

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


#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(plaintext+offset,tmp2,clen);

  alpha = memcmp(tmp2+clen,ctx->tau,BLOCKLEN-clen);

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

  AESNI_encrypt(ctx->x, tmp, ctx->aes_enc);

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

#ifdef DEBUG
  puts("tmp"); for(i=0;i<16;i++) printf("%02x ",tmp[i]); puts("");
  puts("tag"); for(i=0;i<16;i++) printf("%02x ",tag[i]); puts("");
#endif

  beta = memcmp(tmp,tag+(BLOCKLEN-clen),clen);

  return alpha|beta;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r8, %r12
movq %rcx, 0x8(%rsp)
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
xorl %ebp, %ebp
cmpq $0x11, %rdx
jb 0x1efa
leaq (%r14,%rbp), %rsi
leaq (%r12,%rbp), %rdx
movq %r15, %rdi
callq 0x10c0
addq $-0x10, %rbx
addq $0x10, %rbp
cmpq $0x10, %rbx
ja 0x1edc
leaq (,%rbx,8), %rax
addq 0x280(%r15), %rax
movq %rax, 0x280(%r15)
vxorps %xmm0, %xmm0, %xmm0
leaq 0x40(%rsp), %rdi
vmovaps %xmm0, (%rdi)
movq %rax, (%rdi)
movq %rdi, %rsi
movq %r15, %rdx
callq 0x1120
addq %rbp, %r14
leaq 0x10(%rsp), %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x1140
leaq (%rsp,%rbx), %rdi
addq $0x10, %rdi
movl $0x10, %r14d
subq %rbx, %r14
movq 0x8(%rsp), %rsi
movq %r14, %rdx
callq 0x1140
leaq 0x260(%r15), %r13
leaq 0x1b0(%r15), %rdx
movq %rdx, 0x38(%rsp)
movq %r13, %rdi
movq %r13, %rsi
movq %r13, 0x30(%rsp)
callq 0x1030
leaq 0x10(%rsp), %rsi
vmovaps (%rsi), %xmm0
leaq 0x40(%rsp), %rax
vxorps (%rax), %xmm0, %xmm0
vmovaps %xmm0, (%rsi)
vxorps 0x260(%r15), %xmm0, %xmm0
vmovups %xmm0, 0x260(%r15)
leaq 0xb0(%r15), %rdx
movq %r13, %rdi
callq 0x1070
leaq 0x250(%r15), %r13
leaq 0x160(%r15), %rdx
movq %rdx, 0x28(%rsp)
movq %r13, %rdi
movq %r13, %rsi
callq 0x1030
leaq 0x10(%rsp), %rax
vmovdqa (%rax), %xmm0
vmovdqu 0x250(%r15), %xmm1
leaq 0x40(%rsp), %rax
vpternlogq $0x96, (%rax), %xmm0, %xmm1
leaq 0x50(%rsp), %rsi
vmovdqa %xmm1, (%rsi)
vmovdqa %xmm0, 0x250(%r15)
addq %rbp, %r12
movq %r12, %rdi
movq %rbx, %rdx
callq 0x1140
leaq (%rsp,%rbx), %rdi
addq $0x50, %rdi
leaq 0x270(%r15), %rsi
movq %r14, %rdx
callq 0x1130
movl %eax, %r12d
movq %r13, %rdi
movq %r13, %rsi
movq 0x28(%rsp), %rdx
callq 0x1030
vmovdqu 0x210(%r15), %xmm0
vmovdqu 0x250(%r15), %xmm1
vpternlogq $0x96, 0x270(%r15), %xmm0, %xmm1
vmovdqu %xmm1, 0x250(%r15)
movq %r13, %rdi
leaq 0x10(%rsp), %r13
movq %r13, %rsi
movq %r15, %rdx
callq 0x1120
movq 0x30(%rsp), %rdi
movq %rdi, %rsi
movq 0x38(%rsp), %rdx
callq 0x1030
vmovaps (%r13), %xmm0
vxorps 0x260(%r15), %xmm0, %xmm0
vmovaps %xmm0, (%r13)
vxorps 0x210(%r15), %xmm0, %xmm0
vmovaps %xmm0, (%r13)
addq 0x8(%rsp), %r14
movq %r13, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x1130
orl %r12d, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)