// File: /workspace/github_c_makefile_star2_O1/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, 0x10(%rsp)
movq %rcx, 0x28(%rsp)
movq %rsi, 0x18(%rsp)
movq %rdi, 0x20(%rsp)
movq 0xd0(%rsp), %rdi
movq 0xc8(%rsp), %rsi
leaq 0x30(%rsp), %rdx
callq 0x1040
cmpq $0x4, 0xc0(%rsp)
jb 0x1b5c
movq 0xc0(%rsp), %r15
shrq $0x2, %r15
xorl %ebp, %ebp
leaq 0x30(%rsp), %rbx
leaq 0xc(%rsp), %r12
leaq 0x4(%rsp), %r14
movl (%r13,%rbp,4), %esi
movq %rbx, %rdi
movq %r12, %rdx
movq %r14, %rcx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0x10b0
incq %rbp
cmpq %rbp, %r15
jne 0x1b35
movq 0xc0(%rsp), %rax
movq %rax, %r15
andq $-0x4, %r15
cmpq %rax, %r15
jne 0x1b77
movl 0x8(%rsp), %r14d
jmp 0x1bb6
leaq 0x30(%rsp), %rbx
leaq 0xc(%rsp), %r12
leaq 0x4(%rsp), %rbp
movzbl (%r13,%r15), %r14d
movq %rbx, %rdi
movl %r14d, %esi
movq %r12, %rdx
movq %rbp, %rcx
movl $0xff, %r8d
movl $0xff, %r9d
callq 0x1070
incq %r15
cmpq 0xc0(%rsp), %r15
jb 0x1b86
movl %r14d, 0x8(%rsp)
xorl %r14d, %r14d
leaq 0x30(%rsp), %r13
leaq 0xc(%rsp), %rbp
leaq 0x4(%rsp), %rbx
xorl %esi, %esi
testq %r14, %r14
sete %sil
cmpq $0x4, %r14
movl $0x0, %r8d
sbbl %r8d, %r8d
movq %r13, %rdi
movq %rbp, %rdx
movq %rbx, %rcx
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0x10b0
incq %r14
cmpq $0x8, %r14
jne 0x1bcd
movl $0x0, 0x8(%rsp)
movq 0x10(%rsp), %rax
addq $-0x10, %rax
movq 0x18(%rsp), %rdx
movq %rax, (%rdx)
movq %rax, %r14
movq %rax, 0x10(%rsp)
cmpq $0x4, %rax
movq 0x28(%rsp), %r15
jb 0x1c74
xorl %r12d, %r12d
leaq 0x30(%rsp), %r13
leaq 0x4(%rsp), %rbp
movq 0x20(%rsp), %rbx
movl (%r15,%r12,4), %edx
movq %r13, %rdi
movq %rbx, %rsi
movq %rbp, %rcx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
xorl %r9d, %r9d
callq 0x1030
movq 0x18(%rsp), %rdx
incq %r12
movq (%rdx), %r14
movq %r14, %rax
shrq $0x2, %rax
addq $0x4, %rbx
cmpq %rax, %r12
jb 0x1c3e
andq $-0x4, %r14
cmpq (%rdx), %r14
jae 0x1cc4
leaq 0x8(%rsp), %r13
leaq 0x4(%rsp), %rbp
movq 0x20(%rsp), %rbx
movzbl (%r15,%r14), %r12d
leaq 0x30(%rsp), %rdi
movq %r13, %rsi
movl %r12d, %edx
movq %rbp, %rcx
movl $0xff, %r8d
xorl %r9d, %r9d
callq 0x1050
movb 0x8(%rsp), %al
movb %al, (%rbx,%r14)
incq %r14
movq 0x18(%rsp), %rax
cmpq (%rax), %r14
jb 0x1c8c
jmp 0x1cc9
movl 0xc(%rsp), %r12d
leaq 0xc(%rsp), %r15
movl %r12d, (%r15)
xorl %r14d, %r14d
leaq 0x30(%rsp), %rbx
leaq 0x4(%rsp), %r12
xorl %esi, %esi
testq %r14, %r14
sete %sil
cmpq $0x4, %r14
movl $0x0, %r8d
sbbl %r8d, %r8d
movq %rbx, %rdi
movq %r15, %rdx
movq %r12, %rcx
xorl %r9d, %r9d
callq 0x10b0
incq %r14
cmpq $0x8, %r14
jne 0x1cde
movl $0x0, 0x8(%rsp)
leaq 0x70(%rsp), %rbx
leaq 0x30(%rsp), %r8
xorl %edx, %edx
movq %rbx, %rcx
callq 0x10a0
movq 0x10(%rsp), %rax
movq 0x28(%rsp), %rcx
vmovdqu (%rcx,%rax), %xmm0
vpxor (%rbx), %xmm0, %xmm0
xorl %eax, %eax
vptest %xmm0, %xmm0
setne %al
negl %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop