// File: /workspace/github_c_makefile_star2_O2/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, %rbp
movq %rcx, 0x8(%rsp)
movq %rdx, %r14
movq %rsi, %rbx
movq %rdi, %r15
leaq (,%rdx,8), %r13
negq %r13
xorl %r12d, %r12d
movq %rdx, (%rsp)
cmpq $0x11, %r14
jb 0x1e77
leaq (%rbx,%r12), %rsi
leaq (%r12,%rbp), %rdx
movq %r15, %rdi
callq 0x10c0
addq $-0x10, %r14
addq $0x10, %r12
subq $-0x80, %r13
jmp 0x1e53
movq 0x280(%r15), %rax
subq %r13, %rax
movq %rax, 0x280(%r15)
vxorps %xmm0, %xmm0, %xmm0
leaq 0x50(%rsp), %rdi
vmovaps %xmm0, (%rdi)
movq %rax, (%rdi)
movq %rdi, %rsi
movq %r15, %rdx
callq 0x1120
addq %r12, %rbx
leaq 0x10(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x1150
leaq (%rsp,%r14), %rdi
addq $0x10, %rdi
movq %r12, %rax
subq (%rsp), %rax
movq %rax, 0x48(%rsp)
leaq 0x10(%rax), %rdx
movq %rdx, 0x38(%rsp)
movq 0x8(%rsp), %rsi
callq 0x1150
leaq 0x260(%r15), %rbx
leaq 0x1b0(%r15), %rdx
movq %rdx, 0x40(%rsp)
movq %rbx, %rdi
movq %rbx, %rsi
callq 0x1030
movq %r13, %rdi
leaq 0x50(%rsp), %rsi
movq %r13, %rdx
callq 0x1140
movq %rbx, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x1140
leaq 0xb0(%r15), %rdx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1070
leaq 0x250(%r15), %r13
leaq 0x160(%r15), %rdx
movq %rdx, 0x30(%rsp)
movq %r13, %rdi
movq %r13, %rsi
callq 0x1030
leaq 0x20(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movq %r13, %rdx
callq 0x1140
leaq 0x20(%rsp), %rdi
leaq 0x50(%rsp), %rsi
movq %rdi, %rdx
callq 0x1140
leaq 0x10(%rsp), %rax
vmovaps (%rax), %xmm0
vmovaps %xmm0, 0x250(%r15)
addq %r12, %rbp
movq %rbp, %rdi
leaq 0x20(%rsp), %rsi
movq %r14, %rdx
callq 0x1150
movq (%rsp), %rax
leaq (%rsp,%rax), %rdi
addq $0x20, %rdi
subq %r12, %rdi
leaq 0x270(%r15), %r12
movq %r12, %rsi
movq 0x38(%rsp), %rdx
callq 0x1130
movl %eax, %ebp
movq %r13, %rdi
movq %r13, %rsi
movq 0x30(%rsp), %rdx
callq 0x1030
movq %r13, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x1140
leaq 0x210(%r15), %r12
movq %r13, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x1140
movq %r13, %rdi
leaq 0x10(%rsp), %r13
movq %r13, %rsi
movq %r15, %rdx
callq 0x1120
movq %rbx, %rdi
movq %rbx, %rsi
movq 0x40(%rsp), %rdx
callq 0x1030
movq %r13, %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x1140
movq %r13, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x1140
movq 0x48(%rsp), %rax
movq 0x8(%rsp), %rcx
leaq (%rax,%rcx), %rsi
addq $0x10, %rsi
movq %r13, %rdi
movq %r14, %rdx
callq 0x1130
orl %ebp, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop