// File: /workspace/github_c_makefile_star2_O1/mjosaarinen[P]brutus/crypto_aead/pi32cipher256v2/ref2/pi-cipher.c
// Function: pi32_init
int PI_INIT(
        PI_CTX *ctx,
        const void *key,
        size_t key_length_b,
        const void *pmn,
        size_t pmn_length_b)
{
	int i;
	uint8_t setup_buf[PI_IS_BITS / 8];
	if ((key_length_b % 8 != 0) || (pmn_length_b % 8 != 0)) {
		return -1;
	}
    if (key_length_b / 8 + pmn_length_b / 8 + 1 >= PI_IS_BITS / 8) {
        return -1;
    }
    memset(ctx->tag, 0, sizeof(ctx->tag));
    memset(setup_buf, 0, sizeof(setup_buf));
    memcpy(setup_buf, key, key_length_b / 8);
    memcpy(&setup_buf[key_length_b / 8], pmn, pmn_length_b / 8);
    setup_buf[key_length_b / 8 + pmn_length_b / 8] = 1;
    for (i = 0; i < 16; ++i) {
        ctx->cis[i / 4][i % 4] = load_word_little(&setup_buf[i * PI_WORD_SIZE / 8]);
    }
    pi((word_t*)ctx->cis);
    ctx->ctr = 0;
    for (i = 0; i * PI_WORD_SIZE < 64; ++i) {
		ctx->ctr |= (uint64_t)ctx->cis[1][i] << (i * PI_WORD_SIZE);
	}
    return 0;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x40, %rsp
movq %r8, %r14
movq %rcx, %r15
movq %rdx, %r12
movl %r14d, %ecx
orl %r12d, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testb $0x7, %cl
jne 0x1462
shrq $0x3, %r12
shrq $0x3, %r14
leaq (%r14,%r12), %r13
cmpq $0x3e, %r13
ja 0x1462
movq %rdi, %rbx
vxorps %xmm0, %xmm0, %xmm0
vmovups %ymm0, 0x40(%rdi)
movq %rsp, %rdi
vmovups %ymm0, 0x20(%rdi)
vmovups %ymm0, (%rdi)
movq %r12, %rdx
vzeroupper
callq 0x1110
leaq (%rsp,%r12), %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x1110
movb $0x1, (%rsp,%r13)
vpmovsxbq 0x1d3c(%rip), %ymm0 # 0x3070
vpmovsxbq 0x1d37(%rip), %ymm1 # 0x3074
vpbroadcastq 0x1cfa(%rip), %ymm2 # 0x3040
xorl %eax, %eax
vpbroadcastq 0x1cf7(%rip), %ymm3 # 0x3048
vpbroadcastq %rbx, %ymm4
vpbroadcastq 0x1cf0(%rip), %ymm5 # 0x3050
vmovdqu (%rsp,%rax,4), %ymm6
vpmovdb %ymm6, %xmm7
vpsrld $0x8, %ymm6, %ymm8
vpmovdb %ymm8, %xmm8
vpsrld $0x10, %ymm6, %ymm9
vpmovdb %ymm9, %xmm9
vpsrld $0x18, %ymm6, %ymm6
vpmovdb %ymm6, %xmm6
vpmovzxbd %xmm7, %ymm7    # ymm7 = xmm7[0],zero,zero,zero,xmm7[1],zero,zero,zero,xmm7[2],zero,zero,zero,xmm7[3],zero,zero,zero,xmm7[4],zero,zero,zero,xmm7[5],zero,zero,zero,xmm7[6],zero,zero,zero,xmm7[7],zero,zero,zero
vpmovzxbd %xmm8, %ymm8    # ymm8 = xmm8[0],zero,zero,zero,xmm8[1],zero,zero,zero,xmm8[2],zero,zero,zero,xmm8[3],zero,zero,zero,xmm8[4],zero,zero,zero,xmm8[5],zero,zero,zero,xmm8[6],zero,zero,zero,xmm8[7],zero,zero,zero
vpslld $0x8, %ymm8, %ymm8
vpor %ymm7, %ymm8, %ymm7
vpmovzxbd %xmm9, %ymm8    # ymm8 = xmm9[0],zero,zero,zero,xmm9[1],zero,zero,zero,xmm9[2],zero,zero,zero,xmm9[3],zero,zero,zero,xmm9[4],zero,zero,zero,xmm9[5],zero,zero,zero,xmm9[6],zero,zero,zero,xmm9[7],zero,zero,zero
vpslld $0x10, %ymm8, %ymm8
vpmovzxbd %xmm6, %ymm6    # ymm6 = xmm6[0],zero,zero,zero,xmm6[1],zero,zero,zero,xmm6[2],zero,zero,zero,xmm6[3],zero,zero,zero,xmm6[4],zero,zero,zero,xmm6[5],zero,zero,zero,xmm6[6],zero,zero,zero,xmm6[7],zero,zero,zero
vpslld $0x18, %ymm6, %ymm6
vpternlogd $0xfe, %ymm8, %ymm7, %ymm6
vpsrlq $0x2, %ymm0, %ymm7
vpsrlq $0x2, %ymm1, %ymm8
vpand %ymm2, %ymm8, %ymm8
vpand %ymm2, %ymm7, %ymm7
vpand %ymm3, %ymm1, %ymm9
vpand %ymm3, %ymm0, %ymm10
vpsllq $0x4, %ymm7, %ymm7
vpaddq %ymm7, %ymm4, %ymm7
vpsllq $0x4, %ymm8, %ymm8
vpaddq %ymm4, %ymm8, %ymm8
vpsllq $0x2, %ymm9, %ymm9
vpaddq %ymm9, %ymm8, %ymm8
kxnorw %k0, %k0, %k1
vpscatterqd %xmm6, (,%ymm8) {%k1}
vpsllq $0x2, %ymm10, %ymm8
vpaddq %ymm7, %ymm8, %ymm7
vextracti128 $0x1, %ymm6, %xmm6
kxnorw %k0, %k0, %k1
vpscatterqd %xmm6, (,%ymm7) {%k1}
addq $0x8, %rax
vpaddq %ymm5, %ymm1, %ymm1
vpaddq %ymm5, %ymm0, %ymm0
cmpq $0x10, %rax
jne 0x1360
movq %rbx, %rdi
vzeroupper
callq 0x1470
vpmovzxdq 0x10(%rbx), %xmm0
vpsllvq 0x1c0e(%rip), %xmm0, %xmm0 # 0x3060
vpshufd $0xee, %xmm0, %xmm1     # xmm1 = xmm0[2,3,2,3]
vpor %xmm1, %xmm0, %xmm0
vmovq %xmm0, 0x60(%rbx)
xorl %eax, %eax
addq $0x40, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq