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

    if (clen) {
        *clen = plen;
    }

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

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

    // Last message block must be padded if necessary
    memcpy(tmp, plaintext + offset, plen);
    memcpy(tmp + plen, ctx->tau, BLOCKLEN - plen);

    // Process last block + generate the tag
    TOP_HASH;

    xor_block(tmp, s, tmp);
    xor_block(ctx->x, tmp, ctx->x);

    aes_encrypt(ctx->x, tmp, &(ctx->aes_enc));

    BOTTOM_HASH;
    
    xor_block(tmp2, tmp, ctx->y);
    memcpy(ctx->y, tmp, BLOCKLEN);
    xor_block(tmp, s, tmp2);

    // Perform tag splitting if needed
    memcpy(ciphertext + offset, tmp, plen);
    memcpy(tag, tmp + plen, BLOCKLEN - plen);

    // 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);
    
    memcpy(tag + (BLOCKLEN - plen), tmp, plen);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %r9, 0x28(%rsp)
movq %rcx, 0x38(%rsp)
movq %rdx, %r12
movq %rsi, (%rsp)
movq %rdi, 0x18(%rsp)
testq %r8, %r8
je 0x3c58
movq %r12, (%r8)
movq 0x18(%rsp), %rax
leaq 0x30c(%rax), %rcx
movq %rcx, 0x30(%rsp)
leaq 0x1e8(%rax), %rcx
movq %rcx, 0x10(%rsp)
addq $0x31c, %rax            # imm = 0x31C
movq %rax, 0x40(%rsp)
leaq (,%r12,8), %rax
negq %rax
xorl %ebx, %ebx
movq %r12, 0x8(%rsp)
cmpq $0x11, %r12
movq %r12, 0x48(%rsp)
jb 0x3d4b
movq %rax, 0x20(%rsp)
movq (%rsp), %rax
leaq (%rax,%rbx), %r13
movq 0x38(%rsp), %rax
leaq (%rax,%rbx), %r12
movq 0x30(%rsp), %r14
movq %r14, %rdi
movq %r14, %rsi
movq %rbx, %rbp
movq 0x10(%rsp), %rbx
movq %rbx, %rdx
callq 0x10a0
movq %r14, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x3bd2
movq %r14, %rdi
leaq 0x60(%rsp), %r15
movq %r15, %rsi
movq 0x18(%rsp), %r14
movq %r14, %rdx
callq 0x1100
movq 0x40(%rsp), %r13
movq %r13, %rdi
movq %r13, %rsi
movq %rbx, %rdx
movq %rbp, %rbx
callq 0x10a0
movq %r12, %rdi
movq 0x48(%rsp), %r12
movq %r15, %rsi
movq %r13, %rdx
callq 0x3bd2
movq 0x20(%rsp), %rax
vmovaps 0x60(%rsp), %xmm0
vmovups %xmm0, (%r13)
subq $-0x80, 0x340(%r14)
addq $-0x10, %r12
addq $0x10, %rbx
subq $-0x80, %rax
jmp 0x3c92
movq 0x18(%rsp), %r13
movq 0x340(%r13), %rsi
subq %rax, %rsi
movq %rsi, 0x340(%r13)
leaq 0x60(%rsp), %r14
movq %r14, %rdi
callq 0x3f10
movq %r14, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x1100
movq (%rsp), %rsi
addq %rbx, %rsi
leaq 0x50(%rsp), %r15
movq %r15, %rdi
movq %r12, %rdx
callq 0x1110
leaq (%rsp,%r12), %rdi
addq $0x50, %rdi
movq %rdi, 0x78(%rsp)
leaq 0x32c(%r13), %rsi
movq %rsi, (%rsp)
movq %rbx, %rax
subq 0x8(%rsp), %rax
movq %rax, 0x20(%rsp)
leaq 0x10(%rax), %rdx
movq %rdx, 0x8(%rsp)
callq 0x1110
movq 0x30(%rsp), %rbp
movq %rbp, %rdi
movq %rbp, %rsi
movq 0x10(%rsp), %r14
movq %r14, %rdx
callq 0x10a0
movq %r15, %rdi
leaq 0x60(%rsp), %rsi
movq %r15, %rdx
callq 0x3bd2
movq %rbp, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x3bd2
movq %rbp, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x1100
movq 0x40(%rsp), %r15
movq %r15, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x10a0
leaq 0x80(%rsp), %rdi
leaq 0x50(%rsp), %r14
movq %r14, %rsi
movq %r15, %rdx
callq 0x3bd2
vmovaps (%r14), %xmm0
vmovups %xmm0, (%r15)
movq %r14, %rdi
leaq 0x60(%rsp), %rsi
leaq 0x80(%rsp), %rdx
callq 0x3bd2
movq 0x38(%rsp), %rdi
addq %rbx, %rdi
leaq 0x50(%rsp), %rsi
movq %r12, %rdx
callq 0x1110
movq 0x28(%rsp), %rdi
movq 0x78(%rsp), %rsi
movq 0x8(%rsp), %rdx
callq 0x1110
movq %rbp, %rdi
movq %rbp, %rsi
movq 0x10(%rsp), %r14
movq %r14, %rdx
callq 0x10a0
movq %rbp, %rdi
movq (%rsp), %rbx
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x3bd2
movq %rbp, %rdi
leaq 0x50(%rsp), %r12
movq %r12, %rsi
movq %r13, %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 0x20(%rsp), %rax
movq 0x28(%rsp), %rcx
leaq (%rax,%rcx), %rdi
addq $0x10, %rdi
movq %r12, %rsi
movq 0x48(%rsp), %rdx
callq 0x1110
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq