// File: /workspace/github_c_makefile_star2_O3/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 $0x58, %rsp
movq %r9, 0x28(%rsp)
movq %rdx, %rbx
movq %rdi, %r15
testq %r8, %r8
je 0x3d09
movq %rbx, (%r8)
cmpq $0x10, %rbx
movq %rcx, 0x50(%rsp)
movq %rsi, 0x48(%rsp)
jbe 0x3de2
leaq 0x30c(%r15), %rax
movq %rax, (%rsp)
leaq 0x1e8(%r15), %rax
movq %rax, 0x8(%rsp)
leaq 0x31c(%r15), %r12
xorl %r13d, %r13d
movq %rcx, %r14
movq %rsi, %rbp
movq (%rsp), %rdi
movq %rdi, %rsi
movq 0x8(%rsp), %rdx
callq 0x10a0
movl $0x30c, %eax            # imm = 0x30C
movb -0x30c(%rbp,%rax), %cl
xorb %cl, (%r15,%rax)
incq %rax
cmpq $0x31c, %rax            # imm = 0x31C
jne 0x3d5a
movq (%rsp), %rdi
leaq 0x10(%rsp), %rsi
movq %r15, %rdx
callq 0x1100
movq %r12, %rdi
movq %r12, %rsi
movq 0x8(%rsp), %rdx
callq 0x10a0
xorl %eax, %eax
movb (%r12,%rax), %cl
xorb 0x10(%rsp,%rax), %cl
movb %cl, (%r14,%rax)
incq %rax
cmpq $0x10, %rax
jne 0x3d93
vmovaps 0x10(%rsp), %xmm0
vmovups %xmm0, (%r12)
movq 0x340(%r15), %rax
subq $-0x80, %rax
movq %rax, 0x340(%r15)
addq $-0x10, %rbx
addq $0x10, %r13
addq $0x10, %rbp
addq $0x10, %r14
cmpq $0x10, %rbx
ja 0x3d44
jmp 0x3dec
movq 0x340(%r15), %rax
xorl %r13d, %r13d
leaq (%rax,%rbx,8), %rax
movq %rax, 0x340(%r15)
vxorps %xmm0, %xmm0, %xmm0
leaq 0x10(%rsp), %rdi
vmovaps %xmm0, (%rdi)
vpbroadcastq %rax, %ymm0
vpsrlvq 0x338d(%rip), %ymm0, %ymm1 # 0x71a0
vpmovqb %ymm1, (%rdi)
vpsrlvq 0x339e(%rip), %ymm0, %ymm0 # 0x71c0
vpmovqb %ymm0, 0x4(%rdi)
movq %rdi, %rsi
movq %r15, %rdx
vzeroupper
callq 0x1100
movq 0x48(%rsp), %rsi
addq %r13, %rsi
leaq 0x30(%rsp), %rbp
movq %rbp, %rdi
movq %rbx, %rdx
callq 0x1110
leaq (%rsp,%rbx), %rdi
addq $0x30, %rdi
movq %rdi, 0x8(%rsp)
leaq 0x32c(%r15), %rsi
movl $0x10, %r12d
subq %rbx, %r12
movq %r12, %rdx
callq 0x1110
leaq 0x30c(%r15), %r14
leaq 0x1e8(%r15), %rdx
movq %rdx, (%rsp)
movq %r14, %rdi
movq %r14, %rsi
callq 0x10a0
vmovaps (%rbp), %xmm0
leaq 0x10(%rsp), %rax
vxorps (%rax), %xmm0, %xmm0
vmovaps %xmm0, (%rbp)
vxorps 0x30c(%r15), %xmm0, %xmm0
vmovups %xmm0, 0x30c(%r15)
movq %r14, %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x1100
leaq 0x31c(%r15), %rbp
movq %rbp, %rdi
movq %rbp, %rsi
movq (%rsp), %rdx
callq 0x10a0
leaq 0x30(%rsp), %rsi
vmovdqa (%rsi), %xmm0
vmovdqu 0x31c(%r15), %xmm1
vmovdqu %xmm0, 0x31c(%r15)
leaq 0x10(%rsp), %rax
vpternlogq $0x96, (%rax), %xmm0, %xmm1
vmovdqa %xmm1, (%rsi)
movq 0x50(%rsp), %rdi
addq %r13, %rdi
movq %rbx, %rdx
callq 0x1110
movq 0x28(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %r12, %rdx
callq 0x1110
movq %r14, %rdi
movq %r14, %rsi
movq (%rsp), %r13
movq %r13, %rdx
callq 0x10a0
vmovups 0x30c(%r15), %xmm0
vxorps 0x32c(%r15), %xmm0, %xmm0
vmovups %xmm0, 0x30c(%r15)
movq %r14, %rdi
leaq 0x30(%rsp), %r14
movq %r14, %rsi
movq %r15, %rdx
callq 0x1100
movq %rbp, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x10a0
vmovaps (%r14), %xmm0
vxorps 0x31c(%r15), %xmm0, %xmm0
vmovaps %xmm0, (%r14)
vxorps 0x32c(%r15), %xmm0, %xmm0
vmovaps %xmm0, (%r14)
addq 0x28(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x1110
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq