// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x78, %rsp
movq %rdi, 0x70(%rsp)
movq %rsi, 0x68(%rsp)
movq %rdx, 0x60(%rsp)
movq %rcx, 0x58(%rsp)
movq %r8, 0x50(%rsp)
movq %r9, 0x48(%rsp)
movq $0x0, 0x40(%rsp)
cmpq $0x0, 0x48(%rsp)
je 0x6ad0
movq 0x60(%rsp), %rcx
movq 0x48(%rsp), %rax
movq %rcx, (%rax)
jmp 0x6ad2
cmpq $0x10, 0x60(%rsp)
jbe 0x6b16
movq 0x70(%rsp), %rdi
movq 0x68(%rsp), %rsi
addq 0x40(%rsp), %rsi
movq 0x50(%rsp), %rdx
addq 0x40(%rsp), %rdx
callq 0x6dd0
movq 0x60(%rsp), %rax
subq $0x10, %rax
movq %rax, 0x60(%rsp)
movq 0x40(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x40(%rsp)
jmp 0x6ad2
movq 0x60(%rsp), %rcx
shlq $0x3, %rcx
movq 0x70(%rsp), %rax
addq 0x340(%rax), %rcx
movq %rcx, 0x340(%rax)
leaq 0x30(%rsp), %rdi
movq 0x70(%rsp), %rax
movq 0x340(%rax), %rsi
callq 0x6a50
leaq 0x30(%rsp), %rdi
leaq 0x30(%rsp), %rsi
movq 0x70(%rsp), %rdx
callq 0x1110
leaq 0x20(%rsp), %rdi
movq 0x68(%rsp), %rsi
addq 0x40(%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 $0x31c, %rdi            # imm = 0x31C
movq 0x70(%rsp), %rsi
addq $0x31c, %rsi            # imm = 0x31C
movq 0x70(%rsp), %rdx
addq $0x1e8, %rdx            # imm = 0x1E8
callq 0x10b0
leaq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rsi
leaq 0x20(%rsp), %rdx
callq 0x6590
movq 0x70(%rsp), %rdi
addq $0x31c, %rdi            # imm = 0x31C
leaq 0x20(%rsp), %rsi
movq 0x70(%rsp), %rdx
addq $0x31c, %rdx            # imm = 0x31C
callq 0x6590
movq 0x70(%rsp), %rdi
addq $0x31c, %rdi            # imm = 0x31C
leaq 0x20(%rsp), %rsi
movq 0x70(%rsp), %rdx
addq $0xf4, %rdx
callq 0x1100
movq 0x70(%rsp), %rdi
addq $0x30c, %rdi            # imm = 0x30C
movq 0x70(%rsp), %rsi
addq $0x30c, %rsi            # imm = 0x30C
movq 0x70(%rsp), %rdx
addq $0x1e8, %rdx            # imm = 0x1E8
callq 0x10b0
leaq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rsi
movq 0x70(%rsp), %rdx
addq $0x30c, %rdx            # imm = 0x30C
callq 0x6590
leaq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0x6590
movq 0x70(%rsp), %rax
movq 0x20(%rsp), %rcx
movq %rcx, 0x30c(%rax)
movq 0x28(%rsp), %rcx
movq %rcx, 0x314(%rax)
movq 0x50(%rsp), %rdi
addq 0x40(%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 $0x32c, %rsi            # imm = 0x32C
movl $0x10, %edx
subq 0x60(%rsp), %rdx
callq 0x6ed0
movl %eax, 0xc(%rsp)
movq 0x70(%rsp), %rdi
addq $0x30c, %rdi            # imm = 0x30C
movq 0x70(%rsp), %rsi
addq $0x30c, %rsi            # imm = 0x30C
movq 0x70(%rsp), %rdx
addq $0x1e8, %rdx            # imm = 0x1E8
callq 0x10b0
movq 0x70(%rsp), %rdi
addq $0x30c, %rdi            # imm = 0x30C
movq 0x70(%rsp), %rsi
addq $0x32c, %rsi            # imm = 0x32C
movq 0x70(%rsp), %rdx
addq $0x30c, %rdx            # imm = 0x30C
callq 0x6590
movq 0x70(%rsp), %rdi
addq $0x30c, %rdi            # imm = 0x30C
leaq 0x20(%rsp), %rsi
movq 0x70(%rsp), %rdx
callq 0x1110
movq 0x70(%rsp), %rdi
addq $0x31c, %rdi            # imm = 0x31C
movq 0x70(%rsp), %rsi
addq $0x31c, %rsi            # imm = 0x31C
movq 0x70(%rsp), %rdx
addq $0x1e8, %rdx            # imm = 0x1E8
callq 0x10b0
leaq 0x20(%rsp), %rdi
movq 0x70(%rsp), %rsi
addq $0x31c, %rsi            # imm = 0x31C
leaq 0x20(%rsp), %rdx
callq 0x6590
leaq 0x20(%rsp), %rdi
movq 0x70(%rsp), %rsi
addq $0x32c, %rsi            # imm = 0x32C
leaq 0x20(%rsp), %rdx
callq 0x6590
leaq 0x20(%rsp), %rdi
movq 0x58(%rsp), %rsi
movl $0x10, %eax
subq 0x60(%rsp), %rax
addq %rax, %rsi
movq 0x60(%rsp), %rdx
callq 0x6ed0
movl %eax, 0x8(%rsp)
movl 0xc(%rsp), %eax
orl 0x8(%rsp), %eax
addq $0x78, %rsp
retq
nopl (%rax)