// File: /workspace/github_c_makefile_star2_O1/mjosaarinen[P]brutus/crypto_aead/aezv3/aesni/aez_ni.c
// Function: cipher_aez_tiny
int cipher_aez_tiny(aez_ctx_t *ctx, __m128i t, int d, char *src, unsigned bytes, unsigned abytes, char *dst) {
    __m128i l, r, tmp, one, rcon, buf[2], mask_10, mask_ff;
    __m128i I=ctx->I, L=ctx->L, J=ctx->J[0], t_orig = t;
    unsigned rnds, i;

    /* load src into buf, zero pad, update bytes for abytes */
    if (bytes >= 16) {
        buf[0] = load(src);
        buf[1] = zero_pad(load_partial(src+16,bytes-16),32-bytes);
    } else {
        buf[0] = zero_pad(load_partial(src,bytes),16-bytes);
        buf[1] = zero;
    }
    if (!d) bytes += abytes;

    /* load l/r, create 10* padding masks, shift r 4 bits if odd length */
    l = buf[0];
    r = loadu((char*)buf+bytes/2);
    mask_ff = loadu(pad+16-bytes/2);
    mask_10 = loadu(pad+32-bytes/2);
    if (bytes&1) {  /* Odd length. Deal with nibbles. */
        mask_10 = _mm_srli_epi32(mask_10,4);
        ((char*)&mask_ff)[bytes/2] = 0xf0;
        r = bswap16(r);
        r = vor(_mm_slli_epi64(r, 4), _mm_srli_epi64(_mm_slli_si128(r, 8), 60));
        r = bswap16(r);
    }
    r = vor(vand(r, mask_ff), mask_10);

    /* Add tweak offset into t, and determine the number of rounds */
    if (bytes >= 16) {
        t = vxor3(t, ctx->J[1], ctx->J[2]);             /* (0,6) offset */
        rnds = 8;
    } else {
        t = vxor4(t, ctx->J[0], ctx->J[1], ctx->J[2]);  /* (0,7) offset */
        if (bytes>=3) rnds = 10; else if (bytes==2) rnds = 16; else rnds = 24;
    }

    if (!d) {
        one = _mm_insert_epi8(zero,1,15);
        rcon = zero;
    } else {
        one = _mm_insert_epi8(zero,-1,15);
        rcon = _mm_insert_epi8(zero,rnds-1,15);
    }

    if ((d) && (bytes < 16)) {
        tmp = vor(l, loadu(pad+32));
        tmp = aes4(vxor4(tmp,t_orig,ctx->J[0],ctx->J[1]), I, J, L, zero);
        tmp = vand(tmp, loadu(pad+32));
        l = vxor(l, tmp);
    }

    /* Feistel */
    for (i=0; i<rnds; i+=2) {
        l = vor(vand(aes4(vxor3(t,r,rcon), I, J, L, l), mask_ff), mask_10);
        rcon = _mm_add_epi8(rcon,one);
        r = vor(vand(aes4(vxor3(t,l,rcon), I, J, L, r), mask_ff), mask_10);
        rcon = _mm_add_epi8(rcon,one);
    }
    buf[0] = r;
    if (bytes&1) {
        l = bswap16(l);
        l = vor(_mm_srli_epi64(l, 4), _mm_slli_epi64(_mm_srli_si128(l, 8), 60));
        l = bswap16(l);
        r = vand(loadu((char*)buf+bytes/2), _mm_insert_epi8(zero,0xf0,0));
        l = vor(l, r);
    }
    storeu((char*)buf+bytes/2, l);
    if (d) {
        bytes -= abytes;
        if (abytes==16) tmp = loadu((char*)buf+bytes);
        else {
            tmp = zero;
            for (i=0; i<abytes; i++) ((char*)&tmp)[i] = ((char*)buf+bytes)[i];
        }
        if (!_mm_testz_si128(tmp,tmp)) return -1;
    } else if (bytes < 16) {
        tmp = vor(zero_pad(buf[0], 16-bytes), loadu(pad+32));
        tmp = aes4(vxor4(tmp,t_orig,ctx->J[0],ctx->J[1]), I, J, L, zero);
        buf[0] = vxor(buf[0], vand(tmp, loadu(pad+32)));
    }
    for (i=0; i<bytes; i++) dst[i] = ((char*)buf)[i];
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdi, %r14
vmovdqa (%rdi), %xmm10
movq %r9, %rbx
movl %r8d, %ebp
movl %ecx, %r12d
movl %esi, %r15d
vmovdqa %xmm0, %xmm13
vmovdqa 0x10(%rdi), %xmm11
vmovdqa 0x20(%rdi), %xmm12
cmpl $0x10, %ecx
jb 0x2bbc
vmovups (%rdx), %xmm0
vmovaps %xmm0, 0x10(%rsp)
addq $0x10, %rdx
testb $0xf, %dl
je 0x2c11
movl %r12d, %eax
addl $-0x10, %eax
je 0x2bb5
movl %eax, %eax
movq %rsp, %rdi
movq %rdx, %rsi
movq %rax, %rdx
vmovdqa %xmm10, 0x60(%rsp)
vmovdqa %xmm11, 0x50(%rsp)
vmovdqa %xmm12, 0x40(%rsp)
vmovdqa %xmm13, 0x30(%rsp)
callq 0x10c0
vmovdqa 0x30(%rsp), %xmm13
vmovdqa 0x40(%rsp), %xmm12
vmovdqa 0x50(%rsp), %xmm11
vmovdqa 0x60(%rsp), %xmm10
vmovaps (%rsp), %xmm0
jmp 0x2c15
movl %r12d, %r13d
testb $0xf, %dl
je 0x2c2b
testl %r12d, %r12d
je 0x2c09
leaq 0x10(%rsp), %rdi
movq %rdx, %rsi
movq %r13, %rdx
vmovdqa %xmm10, 0x60(%rsp)
vmovdqa %xmm11, 0x50(%rsp)
vmovdqa %xmm12, 0x40(%rsp)
vmovdqa %xmm13, 0x30(%rsp)
callq 0x10c0
vmovdqa 0x30(%rsp), %xmm13
vmovdqa 0x40(%rsp), %xmm12
vmovdqa 0x50(%rsp), %xmm11
vmovdqa 0x60(%rsp), %xmm10
vmovaps 0x10(%rsp), %xmm0
jmp 0x2c2f
vmovups (%rdx), %xmm0
movl $0x20, %eax
subl %r12d, %eax
leaq 0x14dc(%rip), %rcx      # 0x4100
vandps (%rax,%rcx), %xmm0, %xmm0
jmp 0x2c48
vmovups (%rdx), %xmm0
leaq 0x14ca(%rip), %rax      # 0x4100
subq %r13, %rax
vandps 0x10(%rax), %xmm0, %xmm0
vmovaps %xmm0, 0x10(%rsp)
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x20(%rsp)
xorl %r13d, %r13d
testl %r15d, %r15d
cmovel %ebp, %r13d
addl %r12d, %r13d
vmovdqa 0x10(%rsp), %xmm0
movl %r13d, %ecx
shrl %ecx
vmovdqu 0x10(%rsp,%rcx), %xmm1
leaq 0x148d(%rip), %rax      # 0x4100
movq %rax, %rdx
subq %rcx, %rdx
vmovdqu 0x10(%rdx), %xmm3
vmovdqu 0x20(%rdx), %xmm2
vmovdqa %xmm3, 0x70(%rsp)
testb $0x1, %r13b
je 0x2cbe
vpsrld $0x4, %xmm2, %xmm2
movb $-0x10, 0x70(%rsp,%rcx)
vmovdqa 0x13df(%rip), %xmm3     # 0x4080
vpshufb %xmm3, %xmm1, %xmm1
vpslldq $0x8, %xmm1, %xmm4      # xmm4 = zero,zero,zero,zero,zero,zero,zero,zero,xmm1[0,1,2,3,4,5,6,7]
vpsllq $0x4, %xmm1, %xmm1
vpsrlq $0x3c, %xmm4, %xmm4
vpor %xmm4, %xmm1, %xmm1
vpshufb %xmm3, %xmm1, %xmm1
vmovdqa 0x30(%r14), %xmm3
vpternlogq $0x96, 0x40(%r14), %xmm13, %xmm3
movl $0x8, %edx
cmpl $0xf, %r13d
ja 0x2cf3
vpxor %xmm3, %xmm12, %xmm3
movl $0xa, %edx
cmpl $0x2, %r13d
ja 0x2cf3
setne %dl
movzbl %dl, %edx
leal 0x10(,%rdx,8), %edx
leal -0x1(%rdx), %edi
testl %r15d, %r15d
sete %sil
jne 0x2d09
vmovdqa 0x12f9(%rip), %xmm4     # 0x4000
jmp 0x2d11
vmovdqa 0x13af(%rip), %xmm4     # 0x40c0
vpxor %xmm5, %xmm5, %xmm5
je 0x2d1d
vpinsrb $0xf, %edi, %xmm5, %xmm5
vmovdqa 0x70(%rsp), %xmm6
cmpl $0x10, %r13d
setae %dil
orb %dil, %sil
jne 0x2d75
vpmovzxbq 0x13b7(%rip), %xmm7 # 0x40f0
vpor %xmm7, %xmm0, %xmm8
vmovdqa %xmm8, (%rsp)
vpxor 0x30(%r14), %xmm13, %xmm9
vpternlogq $0x96, %xmm12, %xmm8, %xmm9
vaesenc %xmm10, %xmm9, %xmm8
vaesenc %xmm12, %xmm8, %xmm8
vaesenc %xmm11, %xmm8, %xmm8
vpxor %xmm9, %xmm9, %xmm9
vaesenc %xmm9, %xmm8, %xmm8
vpand %xmm7, %xmm8, %xmm7
vmovdqa %xmm7, (%rsp)
vpxor %xmm0, %xmm7, %xmm0
vpternlogq $0xec, %xmm6, %xmm2, %xmm1
xorl %esi, %esi
vmovdqa %xmm1, %xmm7
vpternlogq $0x96, %xmm3, %xmm5, %xmm7
vaesenc %xmm10, %xmm7, %xmm7
vaesenc %xmm12, %xmm7, %xmm7
vaesenc %xmm11, %xmm7, %xmm7
vaesenc %xmm0, %xmm7, %xmm0
vpternlogq $0xec, %xmm6, %xmm2, %xmm0
vpaddb %xmm4, %xmm5, %xmm5
vmovdqa %xmm0, %xmm7
vpternlogq $0x96, %xmm5, %xmm3, %xmm7
vaesenc %xmm10, %xmm7, %xmm7
vaesenc %xmm12, %xmm7, %xmm7
vaesenc %xmm11, %xmm7, %xmm7
vaesenc %xmm1, %xmm7, %xmm1
vpaddb %xmm4, %xmm5, %xmm5
addl $0x2, %esi
vpternlogq $0xec, %xmm6, %xmm2, %xmm1
cmpl %edx, %esi
jb 0x2d7e
vmovdqa %xmm1, 0x10(%rsp)
testb $0x1, %r13b
je 0x2e1b
vmovdqa 0x1293(%rip), %xmm1     # 0x4080
vpshufb %xmm1, %xmm0, %xmm0
vpsrldq $0x8, %xmm0, %xmm2      # xmm2 = xmm0[8,9,10,11,12,13,14,15],zero,zero,zero,zero,zero,zero,zero,zero
vpsrlq $0x4, %xmm0, %xmm0
vpsllq $0x3c, %xmm2, %xmm2
vpor %xmm0, %xmm2, %xmm0
vpshufb %xmm1, %xmm0, %xmm1
vmovdqu 0x10(%rsp,%rcx), %xmm0
vpternlogq $0xec, 0x12b5(%rip), %xmm1, %xmm0 # 0x40d0
vmovdqu %xmm0, 0x10(%rsp,%rcx)
testl %r15d, %r15d
je 0x2e41
subl %ebp, %r13d
cmpl $0x10, %ebp
jne 0x2eaa
movl %r13d, %eax
vmovdqu 0x10(%rsp,%rax), %xmm0
vmovdqa %xmm0, (%rsp)
jmp 0x2ecc
cmpl $0xf, %r13d
ja 0x2ed8
vmovdqa 0x10(%rsp), %xmm0
movl %r13d, %ecx
subq %rcx, %rax
vpmovzxbq 0x1290(%rip), %xmm1 # 0x40f0
vmovdqu 0x10(%rax), %xmm2
vpternlogq $0xea, %xmm1, %xmm0, %xmm2
vmovdqa %xmm2, (%rsp)
vpxor 0x30(%r14), %xmm13, %xmm3
vpternlogq $0x96, %xmm12, %xmm2, %xmm3
vaesenc %xmm10, %xmm3, %xmm2
vaesenc %xmm12, %xmm2, %xmm2
vaesenc %xmm11, %xmm2, %xmm2
vpxor %xmm3, %xmm3, %xmm3
vaesenc %xmm3, %xmm2, %xmm2
vmovdqa %xmm2, (%rsp)
vpternlogq $0x78, %xmm1, %xmm2, %xmm0
vmovdqa %xmm0, 0x10(%rsp)
jmp 0x2ed8
vpxor %xmm0, %xmm0, %xmm0
vmovdqa %xmm0, (%rsp)
testl %ebp, %ebp
je 0x2ecc
movl %r13d, %eax
leaq (%rsp,%rax), %rsi
addq $0x10, %rsi
movl %ebp, %edx
movq %rsp, %rdi
callq 0x10c0
vmovdqa (%rsp), %xmm0
vptest %xmm0, %xmm0
jne 0x2ef1
xorl %ebp, %ebp
testl %r13d, %r13d
je 0x2ef6
movl %r13d, %edx
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x10c0
jmp 0x2ef6
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq