// File: /workspace/github_c_makefile_star2_O1/mjosaarinen[P]brutus/crypto_aead/aezv4/aesni/encrypt.c
// Function: cipher_aez_tiny
int cipher_aez_tiny(aez_ctx_t *ctx, block t, int d, char *src, unsigned bytes, unsigned abytes, char *dst) {
    block l, r, tmp, one, rcon, buf[2], mask_10, mask_ff;
    block I=ctx->I[0], 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 = sll4(mask_10);
        ((char*)&mask_ff)[bytes/2] = (char)0xf0;
        r = bswap16(r);
        r = srl4(r);
        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->I[1], ctx->I[2]);             /* (0,6) offset */
        rnds = 8;
    } else {
        t = vxor4(t, ctx->I[0], ctx->I[1], ctx->I[2]);  /* (0,7) offset */
        if (bytes>=3) rnds = 10; else if (bytes==2) rnds = 16; else rnds = 24;
    }
    
    if (!d) {
        one = zero_set_byte(1,15);
        rcon = zero;
    } else {
        one = zero_set_byte(-1,15);
        rcon = zero_set_byte((char)(rnds-1),15);
    }
    
    if ((d) && (bytes < 16)) {
        tmp = vor(l, loadu(pad+32));
        tmp = aes4(vxor4(tmp,t_orig,ctx->I[0],ctx->I[1]), J, I, 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), J, I, L, l), mask_ff), mask_10);
        rcon = vadd(rcon,one);
        r = vor(vand(aes4(vxor3(t,l,rcon), J, I, L, r), mask_ff), mask_10);
        rcon = vadd(rcon,one);
    }
    buf[0] = r;
    if (bytes&1) {
        l = bswap16(l);
        l = sll4(l);
        l = bswap16(l);
        r = vand(loadu((char*)buf+bytes/2), zero_set_byte((char)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 (!is_zero(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->I[0],ctx->I[1]), J, I, 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 %r14
pushq %rbx
subq $0x40, %rsp
movq %r9, %rbx
vmovups (%rdx), %xmm1
cmpl $0x10, %ecx
jb 0x2b25
vmovaps %xmm1, 0x10(%rsp)
movl $0x20, %eax
subl %ecx, %eax
leaq 0x15a8(%rip), %r9       # 0x40c0
vmovups (%rax,%r9), %xmm1
vandps 0x10(%rdx), %xmm1, %xmm4
jmp 0x2b40
movl %ecx, %eax
leaq 0x1592(%rip), %rdx      # 0x40c0
subq %rax, %rdx
vandps 0x10(%rdx), %xmm1, %xmm1
vmovaps %xmm1, 0x10(%rsp)
vxorps %xmm4, %xmm4, %xmm4
vmovdqa (%rdi), %xmm1
vmovdqa 0x30(%rdi), %xmm3
vmovdqa 0x60(%rdi), %xmm2
vmovaps %xmm4, 0x20(%rsp)
xorl %r14d, %r14d
testl %esi, %esi
cmovel %r8d, %r14d
addl %ecx, %r14d
vmovdqa 0x10(%rsp), %xmm4
movl %r14d, %ecx
shrl %ecx
vmovdqu 0x10(%rsp,%rcx), %xmm5
leaq 0x1548(%rip), %rax      # 0x40c0
movq %rax, %rdx
subq %rcx, %rdx
vmovdqu 0x10(%rdx), %xmm7
vmovupd 0x20(%rdx), %xmm6
vmovdqa %xmm7, 0x30(%rsp)
testb $0x1, %r14b
je 0x2bd2
vpsrldq $0x8, %xmm6, %xmm7      # xmm7 = xmm6[8,9,10,11,12,13,14,15],zero,zero,zero,zero,zero,zero,zero,zero
vpsrlq $0x4, %xmm6, %xmm6
vpsllq $0x3c, %xmm7, %xmm7
vpor %xmm6, %xmm7, %xmm6
movb $-0x10, 0x30(%rsp,%rcx)
vmovdqa 0x147c(%rip), %xmm7     # 0x4030
vpshufb %xmm7, %xmm5, %xmm5
vpslldq $0x8, %xmm5, %xmm8      # xmm8 = zero,zero,zero,zero,zero,zero,zero,zero,xmm5[0,1,2,3,4,5,6,7]
vpsllq $0x4, %xmm5, %xmm5
vpsrlq $0x3c, %xmm8, %xmm8
vpor %xmm5, %xmm8, %xmm5
vpshufb %xmm7, %xmm5, %xmm5
vmovdqa 0x10(%rdi), %xmm7
vpternlogq $0x96, 0x20(%rdi), %xmm0, %xmm7
movl $0x8, %edx
cmpl $0xf, %r14d
ja 0x2c06
vpxor %xmm1, %xmm7, %xmm7
movl $0xa, %edx
cmpl $0x2, %r14d
ja 0x2c06
setne %dl
movzbl %dl, %edx
leal 0x10(,%rdx,8), %edx
leal -0x1(%rdx), %r10d
testl %esi, %esi
sete %r9b
jne 0x2c1c
vmovdqa 0x1466(%rip), %xmm8     # 0x4080
jmp 0x2c24
vmovdqa 0x144c(%rip), %xmm8     # 0x4070
vpxor %xmm9, %xmm9, %xmm9
je 0x2c31
vpinsrb $0xf, %r10d, %xmm9, %xmm9
vmovdqa 0x30(%rsp), %xmm10
cmpl $0x10, %r14d
setae %r10b
orb %r10b, %r9b
jne 0x2c89
vpmovzxbq 0x1463(%rip), %xmm11 # 0x40b0
vpor %xmm4, %xmm11, %xmm12
vmovdqa %xmm12, (%rsp)
vpxor 0x10(%rdi), %xmm0, %xmm13
vpternlogq $0x96, %xmm1, %xmm12, %xmm13
vaesenc %xmm3, %xmm13, %xmm12
vaesenc %xmm1, %xmm12, %xmm12
vaesenc %xmm2, %xmm12, %xmm12
vpxor %xmm13, %xmm13, %xmm13
vaesenc %xmm13, %xmm12, %xmm12
vpand %xmm11, %xmm12, %xmm11
vmovdqa %xmm11, (%rsp)
vpxor %xmm4, %xmm11, %xmm4
vpternlogq $0xec, %xmm10, %xmm6, %xmm5
xorl %r9d, %r9d
vmovdqa %xmm5, %xmm11
vpternlogq $0x96, %xmm7, %xmm9, %xmm11
vaesenc %xmm3, %xmm11, %xmm11
vaesenc %xmm1, %xmm11, %xmm11
vaesenc %xmm2, %xmm11, %xmm11
vaesenc %xmm4, %xmm11, %xmm4
vpternlogq $0xec, %xmm10, %xmm6, %xmm4
vpaddb %xmm8, %xmm9, %xmm9
vmovdqa %xmm4, %xmm11
vpternlogq $0x96, %xmm9, %xmm7, %xmm11
vaesenc %xmm3, %xmm11, %xmm11
vaesenc %xmm1, %xmm11, %xmm11
vaesenc %xmm2, %xmm11, %xmm11
vaesenc %xmm5, %xmm11, %xmm5
vpaddb %xmm8, %xmm9, %xmm9
addl $0x2, %r9d
vpternlogq $0xec, %xmm10, %xmm6, %xmm5
cmpl %edx, %r9d
jb 0x2c93
vmovdqa %xmm5, 0x10(%rsp)
testb $0x1, %r14b
je 0x2d34
vmovdqa 0x132a(%rip), %xmm5     # 0x4030
vpshufb %xmm5, %xmm4, %xmm4
vpsrldq $0x8, %xmm4, %xmm6      # xmm6 = xmm4[8,9,10,11,12,13,14,15],zero,zero,zero,zero,zero,zero,zero,zero
vpsrlq $0x4, %xmm4, %xmm4
vpsllq $0x3c, %xmm6, %xmm6
vpor %xmm4, %xmm6, %xmm4
vpshufb %xmm5, %xmm4, %xmm5
vmovdqu 0x10(%rsp,%rcx), %xmm4
vpternlogq $0xec, 0x135c(%rip), %xmm5, %xmm4 # 0x4090
vmovdqu %xmm4, 0x10(%rsp,%rcx)
testl %esi, %esi
je 0x2d5a
subl %r8d, %r14d
cmpl $0x10, %r8d
jne 0x2dc2
movl %r14d, %eax
vmovdqu 0x10(%rsp,%rax), %xmm0
vmovdqa %xmm0, (%rsp)
jmp 0x2de6
cmpl $0xf, %r14d
ja 0x2df2
vmovdqa 0x10(%rsp), %xmm4
movl %r14d, %ecx
subq %rcx, %rax
vpmovzxbq 0x1337(%rip), %xmm5 # 0x40b0
vmovdqu 0x10(%rax), %xmm6
vpternlogq $0xea, %xmm5, %xmm4, %xmm6
vmovdqa %xmm6, (%rsp)
vpxor 0x10(%rdi), %xmm0, %xmm0
vpternlogq $0x96, %xmm1, %xmm6, %xmm0
vaesenc %xmm3, %xmm0, %xmm0
vaesenc %xmm1, %xmm0, %xmm0
vaesenc %xmm2, %xmm0, %xmm0
vpxor %xmm1, %xmm1, %xmm1
vaesenc %xmm1, %xmm0, %xmm0
vmovdqa %xmm0, (%rsp)
vpternlogq $0x78, %xmm5, %xmm0, %xmm4
vmovdqa %xmm4, 0x10(%rsp)
jmp 0x2df2
vpxor %xmm0, %xmm0, %xmm0
vmovdqa %xmm0, (%rsp)
testl %r8d, %r8d
je 0x2de6
movl %r14d, %eax
leaq (%rsp,%rax), %rsi
addq $0x10, %rsi
movl %r8d, %edx
movq %rsp, %rdi
callq 0x10b0
vmovdqa (%rsp), %xmm0
vptest %xmm0, %xmm0
jne 0x2e0b
xorl %ebp, %ebp
testl %r14d, %r14d
je 0x2e10
movl %r14d, %edx
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x10b0
jmp 0x2e10
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x40, %rsp
popq %rbx
popq %r14
popq %rbp
retq