// File: /workspace/github_c_makefile_star2_O1/mjosaarinen[P]brutus/crypto_aead/aezv3/ref/aez_ref.c
// Function: AEZhash
static void AEZhash(byte *K, unsigned kbytes, byte *N, unsigned nbytes,
    byte *A[], unsigned abytes[], unsigned veclen, unsigned tau, byte *result) {

    byte buf[16], sum[16], *p;
    unsigned i, j, bytes, empty;

    /* Initialize sum with hash of tau */
    memset(buf,0,12); write32_big_endian(tau, buf+12);
    E(K,kbytes,3,1,buf,sum);

    /* Hash nonce, accumulate into sum */
    empty = (nbytes==0);
    for (j=1; nbytes>=16; j++, nbytes-=16, N+=16) {
        E(K,kbytes,4,j,N,buf); xor_bytes(sum, buf, 16, sum);
    }
    if (nbytes || empty) {
        memset(buf,0,16); memcpy(buf,N,nbytes); buf[nbytes]=0x80;
        E(K,kbytes,4,0,buf,buf);
        xor_bytes(sum, buf, 16, sum);
    }

    /* Hash each vector element, accumulate into sum */
    for (i=0; i<veclen; i++) {
        p = A[i]; bytes = abytes[i]; empty = (bytes==0);
        for (j=1; bytes>=16; j++, bytes-=16, p+=16) {
            E(K,kbytes,5+i,j,p,buf); xor_bytes(sum, buf, 16, sum);
        }
        if (bytes || empty) {
            memset(buf,0,16); memcpy(buf,p,bytes); buf[bytes]=0x80;
            E(K,kbytes,5+i,0,buf,buf);
            xor_bytes(sum, buf, 16, sum);
        }
    }
    memcpy(result,sum,16);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %r9, %r15
movq %r8, 0x80(%rsp)
movl %ecx, %ebx
movq %rdx, %r12
movl 0xc8(%rsp), %eax
leaq 0x10(%rsp), %r8
movq $0x0, (%r8)
movl $0x0, 0x8(%r8)
movbel %eax, 0xc(%r8)
movl $0x1, %r13d
leaq 0x30(%rsp), %r9
movq %rdi, 0x28(%rsp)
movl %esi, 0xc(%rsp)
movl $0x3, %edx
movl $0x1, %ecx
callq 0x19a2
cmpl $0x10, %ebx
movq %r15, 0x68(%rsp)
jb 0x1514
leal -0x10(%rbx), %ebp
shrl $0x4, %ebp
vmovaps 0x30(%rsp), %xmm0
negl %ebp
movq %rbx, 0x48(%rsp)
movl %ebx, %r14d
movl 0xc(%rsp), %ebx
movq 0x28(%rsp), %r15
vmovaps %xmm0, 0x50(%rsp)
movq %r15, %rdi
movl %ebx, %esi
movl $0x4, %edx
movl %r13d, %ecx
movq %r12, %r8
leaq 0x10(%rsp), %r9
callq 0x19a2
vmovaps 0x50(%rsp), %xmm0
vxorps 0x10(%rsp), %xmm0, %xmm0
addl $-0x10, %r14d
addq $0x10, %r12
leal 0x1(%rbp,%r13), %eax
movl %r13d, %ecx
incl %ecx
movl %ecx, %r13d
cmpl $0x2, %eax
jne 0x14bc
vmovaps %xmm0, 0x30(%rsp)
movq 0x68(%rsp), %r15
movq 0x48(%rsp), %rbx
jmp 0x1517
movl %ebx, %r14d
movl 0xc0(%rsp), %ebp
testl %ebx, %ebx
setne %al
testl %r14d, %r14d
sete %cl
testb %cl, %al
jne 0x157c
vxorps %xmm0, %xmm0, %xmm0
leaq 0x10(%rsp), %rbx
vmovaps %xmm0, (%rbx)
movl %r14d, %r14d
movq %rbx, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x10d0
movb $-0x80, 0x10(%rsp,%r14)
movq 0x28(%rsp), %rdi
movl 0xc(%rsp), %esi
movl $0x4, %edx
xorl %ecx, %ecx
movq %rbx, %r8
movq %rbx, %r9
callq 0x19a2
vmovaps (%rbx), %xmm0
vxorps 0x30(%rsp), %xmm0, %xmm0
vmovaps %xmm0, 0x30(%rsp)
testl %ebp, %ebp
movl 0xc(%rsp), %r14d
movq 0x28(%rsp), %r12
je 0x16d5
movl %ebp, %eax
movq %rax, 0x48(%rsp)
vmovaps 0x30(%rsp), %xmm0
xorl %ebx, %ebx
leaq 0x10(%rsp), %rdi
movq 0x80(%rsp), %rax
movq (%rax,%rbx,8), %r13
movl (%r15,%rbx,4), %eax
cmpl $0x10, %eax
jb 0x164d
movq %rbx, 0x78(%rsp)
leal 0x5(%rbx), %ecx
movl %ecx, 0xc(%rsp)
leal -0x10(%rax), %r15d
shrl $0x4, %r15d
negl %r15d
movq %r12, %rbx
movl %r14d, %r12d
movl $0x1, %r14d
movq %rax, 0x70(%rsp)
movl %eax, %ebp
vmovaps %xmm0, 0x50(%rsp)
movq %rbx, %rdi
movl %r12d, %esi
movl 0xc(%rsp), %edx
movl %r14d, %ecx
movq %r13, %r8
leaq 0x10(%rsp), %r9
callq 0x19a2
leaq 0x10(%rsp), %rdi
vmovaps 0x50(%rsp), %xmm0
vxorps 0x10(%rsp), %xmm0, %xmm0
addl $-0x10, %ebp
addq $0x10, %r13
leal (%r15,%r14), %eax
incl %eax
movl %r14d, %ecx
incl %ecx
movl %ecx, %r14d
cmpl $0x2, %eax
jne 0x15e5
vmovaps %xmm0, 0x30(%rsp)
movq 0x68(%rsp), %r15
movl %r12d, %r14d
movq %rbx, %r12
movq 0x78(%rsp), %rbx
movq 0x70(%rsp), %rax
jmp 0x164f
movl %eax, %ebp
testl %eax, %eax
setne %al
testl %ebp, %ebp
sete %cl
testb %al, %cl
jne 0x16c7
vxorps %xmm1, %xmm1, %xmm1
vmovaps %xmm1, 0x10(%rsp)
movl %r14d, %r12d
movq %r15, %r14
movq %rbx, %r15
movl %ebp, %ebx
movq %r13, %rsi
movq %rbx, %rdx
vmovaps %xmm0, 0x50(%rsp)
callq 0x10d0
movb $-0x80, 0x10(%rsp,%rbx)
movq %r15, %rbx
movq %r14, %r15
movl %r12d, %r14d
movq 0x28(%rsp), %r12
leal 0x5(%rbx), %edx
movq %r12, %rdi
movl %r14d, %esi
xorl %ecx, %ecx
leaq 0x10(%rsp), %r8
leaq 0x10(%rsp), %r9
callq 0x19a2
leaq 0x10(%rsp), %rdi
vmovaps 0x50(%rsp), %xmm0
vxorps 0x10(%rsp), %xmm0, %xmm0
vmovaps %xmm0, 0x30(%rsp)
incq %rbx
cmpq 0x48(%rsp), %rbx
jne 0x15a2
vmovaps 0x30(%rsp), %xmm0
movq 0xd0(%rsp), %rax
vmovups %xmm0, (%rax)
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq