// File: /workspace/github_c_makefile_star2_O1/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 0x3d15
movq %rbx, (%r8)
cmpq $0x11, %rbx
movq %rcx, 0x50(%rsp)
movq %rsi, 0x48(%rsp)
jb 0x3de4
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 0x3d66
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 0x3d9f
vmovaps 0x10(%rsp), %xmm0
vmovups %xmm0, (%r12)
subq $-0x80, 0x340(%r15)
addq $-0x10, %rbx
addq $0x10, %r13
addq $0x10, %rbp
addq $0x10, %r14
cmpq $0x10, %rbx
ja 0x3d50
jmp 0x3de7
xorl %r13d, %r13d
leaq (,%rbx,8), %rax
addq 0x340(%r15), %rax
movq %rax, 0x340(%r15)
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x10(%rsp)
vpbroadcastq %rax, %ymm0
vpmovsxbq 0x334e(%rip), %ymm1 # 0x7164
xorl %eax, %eax
vpbroadcastq 0x347f(%rip), %ymm2 # 0x72a0
vpsllq $0x3, %ymm1, %ymm3
vpsrlvq %ymm3, %ymm0, %ymm3
vpmovqb %ymm3, 0x10(%rsp,%rax)
addq $0x4, %rax
vpaddq %ymm2, %ymm1, %ymm1
cmpq $0x8, %rax
jne 0x3e21
leaq 0x10(%rsp), %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, %r14d
subq %rbx, %r14
movq %r14, %rdx
callq 0x1110
leaq 0x30c(%r15), %r12
leaq 0x1e8(%r15), %rdx
movq %rdx, (%rsp)
movq %r12, %rdi
movq %r12, %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 %r12, %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 %r14, %rdx
callq 0x1110
movq %r12, %rdi
movq %r12, %rsi
movq (%rsp), %r13
movq %r13, %rdx
callq 0x10a0
vmovups 0x30c(%r15), %xmm0
vxorps 0x32c(%r15), %xmm0, %xmm0
vmovups %xmm0, 0x30c(%r15)
movq %r12, %rdi
leaq 0x30(%rsp), %r12
movq %r12, %rsi
movq %r15, %rdx
callq 0x1100
movq %rbp, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x10a0
vmovaps (%r12), %xmm0
vxorps 0x31c(%r15), %xmm0, %xmm0
vmovaps %xmm0, (%r12)
vxorps 0x32c(%r15), %xmm0, %xmm0
vmovaps %xmm0, (%r12)
addq 0x28(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x1110
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq