// File: /workspace/github_c_makefile_star2_O3/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 0x1b46
movq 0xc0(%rsp), %r14
shrq $0x2, %r14
xorl %r15d, %r15d
leaq 0x30(%rsp), %rbx
leaq 0x8(%rsp), %rbp
leaq 0x4(%rsp), %r12
movl (%r13,%r15,4), %esi
movq %rbx, %rdi
movq %rbp, %rdx
movq %r12, %rcx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0x10b0
incq %r15
cmpq %r15, %r14
jne 0x1b1f
movq 0xc0(%rsp), %r15
movq %r15, %r14
andq $-0x4, %r14
cmpq %r15, %r14
je 0x1b91
leaq 0x30(%rsp), %rbx
leaq 0x8(%rsp), %rbp
leaq 0x4(%rsp), %r12
movzbl (%r13,%r14), %esi
movq %rbx, %rdi
movq %rbp, %rdx
movq %r12, %rcx
movl $0xff, %r8d
movl $0xff, %r9d
callq 0x1070
incq %r14
cmpq %r15, %r14
jb 0x1b69
xorl %r12d, %r12d
leaq 0x30(%rsp), %r13
leaq 0x8(%rsp), %rbp
leaq 0x4(%rsp), %rbx
xorl %esi, %esi
testq %r12, %r12
sete %sil
cmpq $0x4, %r12
movl $0x0, %r8d
sbbl %r8d, %r8d
movq %r13, %rdi
movq %rbp, %rdx
movq %rbx, %rcx
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0x10b0
incq %r12
cmpq $0x8, %r12
jne 0x1ba3
movl $0x0, 0xc(%rsp)
movq 0x10(%rsp), %rcx
addq $-0x10, %rcx
movq 0x18(%rsp), %rax
movq %rcx, (%rax)
movq %rcx, %rax
movq %rcx, 0x10(%rsp)
cmpq $0x4, %rcx
movq 0x28(%rsp), %r15
jb 0x1c4a
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
incq %r12
movq 0x18(%rsp), %rax
movq (%rax), %rax
movq %rax, %rcx
shrq $0x2, %rcx
addq $0x4, %rbx
cmpq %rcx, %r12
jb 0x1c14
movq %rax, %r12
andq $-0x4, %r12
cmpq %rax, %r12
movq 0x20(%rsp), %r14
je 0x1c9b
leaq 0x30(%rsp), %rbx
leaq 0xc(%rsp), %r13
leaq 0x4(%rsp), %rbp
movzbl (%r15,%r12), %edx
movq %rbx, %rdi
movq %r13, %rsi
movq %rbp, %rcx
movl $0xff, %r8d
xorl %r9d, %r9d
callq 0x1050
movb 0xc(%rsp), %al
movb %al, (%r14,%r12)
incq %r12
movq 0x18(%rsp), %rax
cmpq (%rax), %r12
jb 0x1c6a
xorl %r13d, %r13d
leaq 0x30(%rsp), %r15
leaq 0x8(%rsp), %rbx
leaq 0x4(%rsp), %r12
xorl %esi, %esi
testq %r13, %r13
sete %sil
cmpq $0x4, %r13
movl $0x0, %r8d
sbbl %r8d, %r8d
movq %r15, %rdi
movq %rbx, %rdx
movq %r12, %rcx
xorl %r9d, %r9d
callq 0x10b0
incq %r13
cmpq $0x8, %r13
jne 0x1cad
leaq 0x70(%rsp), %rbx
leaq 0x30(%rsp), %r8
xorl %edx, %edx
movq %rbx, %rcx
callq 0x10a0
movq 0x28(%rsp), %rax
movq 0x10(%rsp), %rcx
vmovdqu (%rax,%rcx), %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