// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/aes128poetv2aes4/ref/poet.c
// Function: decrypt_final
int decrypt_final(poet_ctx_t *ctx,
                  const unsigned char *ciphertext,
                  unsigned long long clen,
                  const unsigned char tag[TAGLEN],
                  unsigned char *plaintext, 
                  unsigned long long* plen)
{
    uint64_t offset = 0;
    block s;
    block tmp;
    block tmp2;
    int alpha;
    int beta;

    if (plen) {
        *plen = clen;
    }

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

    // Encrypt the message length
    ctx->mlen += clen * 8;
    encode_length(s, ctx->mlen);
    aes_encrypt(s, s, &(ctx->aes_enc));

    // Pad the final ciphertext block if necessary
    memcpy(tmp, ciphertext + offset, clen);
    memcpy(tmp + clen, tag, BLOCKLEN - clen);

    // Process last block and generate the tag
    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);

    // Perform tag splitting if needed
    memcpy(plaintext + offset, tmp2, clen);
    alpha = compare_blocks(tmp2 + clen, ctx->tau, BLOCKLEN - clen);

    // Generate tag
    TOP_HASH;
    xor_block(ctx->x, ctx->tau , ctx->x);
    aes_encrypt(ctx->x, tmp, &(ctx->aes_enc));

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

    beta = compare_blocks(tmp, tag + (BLOCKLEN - clen), clen);
    return alpha | beta;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %r8, 0x8(%rsp)
movq %rcx, 0x28(%rsp)
movq %rdx, %r12
movq %rsi, (%rsp)
movq %rdi, 0x10(%rsp)
testq %r9, %r9
je 0x3f60
movq %r12, (%r9)
movq 0x10(%rsp), %rax
leaq 0x31c(%rax), %rcx
movq %rcx, 0x30(%rsp)
leaq 0x1e8(%rax), %rcx
movq %rcx, 0x48(%rsp)
leaq 0xf4(%rax), %rcx
movq %rcx, 0x38(%rsp)
addq $0x30c, %rax            # imm = 0x30C
movq %rax, 0x40(%rsp)
leaq (,%r12,8), %rbx
negq %rbx
xorl %r13d, %r13d
movq %r12, 0x20(%rsp)
movq 0x48(%rsp), %r14
cmpq $0x11, %r12
jb 0x4057
movq %r12, %rbp
movq %rbx, 0x18(%rsp)
movq (%rsp), %rax
leaq (%rax,%r13), %r12
movq 0x8(%rsp), %rax
leaq (%rax,%r13), %rbx
movq 0x30(%rsp), %r15
movq %r15, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x10a0
movq %r15, %rdi
movq %r12, %rsi
movq %rbp, %r12
movq %r15, %rdx
callq 0x3bd2
movq %r15, %rdi
leaq 0x50(%rsp), %r15
movq %r15, %rsi
movq 0x38(%rsp), %rdx
callq 0x10f0
movq 0x40(%rsp), %rbp
movq %rbp, %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x10a0
movq %rbx, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x3bd2
vmovaps 0x50(%rsp), %xmm0
vmovups %xmm0, (%rbp)
movq 0x10(%rsp), %rax
subq $-0x80, 0x340(%rax)
movq 0x18(%rsp), %rbx
addq $-0x10, %r12
addq $0x10, %r13
subq $-0x80, %rbx
jmp 0x3fac
movq 0x10(%rsp), %rbp
movq 0x340(%rbp), %rsi
subq %rbx, %rsi
movq %rsi, 0x340(%rbp)
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
callq 0x3f10
movq %rbx, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x1100
movq (%rsp), %rsi
addq %r13, %rsi
movq %r13, %rbx
movq %r13, 0x60(%rsp)
leaq 0x70(%rsp), %r13
movq %r13, %rdi
movq %r12, %rdx
callq 0x1110
leaq (%rsp,%r12), %rdi
addq $0x70, %rdi
movq %r12, 0x68(%rsp)
movq %rbx, %rax
subq 0x20(%rsp), %rax
movq %rax, 0x18(%rsp)
leaq 0x10(%rax), %rdx
movq %rdx, (%rsp)
movq 0x28(%rsp), %rsi
callq 0x1110
movq 0x30(%rsp), %r15
movq %r15, %rdi
movq %r15, %rsi
movq 0x48(%rsp), %r14
movq %r14, %rdx
callq 0x10a0
movq %r13, %rbx
movq %r13, %rdi
leaq 0x50(%rsp), %rsi
movq %r13, %rdx
callq 0x3bd2
movq %r15, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x3bd2
movq %r15, %rdi
movq %r13, %rsi
movq 0x38(%rsp), %rdx
callq 0x10f0
movq 0x40(%rsp), %r12
movq %r12, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x10a0
leaq 0x80(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x3bd2
movq %r13, %rdi
leaq 0x50(%rsp), %rsi
movq %r13, %rdx
callq 0x3bd2
vmovaps (%rbx), %xmm0
vmovups %xmm0, (%r12)
movq 0x8(%rsp), %rdi
movq 0x60(%rsp), %rbx
addq %rbx, %rdi
movq %r13, %rsi
movq 0x68(%rsp), %r13
movq %r13, %rdx
callq 0x1110
movq 0x20(%rsp), %rax
leaq (%rsp,%rax), %rdi
addq $0x80, %rdi
subq %rbx, %rdi
leaq 0x32c(%rbp), %rbx
movq %rbx, %rsi
movq (%rsp), %rdx
callq 0x4232
movl %eax, 0x8(%rsp)
movq %r12, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x10a0
movq %r12, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x3bd2
movq %r12, %rdi
leaq 0x70(%rsp), %r12
movq %r12, %rsi
movq %rbp, %rdx
callq 0x1100
movq %r15, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x10a0
movq %r12, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x3bd2
movq %r12, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x3bd2
movq 0x18(%rsp), %rax
movq 0x28(%rsp), %rcx
leaq (%rax,%rcx), %rsi
addq $0x10, %rsi
movq %r12, %rdi
movq %r13, %rdx
callq 0x4232
orl 0x8(%rsp), %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq