// File: /workspace/github_c_makefile_star2_O0/mjosaarinen[P]brutus/crypto_aead/aezv3/ref/aez_ref.c
// Function: AEZcore
static void AEZcore(byte *K, unsigned kbytes, byte delta[16],
                        byte *in, unsigned inbytes, unsigned d, byte *out) {
    byte tmp[16], X[16], Y[16], S[16];
    byte *in_orig = in, *out_orig = out;
    unsigned j, inbytes_orig = inbytes;

    memset(X,0,16); memset(Y,0,16);
    
    /* Pass 1 over in[0:-32], store intermediate values in out[0:-32] */
    for (j=1; inbytes >= 64; j++, inbytes-=32, in+=32, out+=32) {
        E(K, kbytes, 1, j, in+16, tmp); xor_bytes(in, tmp, 16, out);
        E(K, kbytes, 0, 0, out, tmp); xor_bytes(in+16, tmp, 16, out+16);
        xor_bytes(out+16, X, 16, X);
    }

    /* Finish X calculation */
    inbytes -= 32;                /* inbytes now has fragment length 0..31 */
    if (inbytes >= 16) {
        E(K, kbytes, 0, 4, in, tmp); xor_bytes(X, tmp, 16, X);
        inbytes -= 16; in += 16; out += 16;
        memset(tmp,0,16); memcpy(tmp,in,inbytes); tmp[inbytes] = 0x80;
        E(K, kbytes, 0, 5, tmp, tmp); xor_bytes(X, tmp, 16, X);
    } else if (inbytes > 0) {
        memset(tmp,0,16); memcpy(tmp,in,inbytes); tmp[inbytes] = 0x80;
        E(K, kbytes, 0, 4, tmp, tmp); xor_bytes(X, tmp, 16, X);
    }
    in += inbytes; out += inbytes;

    /* Calculate S */
    E(K, kbytes, 0, 1+d, in+16, tmp);
    xor_bytes(X, in, 16, out);
    xor_bytes(delta, out, 16, out);
    xor_bytes(tmp, out, 16, out);
    E(K, kbytes, -1, 1+d, out, tmp);
    xor_bytes(in+16, tmp, 16, out+16);
    xor_bytes(out, out+16, 16, S);

    /* Pass 2 over intermediate values in out[32..]. Final values written */
    inbytes = inbytes_orig; out = out_orig; in = in_orig;
    for (j=1; inbytes >= 64; j++, inbytes-=32, in+=32, out+=32) {
        E(K, kbytes, 2, j, S, tmp);
        xor_bytes(out, tmp, 16, out); xor_bytes(out+16, tmp, 16, out+16);
        xor_bytes(out, Y, 16, Y);
        E(K, kbytes, 0, 0, out+16, tmp); xor_bytes(out, tmp, 16, out);
        E(K, kbytes, 1, j, out, tmp); xor_bytes(out+16, tmp, 16, out+16);
        memcpy(tmp, out, 16); memcpy(out, out+16, 16); memcpy(out+16, tmp, 16);
    }

    /* Finish Y calculation and finish encryption of fragment bytes */
    inbytes -= 32;                /* inbytes now has fragment length 0..31 */
    if (inbytes >= 16) {
        E(K, kbytes, -1, 4, S, tmp); xor_bytes(in, tmp, 16, out);
        E(K, kbytes, 0, 4, out, tmp); xor_bytes(Y, tmp, 16, Y);
        inbytes -= 16; in += 16; out += 16;
        E(K, kbytes, -1, 5, S, tmp); xor_bytes(in, tmp, inbytes, tmp);
        memcpy(out,tmp,inbytes);
        memset(tmp+inbytes,0,16-inbytes); tmp[inbytes] = 0x80;
        E(K, kbytes, 0, 5, tmp, tmp); xor_bytes(Y, tmp, 16, Y);
    } else if (inbytes > 0) {
        E(K, kbytes, -1, 4, S, tmp); xor_bytes(in, tmp, inbytes, tmp);
        memcpy(out,tmp,inbytes);
        memset(tmp+inbytes,0,16-inbytes); tmp[inbytes] = 0x80;
        E(K, kbytes, 0, 4, tmp, tmp); xor_bytes(Y, tmp, 16, Y);
    }
    in += inbytes; out += inbytes;

    /* Finish encryption of last two blocks */
    E(K, kbytes, -1, 2-d, out+16, tmp);
    xor_bytes(out, tmp, 16, out);
    E(K, kbytes, 0, 2-d, out, tmp);
    xor_bytes(tmp, out+16, 16, out+16);
    xor_bytes(delta, out+16, 16, out+16);
    xor_bytes(Y, out+16, 16, out+16);
    memcpy(tmp, out, 16); memcpy(out, out+16, 16); memcpy(out+16, tmp, 16);
}

// Assembly:
subq $0x88, %rsp
movq 0x90(%rsp), %rax
movq %rdi, 0x80(%rsp)
movl %esi, 0x7c(%rsp)
movq %rdx, 0x70(%rsp)
movq %rcx, 0x68(%rsp)
movl %r8d, 0x64(%rsp)
movl %r9d, 0x60(%rsp)
movq 0x68(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x90(%rsp), %rax
movq %rax, 0x10(%rsp)
movl 0x64(%rsp), %eax
movl %eax, 0x8(%rsp)
leaq 0x40(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x1040
leaq 0x30(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x1040
movl $0x1, 0xc(%rsp)
cmpl $0x40, 0x64(%rsp)
jb 0x2d9a
movq 0x80(%rsp), %rdi
movl 0x7c(%rsp), %esi
movl 0xc(%rsp), %ecx
movq 0x68(%rsp), %r8
addq $0x10, %r8
leaq 0x50(%rsp), %r9
movl $0x1, %edx
callq 0x1c40
movq 0x68(%rsp), %rdi
leaq 0x50(%rsp), %rsi
movq 0x90(%rsp), %rcx
movl $0x10, %edx
callq 0x2170
movq 0x80(%rsp), %rdi
movl 0x7c(%rsp), %esi
movq 0x90(%rsp), %r8
leaq 0x50(%rsp), %r9
xorl %ecx, %ecx
movl %ecx, %edx
callq 0x1c40
movq 0x68(%rsp), %rdi
addq $0x10, %rdi
leaq 0x50(%rsp), %rsi
movq 0x90(%rsp), %rcx
addq $0x10, %rcx
movl $0x10, %edx
callq 0x2170
movq 0x90(%rsp), %rdi
addq $0x10, %rdi
leaq 0x40(%rsp), %rsi
leaq 0x40(%rsp), %rcx
movl $0x10, %edx
callq 0x2170
movl 0xc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc(%rsp)
movl 0x64(%rsp), %eax
subl $0x20, %eax
movl %eax, 0x64(%rsp)
movq 0x68(%rsp), %rax
addq $0x20, %rax
movq %rax, 0x68(%rsp)
movq 0x90(%rsp), %rax
addq $0x20, %rax
movq %rax, 0x90(%rsp)
jmp 0x2ca8
movl 0x64(%rsp), %eax
subl $0x20, %eax
movl %eax, 0x64(%rsp)
cmpl $0x10, 0x64(%rsp)
jb 0x2e84
movq 0x80(%rsp), %rdi
movl 0x7c(%rsp), %esi
movq 0x68(%rsp), %r8
leaq 0x50(%rsp), %r9
xorl %edx, %edx
movl $0x4, %ecx
callq 0x1c40
leaq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rsi
leaq 0x40(%rsp), %rcx
movl $0x10, %edx
callq 0x2170
movl 0x64(%rsp), %eax
subl $0x10, %eax
movl %eax, 0x64(%rsp)
movq 0x68(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x68(%rsp)
movq 0x90(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x90(%rsp)
leaq 0x50(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x1040
leaq 0x50(%rsp), %rdi
movq 0x68(%rsp), %rsi
movl 0x64(%rsp), %eax
movl %eax, %edx
callq 0x10d0
movl 0x64(%rsp), %eax
movb $-0x80, 0x50(%rsp,%rax)
movq 0x80(%rsp), %rdi
movl 0x7c(%rsp), %esi
leaq 0x50(%rsp), %r8
leaq 0x50(%rsp), %r9
xorl %edx, %edx
movl $0x5, %ecx
callq 0x1c40
leaq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rsi
leaq 0x40(%rsp), %rcx
movl $0x10, %edx
callq 0x2170
jmp 0x2ef7
cmpl $0x0, 0x64(%rsp)
jbe 0x2ef5
leaq 0x50(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x1040
leaq 0x50(%rsp), %rdi
movq 0x68(%rsp), %rsi
movl 0x64(%rsp), %eax
movl %eax, %edx
callq 0x10d0
movl 0x64(%rsp), %eax
movb $-0x80, 0x50(%rsp,%rax)
movq 0x80(%rsp), %rdi
movl 0x7c(%rsp), %esi
leaq 0x50(%rsp), %r8
leaq 0x50(%rsp), %r9
xorl %edx, %edx
movl $0x4, %ecx
callq 0x1c40
leaq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rsi
leaq 0x40(%rsp), %rcx
movl $0x10, %edx
callq 0x2170
jmp 0x2ef7
movl 0x64(%rsp), %ecx
movq 0x68(%rsp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, 0x68(%rsp)
movl 0x64(%rsp), %ecx
movq 0x90(%rsp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, 0x90(%rsp)
movq 0x80(%rsp), %rdi
movl 0x7c(%rsp), %esi
movl 0x60(%rsp), %ecx
addl $0x1, %ecx
movq 0x68(%rsp), %r8
addq $0x10, %r8
leaq 0x50(%rsp), %r9
xorl %edx, %edx
callq 0x1c40
leaq 0x40(%rsp), %rdi
movq 0x68(%rsp), %rsi
movq 0x90(%rsp), %rcx
movl $0x10, %edx
callq 0x2170
movq 0x70(%rsp), %rdi
movq 0x90(%rsp), %rsi
movq 0x90(%rsp), %rcx
movl $0x10, %edx
callq 0x2170
leaq 0x50(%rsp), %rdi
movq 0x90(%rsp), %rsi
movq 0x90(%rsp), %rcx
movl $0x10, %edx
callq 0x2170
movq 0x80(%rsp), %rdi
movl 0x7c(%rsp), %esi
movl 0x60(%rsp), %ecx
addl $0x1, %ecx
movq 0x90(%rsp), %r8
leaq 0x50(%rsp), %r9
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x1c40
movq 0x68(%rsp), %rdi
addq $0x10, %rdi
leaq 0x50(%rsp), %rsi
movq 0x90(%rsp), %rcx
addq $0x10, %rcx
movl $0x10, %edx
callq 0x2170
movq 0x90(%rsp), %rdi
movq 0x90(%rsp), %rsi
addq $0x10, %rsi
leaq 0x20(%rsp), %rcx
movl $0x10, %edx
callq 0x2170
movl 0x8(%rsp), %eax
movl %eax, 0x64(%rsp)
movq 0x10(%rsp), %rax
movq %rax, 0x90(%rsp)
movq 0x18(%rsp), %rax
movq %rax, 0x68(%rsp)
movl $0x1, 0xc(%rsp)
cmpl $0x40, 0x64(%rsp)
jb 0x31f0
movq 0x80(%rsp), %rdi
movl 0x7c(%rsp), %esi
movl 0xc(%rsp), %ecx
leaq 0x20(%rsp), %r8
leaq 0x50(%rsp), %r9
movl $0x2, %edx
callq 0x1c40
movq 0x90(%rsp), %rdi
leaq 0x50(%rsp), %rsi
movq 0x90(%rsp), %rcx
movl $0x10, %edx
callq 0x2170
movq 0x90(%rsp), %rdi
addq $0x10, %rdi
leaq 0x50(%rsp), %rsi
movq 0x90(%rsp), %rcx
addq $0x10, %rcx
movl $0x10, %edx
callq 0x2170
movq 0x90(%rsp), %rdi
leaq 0x30(%rsp), %rsi
leaq 0x30(%rsp), %rcx
movl $0x10, %edx
callq 0x2170
movq 0x80(%rsp), %rdi
movl 0x7c(%rsp), %esi
movq 0x90(%rsp), %r8
addq $0x10, %r8
leaq 0x50(%rsp), %r9
xorl %ecx, %ecx
movl %ecx, %edx
callq 0x1c40
movq 0x90(%rsp), %rdi
leaq 0x50(%rsp), %rsi
movq 0x90(%rsp), %rcx
movl $0x10, %edx
callq 0x2170
movq 0x80(%rsp), %rdi
movl 0x7c(%rsp), %esi
movl 0xc(%rsp), %ecx
movq 0x90(%rsp), %r8
leaq 0x50(%rsp), %r9
movl $0x1, %edx
callq 0x1c40
movq 0x90(%rsp), %rdi
addq $0x10, %rdi
leaq 0x50(%rsp), %rsi
movq 0x90(%rsp), %rcx
addq $0x10, %rcx
movl $0x10, %edx
callq 0x2170
movq 0x90(%rsp), %rax
movq (%rax), %rcx
movq %rcx, 0x50(%rsp)
movq 0x8(%rax), %rax
movq %rax, 0x58(%rsp)
movq 0x90(%rsp), %rax
movq 0x90(%rsp), %rcx
movq 0x10(%rcx), %rdx
movq %rdx, (%rax)
movq 0x18(%rcx), %rcx
movq %rcx, 0x8(%rax)
movq 0x90(%rsp), %rax
movq 0x50(%rsp), %rcx
movq %rcx, 0x10(%rax)
movq 0x58(%rsp), %rcx
movq %rcx, 0x18(%rax)
movl 0xc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc(%rsp)
movl 0x64(%rsp), %eax
subl $0x20, %eax
movl %eax, 0x64(%rsp)
movq 0x68(%rsp), %rax
addq $0x20, %rax
movq %rax, 0x68(%rsp)
movq 0x90(%rsp), %rax
addq $0x20, %rax
movq %rax, 0x90(%rsp)
jmp 0x303d
movl 0x64(%rsp), %eax
subl $0x20, %eax
movl %eax, 0x64(%rsp)
cmpl $0x10, 0x64(%rsp)
jb 0x3370
movq 0x80(%rsp), %rdi
movl 0x7c(%rsp), %esi
leaq 0x20(%rsp), %r8
leaq 0x50(%rsp), %r9
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0x4, %ecx
callq 0x1c40
movq 0x68(%rsp), %rdi
leaq 0x50(%rsp), %rsi
movq 0x90(%rsp), %rcx
movl $0x10, %edx
callq 0x2170
movq 0x80(%rsp), %rdi
movl 0x7c(%rsp), %esi
movq 0x90(%rsp), %r8
leaq 0x50(%rsp), %r9
xorl %edx, %edx
movl $0x4, %ecx
callq 0x1c40
leaq 0x30(%rsp), %rdi
leaq 0x50(%rsp), %rsi
leaq 0x30(%rsp), %rcx
movl $0x10, %edx
callq 0x2170
movl 0x64(%rsp), %eax
subl $0x10, %eax
movl %eax, 0x64(%rsp)
movq 0x68(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x68(%rsp)
movq 0x90(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x90(%rsp)
movq 0x80(%rsp), %rdi
movl 0x7c(%rsp), %esi
leaq 0x20(%rsp), %r8
leaq 0x50(%rsp), %r9
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0x5, %ecx
callq 0x1c40
movq 0x68(%rsp), %rdi
leaq 0x50(%rsp), %rsi
movl 0x64(%rsp), %edx
leaq 0x50(%rsp), %rcx
callq 0x2170
movq 0x90(%rsp), %rdi
leaq 0x50(%rsp), %rsi
movl 0x64(%rsp), %eax
movl %eax, %edx
callq 0x10d0
leaq 0x50(%rsp), %rdi
movl 0x64(%rsp), %eax
addq %rax, %rdi
movl $0x10, %eax
subl 0x64(%rsp), %eax
movl %eax, %eax
movl %eax, %edx
xorl %esi, %esi
callq 0x1040
movl 0x64(%rsp), %eax
movb $-0x80, 0x50(%rsp,%rax)
movq 0x80(%rsp), %rdi
movl 0x7c(%rsp), %esi
leaq 0x50(%rsp), %r8
leaq 0x50(%rsp), %r9
xorl %edx, %edx
movl $0x5, %ecx
callq 0x1c40
leaq 0x30(%rsp), %rdi
leaq 0x50(%rsp), %rsi
leaq 0x30(%rsp), %rcx
movl $0x10, %edx
callq 0x2170
jmp 0x3436
cmpl $0x0, 0x64(%rsp)
jbe 0x3434
movq 0x80(%rsp), %rdi
movl 0x7c(%rsp), %esi
leaq 0x20(%rsp), %r8
leaq 0x50(%rsp), %r9
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0x4, %ecx
callq 0x1c40
movq 0x68(%rsp), %rdi
leaq 0x50(%rsp), %rsi
movl 0x64(%rsp), %edx
leaq 0x50(%rsp), %rcx
callq 0x2170
movq 0x90(%rsp), %rdi
leaq 0x50(%rsp), %rsi
movl 0x64(%rsp), %eax
movl %eax, %edx
callq 0x10d0
leaq 0x50(%rsp), %rdi
movl 0x64(%rsp), %eax
addq %rax, %rdi
movl $0x10, %eax
subl 0x64(%rsp), %eax
movl %eax, %eax
movl %eax, %edx
xorl %esi, %esi
callq 0x1040
movl 0x64(%rsp), %eax
movb $-0x80, 0x50(%rsp,%rax)
movq 0x80(%rsp), %rdi
movl 0x7c(%rsp), %esi
leaq 0x50(%rsp), %r8
leaq 0x50(%rsp), %r9
xorl %edx, %edx
movl $0x4, %ecx
callq 0x1c40
leaq 0x30(%rsp), %rdi
leaq 0x50(%rsp), %rsi
leaq 0x30(%rsp), %rcx
movl $0x10, %edx
callq 0x2170
jmp 0x3436
movl 0x64(%rsp), %ecx
movq 0x68(%rsp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, 0x68(%rsp)
movl 0x64(%rsp), %ecx
movq 0x90(%rsp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, 0x90(%rsp)
movq 0x80(%rsp), %rdi
movl 0x7c(%rsp), %esi
movl $0x2, %ecx
subl 0x60(%rsp), %ecx
movq 0x90(%rsp), %r8
addq $0x10, %r8
leaq 0x50(%rsp), %r9
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x1c40
movq 0x90(%rsp), %rdi
leaq 0x50(%rsp), %rsi
movq 0x90(%rsp), %rcx
movl $0x10, %edx
callq 0x2170
movq 0x80(%rsp), %rdi
movl 0x7c(%rsp), %esi
movl $0x2, %ecx
subl 0x60(%rsp), %ecx
movq 0x90(%rsp), %r8
leaq 0x50(%rsp), %r9
xorl %edx, %edx
callq 0x1c40
leaq 0x50(%rsp), %rdi
movq 0x90(%rsp), %rsi
addq $0x10, %rsi
movq 0x90(%rsp), %rcx
addq $0x10, %rcx
movl $0x10, %edx
callq 0x2170
movq 0x70(%rsp), %rdi
movq 0x90(%rsp), %rsi
addq $0x10, %rsi
movq 0x90(%rsp), %rcx
addq $0x10, %rcx
movl $0x10, %edx
callq 0x2170
leaq 0x30(%rsp), %rdi
movq 0x90(%rsp), %rsi
addq $0x10, %rsi
movq 0x90(%rsp), %rcx
addq $0x10, %rcx
movl $0x10, %edx
callq 0x2170
movq 0x90(%rsp), %rax
movq (%rax), %rcx
movq %rcx, 0x50(%rsp)
movq 0x8(%rax), %rax
movq %rax, 0x58(%rsp)
movq 0x90(%rsp), %rax
movq 0x90(%rsp), %rcx
movq 0x10(%rcx), %rdx
movq %rdx, (%rax)
movq 0x18(%rcx), %rcx
movq %rcx, 0x8(%rax)
movq 0x90(%rsp), %rax
movq 0x50(%rsp), %rcx
movq %rcx, 0x10(%rax)
movq 0x58(%rsp), %rcx
movq %rcx, 0x18(%rax)
addq $0x88, %rsp
retq
nopl (%rax)