// File: /workspace/github_c_makefile_star2_O2/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 $0xb8, %rsp
movq %rcx, %r15
movq %rsi, 0x8(%rsp)
movq %rsp, %r14
testq %rdx, %rdx
setne %al
cmpq $0x40, %r8
setb %cl
testb %cl, %al
je 0x1b40
pushq $-0x3
jmp 0x1b84
movq %r9, %rbp
movq %r8, %rbx
movq %rdx, %r13
movq %rdi, %r12
movq 0x110(%rsp), %rdx
movq 0x108(%rsp), %rsi
movq 0x100(%rsp), %r8
movq 0xf8(%rsp), %rcx
shlq $0x3, %rdx
shlq $0x3, %r8
leaq 0x28(%rsp), %rdi
callq 0x1100
testl %eax, %eax
je 0x1b9d
pushq $-0x2
popq %rax
movq %r14, %rsp
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
vzeroupper
retq
movq %r13, 0x20(%rsp)
movq %r15, 0x10(%rsp)
movq %r14, 0x18(%rsp)
movq 0xf0(%rsp), %r15
leaq (,%r15,8), %r13
pushq $0x1
popq %r14
movl %r14d, %ecx
cmpq $0x20, %r15
jb 0x1bee
leal 0x1(%rcx), %r14d
leaq 0x28(%rsp), %rdi
movq %rbp, %rsi
movq %rcx, %rdx
callq 0x10c0
addq $-0x20, %r15
addq $0x20, %rbp
addq $-0x100, %r13
jmp 0x1bc0
leaq 0x28(%rsp), %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x1030
movq 0x8(%rsp), %rax
andq $0x0, (%rax)
movq 0x20(%rsp), %rsi
testq %rsi, %rsi
je 0x1c32
leaq 0x28(%rsp), %rdi
movq 0x10(%rsp), %r15
movq %r15, %rdx
callq 0x10b0
addq $-0x20, %rbx
addq $0x20, %r15
movq 0x18(%rsp), %r14
jmp 0x1c3c
movq 0x18(%rsp), %r14
movq 0x10(%rsp), %r15
addq $-0x20, %rbx
pushq $0x1
popq %rbp
leaq 0x28(%rsp), %r13
movl %ebp, %r8d
cmpq $0x20, %rbx
jb 0x1c7b
incl %ebp
movq %r13, %rdi
movq %r12, %rsi
movq %r15, %rdx
movq %r8, %rcx
callq 0x10d0
addq $0x20, %r12
addq $0x20, %r15
movq 0x8(%rsp), %rax
addq $0x20, (%rax)
addq $-0x20, %rbx
jmp 0x1c48
movq %r15, %r13
leaq 0x28(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x1060
movq 0x8(%rsp), %rax
addq %rbx, (%rax)
leaq 0x90(%rsp), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x1120
vmovdqu (%r12), %ymm0
vpxor (%r13,%rbx), %ymm0, %ymm0
xorl %eax, %eax
vptest %ymm0, %ymm0
setne %al
negl %eax
jmp 0x1b85