// File: /workspace/github_c_makefile_star2_O0/mjosaarinen[P]brutus/crypto_aead/aes128poetv1aes4/ref/poet.c
// Function: encrypt_final
void encrypt_final(struct poet_ctx *ctx, const uint8_t *plaintext,
		   uint64_t plen, uint8_t *ciphertext, uint8_t tag[BLOCKLEN])
{
#ifdef DEBUG
  int i;
#endif
  uint64_t offset=0;
  uint64_t len;
  block s;
  block tmp;
  block tmp2;
  while( plen > BLOCKLEN )
    {
      encrypt_block(ctx,  (plaintext+offset), (ciphertext+offset));
      plen   -= BLOCKLEN;
      offset += BLOCKLEN;
    }

  /* Encrypt length of message */
  ctx->mlen+=(plen*8);
  memset(s,0,BLOCKLEN);
  len =  TO_LITTLE_ENDIAN_64(ctx->mlen);
  memcpy(s, &len,8);
  AES_encrypt(s, s ,&(ctx->aes_enc));

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


  /* Process last block + tag generation */

  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);


#ifdef DEBUG
  puts("S"); for(i=0;i<16;i++) printf("%02x ",s[i]); puts("");
  puts("X"); for(i=0;i<16;i++) printf("%02x ",ctx->x[i]); puts("");
  puts("Y"); for(i=0;i<16;i++) printf("%02x ",ctx->y[i]); puts("");
#endif

  /* Do 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);
  xor_block(ctx->x, ctx->tm, ctx->x);

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

  BOTTOM_HASH;
  xor_block(tmp, ctx->y, tmp);
  xor_block(tmp, ctx->tm, 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 $0x0, 0x38(%rsp)
cmpq $0x10, 0x50(%rsp)
jbe 0x6a7a
movq 0x60(%rsp), %rdi
movq 0x58(%rsp), %rsi
addq 0x38(%rsp), %rsi
movq 0x48(%rsp), %rdx
addq 0x38(%rsp), %rdx
callq 0x10b0
movq 0x50(%rsp), %rax
subq $0x10, %rax
movq %rax, 0x50(%rsp)
movq 0x38(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x38(%rsp)
jmp 0x6a36
movq 0x50(%rsp), %rcx
shlq $0x3, %rcx
movq 0x60(%rsp), %rax
addq 0x450(%rax), %rcx
movq %rcx, 0x450(%rax)
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x1030
movq 0x60(%rsp), %rax
movq 0x450(%rax), %rax
movq %rax, 0x30(%rsp)
movq 0x30(%rsp), %rax
movq %rax, 0x20(%rsp)
leaq 0x20(%rsp), %rdi
leaq 0x20(%rsp), %rsi
movq 0x60(%rsp), %rdx
callq 0x1060
leaq 0x10(%rsp), %rdi
movq 0x58(%rsp), %rsi
addq 0x38(%rsp), %rsi
movq 0x50(%rsp), %rdx
callq 0x1120
leaq 0x10(%rsp), %rdi
addq 0x50(%rsp), %rdi
movq 0x60(%rsp), %rsi
addq $0x440, %rsi            # imm = 0x440
movl $0x10, %edx
subq 0x50(%rsp), %rdx
callq 0x1120
movq 0x60(%rsp), %rdi
addq $0x420, %rdi            # imm = 0x420
movq 0x60(%rsp), %rsi
addq $0x420, %rsi            # imm = 0x420
movq 0x60(%rsp), %rdx
addq $0x1e8, %rdx            # imm = 0x1E8
callq 0x1100
leaq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0x1110
movq 0x60(%rsp), %rdi
addq $0x420, %rdi            # imm = 0x420
leaq 0x10(%rsp), %rsi
movq 0x60(%rsp), %rdx
addq $0x420, %rdx            # imm = 0x420
callq 0x1110
movq 0x60(%rsp), %rdi
addq $0x420, %rdi            # imm = 0x420
leaq 0x10(%rsp), %rsi
movq 0x60(%rsp), %rdx
callq 0x1060
movq 0x60(%rsp), %rdi
addq $0x430, %rdi            # imm = 0x430
movq 0x60(%rsp), %rsi
addq $0x430, %rsi            # imm = 0x430
movq 0x60(%rsp), %rdx
addq $0x2dc, %rdx            # imm = 0x2DC
callq 0x1100
movq %rsp, %rdi
leaq 0x10(%rsp), %rsi
movq 0x60(%rsp), %rdx
addq $0x430, %rdx            # imm = 0x430
callq 0x1110
movq 0x60(%rsp), %rax
movq 0x10(%rsp), %rcx
movq %rcx, 0x430(%rax)
movq 0x18(%rsp), %rcx
movq %rcx, 0x438(%rax)
leaq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rsi
movq %rsp, %rdx
callq 0x1110
movq 0x48(%rsp), %rdi
addq 0x38(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movq 0x50(%rsp), %rdx
callq 0x1120
movq 0x40(%rsp), %rdi
leaq 0x10(%rsp), %rsi
addq 0x50(%rsp), %rsi
movl $0x10, %edx
subq 0x50(%rsp), %rdx
callq 0x1120
movq 0x60(%rsp), %rdi
addq $0x420, %rdi            # imm = 0x420
movq 0x60(%rsp), %rsi
addq $0x420, %rsi            # imm = 0x420
movq 0x60(%rsp), %rdx
addq $0x1e8, %rdx            # imm = 0x1E8
callq 0x1100
movq 0x60(%rsp), %rdi
addq $0x420, %rdi            # imm = 0x420
movq 0x60(%rsp), %rsi
addq $0x440, %rsi            # imm = 0x440
movq 0x60(%rsp), %rdx
addq $0x420, %rdx            # imm = 0x420
callq 0x1110
movq 0x60(%rsp), %rdi
addq $0x420, %rdi            # imm = 0x420
movq 0x60(%rsp), %rsi
addq $0x3e0, %rsi            # imm = 0x3E0
movq 0x60(%rsp), %rdx
addq $0x420, %rdx            # imm = 0x420
callq 0x1110
movq 0x60(%rsp), %rdi
addq $0x420, %rdi            # imm = 0x420
leaq 0x10(%rsp), %rsi
movq 0x60(%rsp), %rdx
callq 0x1060
movq 0x60(%rsp), %rdi
addq $0x430, %rdi            # imm = 0x430
movq 0x60(%rsp), %rsi
addq $0x430, %rsi            # imm = 0x430
movq 0x60(%rsp), %rdx
addq $0x2dc, %rdx            # imm = 0x2DC
callq 0x1100
leaq 0x10(%rsp), %rdi
movq 0x60(%rsp), %rsi
addq $0x430, %rsi            # imm = 0x430
leaq 0x10(%rsp), %rdx
callq 0x1110
leaq 0x10(%rsp), %rdi
movq 0x60(%rsp), %rsi
addq $0x3e0, %rsi            # imm = 0x3E0
leaq 0x10(%rsp), %rdx
callq 0x1110
movq 0x40(%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
nopw %cs:(%rax,%rax)