// File: /workspace/github_c_makefile_star2_O3/mjosaarinen[P]brutus/crypto_aead/lunarkeyakv2/Haswell/KeccakP-1600-AVX2.cpp
// Function: KeccakP1600_AddBytes
void KeccakP1600_AddBytes(void *state, const UINT8 *data, size_t offset, size_t length)
//*****************************************************************************
{
    keccak_state_t  &s = *(keccak_state_t *)state;
    UINT64      *d = (UINT64 *)data;
    UINT8       *t1, *d1;
    UINT64      t[25];
    UINT64      *t0;
    ptrdiff_t   lane_n = length / sizeof(UINT64);
    ptrdiff_t   byte_n = length % sizeof(UINT64);
    ptrdiff_t   i;

    KeccakP1600_ExtractBytes(state, (UINT8 *)t, 0, sizeof(t));

/*
    // "trailingBits + 256" is passed as offset to do "state ^ trailingBits".
    if (offset >= 256)
    {
        if (length < sizeof(t))
            ((UINT8 *)t)[length] ^= (UINT8)offset;
        offset = 0;
    }
*/

    t0 = (UINT64 *)((UINT8 *)t + offset);

    for (i = 0; i < lane_n; i++)
        t0[i] ^= d[i];

    if (byte_n)
    {
        t1 = (UINT8 *)(t0 + i);
        d1 = (UINT8 *)(d + i);

        for (i = 0; i < byte_n; i++)
            t1[i] ^= d1[i];
    }

    s.a0 = LOAD(t + 0*5);
    s.a1 = LOAD(t + 1*5);
    s.a2 = LOAD(t + 2*5);
    s.a3 = LOAD(t + 3*5);
    s.a4 = LOAD(t + 4*5);
    s.c4 = SET(t[0*5 + 4], t[1*5 + 4], t[2*5 + 4], t[3*5 + 4]);
    s.a44 = _mm256_set1_epi64x(t[4*5 + 4]);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rcx, %r15
movq %rdx, %r12
movq %rsi, %r14
movq %rdi, %rbx
movl %r15d, %r13d
andl $0x7, %r13d
xorl %ebp, %ebp
movq %rsp, %rsi
movl $0xc8, %ecx
xorl %edx, %edx
callq 0x5060
leaq (%rsp,%r12), %rax
cmpq $0x8, %r15
jb 0x586e
shrq $0x3, %r15
leaq 0x3(%r15), %rcx
andq $-0x4, %rcx
leaq -0x1(%r15), %rdx
vpbroadcastq %rdx, %ymm0
xorl %edx, %edx
vpmovsxbq 0x1c9c8(%rip), %ymm1 # 0x22200
vpbroadcastq %rdx, %ymm2
vpor %ymm1, %ymm2, %ymm2
vpcmpleuq %ymm0, %ymm2, %k1
vmovdqu64 (%r14,%rdx,8), %ymm2 {%k1} {z}
vmovdqu64 (%rax,%rdx,8), %ymm3 {%k1} {z}
vpxor %ymm2, %ymm3, %ymm2
vmovdqu64 %ymm2, (%rax,%rdx,8) {%k1}
addq $0x4, %rdx
cmpq %rdx, %rcx
jne 0x5838
movq %r15, %rbp
testq %r13, %r13
je 0x58d9
decq %r13
vpbroadcastq %r13, %ymm0
vpcmpnltuq 0x1c779(%rip), %ymm0, %k0 # 0x22000
vpcmpnltuq 0x1c78e(%rip), %ymm0, %k1 # 0x22020
kshiftlb $0x4, %k1, %k1
vpcmpnltuq 0x1c79d(%rip), %ymm0, %k2 # 0x22040
vpcmpnltuq 0x1c7b2(%rip), %ymm0, %k3 # 0x22060
korb %k1, %k0, %k0
kshiftlb $0x4, %k3, %k1
korb %k1, %k2, %k1
kunpckbw %k0, %k1, %k1
vmovdqu8 (%r14,%rbp,8), %xmm0 {%k1} {z}
vmovdqu8 (%rax,%rbp,8), %xmm1 {%k1} {z}
vpxor %xmm0, %xmm1, %xmm0
vmovdqu8 %xmm0, (%rax,%rbp,8) {%k1}
vmovups (%rsp), %ymm0
vmovaps %ymm0, (%rbx)
vmovups 0x28(%rsp), %ymm0
vmovaps %ymm0, 0x20(%rbx)
vmovups 0x50(%rsp), %ymm0
vmovaps %ymm0, 0x40(%rbx)
vmovups 0x78(%rsp), %ymm0
vmovaps %ymm0, 0x60(%rbx)
vmovups 0xa0(%rsp), %ymm0
vmovaps %ymm0, 0x80(%rbx)
vmovdqa 0x20(%rsp), %xmm0
vpinsrq $0x1, 0x48(%rsp), %xmm0, %xmm0
vpbroadcastq 0x70(%rsp), %ymm1
vpblendd $0x30, %ymm1, %ymm0, %ymm0 # ymm0 = ymm0[0,1,2,3],ymm1[4,5],ymm0[6,7]
vpbroadcastq 0x98(%rsp), %ymm1
vpblendd $0xc0, %ymm1, %ymm0, %ymm0 # ymm0 = ymm0[0,1,2,3,4,5],ymm1[6,7]
vmovdqa %ymm0, 0xa0(%rbx)
vbroadcastsd 0xc0(%rsp), %ymm0
vmovaps %ymm0, 0xc0(%rbx)
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
vzeroupper
retq