// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/acorn128v2/opt/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 tag[16],check=0;
    unsigned int plaintextword, ciphertextword, ksword;

    unsigned long long state[7];
    unsigned int ca, cb;

    //initialization stage
    acorn128_initialization_32bitversion(k, npub, state);

    //process the associated data
    ca = 0xffffffff;
    cb = 0xffffffff;
    for (i = 0; i < adlen/4; i = i+1)
    {
        encrypt_32bits(state, ((unsigned int*)ad)[i], &ciphertextword, &ksword, ca, cb);
    }

    for (i = adlen & 0xfffffffffffffffcULL; i < adlen; i++)
    {
        ca = 0xff;
        cb = 0xff;
        plaintextword = ad[i];
        encrypt_8bits(state, plaintextword, &ciphertextword, &ksword, ca, cb);
    }

    //256bits padding after the associated data
    for (i = 0; i < 256/32; i++)
    {
        if (i == 0) plaintextword = 0x1;
        else plaintextword = 0;

        if ( i < 128/32) ca = 0xffffffff;
        else ca = 0;

        cb = 0xffffffff;

        encrypt_32bits(state, plaintextword, &ciphertextword, &ksword, ca, cb);
    }

    //process the plaintext
    ca = 0xffffffff;
    cb = 0;
    *mlen = clen - 16;

    for (i = 0; i < *mlen/4; i=i+1)
    {
        decrypt_32bits(state, &(((unsigned int*)m)[i]), ((unsigned int*)c)[i], &ksword, ca, cb);  //&c[i], &ksword, ca, cb);
    }

    for (i = *mlen & 0xfffffffffffffffcULL; i < *mlen; i++)
    {
        ca = 0xff;
        cb = 0x0;
        ciphertextword = c[i];
        decrypt_8bits(state, &plaintextword, ciphertextword, &ksword, ca, cb);
        m[i] = plaintextword;
    }

    //256 bits padding after the plaintext
    for (i = 0; i < 256/32; i++)
    {
        if ( i == 0 ) plaintextword = 0x1;
        else plaintextword = 0;

        if ( i < 128/32)   ca = 0xffffffff;
        else ca = 0;

        cb = 0;

        encrypt_32bits(state, plaintextword, &ciphertextword, &ksword, ca, cb);
    }

    //finalization stage, we assume that the tag length is a multiple of bytes
    acorn128_tag_generation_32bits_version(*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 $0x88, %rsp
movq %r9, %r13
movq %r8, 0x18(%rsp)
movq %rcx, %rbx
movq %rsi, 0x28(%rsp)
movq %rdi, 0x20(%rsp)
movq 0xc0(%rsp), %r15
movq 0xd0(%rsp), %rdi
movq 0xc8(%rsp), %rsi
leaq 0x30(%rsp), %rdx
callq 0x1040
shrq $0x2, %r15
xorl %r14d, %r14d
leaq 0xc(%rsp), %r12
pushq $-0x1
popq %rbp
cmpq %r14, %r15
je 0x1aec
movl (%r13,%r14,4), %esi
leaq 0x30(%rsp), %rdi
leaq 0x10(%rsp), %rdx
movq %r12, %rcx
movl %ebp, %r8d
movl %ebp, %r9d
callq 0x10b0
incq %r14
jmp 0x1ac5
movq 0xc0(%rsp), %r15
andq $-0x4, %r15
leaq 0x30(%rsp), %r14
leaq 0x10(%rsp), %r12
leaq 0xc(%rsp), %rbp
cmpq 0xc0(%rsp), %r15
jae 0x1b36
movzbl (%r13,%r15), %esi
movq %r14, %rdi
movq %r12, %rdx
movq %rbp, %rcx
movl $0xff, %r8d
movl $0xff, %r9d
callq 0x1070
incq %r15
jmp 0x1b07
xorl %r13d, %r13d
leaq 0x10(%rsp), %r14
leaq 0xc(%rsp), %r12
pushq $-0x1
popq %rbp
movq 0x28(%rsp), %r15
cmpq $0x8, %r13
je 0x1b7f
xorl %esi, %esi
testq %r13, %r13
sete %sil
cmpq $0x4, %r13
movl $0x0, %r8d
sbbl %r8d, %r8d
leaq 0x30(%rsp), %rdi
movq %r14, %rdx
movq %r12, %rcx
movl %ebp, %r9d
callq 0x10b0
incq %r13
jmp 0x1b4b
andl $0x0, 0x14(%rsp)
movq 0x18(%rsp), %rax
addq $-0x10, %rax
movq %rax, (%r15)
xorl %r13d, %r13d
leaq 0xc(%rsp), %rbp
pushq $-0x1
popq %r14
movq 0x20(%rsp), %r12
movq %rax, 0x18(%rsp)
movq %rax, %rcx
shrq $0x2, %rcx
cmpq %rcx, %r13
jae 0x1bd8
movl (%rbx,%r13,4), %edx
leaq 0x30(%rsp), %rdi
movq %r12, %rsi
movq %rbp, %rcx
movl %r14d, %r8d
xorl %r9d, %r9d
callq 0x1030
incq %r13
movq (%r15), %rax
addq $0x4, %r12
jmp 0x1ba6
movq %rax, %rbp
andq $-0x4, %rbp
leaq 0x30(%rsp), %r14
leaq 0x14(%rsp), %r12
leaq 0xc(%rsp), %r13
cmpq %rax, %rbp
jae 0x1c22
movzbl (%rbx,%rbp), %edx
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rcx
movl $0xff, %r8d
xorl %r9d, %r9d
callq 0x1050
movb 0x14(%rsp), %al
movq 0x20(%rsp), %rcx
movb %al, (%rcx,%rbp)
incq %rbp
movq (%r15), %rax
jmp 0x1bee
xorl %r13d, %r13d
leaq 0x30(%rsp), %r14
leaq 0x10(%rsp), %r15
leaq 0xc(%rsp), %r12
cmpq $0x8, %r13
je 0x1c66
xorl %esi, %esi
testq %r13, %r13
sete %sil
cmpq $0x4, %r13
movl $0x0, %r8d
sbbl %r8d, %r8d
movq %r14, %rdi
movq %r15, %rdx
movq %r12, %rcx
xorl %r9d, %r9d
callq 0x10b0
incq %r13
jmp 0x1c34
xorl %r14d, %r14d
leaq 0x70(%rsp), %rcx
leaq 0x30(%rsp), %r8
xorl %edx, %edx
callq 0x10a0
addq 0x18(%rsp), %rbx
xorl %ecx, %ecx
cmpq $0x10, %r14
je 0x1c97
movb (%rbx,%r14), %al
xorb 0x70(%rsp,%r14), %al
orb %al, %cl
incq %r14
jmp 0x1c81
xorl %eax, %eax
negb %cl
sbbl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop