// File: /workspace/github_c_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x158, %rsp            # imm = 0x158
movq %rdi, 0x18(%rsp)
cmpq $0x10, %r8
jae 0x179f
pushq $-0x1
popq %rax
jmp 0x1916
movq %r9, %r13
movq %rcx, %rbx
movq %rsi, 0x8(%rsp)
movq %r8, 0x10(%rsp)
movq 0x1a0(%rsp), %rdi
movq 0x198(%rsp), %rsi
leaq 0x30(%rsp), %r14
movq %r14, %rdx
callq 0x10c0
xorl %r15d, %r15d
leaq 0x7(%rsp), %rbp
leaq 0x6(%rsp), %r12
cmpq %r15, 0x190(%rsp)
je 0x1808
movzbl (%r13,%r15), %esi
movq %r14, %rdi
movq %rbp, %rdx
movq %r12, %rcx
movl $0xff, %r8d
movl $0xff, %r9d
callq 0x10d0
incq %r15
jmp 0x17d9
xorl %r12d, %r12d
leaq 0x30(%rsp), %r13
leaq 0x7(%rsp), %rbp
leaq 0x6(%rsp), %r14
cmpq $0x20, %r12
je 0x1851
xorl %esi, %esi
testq %r12, %r12
sete %sil
cmpq $0x10, %r12
movl $0x0, %eax
sbbl %eax, %eax
movzbl %al, %r8d
movq %r13, %rdi
movq %rbp, %rdx
movq %r14, %rcx
movl $0xff, %r9d
callq 0x10d0
incq %r12
jmp 0x181a
movq 0x10(%rsp), %rbp
addq $-0x10, %rbp
movq 0x8(%rsp), %r12
movq %rbp, (%r12)
xorl %r15d, %r15d
leaq 0x30(%rsp), %r14
movq %rbp, %rax
movq 0x18(%rsp), %r13
cmpq %rax, %r15
jae 0x189b
leaq (%r15,%r13), %rsi
movzbl (%rbx,%r15), %edx
movq %r14, %rdi
movl $0xff, %r8d
xorl %r9d, %r9d
callq 0x10b0
incq %r15
movq (%r12), %rax
jmp 0x1873
xorl %r13d, %r13d
leaq 0x30(%rsp), %r15
leaq 0x7(%rsp), %r14
leaq 0x6(%rsp), %r12
cmpq $0x20, %r13
je 0x18e1
xorl %esi, %esi
testq %r13, %r13
sete %sil
cmpq $0x10, %r13
movl $0x0, %eax
sbbl %eax, %eax
movzbl %al, %r8d
movq %r15, %rdi
movq %r14, %rdx
movq %r12, %rcx
xorl %r9d, %r9d
callq 0x10d0
incq %r13
jmp 0x18ad
xorl %r14d, %r14d
leaq 0x20(%rsp), %rcx
leaq 0x30(%rsp), %r8
xorl %edx, %edx
callq 0x1090
addq %rbp, %rbx
xorl %ecx, %ecx
cmpq $0x10, %r14
je 0x1910
movb (%rbx,%r14), %al
xorb 0x20(%rsp,%r14), %al
orb %al, %cl
incq %r14
jmp 0x18fa
xorl %eax, %eax
negb %cl
sbbl %eax, %eax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq