// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x68, %rsp
movq %rdi, 0x60(%rsp)
movq %rsi, 0x58(%rsp)
movq %rdx, 0x50(%rsp)
movq %rcx, 0x48(%rsp)
movq %r8, 0x40(%rsp)
movq %r9, 0x38(%rsp)
movq $0x0, 0x30(%rsp)
cmpq $0x0, 0x40(%rsp)
je 0x6670
movq 0x50(%rsp), %rcx
movq 0x40(%rsp), %rax
movq %rcx, (%rax)
jmp 0x6672
cmpq $0x10, 0x50(%rsp)
jbe 0x66b6
movq 0x60(%rsp), %rdi
movq 0x58(%rsp), %rsi
addq 0x30(%rsp), %rsi
movq 0x48(%rsp), %rdx
addq 0x30(%rsp), %rdx
callq 0x6950
movq 0x50(%rsp), %rax
subq $0x10, %rax
movq %rax, 0x50(%rsp)
movq 0x30(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x30(%rsp)
jmp 0x6672
movq 0x50(%rsp), %rcx
shlq $0x3, %rcx
movq 0x60(%rsp), %rax
addq 0x340(%rax), %rcx
movq %rcx, 0x340(%rax)
leaq 0x20(%rsp), %rdi
movq 0x60(%rsp), %rax
movq 0x340(%rax), %rsi
callq 0x6a50
leaq 0x20(%rsp), %rdi
leaq 0x20(%rsp), %rsi
movq 0x60(%rsp), %rdx
callq 0x1110
leaq 0x10(%rsp), %rdi
movq 0x58(%rsp), %rsi
addq 0x30(%rsp), %rsi
movq 0x50(%rsp), %rdx
callq 0x1120
leaq 0x10(%rsp), %rdi
addq 0x50(%rsp), %rdi
movq 0x60(%rsp), %rsi
addq $0x32c, %rsi            # imm = 0x32C
movl $0x10, %edx
subq 0x50(%rsp), %rdx
callq 0x1120
movq 0x60(%rsp), %rdi
addq $0x30c, %rdi            # imm = 0x30C
movq 0x60(%rsp), %rsi
addq $0x30c, %rsi            # imm = 0x30C
movq 0x60(%rsp), %rdx
addq $0x1e8, %rdx            # imm = 0x1E8
callq 0x10b0
leaq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0x6590
movq 0x60(%rsp), %rdi
addq $0x30c, %rdi            # imm = 0x30C
leaq 0x10(%rsp), %rsi
movq 0x60(%rsp), %rdx
addq $0x30c, %rdx            # imm = 0x30C
callq 0x6590
movq 0x60(%rsp), %rdi
addq $0x30c, %rdi            # imm = 0x30C
leaq 0x10(%rsp), %rsi
movq 0x60(%rsp), %rdx
callq 0x1110
movq 0x60(%rsp), %rdi
addq $0x31c, %rdi            # imm = 0x31C
movq 0x60(%rsp), %rsi
addq $0x31c, %rsi            # imm = 0x31C
movq 0x60(%rsp), %rdx
addq $0x1e8, %rdx            # imm = 0x1E8
callq 0x10b0
movq %rsp, %rdi
leaq 0x10(%rsp), %rsi
movq 0x60(%rsp), %rdx
addq $0x31c, %rdx            # imm = 0x31C
callq 0x6590
movq 0x60(%rsp), %rax
movq 0x10(%rsp), %rcx
movq %rcx, 0x31c(%rax)
movq 0x18(%rsp), %rcx
movq %rcx, 0x324(%rax)
leaq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rsi
movq %rsp, %rdx
callq 0x6590
movq 0x48(%rsp), %rdi
addq 0x30(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movq 0x50(%rsp), %rdx
callq 0x1120
movq 0x38(%rsp), %rdi
leaq 0x10(%rsp), %rsi
addq 0x50(%rsp), %rsi
movl $0x10, %edx
subq 0x50(%rsp), %rdx
callq 0x1120
movq 0x60(%rsp), %rdi
addq $0x30c, %rdi            # imm = 0x30C
movq 0x60(%rsp), %rsi
addq $0x30c, %rsi            # imm = 0x30C
movq 0x60(%rsp), %rdx
addq $0x1e8, %rdx            # imm = 0x1E8
callq 0x10b0
movq 0x60(%rsp), %rdi
addq $0x30c, %rdi            # imm = 0x30C
movq 0x60(%rsp), %rsi
addq $0x32c, %rsi            # imm = 0x32C
movq 0x60(%rsp), %rdx
addq $0x30c, %rdx            # imm = 0x30C
callq 0x6590
movq 0x60(%rsp), %rdi
addq $0x30c, %rdi            # imm = 0x30C
leaq 0x10(%rsp), %rsi
movq 0x60(%rsp), %rdx
callq 0x1110
movq 0x60(%rsp), %rdi
addq $0x31c, %rdi            # imm = 0x31C
movq 0x60(%rsp), %rsi
addq $0x31c, %rsi            # imm = 0x31C
movq 0x60(%rsp), %rdx
addq $0x1e8, %rdx            # imm = 0x1E8
callq 0x10b0
leaq 0x10(%rsp), %rdi
movq 0x60(%rsp), %rsi
addq $0x31c, %rsi            # imm = 0x31C
leaq 0x10(%rsp), %rdx
callq 0x6590
leaq 0x10(%rsp), %rdi
movq 0x60(%rsp), %rsi
addq $0x32c, %rsi            # imm = 0x32C
leaq 0x10(%rsp), %rdx
callq 0x6590
movq 0x38(%rsp), %rdi
movl $0x10, %eax
subq 0x50(%rsp), %rax
addq %rax, %rdi
leaq 0x10(%rsp), %rsi
movq 0x50(%rsp), %rdx
callq 0x1120
addq $0x68, %rsp
retq
nop