// File: /workspace/github_c_makefile_star2_O3/mjosaarinen[P]brutus/crypto_aead/aezv3/ref/aez_ref.c
// Function: AEZtiny
static void AEZtiny(byte *K, unsigned kbytes, byte delta[16],
                        byte *in, unsigned inbytes, unsigned d, byte *out) {
    unsigned rounds,i,j=7,k;
    int step;
    byte mask=0x00, pad=0x80, L[16], R[16], buf[32];
    if      (inbytes==1) rounds=24;
    else if (inbytes==2) rounds=16;
    else if (inbytes<16) rounds=10;
    else {          j=6; rounds=8; }
    /* Split (inbytes*8)/2 bits into L and R. Beware: May end in nibble. */
    memcpy(L, in,           (inbytes+1)/2);
    memcpy(R, in+inbytes/2, (inbytes+1)/2);
    if (inbytes&1) {                     /* Must shift R left by half a byte */
        for (i=0; i<inbytes/2; i++)
            R[i] = (byte)((R[i] << 4) | (R[i+1] >> 4));
        R[inbytes/2] = (byte)(R[inbytes/2] << 4);
        pad = 0x08; mask = 0xf0;
    }
    if (d) {
        if (inbytes < 16) {
            memset(buf,0,16); memcpy(buf,in,inbytes); buf[0] |= 0x80;
            xor_bytes(delta, buf, 16, buf);
            E(K, kbytes,0,3,buf,buf);
            L[0] ^= (buf[0] & 0x80);
        }
        i = rounds-1; step = -1;
    } else {
        i = 0; step = 1;
    }
    for (k=0; k<rounds/2; k++,i=(unsigned)((int)i+2*step)) {
        memset(buf, 0, 16);
        memcpy(buf,R,(inbytes+1)/2);
        buf[inbytes/2] = (buf[inbytes/2] & mask) | pad;
        xor_bytes(buf, delta, 16, buf);
        buf[15] ^= (byte)i;
        E(K, kbytes,0,j,buf,buf);
        xor_bytes(L, buf, 16, L);

        memset(buf, 0, 16);
        memcpy(buf,L,(inbytes+1)/2);
        buf[inbytes/2] = (buf[inbytes/2] & mask) | pad;
        xor_bytes(buf, delta, 16, buf);
        buf[15] ^= (byte)((int)i+step);
        E(K, kbytes,0,j,buf,buf);
        xor_bytes(R, buf, 16, R);
    }
    memcpy(buf,           R, inbytes/2);
    memcpy(buf+inbytes/2, L, (inbytes+1)/2);
    if (inbytes&1) {
        for (i=inbytes-1; i>inbytes/2; i--)
            buf[i] = (byte)((buf[i] >> 4) | (buf[i-1] << 4));
        buf[inbytes/2] = (byte)((L[0] >> 4) | (R[inbytes/2] & 0xf0));
    }
    memcpy(out,buf,inbytes);
    if ((inbytes < 16) && !d) {
        memset(buf+inbytes,0,16-inbytes); buf[0] |= 0x80;
        xor_bytes(delta, buf, 16, buf);
        E(K, kbytes,0,3,buf,buf);
        out[0] ^= (buf[0] & 0x80);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movl %r9d, 0x4(%rsp)
movl %r8d, %ebx
movq %rcx, 0x70(%rsp)
movq %rdi, %rbp
cmpl $0x1, %r8d
movl %esi, (%rsp)
movq %rdx, %r12
je 0x200c
cmpl $0x2, %ebx
jne 0x201e
movl $0x7, %eax
movq %rax, 0x38(%rsp)
movl $0x10, %r14d
jmp 0x2036
movl $0x7, %eax
movq %rax, 0x38(%rsp)
movl $0x18, %r14d
jmp 0x2036
xorl %eax, %eax
cmpl $0x10, %ebx
setb %al
leal 0x8(,%rax,2), %r14d
orl $0x6, %eax
movq %rax, 0x38(%rsp)
leal 0x1(%rbx), %r15d
shrl %r15d
leaq 0x60(%rsp), %rdi
movq 0x70(%rsp), %r13
movq %r13, %rsi
movq %r15, %rdx
callq 0x10d0
movl %ebx, %eax
shrl %eax
movq %rax, 0x30(%rsp)
leaq (%rax,%r13), %rsi
leaq 0x40(%rsp), %rdi
movq %r15, 0x58(%rsp)
movq %r15, %rdx
callq 0x10d0
movl $0x80, %eax
movl $0x0, %ecx
testb $0x1, %bl
movq %r12, %r13
je 0x21f3
cmpl $0x2, %ebx
jb 0x21df
movq 0x30(%rsp), %rcx
leal 0x1f(%rcx), %eax
andl $-0x20, %eax
leaq -0x1(%rcx), %rdx
leaq 0x41(%rsp), %rcx
vpbroadcastb -0x1(%rcx), %ymm9
vpbroadcastq %rdx, %ymm0
xorl %edx, %edx
vpmovsxbq 0x3045(%rip), %ymm1 # 0x5100
vpmovsxbq 0x3040(%rip), %ymm2 # 0x5104
vpmovsxbq 0x303b(%rip), %ymm3 # 0x5108
vpmovsxbq 0x3036(%rip), %ymm4 # 0x510c
vpmovsxbq 0x3031(%rip), %ymm5 # 0x5110
vpmovsxbq 0x302c(%rip), %ymm6 # 0x5114
vpmovsxbq 0x3027(%rip), %ymm7 # 0x5118
vpmovsxbq 0x3022(%rip), %ymm8 # 0x511c
vpbroadcastd 0x3021(%rip), %ymm10 # 0x5124
vpbroadcastq %rdx, %ymm11
vpor %ymm1, %ymm11, %ymm12
vpor %ymm2, %ymm11, %ymm13
vpor %ymm3, %ymm11, %ymm14
vpor %ymm4, %ymm11, %ymm15
vporq %ymm5, %ymm11, %ymm16
vporq %ymm6, %ymm11, %ymm17
vporq %ymm7, %ymm11, %ymm18
vpor %ymm8, %ymm11, %ymm11
vpcmpleuq %ymm0, %ymm11, %k0
vpcmpleuq %ymm0, %ymm18, %k1
kshiftlb $0x4, %k1, %k1
korb %k1, %k0, %k0
vpcmpleuq %ymm0, %ymm17, %k1
vpcmpleuq %ymm0, %ymm16, %k2
kshiftlb $0x4, %k2, %k2
korb %k2, %k1, %k1
kunpckbw %k0, %k1, %k0
vpcmpleuq %ymm0, %ymm15, %k1
vpcmpleuq %ymm0, %ymm14, %k2
kshiftlb $0x4, %k2, %k2
korb %k2, %k1, %k1
vpcmpleuq %ymm0, %ymm13, %k2
vpcmpleuq %ymm0, %ymm12, %k3
kshiftlb $0x4, %k3, %k3
korb %k3, %k2, %k2
kunpckbw %k1, %k2, %k1
kunpckwd %k0, %k1, %k1
vmovdqu8 (%rcx,%rdx), %ymm11 {%k1} {z}
vperm2i128 $0x21, %ymm11, %ymm9, %ymm9 # ymm9 = ymm9[2,3],ymm11[0,1]
vpalignr $0xf, %ymm9, %ymm11, %ymm9 # ymm9 = ymm9[15],ymm11[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14],ymm9[31],ymm11[16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]
vpsllw $0x4, %ymm9, %ymm9
vpsrlw $0x4, %ymm11, %ymm12
vpternlogd $0xd8, %ymm10, %ymm9, %ymm12
vmovdqu8 %ymm12, -0x1(%rcx,%rdx) {%k1}
addq $0x20, %rdx
vmovdqa %ymm11, %ymm9
cmpq %rdx, %rax
jne 0x2103
movq 0x30(%rsp), %rax
shlb $0x4, 0x40(%rsp,%rax)
movl $0x8, %eax
movl $0xf0, %ecx
movl %ecx, 0x8(%rsp)
movl %eax, 0xc(%rsp)
movl %ebx, %eax
movq %rax, 0x88(%rsp)
cmpl $0x0, 0x4(%rsp)
movq %rbx, 0xa8(%rsp)
je 0x2281
cmpl $0xf, %ebx
movl (%rsp), %r15d
ja 0x2273
vpxor %xmm0, %xmm0, %xmm0
leaq 0x10(%rsp), %rbx
vmovdqa %xmm0, (%rbx)
movq %rbx, %rdi
movq 0x70(%rsp), %rsi
movq 0x88(%rsp), %rdx
vzeroupper
callq 0x10d0
orb $-0x80, (%rbx)
vmovdqa (%rbx), %xmm0
vpxor (%r13), %xmm0, %xmm0
vmovdqa %xmm0, (%rbx)
movq %rbp, %rdi
movl %r15d, %esi
xorl %edx, %edx
movl $0x3, %ecx
movq %rbx, %r8
movq %rbx, %r9
callq 0x19ae
movb (%rbx), %al
andb $-0x80, %al
xorb %al, 0x60(%rsp)
movq %r13, %rcx
leal -0x1(%r14), %ebx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
jmp 0x228b
movq %r13, %rcx
movl $0x1, %esi
xorl %ebx, %ebx
movq %rcx, 0x98(%rsp)
movq %rbp, 0xa0(%rsp)
shrl %r14d
movq 0x30(%rsp), %rdx
leaq (%rsp,%rdx), %rax
addq $0x10, %rax
movq %rax, 0x90(%rsp)
leaq 0x60(%rsp), %rdx
vmovaps (%rdx), %xmm0
vmovaps %xmm0, 0x70(%rsp)
leaq 0x40(%rsp), %rax
vmovaps (%rax), %xmm0
leal (%rbx,%rsi), %r12d
addb %sil, %sil
movq %rsi, 0xb0(%rsp)
movq 0x90(%rsp), %r15
movq 0x98(%rsp), %r13
movl (%rsp), %ebp
vmovaps %xmm0, 0xc0(%rsp)
movq %rbx, 0xb8(%rsp)
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x10(%rsp)
leaq 0x10(%rsp), %rdi
leaq 0x40(%rsp), %rsi
movq 0x58(%rsp), %rdx
vzeroupper
callq 0x10d0
movzbl (%r15), %eax
andl 0x8(%rsp), %eax
orl 0xc(%rsp), %eax
movb %al, (%r15)
vmovups (%r13), %xmm0
vxorps 0x10(%rsp), %xmm0, %xmm0
vmovaps %xmm0, 0x10(%rsp)
xorb %bl, 0x1f(%rsp)
movq 0xa0(%rsp), %rbx
movq %rbx, %rdi
movl %ebp, %esi
xorl %edx, %edx
movq 0x38(%rsp), %rcx
leaq 0x10(%rsp), %r8
movq %r8, %r9
callq 0x19ae
vmovaps 0x70(%rsp), %xmm0
vxorps 0x10(%rsp), %xmm0, %xmm0
vmovaps %xmm0, 0x70(%rsp)
vmovaps %xmm0, 0x60(%rsp)
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x10(%rsp)
leaq 0x10(%rsp), %rdi
leaq 0x60(%rsp), %rsi
movq 0x58(%rsp), %rdx
callq 0x10d0
movzbl (%r15), %eax
andl 0x8(%rsp), %eax
orl 0xc(%rsp), %eax
movb %al, (%r15)
vmovups (%r13), %xmm0
vxorps 0x10(%rsp), %xmm0, %xmm0
vmovaps %xmm0, 0x10(%rsp)
xorb %r12b, 0x1f(%rsp)
movq %rbx, %rdi
movq 0xb8(%rsp), %rbx
movl %ebp, %esi
xorl %edx, %edx
movq 0x38(%rsp), %rcx
leaq 0x10(%rsp), %r8
movq %r8, %r9
callq 0x19ae
vmovaps 0xc0(%rsp), %xmm0
vxorps 0x10(%rsp), %xmm0, %xmm0
vmovaps %xmm0, 0x40(%rsp)
movq 0xb0(%rsp), %rax
addb %al, %r12b
addb %al, %bl
decl %r14d
jne 0x22ed
leaq 0x10(%rsp), %rdi
leaq 0x40(%rsp), %rsi
movq 0x30(%rsp), %rbx
movq %rbx, %rdx
callq 0x10d0
leaq 0x60(%rsp), %rsi
movq 0x90(%rsp), %rdi
movq 0x58(%rsp), %rdx
callq 0x10d0
movq 0xa8(%rsp), %r9
testb $0x1, %r9b
movl (%rsp), %ebp
movq 0xa0(%rsp), %r14
movq 0x98(%rsp), %r15
movq 0x88(%rsp), %r13
movq 0x110(%rsp), %rdi
je 0x24be
leal -0x1(%r9), %eax
cmpl %ebx, %eax
jbe 0x24aa
movl %eax, %edx
leal -0x2(%r9), %ecx
addq %rsp, %rcx
addq $0x10, %rcx
addq %rsp, %rdx
addq $0x10, %rdx
xorl %esi, %esi
movb (%rdx,%rsi), %r10b
movb (%rcx,%rsi), %r8b
shrb $0x4, %r10b
shlb $0x4, %r8b
orb %r10b, %r8b
movb %r8b, (%rdx,%rsi)
decq %rsi
decl %eax
cmpl %eax, %ebx
jb 0x248a
movb 0x60(%rsp), %al
shrb $0x4, %al
movb 0x40(%rsp,%rbx), %cl
andb $-0x10, %cl
orb %al, %cl
movb %cl, 0x10(%rsp,%rbx)
cmpl $0x0, 0x4(%rsp)
movq %r9, %r12
setne %bl
leaq 0x10(%rsp), %rsi
movq %r13, %rdx
callq 0x10d0
cmpl $0x10, %r12d
setae %al
orb %bl, %al
jne 0x2532
leaq (%rsp,%r13), %rdi
addq $0x10, %rdi
movl $0x10, %edx
subl %r12d, %edx
xorl %esi, %esi
callq 0x1040
leaq 0x10(%rsp), %rbx
orb $-0x80, (%rbx)
vmovaps (%rbx), %xmm0
vxorps (%r15), %xmm0, %xmm0
vmovaps %xmm0, (%rbx)
movq %r14, %rdi
movl %ebp, %esi
xorl %edx, %edx
movl $0x3, %ecx
movq %rbx, %r8
movq %rbx, %r9
callq 0x19ae
movb (%rbx), %al
andb $-0x80, %al
movq 0x110(%rsp), %rcx
xorb %al, (%rcx)
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq