// File: /workspace/github_c_makefile_star2_O1/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, 0x34(%rsp)
movl %r8d, %ebx
movq %rcx, 0x80(%rsp)
movq %rdi, 0x60(%rsp)
cmpl $0x1, %r8d
movl %esi, 0xc(%rsp)
movq %rdx, %r13
je 0x2006
cmpl $0x2, %ebx
jne 0x2018
movl $0x7, %eax
movq %rax, 0x58(%rsp)
movl $0x10, %r14d
jmp 0x2030
movl $0x7, %eax
movq %rax, 0x58(%rsp)
movl $0x18, %r14d
jmp 0x2030
xorl %eax, %eax
cmpl $0x10, %ebx
setb %al
leal 0x8(,%rax,2), %r14d
orl $0x6, %eax
movq %rax, 0x58(%rsp)
leal 0x1(%rbx), %r15d
shrl %r15d
leaq 0x70(%rsp), %rdi
movq 0x80(%rsp), %r12
movq %r12, %rsi
movq %r15, %rdx
callq 0x10d0
movl %ebx, %eax
shrl %eax
movq %rax, %rbp
leaq (%r12,%rax), %rsi
leaq 0x40(%rsp), %rdi
movq %r15, 0xa8(%rsp)
movq %r15, %rdx
callq 0x10d0
movl $0x80, %eax
movl $0x0, %ecx
testb $0x1, %bl
je 0x21d3
cmpl $0x2, %ebx
jb 0x21c4
movq %rbp, %rax
movl %eax, %ecx
leal 0x1f(%rbp), %eax
andl $-0x20, %eax
decq %rcx
vpbroadcastq %rcx, %ymm0
xorl %ecx, %ecx
vpmovsxbq 0x3056(%rip), %ymm1 # 0x5100
vpmovsxbq 0x3051(%rip), %ymm2 # 0x5104
vpmovsxbq 0x304c(%rip), %ymm3 # 0x5108
vpmovsxbq 0x3047(%rip), %ymm4 # 0x510c
vpmovsxbq 0x3042(%rip), %ymm5 # 0x5110
vpmovsxbq 0x303d(%rip), %ymm6 # 0x5114
vpmovsxbq 0x3038(%rip), %ymm7 # 0x5118
vpmovsxbq 0x3033(%rip), %ymm8 # 0x511c
vpbroadcastd 0x3032(%rip), %ymm9 # 0x5124
vpbroadcastq %rcx, %ymm10
vpor %ymm1, %ymm10, %ymm11
vpor %ymm2, %ymm10, %ymm12
vpor %ymm3, %ymm10, %ymm13
vpor %ymm4, %ymm10, %ymm14
vpor %ymm5, %ymm10, %ymm15
vporq %ymm6, %ymm10, %ymm16
vporq %ymm7, %ymm10, %ymm17
vpor %ymm8, %ymm10, %ymm10
vpcmpleuq %ymm0, %ymm10, %k0
vpcmpleuq %ymm0, %ymm17, %k1
kshiftlb $0x4, %k1, %k1
korb %k1, %k0, %k0
vpcmpleuq %ymm0, %ymm16, %k1
vpcmpleuq %ymm0, %ymm15, %k2
kshiftlb $0x4, %k2, %k2
korb %k2, %k1, %k1
kunpckbw %k0, %k1, %k0
vpcmpleuq %ymm0, %ymm14, %k1
vpcmpleuq %ymm0, %ymm13, %k2
kshiftlb $0x4, %k2, %k2
korb %k2, %k1, %k1
vpcmpleuq %ymm0, %ymm12, %k2
vpcmpleuq %ymm0, %ymm11, %k3
kshiftlb $0x4, %k3, %k3
korb %k3, %k2, %k2
kunpckbw %k1, %k2, %k1
kunpckwd %k0, %k1, %k1
vmovdqu8 0x40(%rsp,%rcx), %ymm10 {%k1} {z}
vmovdqu8 0x41(%rsp,%rcx), %ymm11 {%k1} {z}
vpsllw $0x4, %ymm10, %ymm10
vpsrlw $0x4, %ymm11, %ymm11
vpternlogd $0xd8, %ymm9, %ymm10, %ymm11
vmovdqu8 %ymm11, 0x40(%rsp,%rcx) {%k1}
addq $0x20, %rcx
cmpq %rcx, %rax
jne 0x20f2
shlb $0x4, 0x40(%rsp,%rbp)
movl $0x8, %eax
movl $0xf0, %ecx
movl %eax, 0x3c(%rsp)
movl %ebx, %eax
movq %rax, 0x98(%rsp)
cmpl $0x0, 0x34(%rsp)
movq %r13, 0x68(%rsp)
movq %rbx, 0xb0(%rsp)
movl %ecx, 0x38(%rsp)
je 0x2269
cmpl $0xf, %ebx
movl 0xc(%rsp), %r15d
ja 0x225e
vpxor %xmm0, %xmm0, %xmm0
leaq 0x10(%rsp), %rbx
vmovdqa %xmm0, (%rbx)
movq %rbx, %rdi
movq 0x80(%rsp), %rsi
movq 0x98(%rsp), %rdx
vzeroupper
callq 0x10d0
orb $-0x80, (%rbx)
vmovdqa (%rbx), %xmm0
vpxor (%r13), %xmm0, %xmm0
vmovdqa %xmm0, (%rbx)
movq 0x60(%rsp), %rdi
movl %r15d, %esi
xorl %edx, %edx
movl $0x3, %ecx
movq %rbx, %r8
movq %rbx, %r9
callq 0x19a2
movb (%rbx), %al
andb $-0x80, %al
xorb %al, 0x70(%rsp)
leal -0x1(%r14), %ebx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x2270
movl $0x1, %eax
xorl %ebx, %ebx
shrl %r14d
leaq 0x70(%rsp), %rcx
vmovaps (%rcx), %xmm0
vmovaps %xmm0, 0x80(%rsp)
leaq 0x40(%rsp), %rcx
vmovaps (%rcx), %xmm0
leal (%rbx,%rax), %r12d
addb %al, %al
movq %rax, 0xb8(%rsp)
movq %rbp, 0xa0(%rsp)
movq 0xa8(%rsp), %rbp
movq 0xa0(%rsp), %r15
movl 0xc(%rsp), %r13d
vmovaps %xmm0, 0xc0(%rsp)
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x10(%rsp)
leaq 0x10(%rsp), %rdi
leaq 0x40(%rsp), %rsi
movq %rbp, %rdx
vzeroupper
callq 0x10d0
movzbl 0x10(%rsp,%r15), %eax
andl 0x38(%rsp), %eax
orl 0x3c(%rsp), %eax
movb %al, 0x10(%rsp,%r15)
movq 0x68(%rsp), %rax
vmovups (%rax), %xmm0
vxorps 0x10(%rsp), %xmm0, %xmm0
vmovaps %xmm0, 0x10(%rsp)
xorb %bl, 0x1f(%rsp)
movq 0x60(%rsp), %rdi
movl %r13d, %esi
xorl %edx, %edx
movq 0x58(%rsp), %rcx
leaq 0x10(%rsp), %r8
movq %r8, %r9
callq 0x19a2
vmovaps 0x80(%rsp), %xmm0
vxorps 0x10(%rsp), %xmm0, %xmm0
vmovaps %xmm0, 0x80(%rsp)
vmovaps %xmm0, 0x70(%rsp)
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x10(%rsp)
leaq 0x10(%rsp), %rdi
leaq 0x70(%rsp), %rsi
movq %rbp, %rdx
callq 0x10d0
movzbl 0x10(%rsp,%r15), %eax
andl 0x38(%rsp), %eax
orl 0x3c(%rsp), %eax
movb %al, 0x10(%rsp,%r15)
movq 0x68(%rsp), %rax
vmovups (%rax), %xmm0
vxorps 0x10(%rsp), %xmm0, %xmm0
vmovaps %xmm0, 0x10(%rsp)
xorb %r12b, 0x1f(%rsp)
movq 0x60(%rsp), %rdi
movl %r13d, %esi
xorl %edx, %edx
movq 0x58(%rsp), %rcx
leaq 0x10(%rsp), %r8
movq %r8, %r9
callq 0x19a2
vmovaps 0xc0(%rsp), %xmm0
vxorps 0x10(%rsp), %xmm0, %xmm0
vmovaps %xmm0, 0x40(%rsp)
movq 0xb8(%rsp), %rax
addb %al, %r12b
addb %al, %bl
decl %r14d
jne 0x22b9
leaq 0x10(%rsp), %rdi
leaq 0x40(%rsp), %rsi
movq 0xa0(%rsp), %rbp
movq %rbp, %rdx
callq 0x10d0
leaq (%rsp,%rbp), %rdi
addq $0x10, %rdi
leaq 0x70(%rsp), %rsi
movq 0xa8(%rsp), %rdx
callq 0x10d0
movq 0xb0(%rsp), %r9
testb $0x1, %r9b
movl 0xc(%rsp), %r13d
movq 0x98(%rsp), %r15
movq 0x110(%rsp), %r12
je 0x2480
leal -0x1(%r9), %eax
cmpl %ebp, %eax
jbe 0x246c
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), %dil
movb (%rcx,%rsi), %r8b
shrb $0x4, %dil
shlb $0x4, %r8b
orb %dil, %r8b
movb %r8b, (%rdx,%rsi)
decq %rsi
decl %eax
cmpl %eax, %ebp
jb 0x244c
movb 0x70(%rsp), %al
shrb $0x4, %al
movb 0x40(%rsp,%rbp), %cl
andb $-0x10, %cl
orb %al, %cl
movb %cl, 0x10(%rsp,%rbp)
cmpl $0x0, 0x34(%rsp)
movq %r9, %r14
setne %bl
leaq 0x10(%rsp), %rsi
movq %r12, %rdi
movq %r15, %rdx
callq 0x10d0
cmpl $0x10, %r14d
setae %al
orb %bl, %al
jne 0x24f8
leaq (%rsp,%r15), %rdi
addq $0x10, %rdi
movl $0x10, %edx
subl %r14d, %edx
xorl %esi, %esi
callq 0x1040
leaq 0x10(%rsp), %rbx
orb $-0x80, (%rbx)
vmovaps (%rbx), %xmm0
movq 0x68(%rsp), %rax
vxorps (%rax), %xmm0, %xmm0
vmovaps %xmm0, (%rbx)
movq 0x60(%rsp), %rdi
movl %r13d, %esi
xorl %edx, %edx
movl $0x3, %ecx
movq %rbx, %r8
movq %rbx, %r9
callq 0x19a2
movb (%rbx), %al
andb $-0x80, %al
xorb %al, (%r12)
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq