// File: /workspace/github_c_makefile_star2_O3/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 %r15
pushq %r14
pushq %rbx
subq $0x48, %rsp
movq %r9, %rbx
movl %ecx, %r14d
vmovdqu (%rdx), %xmm4
cmpl $0x10, %ecx
jb 0x2aed
movl $0x20, %eax
subl %r14d, %eax
leaq 0x15df(%rip), %rcx      # 0x40c0
vmovdqu (%rax,%rcx), %xmm1
vpand 0x10(%rdx), %xmm1, %xmm5
jmp 0x2b03
movl %r14d, %eax
leaq 0x15c9(%rip), %rcx      # 0x40c0
subq %rax, %rcx
vpand 0x10(%rcx), %xmm4, %xmm4
vpxor %xmm5, %xmm5, %xmm5
vmovdqa (%rdi), %xmm1
vmovdqa 0x30(%rdi), %xmm3
vmovdqa 0x60(%rdi), %xmm2
vmovdqa %xmm4, (%rsp)
vmovdqa %xmm5, 0x10(%rsp)
leal (%r8,%r14), %eax
testl %esi, %esi
movl %r14d, %r9d
cmovel %eax, %r9d
movl %r9d, %edx
shrl %edx
vmovdqu (%rsp,%rdx), %xmm6
leaq 0x1586(%rip), %rcx      # 0x40c0
movq %rcx, %r10
subq %rdx, %r10
vmovdqu 0x10(%r10), %xmm8
vmovupd 0x20(%r10), %xmm7
vmovdqa %xmm8, 0x20(%rsp)
testb $0x1, %r9b
je 0x2b9c
vpsrldq $0x8, %xmm7, %xmm5      # xmm5 = xmm7[8,9,10,11,12,13,14,15],zero,zero,zero,zero,zero,zero,zero,zero
vpsrlq $0x4, %xmm7, %xmm7
vpsllq $0x3c, %xmm5, %xmm5
vpor %xmm7, %xmm5, %xmm7
movb $-0x10, 0x20(%rsp,%rdx)
vmovdqa 0x14b8(%rip), %xmm5     # 0x4030
vpshufb %xmm5, %xmm6, %xmm6
vpslldq $0x8, %xmm6, %xmm8      # xmm8 = zero,zero,zero,zero,zero,zero,zero,zero,xmm6[0,1,2,3,4,5,6,7]
vpsllq $0x4, %xmm6, %xmm6
vpsrlq $0x3c, %xmm8, %xmm8
vpor %xmm6, %xmm8, %xmm6
vpshufb %xmm5, %xmm6, %xmm6
vmovdqa 0x20(%rsp), %xmm8
vmovdqa 0x10(%rdi), %xmm5
vmovdqa 0x20(%rdi), %xmm9
vpternlogq $0x96, %xmm0, %xmm5, %xmm9
movl $0x8, %edi
cmpl $0xf, %r9d
ja 0x2bd6
vpxor %xmm1, %xmm9, %xmm9
movl $0xa, %edi
cmpl $0x2, %r9d
ja 0x2bd6
setne %dil
movzbl %dil, %edi
leal 0x10(,%rdi,8), %edi
testl %esi, %esi
je 0x2c36
leal -0x1(%rdi), %r10d
vpxor %xmm12, %xmm12, %xmm12
vpinsrb $0xf, %r10d, %xmm12, %xmm11
vmovdqa 0x147f(%rip), %xmm10    # 0x4070
cmpl $0xf, %r14d
ja 0x2c4c
vpmovzxbq 0x14b0(%rip), %xmm13 # 0x40b0
vmovdqa %xmm0, %xmm14
vpternlogq $0x1e, %xmm13, %xmm4, %xmm14
vpternlogq $0x96, %xmm5, %xmm1, %xmm14
vaesenc %xmm3, %xmm14, %xmm14
vaesenc %xmm1, %xmm14, %xmm14
vaesenc %xmm2, %xmm14, %xmm14
vaesenc %xmm12, %xmm14, %xmm12
vpternlogq $0x6c, %xmm13, %xmm4, %xmm12
movb $0x1, %r10b
vmovdqa %xmm12, %xmm4
jmp 0x2c4f
cmpl $0x10, %eax
setb %r10b
vmovdqa 0x143b(%rip), %xmm10    # 0x4080
vpxor %xmm11, %xmm11, %xmm11
jmp 0x2c4f
xorl %r10d, %r10d
vpternlogq $0xec, %xmm8, %xmm7, %xmm6
xorl %r11d, %r11d
vmovdqa %xmm6, %xmm12
vpternlogq $0x96, %xmm9, %xmm11, %xmm12
vaesenc %xmm3, %xmm12, %xmm12
vaesenc %xmm1, %xmm12, %xmm12
vaesenc %xmm2, %xmm12, %xmm12
vaesenc %xmm4, %xmm12, %xmm4
vpternlogq $0xec, %xmm8, %xmm7, %xmm4
vpaddb %xmm11, %xmm10, %xmm11
vmovdqa %xmm4, %xmm12
vpternlogq $0x96, %xmm11, %xmm9, %xmm12
vaesenc %xmm3, %xmm12, %xmm12
vaesenc %xmm1, %xmm12, %xmm12
vaesenc %xmm2, %xmm12, %xmm12
vaesenc %xmm6, %xmm12, %xmm6
vpaddb %xmm10, %xmm11, %xmm11
addl $0x2, %r11d
vpternlogq $0xec, %xmm8, %xmm7, %xmm6
cmpl %edi, %r11d
jb 0x2c59
vmovdqa %xmm6, (%rsp)
testb $0x1, %r9b
je 0x2cf8
vmovdqa 0x1365(%rip), %xmm6     # 0x4030
vpshufb %xmm6, %xmm4, %xmm4
vpsrldq $0x8, %xmm4, %xmm7      # xmm7 = xmm4[8,9,10,11,12,13,14,15],zero,zero,zero,zero,zero,zero,zero,zero
vpsrlq $0x4, %xmm4, %xmm4
vpsllq $0x3c, %xmm7, %xmm7
vpor %xmm4, %xmm7, %xmm4
vpshufb %xmm6, %xmm4, %xmm6
vmovdqu (%rsp,%rdx), %xmm4
vpternlogq $0xec, 0x1398(%rip), %xmm6, %xmm4 # 0x4090
vmovdqu %xmm4, (%rsp,%rdx)
testl %esi, %esi
je 0x2d14
subl %r8d, %r14d
cmpl $0x10, %r8d
jne 0x2d68
movl %r14d, %eax
vmovdqu (%rsp,%rax), %xmm0
jmp 0x2d93
testb %r10b, %r10b
je 0x2d63
vmovdqa (%rsp), %xmm4
movl %eax, %edx
subq %rdx, %rcx
vpand 0x10(%rcx), %xmm4, %xmm6
vpmovzxbq 0x137f(%rip), %xmm7 # 0x40b0
vpternlogq $0x1e, %xmm7, %xmm6, %xmm0
vpternlogq $0x96, %xmm5, %xmm1, %xmm0
vaesenc %xmm3, %xmm0, %xmm0
vaesenc %xmm1, %xmm0, %xmm0
vaesenc %xmm2, %xmm0, %xmm0
vpxor %xmm1, %xmm1, %xmm1
vaesenc %xmm1, %xmm0, %xmm0
vpternlogq $0x6c, %xmm7, %xmm4, %xmm0
vmovdqa %xmm0, (%rsp)
movl %eax, %r14d
jmp 0x2d9a
vpxor %xmm0, %xmm0, %xmm0
vmovdqa %xmm0, 0x30(%rsp)
testl %r8d, %r8d
je 0x2d93
movl %r14d, %eax
leaq (%rsp,%rax), %rsi
movl %r8d, %edx
leaq 0x30(%rsp), %r15
movq %r15, %rdi
callq 0x10b0
vmovdqa (%r15), %xmm0
vptest %xmm0, %xmm0
jne 0x2db1
xorl %ebp, %ebp
testl %r14d, %r14d
je 0x2db6
movl %r14d, %edx
movq %rsp, %rsi
movq %rbx, %rdi
callq 0x10b0
jmp 0x2db6
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq