// File: /workspace/github_c_makefile_star2_O0/mjosaarinen[P]brutus/crypto_aead/aes128poetv1aes4/ref/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);
  AES_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);

  AES_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);

  AES_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:
subq $0x78, %rsp
movq %rdi, 0x70(%rsp)
movq %rsi, 0x68(%rsp)
movq %rdx, 0x60(%rsp)
movq %rcx, 0x58(%rsp)
movq %r8, 0x50(%rsp)
movq $0x0, 0x48(%rsp)
cmpq $0x10, 0x60(%rsp)
jbe 0x6eca
movq 0x70(%rsp), %rdi
movq 0x68(%rsp), %rsi
addq 0x48(%rsp), %rsi
movq 0x50(%rsp), %rdx
addq 0x48(%rsp), %rdx
callq 0x10a0
movq 0x60(%rsp), %rax
subq $0x10, %rax
movq %rax, 0x60(%rsp)
movq 0x48(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x48(%rsp)
jmp 0x6e86
movq 0x60(%rsp), %rcx
shlq $0x3, %rcx
movq 0x70(%rsp), %rax
addq 0x450(%rax), %rcx
movq %rcx, 0x450(%rax)
leaq 0x30(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x1030
movq 0x70(%rsp), %rax
movq 0x450(%rax), %rax
movq %rax, (%rsp)
movq (%rsp), %rax
movq %rax, 0x30(%rsp)
leaq 0x30(%rsp), %rdi
leaq 0x30(%rsp), %rsi
movq 0x70(%rsp), %rdx
callq 0x1060
leaq 0x20(%rsp), %rdi
movq 0x68(%rsp), %rsi
addq 0x48(%rsp), %rsi
movq 0x60(%rsp), %rdx
callq 0x1120
leaq 0x20(%rsp), %rdi
addq 0x60(%rsp), %rdi
movq 0x58(%rsp), %rsi
movl $0x10, %edx
subq 0x60(%rsp), %rdx
callq 0x1120
movq 0x70(%rsp), %rdi
addq $0x430, %rdi            # imm = 0x430
movq 0x70(%rsp), %rsi
addq $0x430, %rsi            # imm = 0x430
movq 0x70(%rsp), %rdx
addq $0x2dc, %rdx            # imm = 0x2DC
callq 0x1100
leaq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rsi
leaq 0x20(%rsp), %rdx
callq 0x1110
movq 0x70(%rsp), %rdi
addq $0x430, %rdi            # imm = 0x430
leaq 0x20(%rsp), %rsi
movq 0x70(%rsp), %rdx
addq $0x430, %rdx            # imm = 0x430
callq 0x1110
movq 0x70(%rsp), %rdi
addq $0x430, %rdi            # imm = 0x430
leaq 0x20(%rsp), %rsi
movq 0x70(%rsp), %rdx
addq $0xf4, %rdx
callq 0x10c0
movq 0x70(%rsp), %rdi
addq $0x420, %rdi            # imm = 0x420
movq 0x70(%rsp), %rsi
addq $0x420, %rsi            # imm = 0x420
movq 0x70(%rsp), %rdx
addq $0x1e8, %rdx            # imm = 0x1E8
callq 0x1100
leaq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rsi
movq 0x70(%rsp), %rdx
addq $0x420, %rdx            # imm = 0x420
callq 0x1110
leaq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0x1110
movq 0x70(%rsp), %rax
movq 0x20(%rsp), %rcx
movq %rcx, 0x420(%rax)
movq 0x28(%rsp), %rcx
movq %rcx, 0x428(%rax)
movq 0x50(%rsp), %rdi
addq 0x48(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movq 0x60(%rsp), %rdx
callq 0x1120
leaq 0x10(%rsp), %rdi
addq 0x60(%rsp), %rdi
movq 0x70(%rsp), %rsi
addq $0x440, %rsi            # imm = 0x440
movl $0x10, %edx
subq 0x60(%rsp), %rdx
callq 0x10f0
movl %eax, 0xc(%rsp)
movq 0x70(%rsp), %rdi
addq $0x420, %rdi            # imm = 0x420
movq 0x70(%rsp), %rsi
addq $0x420, %rsi            # imm = 0x420
movq 0x70(%rsp), %rdx
addq $0x1e8, %rdx            # imm = 0x1E8
callq 0x1100
movq 0x70(%rsp), %rdi
addq $0x420, %rdi            # imm = 0x420
movq 0x70(%rsp), %rsi
addq $0x440, %rsi            # imm = 0x440
movq 0x70(%rsp), %rdx
addq $0x420, %rdx            # imm = 0x420
callq 0x1110
movq 0x70(%rsp), %rdi
addq $0x420, %rdi            # imm = 0x420
movq 0x70(%rsp), %rsi
addq $0x3e0, %rsi            # imm = 0x3E0
movq 0x70(%rsp), %rdx
addq $0x420, %rdx            # imm = 0x420
callq 0x1110
movq 0x70(%rsp), %rdi
addq $0x420, %rdi            # imm = 0x420
leaq 0x20(%rsp), %rsi
movq 0x70(%rsp), %rdx
callq 0x1060
movq 0x70(%rsp), %rdi
addq $0x430, %rdi            # imm = 0x430
movq 0x70(%rsp), %rsi
addq $0x430, %rsi            # imm = 0x430
movq 0x70(%rsp), %rdx
addq $0x2dc, %rdx            # imm = 0x2DC
callq 0x1100
leaq 0x20(%rsp), %rdi
movq 0x70(%rsp), %rsi
addq $0x430, %rsi            # imm = 0x430
leaq 0x20(%rsp), %rdx
callq 0x1110
leaq 0x20(%rsp), %rdi
movq 0x70(%rsp), %rsi
addq $0x3e0, %rsi            # imm = 0x3E0
leaq 0x20(%rsp), %rdx
callq 0x1110
leaq 0x20(%rsp), %rdi
movq 0x58(%rsp), %rsi
movl $0x10, %eax
subq 0x60(%rsp), %rax
addq %rax, %rsi
movq 0x60(%rsp), %rdx
callq 0x10f0
movl %eax, 0x8(%rsp)
movl 0xc(%rsp), %eax
orl 0x8(%rsp), %eax
addq $0x78, %rsp
retq
nopw (%rax,%rax)