// File: /workspace/github_c_makefile_star2_O1/mjosaarinen[P]brutus/crypto_aead/aes128poetv2aes4/ref/poet.c
// Function: process_header
void process_header(poet_ctx_t *ctx,
                    const unsigned char *header,
                    unsigned long long header_len)
{
    block mask;
    block in;
    block out;
    uint64_t offset = 0;

    ctx->mlen = 0;
    memset(ctx->tau, 0, BLOCKLEN);
    memcpy(mask, ctx->l, BLOCKLEN);

    // Process parameters
    encode_parameters(in, NUM_BLOCKS_PER_PART, INTERMEDIATE_TAGLEN);

    xor_block(in, in, mask);

    aes_encrypt(in, out, &(ctx->aes_enc));
    xor_block(ctx->tau, out, ctx->tau);
    gf128_double(mask);

    while (header_len >= BLOCKLEN) {
        xor_block(in, header + offset, mask);
        aes_encrypt(in, out, &(ctx->aes_enc));
        xor_block(ctx->tau, out, ctx->tau);

        offset += BLOCKLEN;
        header_len -= BLOCKLEN;

        gf128_double(mask);
    }

    //  Final block 
    memset(in, 0, BLOCKLEN);
    memcpy(in, header + offset, header_len);
    in[header_len] = 0x80;
    xor_block(in, mask, in);
    aes_encrypt(in, out, &(ctx->aes_enc));
    
    xor_block(ctx->tau, out, ctx->tau);
    aes_encrypt(ctx->tau, ctx->tau, &(ctx->aes_enc));

    memcpy(ctx->x, ctx->tau, BLOCKLEN);
    memcpy(ctx->y, ctx->tau, BLOCKLEN);
    ctx->y[BLOCKLEN - 1] ^= 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %rbx
movq $0x0, 0x340(%rdi)
vxorps %xmm0, %xmm0, %xmm0
vmovups %xmm0, 0x32c(%rdi)
vmovups 0x2ec(%rdi), %xmm0
leaq 0x32c(%rdi), %rax
movq %rax, 0x40(%rsp)
vmovaps %xmm0, 0x10(%rsp)
leaq 0x20(%rsp), %rdi
vmovaps %xmm0, (%rdi)
leaq 0x30(%rsp), %r13
movq %r13, %rsi
movq %rbx, %rdx
callq 0x1100
vmovups 0x32c(%rbx), %xmm0
vxorps (%r13), %xmm0, %xmm0
vmovups %xmm0, 0x32c(%rbx)
vmovdqa 0x10(%rsp), %xmm0
vmovdqa 0x3618(%rip), %xmm2     # 0x7130
vpshufb %xmm2, %xmm0, %xmm0
movw $0xfffe, %ax            # imm = 0xFFFE
kmovd %eax, %k1
vmovdqu8 0xf(%rsp), %xmm1 {%k1} {z}
vpshufb %xmm2, %xmm1, %xmm1
vpsllw $0x7, %xmm1, %xmm1
vpsrlw $0x1, %xmm0, %xmm0
vpternlogd $0xd8, 0x3616(%rip){1to4}, %xmm1, %xmm0 # 0x7160
vpshufb 0x35ed(%rip), %xmm0, %xmm0 # 0x7140
xorl %r14d, %r14d
testb $0x1, 0x1f(%rsp)
vmovdqu8 %xmm0, 0x10(%rsp) {%k1}
movb 0x10(%rsp), %cl
movl $0xe1, %edx
movl $0x0, %r13d
cmovnel %edx, %r13d
shrb %cl
xorb %cl, %r13b
movb %r13b, 0x10(%rsp)
cmpq $0x10, %r15
jb 0x3c51
leaq 0x30(%rsp), %rbp
kmovd %eax, %k1
kmovw %k1, 0xe(%rsp)
vmovaps 0x10(%rsp), %xmm0
vxorps (%r12,%r14), %xmm0, %xmm0
vmovaps %xmm0, 0x20(%rsp)
leaq 0x20(%rsp), %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x1100
movl $0xe1, %ecx
vmovdqa 0x3568(%rip), %xmm2     # 0x7130
vmovups 0x32c(%rbx), %xmm0
vxorps 0x30(%rsp), %xmm0, %xmm0
vmovups %xmm0, 0x32c(%rbx)
addq $0x10, %r14
vmovdqa 0x10(%rsp), %xmm0
leaq 0xf(%rsp), %rax
kmovw 0xe(%rsp), %k1
vmovdqu8 (%rax), %xmm1 {%k1} {z}
vpshufb %xmm2, %xmm0, %xmm0
vpshufb %xmm2, %xmm1, %xmm1
vpsllw $0x7, %xmm1, %xmm1
vpsrlw $0x1, %xmm0, %xmm0
vpternlogd $0xd8, 0x3548(%rip){1to4}, %xmm1, %xmm0 # 0x7160
vpshufb 0x351f(%rip), %xmm0, %xmm0 # 0x7140
addq $-0x10, %r15
shrb %r13b
testb $0x1, 0x1f(%rsp)
vmovdqu8 %xmm0, 0x10(%rsp) {%k1}
movl $0x0, %eax
cmovnel %ecx, %eax
xorb %r13b, %al
movb %al, 0x10(%rsp)
movl %eax, %r13d
cmpq $0xf, %r15
ja 0x3b99
leaq 0x20(%rsp), %r13
vpxor %xmm0, %xmm0, %xmm0
vmovdqa %xmm0, (%r13)
addq %r14, %r12
movq %r13, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x1110
movb $-0x80, 0x20(%rsp,%r15)
vmovaps (%r13), %xmm0
vxorps 0x10(%rsp), %xmm0, %xmm0
vmovaps %xmm0, (%r13)
leaq 0x30(%rsp), %r15
movq %r13, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x1100
vmovups 0x32c(%rbx), %xmm0
vxorps (%r15), %xmm0, %xmm0
vmovups %xmm0, 0x32c(%rbx)
movq 0x40(%rsp), %r14
movq %r14, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x1100
vmovups (%r14), %xmm0
vmovups %xmm0, 0x30c(%rbx)
vmovups (%r14), %xmm0
vmovups %xmm0, 0x31c(%rbx)
xorb $0x1, 0x32b(%rbx)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq