// File: /workspace/github_c_makefile_star2_O1/mjosaarinen[P]brutus/crypto_aead/pi32cipher256v2/ref2/pi-cipher.c
// Function: pi32_decrypt_simple
int PI_DECRYPT_SIMPLE(
        void *msg,
        size_t *msg_len_B,
		void *nonce_secret,
		const void *cipher,
        size_t cipher_len_B,
        const void *ad,
        size_t ad_len_B,
        const void *nonce_public,
        size_t nonce_public_len_B,
        const void *key,
        size_t key_len_B
        )
{
    unsigned i;
    PI_CTX ctx;

    unsigned long clen = cipher_len_B, alen = ad_len_B;
    uint8_t bck_c[clen], bck_ad[alen];
    memcpy(bck_c, cipher, clen);
    memcpy(bck_ad, ad, alen);

    uint8_t tmp_tag[PI_TAG_BYTES];
    if (nonce_secret && (cipher_len_B < PI_CT_BLOCK_LENGTH_BYTES + PI_TAG_BYTES)) {
    	return -3;
    }
    if (PI_INIT(&ctx, key, key_len_B * 8, nonce_public, nonce_public_len_B * 8)) {
        printf("ERROR! <%s %s %d>\n", __FILE__, __func__, __LINE__);
        return -2;
    }
    i = 1;
    while (ad_len_B >= PI_AD_BLOCK_LENGTH_BYTES) {
        PI_PROCESS_AD_BLOCK(&ctx, ad, i++);
        ad_len_B -= PI_AD_BLOCK_LENGTH_BYTES;
        ad = &((const uint8_t*)ad)[PI_AD_BLOCK_LENGTH_BYTES];
    }
    PI_PROCESS_AD_LAST_BLOCK(&ctx, ad, ad_len_B * 8, i);
    *msg_len_B = 0;
    if (nonce_secret) {
        PI_DECRYPT_SMN(&ctx, nonce_secret, cipher);
        cipher_len_B -= PI_CT_BLOCK_LENGTH_BYTES;
	cipher = &((uint8_t*)cipher)[PI_CT_BLOCK_LENGTH_BYTES];
    }
    i = 1;
    while (cipher_len_B - PI_TAG_BYTES >= PI_PT_BLOCK_LENGTH_BYTES) {
        PI_DECRYPT_BLOCK(&ctx, msg, cipher, i++);
        msg = &((uint8_t*)msg)[PI_PT_BLOCK_LENGTH_BYTES];
        cipher = &((uint8_t*)cipher)[PI_CT_BLOCK_LENGTH_BYTES];
        cipher_len_B -= PI_CT_BLOCK_LENGTH_BYTES;
        *msg_len_B += PI_PT_BLOCK_LENGTH_BYTES;
    }
    PI_DECRYPT_LAST_BLOCK(&ctx, msg, cipher, cipher_len_B - PI_TAG_BYTES, i);
    *msg_len_B += cipher_len_B - PI_TAG_BYTES;
    cipher = &((uint8_t*)cipher)[cipher_len_B - PI_TAG_BYTES];
    PI_EXTRACT_TAG(&ctx, tmp_tag);
    if (memcmp(tmp_tag, cipher, PI_TAG_BYTES)) {
#if DEBUG
    	printf("DBG: verification failed: clen = %lu; alen = %lu\n", clen, alen);
    	printf("Key:\n");
    	hexdump_block(key, key_len_B, 4, 16);
    	printf("\nNonce:\n");
    	hexdump_block(nonce_public, nonce_public_len_B, 4, 16);
    	printf("\nAD:\n");
    	hexdump_block(bck_ad, alen, 4, 16);
    	printf("\nCiphertext:\n");
    	hexdump_block(bck_c, clen, 4, 16);
    	printf("\nShould-Tag:\n");
		hexdump_block(cipher, PI_TAG_BYTES, 4, 16);
		printf("\nIS-Tag:\n");
		hexdump_block(tmp_tag, PI_TAG_BYTES, 4, 16);
		puts("");
#endif /* DEBUG */
    	return -1;
    }
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rcx, %r14
movq %rdx, %r13
movq %rsi, (%rsp)
movq %rsp, %r15
testq %rdx, %rdx
setne %cl
cmpq $0x40, %r8
setb %dl
movl $0xfffffffd, %eax       # imm = 0xFFFFFFFD
testb %dl, %cl
jne 0x1ed0
movq %r9, %rbp
movq %r8, %rbx
movq %rdi, %r12
movq 0x100(%rsp), %rdx
movq 0xf8(%rsp), %rsi
movq 0xf0(%rsp), %r8
movq 0xe8(%rsp), %rcx
shlq $0x3, %rdx
shlq $0x3, %r8
leaq 0x18(%rsp), %rdi
callq 0x1100
movl %eax, %ecx
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
testl %ecx, %ecx
jne 0x1ed0
movq %r13, 0x8(%rsp)
movq 0xe0(%rsp), %r13
movl $0x1, %ecx
cmpq $0x20, %r13
movq %r15, 0x10(%rsp)
jb 0x1dcd
movl $0x1, %r15d
movl %r15d, %edx
incl %r15d
leaq 0x18(%rsp), %rdi
movq %rbp, %rsi
callq 0x10c0
addq $-0x20, %r13
addq $0x20, %rbp
cmpq $0x1f, %r13
ja 0x1da9
movl %r15d, %ecx
shlq $0x3, %r13
leaq 0x18(%rsp), %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x1030
movq (%rsp), %r15
movq $0x0, (%r15)
movq 0x8(%rsp), %rsi
testq %rsi, %rsi
je 0x1e0b
leaq 0x18(%rsp), %rdi
movq %r14, %rdx
callq 0x10b0
addq $-0x20, %rbx
addq $0x20, %r14
addq $-0x20, %rbx
movl $0x1, %r8d
cmpq $0x20, %rbx
jb 0x1e50
movl $0x1, %ebp
leaq 0x18(%rsp), %r13
movl %ebp, %ecx
incl %ebp
movq %r13, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x10d0
addq $0x20, %r12
addq $0x20, %r14
addq $0x20, (%r15)
addq $-0x20, %rbx
cmpq $0x1f, %rbx
ja 0x1e25
movl %ebp, %r8d
leaq 0x18(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x1060
movq (%rsp), %rax
addq %rbx, (%rax)
vmovdqu 0x40(%r15), %ymm0
vpsrld $0x8, %ymm0, %ymm1
vpsrld $0x10, %ymm0, %ymm2
vpsrld $0x18, %ymm0, %ymm3
vpmovdb %ymm0, %xmm0
vpmovdb %ymm1, %xmm1
vpunpcklbw %xmm1, %xmm0, %xmm0 # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1],xmm0[2],xmm1[2],xmm0[3],xmm1[3],xmm0[4],xmm1[4],xmm0[5],xmm1[5],xmm0[6],xmm1[6],xmm0[7],xmm1[7]
vpmovdb %ymm3, %xmm1
vpmovdb %ymm2, %xmm2
vpunpcklbw %xmm1, %xmm2, %xmm1 # xmm1 = xmm2[0],xmm1[0],xmm2[1],xmm1[1],xmm2[2],xmm1[2],xmm2[3],xmm1[3],xmm2[4],xmm1[4],xmm2[5],xmm1[5],xmm2[6],xmm1[6],xmm2[7],xmm1[7]
vpunpcklwd %xmm1, %xmm0, %xmm2 # xmm2 = xmm0[0],xmm1[0],xmm0[1],xmm1[1],xmm0[2],xmm1[2],xmm0[3],xmm1[3]
vpunpckhwd %xmm1, %xmm0, %xmm0 # xmm0 = xmm0[4],xmm1[4],xmm0[5],xmm1[5],xmm0[6],xmm1[6],xmm0[7],xmm1[7]
vinserti128 $0x1, %xmm0, %ymm2, %ymm0
vmovdqu %ymm0, 0x80(%rsp)
vpxor (%r14,%rbx), %ymm0, %ymm0
xorl %eax, %eax
vptest %ymm0, %ymm0
setne %al
negl %eax
movq 0x10(%rsp), %r15
movq %r15, %rsp
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
vzeroupper
retq