// File: /workspace/github_c_makefile_star2_O0/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:
subq $0xb8, %rsp
movq 0xd0(%rsp), %rax
movq 0xc8(%rsp), %rax
movq 0xc0(%rsp), %rax
movq %rdi, 0xa8(%rsp)
movq %rsi, 0xa0(%rsp)
movq %rdx, 0x98(%rsp)
movq %rcx, 0x90(%rsp)
movq %r8, 0x88(%rsp)
movq %r9, 0x80(%rsp)
movb $0x0, 0x5f(%rsp)
movq 0xd0(%rsp), %rdi
movq 0xc8(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0x1040
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
movl $0xffffffff, 0x8(%rsp)  # imm = 0xFFFFFFFF
movq $0x0, 0x78(%rsp)
movq 0x78(%rsp), %rax
movq 0xc0(%rsp), %rcx
shrq $0x2, %rcx
cmpq %rcx, %rax
jae 0x284b
leaq 0x10(%rsp), %rdi
movq 0x80(%rsp), %rax
movq 0x78(%rsp), %rcx
movl (%rax,%rcx,4), %esi
movl 0xc(%rsp), %r8d
movl 0x8(%rsp), %r9d
leaq 0x54(%rsp), %rdx
leaq 0x50(%rsp), %rcx
callq 0x10b0
movq 0x78(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x78(%rsp)
jmp 0x27f7
movq 0xc0(%rsp), %rax
andq $-0x4, %rax
movq %rax, 0x78(%rsp)
movq 0x78(%rsp), %rax
cmpq 0xc0(%rsp), %rax
jae 0x28c2
movl $0xff, 0xc(%rsp)
movl $0xff, 0x8(%rsp)
movq 0x80(%rsp), %rax
movq 0x78(%rsp), %rcx
movzbl (%rax,%rcx), %eax
movl %eax, 0x58(%rsp)
leaq 0x10(%rsp), %rdi
movl 0x58(%rsp), %esi
movl 0xc(%rsp), %r8d
movl 0x8(%rsp), %r9d
leaq 0x54(%rsp), %rdx
leaq 0x50(%rsp), %rcx
callq 0x1070
movq 0x78(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x78(%rsp)
jmp 0x285c
movq $0x0, 0x78(%rsp)
cmpq $0x8, 0x78(%rsp)
jae 0x2941
cmpq $0x0, 0x78(%rsp)
jne 0x28e5
movl $0x1, 0x58(%rsp)
jmp 0x28ed
movl $0x0, 0x58(%rsp)
cmpq $0x4, 0x78(%rsp)
jae 0x28ff
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
jmp 0x2907
movl $0x0, 0xc(%rsp)
movl $0xffffffff, 0x8(%rsp)  # imm = 0xFFFFFFFF
leaq 0x10(%rsp), %rdi
movl 0x58(%rsp), %esi
movl 0xc(%rsp), %r8d
movl 0x8(%rsp), %r9d
leaq 0x54(%rsp), %rdx
leaq 0x50(%rsp), %rcx
callq 0x10b0
movq 0x78(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x78(%rsp)
jmp 0x28cb
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
movl $0x0, 0x8(%rsp)
movq 0x88(%rsp), %rcx
subq $0x10, %rcx
movq 0xa0(%rsp), %rax
movq %rcx, (%rax)
movq $0x0, 0x78(%rsp)
movq 0x78(%rsp), %rax
movq 0xa0(%rsp), %rcx
movq (%rcx), %rcx
shrq $0x2, %rcx
cmpq %rcx, %rax
jae 0x29d7
leaq 0x10(%rsp), %rdi
movq 0xa8(%rsp), %rsi
movq 0x78(%rsp), %rax
shlq $0x2, %rax
addq %rax, %rsi
movq 0x90(%rsp), %rax
movq 0x78(%rsp), %rcx
movl (%rax,%rcx,4), %edx
movl 0xc(%rsp), %r8d
movl 0x8(%rsp), %r9d
leaq 0x50(%rsp), %rcx
callq 0x1030
movq 0x78(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x78(%rsp)
jmp 0x2971
movq 0xa0(%rsp), %rax
movq (%rax), %rax
andq $-0x4, %rax
movq %rax, 0x78(%rsp)
movq 0x78(%rsp), %rax
movq 0xa0(%rsp), %rcx
cmpq (%rcx), %rax
jae 0x2a6a
movl $0xff, 0xc(%rsp)
movl $0x0, 0x8(%rsp)
movq 0x90(%rsp), %rax
movq 0x78(%rsp), %rcx
movzbl (%rax,%rcx), %eax
movl %eax, 0x54(%rsp)
leaq 0x10(%rsp), %rdi
movl 0x54(%rsp), %edx
movl 0xc(%rsp), %r8d
movl 0x8(%rsp), %r9d
leaq 0x58(%rsp), %rsi
leaq 0x50(%rsp), %rcx
callq 0x1050
movl 0x58(%rsp), %eax
movb %al, %dl
movq 0xa8(%rsp), %rax
movq 0x78(%rsp), %rcx
movb %dl, (%rax,%rcx)
movq 0x78(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x78(%rsp)
jmp 0x29eb
movq $0x0, 0x78(%rsp)
cmpq $0x8, 0x78(%rsp)
jae 0x2ae9
cmpq $0x0, 0x78(%rsp)
jne 0x2a8d
movl $0x1, 0x58(%rsp)
jmp 0x2a95
movl $0x0, 0x58(%rsp)
cmpq $0x4, 0x78(%rsp)
jae 0x2aa7
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
jmp 0x2aaf
movl $0x0, 0xc(%rsp)
movl $0x0, 0x8(%rsp)
leaq 0x10(%rsp), %rdi
movl 0x58(%rsp), %esi
movl 0xc(%rsp), %r8d
movl 0x8(%rsp), %r9d
leaq 0x54(%rsp), %rdx
leaq 0x50(%rsp), %rcx
callq 0x10b0
movq 0x78(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x78(%rsp)
jmp 0x2a73
movq 0xa0(%rsp), %rax
movq (%rax), %rdi
movq 0xc0(%rsp), %rsi
leaq 0x60(%rsp), %rcx
leaq 0x10(%rsp), %r8
movl $0x10, %edx
callq 0x10a0
movq $0x0, 0x78(%rsp)
cmpq $0x10, 0x78(%rsp)
jae 0x2b65
movq 0x78(%rsp), %rax
movzbl 0x60(%rsp,%rax), %ecx
movq 0x90(%rsp), %rax
movq 0x88(%rsp), %rdx
subq $0x10, %rdx
addq 0x78(%rsp), %rdx
movzbl (%rax,%rdx), %eax
xorl %eax, %ecx
movzbl 0x5f(%rsp), %eax
orl %ecx, %eax
movb %al, 0x5f(%rsp)
movq 0x78(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x78(%rsp)
jmp 0x2b19
movzbl 0x5f(%rsp), %eax
cmpl $0x0, %eax
jne 0x2b7c
movl $0x0, 0xb4(%rsp)
jmp 0x2b87
movl $0xffffffff, 0xb4(%rsp) # imm = 0xFFFFFFFF
movl 0xb4(%rsp), %eax
addq $0xb8, %rsp
retq
nopw %cs:(%rax,%rax)