// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/aezv4/ref/encrypt.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, k, 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 (i=1; nbytes>=16; i++, nbytes-=16, N+=16) {
        E(K,kbytes,4,i,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 (k=0; k<veclen; k++) {
        p = A[k]; bytes = abytes[k]; empty = (bytes==0);
        for (i=1; bytes>=16; i++, bytes-=16, p+=16) {
            E(K,kbytes,5+k,i,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+k,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 $0x68, %rsp
movq %r9, 0x50(%rsp)
movq %r8, 0x48(%rsp)
movl %ecx, %r14d
movq %rdx, %rbx
movl 0xa0(%rsp), %r15d
movl 0xa8(%rsp), %eax
leaq 0x20(%rsp), %r13
andq $0x0, (%r13)
andl $0x0, 0x8(%r13)
movbel %eax, 0xc(%r13)
pushq $0x3
popq %rdx
pushq $0x1
popq %rbp
leaq 0x30(%rsp), %r12
movq %rdi, 0x10(%rsp)
movl %esi, 0xc(%rsp)
movl %ebp, %ecx
movq %r13, %r8
movq %r12, %r9
callq 0x1e84
movl %r14d, 0x18(%rsp)
movl %r14d, %r14d
cmpl $0x10, %r14d
jb 0x1a80
movq 0x10(%rsp), %rdi
movl 0xc(%rsp), %esi
pushq $0x4
popq %rdx
movl %ebp, %ecx
movq %rbx, %r8
movq %r13, %r9
callq 0x1e84
movq %r12, %rdi
movq %r13, %rsi
pushq $0x10
popq %rdx
movq %r12, %rcx
callq 0x21da
incl %ebp
addq $0x10, %rbx
addq $-0x10, %r14
jmp 0x1a44
cmpl $0x0, 0x18(%rsp)
setne %al
testl %r14d, %r14d
sete %cl
testb %cl, %al
jne 0x1ae6
vxorps %xmm0, %xmm0, %xmm0
movl %r15d, %ebp
leaq 0x20(%rsp), %r15
vmovaps %xmm0, (%r15)
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x1110
movb $-0x80, 0x20(%rsp,%r14)
pushq $0x4
popq %rdx
movq 0x10(%rsp), %rdi
movl 0xc(%rsp), %esi
xorl %ecx, %ecx
movq %r15, %r8
movq %r15, %r9
callq 0x1e84
pushq $0x10
popq %rdx
leaq 0x30(%rsp), %rcx
movq %rcx, %rdi
movq %r15, %rsi
movl %ebp, %r15d
callq 0x21da
movl %r15d, %eax
movq %rax, 0x58(%rsp)
xorl %ecx, %ecx
leaq 0x20(%rsp), %r12
leaq 0x30(%rsp), %rbx
cmpq 0x58(%rsp), %rcx
je 0x1bcd
movq 0x48(%rsp), %rax
movq (%rax,%rcx,8), %r13
movq 0x50(%rsp), %rax
movl (%rax,%rcx,4), %ebp
movq %rcx, 0x18(%rsp)
leal 0x5(%rcx), %r14d
movq %rbp, 0x60(%rsp)
pushq $0x1
popq %r15
cmpl $0x10, %ebp
jb 0x1b65
movq 0x10(%rsp), %rdi
movl 0xc(%rsp), %esi
movl %r14d, %edx
movl %r15d, %ecx
movq %r13, %r8
movq %r12, %r9
callq 0x1e84
movq %rbx, %rdi
movq %r12, %rsi
pushq $0x10
popq %rdx
movq %rbx, %rcx
callq 0x21da
incl %r15d
addq $0x10, %r13
addq $-0x10, %rbp
jmp 0x1b28
cmpl $0x0, 0x60(%rsp)
sete %al
testl %ebp, %ebp
setne %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x1bc0
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x20(%rsp)
movq %r12, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x1110
movb $-0x80, 0x20(%rsp,%rbp)
movq 0x10(%rsp), %rdi
movl 0xc(%rsp), %esi
movl %r14d, %edx
xorl %ecx, %ecx
movq %r12, %r8
movq %r12, %r9
callq 0x1e84
movq %rbx, %rdi
movq %r12, %rsi
pushq $0x10
popq %rdx
movq %rbx, %rcx
callq 0x21da
movq 0x18(%rsp), %rcx
incq %rcx
jmp 0x1afa
vmovaps 0x30(%rsp), %xmm0
movq 0xb0(%rsp), %rax
vmovups %xmm0, (%rax)
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq