// File: /workspace/github_c_makefile_star2_O2/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 $0x108, %rsp            # imm = 0x108
movq %r9, %rbp
movq %rcx, %rbx
movq %rsi, %r15
movq %rdi, %r12
movq 0x140(%rsp), %r13
movq 0x148(%rsp), %r14
movq 0x150(%rsp), %rdi
addq $-0x8, %r8
movq %r8, 0xc8(%rsp)
movq %r8, (%rsi)
callq 0x1060
movq %r13, %rsi
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 0x350d(%rip), %r14      # 0x4ff8
vmovdqa (%r14), %xmm1
vmovdqa 0x10(%r14), %xmm2
vmovdqa 0x20(%r14), %xmm3
vmovdqa 0x30(%r14), %xmm4
vpxor %xmm0, %xmm1, %xmm0
vaesenc %xmm2, %xmm0, %xmm0
vaesenc %xmm3, %xmm0, %xmm0
vaesenc %xmm4, %xmm0, %xmm0
vmovdqa 0x40(%r14), %xmm5
vmovdqa 0x50(%r14), %xmm6
vaesenc %xmm5, %xmm0, %xmm0
vaesenc %xmm6, %xmm0, %xmm0
vmovdqa 0x60(%r14), %xmm7
vmovdqa 0x70(%r14), %xmm8
vaesenc %xmm7, %xmm0, %xmm0
vaesenc %xmm8, %xmm0, %xmm0
vmovdqa 0x80(%r14), %xmm9
vmovdqa 0x90(%r14), %xmm10
vaesenc %xmm9, %xmm0, %xmm0
vaesenc %xmm10, %xmm0, %xmm0
vmovdqa 0xa0(%r14), %xmm11
vaesenclast %xmm11, %xmm0, %xmm0
movq %r13, %rax
shrq $0x4, %rax
movq %rbp, %rcx
subq $0x1, %rax
jb 0x1bba
vpternlogq $0x96, (%rcx), %xmm1, %xmm0
vaesenc %xmm2, %xmm0, %xmm0
vaesenc %xmm3, %xmm0, %xmm0
vaesenc %xmm4, %xmm0, %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
vaesenclast %xmm11, %xmm0, %xmm0
addq $0x10, %rcx
jmp 0x1b75
vmovdqa %xmm0, 0xe0(%rsp)
movq %rsi, %r13
andq $0xf, %r13
vmovdqa %xmm1, 0x30(%rsp)
vmovdqa %xmm2, 0x20(%rsp)
vmovdqa %xmm3, 0x10(%rsp)
vmovdqa %xmm4, 0xb0(%rsp)
vmovdqa %xmm5, 0xa0(%rsp)
vmovdqa %xmm6, 0x90(%rsp)
vmovdqa %xmm7, 0x80(%rsp)
vmovdqa %xmm8, 0x70(%rsp)
vmovdqa %xmm9, 0x60(%rsp)
vmovdqa %xmm10, 0x50(%rsp)
vmovdqa %xmm11, 0x40(%rsp)
je 0x1ccc
movq %rsi, %rax
andq $-0x10, %rax
addq %rax, %rbp
movq %rsp, %rdi
movq %rbp, %rsi
movq %r13, %rdx
vmovdqa %xmm0, 0xf0(%rsp)
callq 0x1090
leaq (%rsp,%r13), %rdi
pushq $0x10
popq %rdx
subq %r13, %rdx
xorl %esi, %esi
callq 0x1030
movq 0x140(%rsp), %rsi
vmovdqa 0x30(%rsp), %xmm0
movq %rsp, %rax
vmovdqa 0xf0(%rsp), %xmm1
vpternlogq $0x96, (%rax), %xmm0, %xmm1
vaesenc 0x20(%rsp), %xmm1, %xmm0
vaesenc 0x10(%rsp), %xmm0, %xmm0
vaesenc 0xb0(%rsp), %xmm0, %xmm0
vaesenc 0xa0(%rsp), %xmm0, %xmm0
vaesenc 0x90(%rsp), %xmm0, %xmm0
vaesenc 0x80(%rsp), %xmm0, %xmm0
vaesenc 0x70(%rsp), %xmm0, %xmm0
vaesenc 0x60(%rsp), %xmm0, %xmm0
vaesenc 0x50(%rsp), %xmm0, %xmm0
vaesenclast 0x40(%rsp), %xmm0, %xmm0
vmovdqa %xmm0, 0xe0(%rsp)
leaq 0xe0(%rsp), %r13
movq %r13, %rdi
callq 0x1080
vmovdqa (%r13), %xmm0
vmovdqa 0x1316(%rip), %xmm1     # 0x3000
vpalignr $0x1, %xmm0, %xmm0, %xmm2 # xmm2 = xmm0[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0]
vpshufb %xmm1, %xmm0, %xmm0
vpxor %xmm0, %xmm2, %xmm0
vmovdqa64 0x30(%rsp), %xmm16
vpxorq %xmm0, %xmm16, %xmm3
vmovdqa 0x20(%rsp), %xmm4
vaesenc %xmm4, %xmm3, %xmm3
vmovdqa 0x10(%rsp), %xmm5
vaesenc %xmm5, %xmm3, %xmm3
vmovdqa 0xb0(%rsp), %xmm6
vaesenc %xmm6, %xmm3, %xmm3
vmovdqa 0xa0(%rsp), %xmm7
vaesenc %xmm7, %xmm3, %xmm3
vmovdqa 0x90(%rsp), %xmm8
vaesenc %xmm8, %xmm3, %xmm3
vmovdqa 0x80(%rsp), %xmm9
vaesenc %xmm9, %xmm3, %xmm3
vmovdqa 0x70(%rsp), %xmm10
vaesenc %xmm10, %xmm3, %xmm3
vmovdqa 0x60(%rsp), %xmm11
vaesenc %xmm11, %xmm3, %xmm3
vmovdqa 0x50(%rsp), %xmm12
vaesenc %xmm12, %xmm3, %xmm3
vmovdqa 0x40(%rsp), %xmm13
vaesenclast %xmm13, %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 %xmm1, %xmm2, %xmm1
vpternlogq $0x96, %xmm0, %xmm16, %xmm1
vaesenc %xmm4, %xmm1, %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
vaesenclast %xmm13, %xmm0, %xmm14
xorl %r13d, %r13d
vpmovzxbq 0x1271(%rip), %xmm0 # 0x3042
vmovdqa64 %xmm16, %xmm3
leaq 0x10(%r13), %rax
movq (%r15), %rcx
cmpq %rcx, %rax
jae 0x1ebb
vmovdqa (%rbx,%r13), %xmm1
vpxor %xmm1, %xmm15, %xmm2
vmovdqa %xmm2, (%r12,%r13)
vmovdqa (%r14), %xmm3
vmovdqa 0x10(%r14), %xmm4
vmovdqa 0x20(%r14), %xmm5
vmovdqa 0x30(%r14), %xmm6
vpternlogq $0x96, %xmm1, %xmm3, %xmm14
vaesenc %xmm4, %xmm14, %xmm2
vaesenc %xmm5, %xmm2, %xmm2
vaesenc %xmm6, %xmm2, %xmm2
vmovdqa 0x40(%r14), %xmm7
vaesenc %xmm7, %xmm2, %xmm2
vmovdqa 0x50(%r14), %xmm8
vaesenc %xmm8, %xmm2, %xmm2
vmovdqa 0x60(%r14), %xmm9
vaesenc %xmm9, %xmm2, %xmm2
vmovdqa 0x70(%r14), %xmm10
vaesenc %xmm10, %xmm2, %xmm2
vmovdqa 0x80(%r14), %xmm11
vaesenc %xmm11, %xmm2, %xmm2
vmovdqa 0x90(%r14), %xmm12
vaesenc %xmm12, %xmm2, %xmm2
vmovdqa 0xa0(%r14), %xmm13
vaesenclast %xmm13, %xmm2, %xmm14
vpternlogq $0x36, %xmm0, %xmm3, %xmm1
vaesenc %xmm4, %xmm1, %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
vaesenclast %xmm13, %xmm1, %xmm15
movq %rax, %r13
jmp 0x1dd7
vmovdqa %xmm14, 0xd0(%rsp)
subq %r13, %rcx
jne 0x1f1c
vmovdqa %xmm3, 0x30(%rsp)
vmovdqa %xmm4, 0x20(%rsp)
vmovdqa %xmm5, 0x10(%rsp)
vmovdqa %xmm6, 0xb0(%rsp)
vmovdqa %xmm7, 0xa0(%rsp)
vmovdqa %xmm8, 0x90(%rsp)
vmovdqa %xmm9, 0x80(%rsp)
vmovdqa %xmm10, 0x70(%rsp)
vmovdqa %xmm11, 0x60(%rsp)
vmovdqa %xmm12, 0x50(%rsp)
vmovdqa %xmm13, 0x40(%rsp)
jmp 0x207e
leaq (%rbx,%r13), %rsi
movslq %ecx, %rbp
movq %rsp, %rdi
movq %rbp, %rdx
vmovdqa %xmm14, 0x20(%rsp)
vmovdqa %xmm15, 0x10(%rsp)
callq 0x1090
leaq (%rsp,%rbp), %rdi
pushq $0x10
popq %rax
subl %ebp, %eax
movslq %eax, %rdx
xorl %esi, %esi
callq 0x1030
movq %rsp, %rax
vmovaps (%rax), %xmm0
vmovaps %xmm0, 0x30(%rsp)
vxorps 0x10(%rsp), %xmm0, %xmm0
addq %r13, %r12
vmovaps %xmm0, (%rax)
movq %rsp, %rsi
movq %r12, %rdi
movq %rbp, %rdx
callq 0x1090
shll $0x18, %ebp
movl $0x10000000, %eax       # imm = 0x10000000
subl %ebp, %eax
shrl $0x18, %eax
vpbroadcastb %eax, %xmm0
vpaddb 0x10a0(%rip), %xmm0, %xmm0 # 0x3030
vpcmpeqd %xmm1, %xmm1, %xmm1
vpshufb %xmm0, %xmm1, %xmm0
vpand 0x30(%rsp), %xmm0, %xmm0
vmovdqa (%r14), %xmm1
vmovdqa 0x10(%r14), %xmm2
vmovdqa 0x20(%r14), %xmm3
vmovdqa 0x30(%r14), %xmm4
vmovdqa %xmm1, 0x30(%rsp)
vpternlogq $0x96, 0x20(%rsp), %xmm1, %xmm0
vmovdqa %xmm2, 0x20(%rsp)
vaesenc %xmm2, %xmm0, %xmm0
vmovdqa %xmm3, 0x10(%rsp)
vaesenc %xmm3, %xmm0, %xmm0
vmovdqa %xmm4, 0xb0(%rsp)
vaesenc %xmm4, %xmm0, %xmm0
vmovdqa 0x40(%r14), %xmm1
vmovdqa %xmm1, 0xa0(%rsp)
vaesenc %xmm1, %xmm0, %xmm0
vmovdqa 0x50(%r14), %xmm1
vmovdqa %xmm1, 0x90(%rsp)
vaesenc %xmm1, %xmm0, %xmm0
vmovdqa 0x60(%r14), %xmm1
vmovdqa %xmm1, 0x80(%rsp)
vaesenc %xmm1, %xmm0, %xmm0
vmovdqa 0x70(%r14), %xmm1
vmovdqa %xmm1, 0x70(%rsp)
vaesenc %xmm1, %xmm0, %xmm0
vmovdqa 0x80(%r14), %xmm1
vmovdqa %xmm1, 0x60(%rsp)
vaesenc %xmm1, %xmm0, %xmm0
vmovdqa 0x90(%r14), %xmm1
vmovdqa %xmm1, 0x50(%rsp)
vaesenc %xmm1, %xmm0, %xmm0
vmovdqa 0xa0(%r14), %xmm1
vmovdqa %xmm1, 0x40(%rsp)
vaesenclast %xmm1, %xmm0, %xmm0
vmovdqa %xmm0, 0xd0(%rsp)
movq (%r15), %r13
leaq 0xd0(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x1080
vmovdqa (%r14), %xmm0
vpshufb 0xf61(%rip), %xmm0, %xmm1 # 0x3000
vpalignr $0x1, %xmm0, %xmm0, %xmm0 # xmm0 = xmm0[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0]
vpternlogq $0x96, 0x30(%rsp), %xmm0, %xmm1
vaesenc 0x20(%rsp), %xmm1, %xmm0
vaesenc 0x10(%rsp), %xmm0, %xmm0
vaesenc 0xb0(%rsp), %xmm0, %xmm0
vaesenc 0xa0(%rsp), %xmm0, %xmm0
vaesenc 0x90(%rsp), %xmm0, %xmm0
vaesenc 0x80(%rsp), %xmm0, %xmm0
vaesenc 0x70(%rsp), %xmm0, %xmm0
vaesenc 0x60(%rsp), %xmm0, %xmm0
vaesenc 0x50(%rsp), %xmm0, %xmm0
vaesenclast 0x40(%rsp), %xmm0, %xmm0
vmovdqa %xmm0, (%rsp)
addq 0xc8(%rsp), %rbx
xorl %eax, %eax
pushq $-0x1
popq %rcx
xorl %edx, %edx
cmpq $0x8, %rdx
je 0x212d
movb (%rsp,%rdx), %sil
leaq 0x1(%rdx), %rdi
cmpb (%rbx,%rdx), %sil
movq %rdi, %rdx
je 0x2114
movl %ecx, %eax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop