// File: /workspace/github_c_makefile_star2_O2/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:
vmovdqu (%rdx), %xmm4
cmpl $0x10, %ecx
jb 0x2a0f
movl $0x20, %eax
subl %ecx, %eax
leaq 0x168e(%rip), %r10      # 0x4090
vmovdqu (%rax,%r10), %xmm1
vpand 0x10(%rdx), %xmm1, %xmm5
jmp 0x2a24
movl %ecx, %eax
leaq 0x1678(%rip), %rdx      # 0x4090
subq %rax, %rdx
vpand 0x10(%rdx), %xmm4, %xmm4
vpxor %xmm5, %xmm5, %xmm5
pushq %rbp
pushq %rbx
vmovdqa (%rdi), %xmm1
vmovdqa 0x30(%rdi), %xmm3
vmovdqa 0x60(%rdi), %xmm2
vmovdqa %xmm4, -0x48(%rsp)
vmovdqa %xmm5, -0x38(%rsp)
leal (%r8,%rcx), %eax
testl %esi, %esi
movl %ecx, %r11d
cmovel %eax, %r11d
movl %r11d, %r10d
shrl %r10d
vmovdqu -0x48(%rsp,%r10), %xmm6
leaq 0x162f(%rip), %rdx      # 0x4090
movq %rdx, %rbx
subq %r10, %rbx
vmovdqu 0x10(%rbx), %xmm8
vmovupd 0x20(%rbx), %xmm7
vmovdqa %xmm8, -0x28(%rsp)
testb $0x1, %r11b
je 0x2ac2
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, -0x28(%rsp,%r10)
vmovdqa 0x1562(%rip), %xmm5     # 0x4000
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 -0x28(%rsp), %xmm8
vmovdqa 0x10(%rdi), %xmm5
vmovdqa 0x20(%rdi), %xmm9
vpternlogq $0x96, %xmm0, %xmm5, %xmm9
cmpl $0xf, %r11d
jbe 0x2ae0
movl $0x8, %edi
jmp 0x2b00
vpxor %xmm1, %xmm9, %xmm9
cmpl $0x2, %r11d
jbe 0x2af1
movl $0xa, %edi
jmp 0x2b00
setne %dil
movzbl %dil, %edi
leal 0x10(,%rdi,8), %edi
testl %esi, %esi
je 0x2b5d
leal -0x1(%rdi), %ebx
vpxor %xmm12, %xmm12, %xmm12
vpinsrb $0xf, %ebx, %xmm12, %xmm11
vmovdqa 0x1526(%rip), %xmm10    # 0x4040
cmpl $0xf, %ecx
ja 0x2b72
vpmovzxbq 0x1558(%rip), %xmm13 # 0x4080
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, %bl
vmovdqa %xmm12, %xmm4
jmp 0x2b74
cmpl $0x10, %eax
setb %bl
vmovdqa 0x14e5(%rip), %xmm10    # 0x4050
vpxor %xmm11, %xmm11, %xmm11
jmp 0x2b74
xorl %ebx, %ebx
xorl %ebp, %ebp
vpternlogq $0xec, %xmm8, %xmm7, %xmm6
cmpl %edi, %ebp
jae 0x2bd5
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, %ebp
jmp 0x2b76
vmovdqa %xmm6, -0x48(%rsp)
testb $0x1, %r11b
je 0x2c18
vmovdqa 0x1417(%rip), %xmm6     # 0x4000
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 -0x48(%rsp,%r10), %xmm4
vpternlogq $0xec, 0x1448(%rip), %xmm6, %xmm4 # 0x4060
vmovdqu %xmm4, -0x48(%rsp,%r10)
testl %esi, %esi
je 0x2c4c
subl %r8d, %ecx
cmpl $0x10, %r8d
jne 0x2cb7
movl %ecx, %eax
addq %rsp, %rax
addq $-0x48, %rax
vmovdqu (%rax), %xmm0
vptest %xmm0, %xmm0
je 0x2c9e
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
popq %rbx
popq %rbp
retq
testb %bl, %bl
je 0x2c9c
vmovdqa -0x48(%rsp), %xmm4
movl %eax, %ecx
subq %rcx, %rdx
vpand 0x10(%rdx), %xmm4, %xmm6
vpmovzxbq 0x1417(%rip), %xmm7 # 0x4080
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, -0x48(%rsp)
movl %eax, %ecx
movl %ecx, %ecx
xorl %eax, %eax
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x2c49
movb -0x48(%rsp,%rdx), %sil
movb %sil, (%r9,%rdx)
incq %rdx
jmp 0x2ca4
vpxor %xmm0, %xmm0, %xmm0
leaq -0x18(%rsp), %rax
vmovdqa %xmm0, (%rax)
movl %ecx, %edx
addq %rsp, %rdx
addq $-0x48, %rdx
movl %r8d, %esi
xorl %edi, %edi
cmpq %rdi, %rsi
je 0x2c39
movb (%rdx,%rdi), %r8b
movb %r8b, -0x18(%rsp,%rdi)
incq %rdi
jmp 0x2cd2