// File: /workspace/github_c_makefile_star2_O2/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 $0xb8, %rsp
movl %r9d, %ebp
movl %r8d, %r14d
movq %rcx, %rbx
movq %rdx, 0x28(%rsp)
movl %esi, 0x4(%rsp)
movq %rdi, 0x30(%rsp)
pushq $0x7
popq %rax
movq %rax, 0x80(%rsp)
cmpl $0x1, %r8d
je 0x1e39
cmpl $0x2, %r14d
jne 0x1e3f
pushq $0x10
jmp 0x1e3b
pushq $0x18
popq %r12
jmp 0x1e5b
xorl %eax, %eax
cmpl $0x10, %r14d
setb %al
leal 0x8(,%rax,2), %r12d
orl $0x6, %eax
movq %rax, 0x80(%rsp)
leal 0x1(%r14), %r13d
shrl %r13d
leaq 0x90(%rsp), %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x10d0
movl %r14d, %r15d
shrl %r15d
leaq (%rbx,%r15), %rsi
leaq 0x10(%rsp), %rdi
movq %r13, 0x38(%rsp)
movq %r13, %rdx
callq 0x10d0
movl $0x80, 0xc(%rsp)
xorl %r13d, %r13d
movl $0x0, 0x8(%rsp)
testb $0x1, %r14b
je 0x1edf
xorl %eax, %eax
cmpq %rax, %r15
je 0x1eca
movb 0x10(%rsp,%rax), %cl
movb 0x11(%rsp,%rax), %dl
shrb $0x4, %dl
shlb $0x4, %cl
orb %dl, %cl
movb %cl, 0x10(%rsp,%rax)
incq %rax
jmp 0x1eac
shlb $0x4, 0x10(%rsp,%r15)
pushq $0x8
popq %rax
movl %eax, 0xc(%rsp)
movl $0xf0, 0x8(%rsp)
movl %r14d, %eax
movq %rax, 0x78(%rsp)
testl %ebp, %ebp
movq %r14, 0xa8(%rsp)
je 0x1f5c
cmpl $0xf, %r14d
ja 0x1f53
vxorps %xmm0, %xmm0, %xmm0
leaq 0x50(%rsp), %r14
vmovaps %xmm0, (%r14)
movq %r14, %rdi
movq %rbx, %rsi
movq 0x78(%rsp), %rdx
callq 0x10d0
orb $-0x80, (%r14)
pushq $0x10
popq %rdx
movq 0x28(%rsp), %rdi
movq %r14, %rsi
movq %r14, %rcx
callq 0x1d24
pushq $0x3
popq %rcx
movq 0x30(%rsp), %rdi
movl 0x4(%rsp), %esi
xorl %edx, %edx
movq %r14, %r8
movq %r14, %r9
callq 0x17ac
movb (%r14), %al
andb $-0x80, %al
xorb %al, 0x90(%rsp)
leal -0x1(%r12), %r13d
pushq $-0x1
jmp 0x1f5e
pushq $0x1
popq %rax
movl %ebp, 0x40(%rsp)
shrl %r12d
movq %r15, 0xa0(%rsp)
leaq (%rsp,%r15), %rcx
addq $0x50, %rcx
movq %rcx, 0x88(%rsp)
leal (%rax,%rax), %ecx
movl %ecx, 0x44(%rsp)
movl %r13d, %ebx
addl %eax, %ebx
leaq 0x50(%rsp), %r14
movq 0x30(%rsp), %rbp
subl $0x1, %r12d
jb 0x20bb
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x50(%rsp)
movq %r14, %rdi
leaq 0x10(%rsp), %rsi
movq 0x38(%rsp), %rdx
callq 0x10d0
movl %ebx, 0x48(%rsp)
movq 0x88(%rsp), %rbx
movzbl (%rbx), %eax
andl 0x8(%rsp), %eax
orl 0xc(%rsp), %eax
movb %al, (%rbx)
movq %r14, %rdi
movq %r12, 0xb0(%rsp)
movq 0x28(%rsp), %r12
movq %r12, %rsi
pushq $0x10
popq %rdx
movq %r14, %rcx
callq 0x1d24
xorb %r13b, 0x5f(%rsp)
movq %rbp, %rdi
movl 0x4(%rsp), %esi
xorl %edx, %edx
movl %r13d, 0x4c(%rsp)
movq 0x80(%rsp), %r13
movl %r13d, %ecx
movq %r14, %r8
movq %r14, %r9
callq 0x17ac
leaq 0x90(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
pushq $0x10
popq %rdx
movq %r15, %rcx
callq 0x1d24
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x50(%rsp)
movq %r14, %rdi
movq %r15, %rsi
movq 0x38(%rsp), %rdx
callq 0x10d0
movzbl (%rbx), %eax
andl 0x8(%rsp), %eax
orl 0xc(%rsp), %eax
movb %al, (%rbx)
movl 0x48(%rsp), %ebx
movq %r14, %rdi
movq %r12, %rsi
movq 0xb0(%rsp), %r12
pushq $0x10
popq %rdx
movl %edx, %r15d
movq %r14, %rcx
callq 0x1d24
xorb %bl, 0x5f(%rsp)
movq %rbp, %rdi
movl 0x4(%rsp), %esi
xorl %edx, %edx
movl %r13d, %ecx
movl 0x4c(%rsp), %r13d
movq %r14, %r8
movq %r14, %r9
callq 0x17ac
leaq 0x10(%rsp), %rcx
movq %rcx, %rdi
movq %r14, %rsi
movl %r15d, %edx
callq 0x1d24
movl 0x44(%rsp), %eax
addb %al, %r13b
addb %al, %bl
jmp 0x1f94
leaq 0x50(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movq 0xa0(%rsp), %r15
movq %r15, %rdx
callq 0x10d0
leaq 0x90(%rsp), %rsi
movq 0x88(%rsp), %rdi
movq 0x38(%rsp), %rdx
callq 0x10d0
movq 0xa8(%rsp), %r14
testb $0x1, %r14b
movl 0x40(%rsp), %edi
movq 0xf0(%rsp), %r12
je 0x2156
leal -0x2(%r14), %eax
leal -0x1(%r14), %ecx
addq %rsp, %rcx
addq $0x50, %rcx
leal 0x1(%rax), %edx
cmpl %r15d, %edx
jbe 0x213d
movb (%rcx), %dl
movl %eax, %esi
movb 0x50(%rsp,%rsi), %sil
shrb $0x4, %dl
shlb $0x4, %sil
orb %dl, %sil
movb %sil, (%rcx)
decl %eax
decq %rcx
jmp 0x2118
movb 0x90(%rsp), %al
shrb $0x4, %al
movb 0x10(%rsp,%r15), %cl
andb $-0x10, %cl
orb %al, %cl
movb %cl, 0x50(%rsp,%r15)
testl %edi, %edi
setne %bl
leaq 0x50(%rsp), %rsi
movq %r12, %rdi
movq 0x78(%rsp), %r15
movq %r15, %rdx
callq 0x10d0
cmpl $0x10, %r14d
setae %al
orb %bl, %al
jne 0x21cd
leaq (%rsp,%r15), %rdi
addq $0x50, %rdi
pushq $0x10
popq %rbp
movl %ebp, %edx
subl %r14d, %edx
xorl %esi, %esi
callq 0x1040
leaq 0x50(%rsp), %rbx
orb $-0x80, (%rbx)
movq 0x28(%rsp), %rdi
movq %rbx, %rsi
movl %ebp, %edx
movq %rbx, %rcx
callq 0x1d24
pushq $0x3
popq %rcx
movq 0x30(%rsp), %rdi
movl 0x4(%rsp), %esi
xorl %edx, %edx
movq %rbx, %r8
movq %rbx, %r9
callq 0x17ac
movb (%rbx), %al
andb $-0x80, %al
xorb %al, (%r12)
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq