// File: /workspace/github_c_makefile_star2_O3/mjosaarinen[P]brutus/crypto_aead/aes128n12t8silcv2/aesni/encrypt.c
// Function: crypto_aead_decrypt
int crypto_aead_decrypt(
                        unsigned char *m,unsigned long long *mlen,          // message
                        unsigned char *nsec,                                // not relavent to CLOC or SLIC
                        const unsigned char *c,unsigned long long clen,     // ciphertext
                        const unsigned char *ad,unsigned long long adlen,   // associated data
                        const unsigned char *npub,                          // nonce
                        const unsigned char *k                              // the master key
                        )
{

    block estate, tstate, tmp;  // encryption state, tag state, and temporary state
    estate = SETZERO();
    unsigned char ltag[16];     // local copy of temporary tag value
    
    /* set message length */
    *mlen = clen - CRYPTO_ABYTES;
    
    /* generate round keys from master key */
    AES128_KeyExpansion(k);
    
    /* process nonce */
    load_partial_block(&estate, npub, CRYPTO_NPUBBYTES, PARAM_ZP);
    
    AES128_encrypt(estate, estate);
    
    /* process assoicated data */
    unsigned long long i, lastblocklen;
    int j;
    for(i = 0; i < (unsigned long long)(adlen >> 4); i++){
        estate = XOR(estate, LOAD(ad+(i<<4)));
        AES128_encrypt(estate, estate);
    }
    
    /* process the last partial block if any */
    lastblocklen = adlen % STATE_LEN;
    if(lastblocklen){
        load_partial_block(&tmp, ad+(i<<4), lastblocklen, ZERO_APPEND);
        estate = XOR(estate, tmp);
        AES128_encrypt(estate, estate);
    }
    
    /* encode the adlen and process */
    xor_length(&estate, adlen);
    estate = g(estate);
    tstate = estate;
    
    /* process ciphertext */
    
    AES128_encrypt(estate, estate);
    tstate = g(tstate);
    AES128_encrypt(tstate, tstate);
    
    i = 0;
    /* process all the message except for the last message/ciphertext block */
    while((i + STATE_LEN) < (*mlen)){
        tmp = LOAD(c+i);
        estate = XOR(estate, tmp);
        STORE(m+i, estate);
        tstate = XOR(tmp, tstate);
        AES128_encrypt(tstate, tstate);
        fix1(tmp);
        print_state("after applying fix1\n", estate);
        AES128_encrypt(tmp, estate);
        i += STATE_LEN;
    }
    
    /* process the last block of the message/ciphetext */
    lastblocklen = (*mlen) - i;
    
    if(lastblocklen > 0){
        load_partial_block(&tmp, c+i, lastblocklen, ZERO_APPEND);
        estate = XOR(estate, tmp);
        store_partial_block(m+i, estate, lastblocklen);
        char shift_bytes = (char)(STATE_LEN - lastblocklen);
        tmp = AND(SHR(_mm_set1_epi8(0xff), shift_bytes), tmp);
        tstate = XOR(tstate, tmp);
        AES128_encrypt(tstate, tstate);
    }
    /* encode the message length and process */
    
    xor_length(&tstate, (*mlen));
    tstate = g(tstate);
    AES128_encrypt(tstate, tstate);
    
    /* compare tag and output message */
    STORE(ltag, tstate);
    for(j = 0; j < CRYPTO_ABYTES; j++){
        if(ltag[j] != c[clen - CRYPTO_ABYTES + j])
            return RETURN_TAG_NO_MATCH;
    }
    return RETURN_SUCCESS;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %r9, %r13
movq %rcx, %rbx
movq %rsi, %r15
movq %rdi, %r12
movq 0x120(%rsp), %rbp
movq 0x128(%rsp), %r14
movq 0x130(%rsp), %rdi
addq $-0x8, %r8
movq %r8, 0x48(%rsp)
movq %r8, (%rsi)
callq 0x1060
vmovq (%r14), %xmm0
vpinsrd $0x2, 0x8(%r14), %xmm0, %xmm0
vpslldq $0x4, %xmm0, %xmm0      # xmm0 = zero,zero,zero,zero,xmm0[0,1,2,3,4,5,6,7,8,9,10,11]
movl $0xc0, %eax
vpinsrb $0x3, %eax, %xmm0, %xmm0
movq 0x35e7(%rip), %r14      # 0x4ff8
vmovdqa (%r14), %xmm4
vmovdqa 0x10(%r14), %xmm5
vmovdqa 0x20(%r14), %xmm6
vmovdqa 0x30(%r14), %xmm7
vpxor %xmm0, %xmm4, %xmm0
vaesenc %xmm5, %xmm0, %xmm0
vaesenc %xmm6, %xmm0, %xmm0
vaesenc %xmm7, %xmm0, %xmm0
vmovdqa 0x40(%r14), %xmm8
vmovdqa 0x50(%r14), %xmm9
vaesenc %xmm8, %xmm0, %xmm0
vaesenc %xmm9, %xmm0, %xmm0
vmovdqa 0x60(%r14), %xmm10
vmovdqa 0x70(%r14), %xmm11
vaesenc %xmm10, %xmm0, %xmm0
vaesenc %xmm11, %xmm0, %xmm0
vmovdqa 0x80(%r14), %xmm12
vmovdqa 0x90(%r14), %xmm13
vaesenc %xmm12, %xmm0, %xmm0
vaesenc %xmm13, %xmm0, %xmm0
vmovdqa 0xa0(%r14), %xmm14
vaesenclast %xmm14, %xmm0, %xmm1
cmpq $0x10, %rbp
jae 0x1a9e
movq %r13, %rsi
xorl %eax, %eax
jmp 0x1af4
movq %rbp, %rax
shrq $0x4, %rax
movq %r13, %rcx
vpternlogq $0x96, (%rcx), %xmm4, %xmm1
vaesenc %xmm5, %xmm1, %xmm0
vaesenc %xmm6, %xmm0, %xmm0
vaesenc %xmm7, %xmm0, %xmm0
vaesenc %xmm8, %xmm0, %xmm0
vaesenc %xmm9, %xmm0, %xmm0
vaesenc %xmm10, %xmm0, %xmm0
vaesenc %xmm11, %xmm0, %xmm0
vaesenc %xmm12, %xmm0, %xmm0
vaesenc %xmm13, %xmm0, %xmm0
vaesenclast %xmm14, %xmm0, %xmm1
addq $0x10, %rcx
decq %rax
jne 0x1aa8
movq %r13, %rsi
movq %rbp, %rax
andq $-0x10, %rax
movq %rbp, %r13
andq $0xf, %r13
je 0x1c12
addq %rax, %rsi
movq %rsp, %rdi
movq %r13, %rdx
vmovdqa %xmm4, 0x30(%rsp)
vmovdqa %xmm5, 0x20(%rsp)
vmovdqa %xmm6, 0x10(%rsp)
vmovdqa %xmm7, 0xd0(%rsp)
vmovdqa %xmm8, 0xc0(%rsp)
vmovdqa %xmm9, 0xb0(%rsp)
vmovdqa %xmm10, 0xa0(%rsp)
vmovdqa %xmm11, 0x90(%rsp)
vmovdqa %xmm12, 0x80(%rsp)
vmovdqa %xmm13, 0x70(%rsp)
vmovdqa %xmm14, 0x60(%rsp)
vmovdqa %xmm1, 0x50(%rsp)
callq 0x1080
leaq (%rsp,%r13), %rdi
movl $0x10, %edx
subq %r13, %rdx
xorl %esi, %esi
callq 0x1030
vmovdqa 0x60(%rsp), %xmm14
vmovdqa 0x70(%rsp), %xmm13
vmovdqa 0x80(%rsp), %xmm12
vmovdqa 0x90(%rsp), %xmm11
vmovdqa 0xa0(%rsp), %xmm10
vmovdqa 0xb0(%rsp), %xmm9
vmovdqa 0xc0(%rsp), %xmm8
vmovdqa 0xd0(%rsp), %xmm7
vmovdqa 0x10(%rsp), %xmm6
vmovdqa 0x20(%rsp), %xmm5
vmovdqa 0x30(%rsp), %xmm4
movq %rsp, %rax
vmovdqa 0x50(%rsp), %xmm0
vpternlogq $0x96, (%rax), %xmm4, %xmm0
vaesenc %xmm5, %xmm0, %xmm0
vaesenc %xmm6, %xmm0, %xmm0
vaesenc %xmm7, %xmm0, %xmm0
vaesenc %xmm8, %xmm0, %xmm0
vaesenc %xmm9, %xmm0, %xmm0
vaesenc %xmm10, %xmm0, %xmm0
vaesenc %xmm11, %xmm0, %xmm0
vaesenc %xmm12, %xmm0, %xmm0
vaesenc %xmm13, %xmm0, %xmm0
vaesenclast %xmm14, %xmm0, %xmm1
vpxor %xmm0, %xmm0, %xmm0
vmovdqa %xmm0, (%rsp)
movl $0x8, %eax
movb %bpl, 0x7(%rsp,%rax)
shrq $0x8, %rbp
decq %rax
jne 0x1c20
vpxor (%rsp), %xmm1, %xmm0
vpalignr $0x1, %xmm0, %xmm0, %xmm1 # xmm1 = xmm0[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0]
vmovdqa 0x13bf(%rip), %xmm2     # 0x3000
vpshufb %xmm2, %xmm0, %xmm0
vpxor %xmm0, %xmm1, %xmm0
vpxor %xmm0, %xmm4, %xmm3
vaesenc %xmm5, %xmm3, %xmm3
vaesenc %xmm6, %xmm3, %xmm3
vaesenc %xmm7, %xmm3, %xmm3
vaesenc %xmm8, %xmm3, %xmm3
vaesenc %xmm9, %xmm3, %xmm3
vaesenc %xmm10, %xmm3, %xmm3
vaesenc %xmm11, %xmm3, %xmm3
vaesenc %xmm12, %xmm3, %xmm3
vaesenc %xmm13, %xmm3, %xmm3
vaesenclast %xmm14, %xmm3, %xmm15
vpalignr $0x1, %xmm0, %xmm0, %xmm0 # xmm0 = xmm0[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0]
vpshufb %xmm2, %xmm1, %xmm1
vpternlogq $0x96, %xmm0, %xmm4, %xmm1
vaesenc %xmm5, %xmm1, %xmm0
vaesenc %xmm6, %xmm0, %xmm0
vaesenc %xmm7, %xmm0, %xmm0
vaesenc %xmm8, %xmm0, %xmm0
vaesenc %xmm9, %xmm0, %xmm0
vaesenc %xmm10, %xmm0, %xmm0
vaesenc %xmm11, %xmm0, %xmm0
vaesenc %xmm12, %xmm0, %xmm0
vaesenc %xmm13, %xmm0, %xmm0
vaesenclast %xmm14, %xmm0, %xmm3
movq (%r15), %rcx
cmpq $0x11, %rcx
jb 0x1dc0
xorl %eax, %eax
vpmovzxbq 0x1366(%rip), %xmm0 # 0x3042
vmovdqa (%rbx,%rax), %xmm1
vpxor %xmm1, %xmm15, %xmm2
vmovdqa %xmm2, (%r12,%rax)
vmovdqa (%r14), %xmm4
vmovdqa 0x10(%r14), %xmm5
vmovdqa 0x20(%r14), %xmm6
vmovdqa 0x30(%r14), %xmm7
vpternlogq $0x96, %xmm4, %xmm1, %xmm3
vaesenc %xmm5, %xmm3, %xmm2
vaesenc %xmm6, %xmm2, %xmm2
vaesenc %xmm7, %xmm2, %xmm2
vmovdqa 0x40(%r14), %xmm8
vaesenc %xmm8, %xmm2, %xmm2
vmovdqa 0x50(%r14), %xmm9
vaesenc %xmm9, %xmm2, %xmm2
vmovdqa 0x60(%r14), %xmm10
vaesenc %xmm10, %xmm2, %xmm2
vmovdqa 0x70(%r14), %xmm11
vaesenc %xmm11, %xmm2, %xmm2
vmovdqa 0x80(%r14), %xmm12
vaesenc %xmm12, %xmm2, %xmm2
vmovdqa 0x90(%r14), %xmm13
vaesenc %xmm13, %xmm2, %xmm2
vmovdqa 0xa0(%r14), %xmm14
vaesenclast %xmm14, %xmm2, %xmm3
vpternlogq $0x36, %xmm0, %xmm4, %xmm1
vaesenc %xmm5, %xmm1, %xmm1
vaesenc %xmm6, %xmm1, %xmm1
vaesenc %xmm7, %xmm1, %xmm1
vaesenc %xmm8, %xmm1, %xmm1
vaesenc %xmm9, %xmm1, %xmm1
vaesenc %xmm10, %xmm1, %xmm1
vaesenc %xmm11, %xmm1, %xmm1
vaesenc %xmm12, %xmm1, %xmm1
vaesenc %xmm13, %xmm1, %xmm1
vaesenclast %xmm14, %xmm1, %xmm15
movq (%r15), %rcx
leaq 0x10(%rax), %rbp
addq $0x20, %rax
cmpq %rcx, %rax
movq %rbp, %rax
jb 0x1cdc
jmp 0x1dc2
xorl %ebp, %ebp
subq %rbp, %rcx
je 0x1edf
leaq (%rbx,%rbp), %rsi
movslq %ecx, %r13
movq %rsp, %rdi
movq %r13, %rdx
vmovdqa %xmm3, 0x30(%rsp)
vmovdqa %xmm15, 0x10(%rsp)
callq 0x1080
leaq (%rsp,%r13), %rdi
movl $0x10, %eax
subl %r13d, %eax
movslq %eax, %rdx
xorl %esi, %esi
callq 0x1030
movq %rsp, %rax
vmovaps (%rax), %xmm0
vmovaps %xmm0, 0x20(%rsp)
vxorps 0x10(%rsp), %xmm0, %xmm0
addq %rbp, %r12
vmovaps %xmm0, (%rax)
movq %rsp, %rsi
movq %r12, %rdi
movq %r13, %rdx
callq 0x1080
shll $0x18, %r13d
movl $0x10000000, %eax       # imm = 0x10000000
subl %r13d, %eax
shrl $0x18, %eax
vpbroadcastb %eax, %xmm0
vpaddb 0x11ec(%rip), %xmm0, %xmm0 # 0x3030
vpcmpeqd %xmm1, %xmm1, %xmm1
vpshufb %xmm0, %xmm1, %xmm0
vpand 0x20(%rsp), %xmm0, %xmm0
vmovdqa (%r14), %xmm4
vmovdqa 0x10(%r14), %xmm5
vmovdqa 0x20(%r14), %xmm6
vmovdqa 0x30(%r14), %xmm7
vmovdqa 0x30(%rsp), %xmm1
vpternlogq $0x96, %xmm0, %xmm4, %xmm1
vaesenc %xmm5, %xmm1, %xmm0
vaesenc %xmm6, %xmm0, %xmm0
vaesenc %xmm7, %xmm0, %xmm0
vmovdqa 0x40(%r14), %xmm8
vmovdqa 0x50(%r14), %xmm9
vaesenc %xmm8, %xmm0, %xmm0
vaesenc %xmm9, %xmm0, %xmm0
vmovdqa 0x60(%r14), %xmm10
vmovdqa 0x70(%r14), %xmm11
vaesenc %xmm10, %xmm0, %xmm0
vaesenc %xmm11, %xmm0, %xmm0
vmovdqa 0x80(%r14), %xmm12
vmovdqa 0x90(%r14), %xmm13
vaesenc %xmm12, %xmm0, %xmm0
vaesenc %xmm13, %xmm0, %xmm0
vmovdqa 0xa0(%r14), %xmm14
vaesenclast %xmm14, %xmm0, %xmm3
movq (%r15), %rbp
vpxor %xmm0, %xmm0, %xmm0
vmovdqa %xmm0, (%rsp)
movl $0x8, %eax
movb %bpl, 0x7(%rsp,%rax)
shrq $0x8, %rbp
decq %rax
jne 0x1eed
vpxor (%rsp), %xmm3, %xmm0
vpalignr $0x1, %xmm0, %xmm0, %xmm1 # xmm1 = xmm0[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0]
vpshufb 0x10f1(%rip), %xmm0, %xmm0 # 0x3000
vpternlogq $0x96, %xmm0, %xmm1, %xmm4
vaesenc %xmm5, %xmm4, %xmm0
vaesenc %xmm6, %xmm0, %xmm0
vaesenc %xmm7, %xmm0, %xmm0
vaesenc %xmm8, %xmm0, %xmm0
vaesenc %xmm9, %xmm0, %xmm0
vaesenc %xmm10, %xmm0, %xmm0
vaesenc %xmm11, %xmm0, %xmm0
vaesenc %xmm12, %xmm0, %xmm0
vaesenc %xmm13, %xmm0, %xmm0
vaesenclast %xmm14, %xmm0, %xmm0
vmovdqa %xmm0, (%rsp)
addq 0x48(%rsp), %rbx
xorl %eax, %eax
movb (%rsp,%rax), %cl
cmpb (%rbx,%rax), %cl
jne 0x1f69
incq %rax
cmpq $0x8, %rax
jne 0x1f54
xorl %eax, %eax
jmp 0x1f6e
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq