// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq 0x10(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0xb0(%rbp), %rax
movq %rsp, %rcx
movq %rcx, -0xc0(%rbp)
movq %rax, %rcx
addq $0xf, %rcx
andq $-0x10, %rcx
movq %rsp, %rdi
subq %rcx, %rdi
movq %rdi, %rsp
movq %rax, -0xc8(%rbp)
movq -0xb8(%rbp), %rax
movq %rax, %rdx
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x100(%rbp)
movq %rcx, %rsp
movq %rax, -0xd0(%rbp)
movq -0x28(%rbp), %rsi
movq -0xb0(%rbp), %rdx
callq 0x1120
movq -0x100(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0xb8(%rbp), %rdx
callq 0x1120
cmpq $0x0, -0x20(%rbp)
je 0x24cd
cmpq $0x40, -0x30(%rbp)
jae 0x24cd
movl $0xfffffffd, -0x4(%rbp) # imm = 0xFFFFFFFD
movl $0x1, -0xf4(%rbp)
jmp 0x26bf
movq 0x28(%rbp), %rsi
movq 0x30(%rbp), %rdx
shlq $0x3, %rdx
movq 0x18(%rbp), %rcx
movq 0x20(%rbp), %r8
shlq $0x3, %r8
leaq -0xa8(%rbp), %rdi
callq 0x1110
cmpl $0x0, %eax
je 0x250c
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
movl $0x1, -0xf4(%rbp)
jmp 0x26bf
movl $0x1, -0x3c(%rbp)
cmpq $0x20, 0x10(%rbp)
jb 0x2553
movq -0x38(%rbp), %rsi
movl -0x3c(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x3c(%rbp)
movl %eax, %eax
movl %eax, %edx
leaq -0xa8(%rbp), %rdi
callq 0x10d0
movq 0x10(%rbp), %rax
subq $0x20, %rax
movq %rax, 0x10(%rbp)
movq -0x38(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x38(%rbp)
jmp 0x2513
movq -0x38(%rbp), %rsi
movq 0x10(%rbp), %rdx
shlq $0x3, %rdx
movl -0x3c(%rbp), %eax
movl %eax, %ecx
leaq -0xa8(%rbp), %rdi
callq 0x1040
movq -0x18(%rbp), %rax
movq $0x0, (%rax)
cmpq $0x0, -0x20(%rbp)
je 0x25ae
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
leaq -0xa8(%rbp), %rdi
callq 0x10c0
movq -0x30(%rbp), %rax
subq $0x20, %rax
movq %rax, -0x30(%rbp)
movq -0x28(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x28(%rbp)
movl $0x1, -0x3c(%rbp)
movq -0x30(%rbp), %rax
subq $0x20, %rax
cmpq $0x20, %rax
jb 0x261a
movq -0x10(%rbp), %rsi
movq -0x28(%rbp), %rdx
movl -0x3c(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x3c(%rbp)
movl %eax, %eax
movl %eax, %ecx
leaq -0xa8(%rbp), %rdi
callq 0x10e0
movq -0x10(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x10(%rbp)
movq -0x28(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x28(%rbp)
movq -0x30(%rbp), %rax
subq $0x20, %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rcx
addq $0x20, %rcx
movq %rcx, (%rax)
jmp 0x25b5
movq -0x10(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x30(%rbp), %rcx
subq $0x20, %rcx
movl -0x3c(%rbp), %eax
movl %eax, %r8d
leaq -0xa8(%rbp), %rdi
callq 0x1070
movq -0x30(%rbp), %rcx
subq $0x20, %rcx
movq -0x18(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
movq -0x28(%rbp), %rax
movq -0x30(%rbp), %rcx
subq $0x20, %rcx
addq %rcx, %rax
movq %rax, -0x28(%rbp)
leaq -0xf0(%rbp), %rsi
leaq -0xa8(%rbp), %rdi
callq 0x1130
leaq -0xf0(%rbp), %rax
movq -0x28(%rbp), %rcx
vmovdqu (%rcx), %ymm1
vmovdqu (%rax), %ymm0
vpxor %ymm1, %ymm0, %ymm0
vptest %ymm0, %ymm0
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
je 0x26ae
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0xf4(%rbp)
jmp 0x26bf
movl $0x0, -0x4(%rbp)
movl $0x1, -0xf4(%rbp)
movq -0xc0(%rbp), %rax
movq %rax, %rsp
movl -0x4(%rbp), %eax
movq %rbp, %rsp
popq %rbp
vzeroupper
retq
nopw %cs:(%rax,%rax)