// File: /workspace/github_c_makefile_star2_O1/mjosaarinen[P]brutus/crypto_aead/acorn128v2/ref/encrypt.c
// Function: crypto_aead_decrypt
int crypto_aead_decrypt(
	unsigned char *m,unsigned long long *mlen,
	unsigned char *nsec,
	const unsigned char *c,unsigned long long clen,
	const unsigned char *ad,unsigned long long adlen,
	const unsigned char *npub,
	const unsigned char *k
	)
{
    unsigned long long i;
    unsigned char plaintextbyte, ciphertextbyte, ksbyte;
    unsigned char state[293];
    unsigned char tag[16];
    unsigned char check = 0;
    unsigned char ca, cb;

    if (clen < 16) return -1;

    //initialization stage
    acorn128_initialization(k, npub, state);

    //process the associated data
    for (i = 0; i < adlen; i++)
    {
        acorn128_enc_onebyte(state, ad[i], &ciphertextbyte, &ksbyte, 0xff, 0xff);
    }

    for (i = 0; i < 256/8; i++)
    {
        if ( i == 0 ) plaintextbyte = 0x1;
        else plaintextbyte = 0;

        if ( i < 128/8)   ca = 0xff;
        else ca = 0;

        cb = 0xff;

        acorn128_enc_onebyte(state, plaintextbyte, &ciphertextbyte, &ksbyte, ca, cb);
    }

    //process the ciphertext
    *mlen = clen - 16;

    for (i = 0; i < *mlen; i++)
    {
        acorn128_dec_onebyte(state, &m[i], c[i], &ksbyte, 0xff, 0);
    }

    for (i = 0; i < 256/8; i++)
    {
        if ( i == 0 ) plaintextbyte = 0x1;
        else plaintextbyte = 0;

        if ( i < 128/8)   ca = 0xff;
        else ca = 0;

        cb = 0;

        acorn128_enc_onebyte(state, plaintextbyte, &ciphertextbyte, &ksbyte, ca, cb);
    }

    //finalization stage, we assume that the tag length is a multiple of bytes
    acorn128_tag_generation(*mlen, adlen, 16, tag, state);

    for (i = 0; i  < 16; i++) check |= (tag[i] ^ c[clen - 16 + i]);
    if (check == 0) return 0;
    else return -1;
}

// Assembly:
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq $0x10, %r8
jb 0x1920
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movq %r9, %r13
movq %rdi, 0x10(%rsp)
movq %rsi, 0x18(%rsp)
movq %rcx, 0x20(%rsp)
movq %r8, 0x28(%rsp)
movq 0x1b0(%rsp), %rdi
movq 0x1a8(%rsp), %rsi
movq 0x1a0(%rsp), %r12
leaq 0x40(%rsp), %rdx
callq 0x10d0
testq %r12, %r12
je 0x1811
xorl %r15d, %r15d
leaq 0x40(%rsp), %rbx
leaq 0xf(%rsp), %rbp
leaq 0xe(%rsp), %r14
movzbl (%r13,%r15), %esi
movq %rbx, %rdi
movq %rbp, %rdx
movq %r14, %rcx
movl $0xff, %r8d
movl $0xff, %r9d
callq 0x10e0
incq %r15
cmpq %r15, %r12
jne 0x17e9
xorl %r14d, %r14d
leaq 0x40(%rsp), %r13
leaq 0xf(%rsp), %rbp
leaq 0xe(%rsp), %rbx
xorl %esi, %esi
testq %r14, %r14
sete %sil
cmpq $0x10, %r14
movl $0x0, %eax
sbbl %eax, %eax
movzbl %al, %r8d
movq %r13, %rdi
movq %rbp, %rdx
movq %rbx, %rcx
movl $0xff, %r9d
callq 0x10e0
incq %r14
cmpq $0x20, %r14
jne 0x1823
movq 0x28(%rsp), %r13
addq $-0x10, %r13
movq 0x18(%rsp), %r15
movq %r13, (%r15)
movq 0x20(%rsp), %rbp
movq 0x10(%rsp), %r12
je 0x18a0
xorl %r14d, %r14d
leaq 0x40(%rsp), %rbx
leaq (%r12,%r14), %rsi
movzbl (%rbp,%r14), %edx
movq %rbx, %rdi
movl $0xff, %r8d
xorl %r9d, %r9d
callq 0x10c0
incq %r14
cmpq (%r15), %r14
jb 0x187d
xorl %r14d, %r14d
leaq 0x40(%rsp), %r15
leaq 0xf(%rsp), %rbx
leaq 0xe(%rsp), %r12
xorl %esi, %esi
testq %r14, %r14
sete %sil
cmpq $0x10, %r14
movl $0x0, %eax
sbbl %eax, %eax
movzbl %al, %r8d
movq %r15, %rdi
movq %rbx, %rdx
movq %r12, %rcx
xorl %r9d, %r9d
callq 0x10e0
incq %r14
cmpq $0x20, %r14
jne 0x18b2
leaq 0x30(%rsp), %rbx
leaq 0x40(%rsp), %r8
xorl %edx, %edx
movq %rbx, %rcx
callq 0x10a0
vmovdqu (%rbp,%r13), %xmm0
vpxor (%rbx), %xmm0, %xmm0
xorl %eax, %eax
vptest %xmm0, %xmm0
setne %al
negl %eax
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)