// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/aes128n12t8clocv2/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
    unsigned long long i, lastblocklen,j;
    
    /* set ciphertext length */
    *mlen = clen - CRYPTO_ABYTES;
    
    /* generate round keys from master key */
    AES128_KeyExpansion(k);
    
    
    /* process the first (partial) block of ad */
    load_partial_block(&estate, ad, (adlen>STATE_LEN)?STATE_LEN:adlen, ONE_ZERO_PADDING);
    fix0(estate);
    AES128_encrypt(estate, estate);
    if((ad[0] & 0x80) || (adlen == 0)){
        // appy h
        h(estate);
    }
    else{
        // do nothing
    }
    
    if(adlen > STATE_LEN){ // ad is of moer than one block
        i = STATE_LEN;
        
        /* process the middle ad blocks, excluding the first and last (partial) block */
        while((i+STATE_LEN) < adlen)
        {
            tmp = LOAD(ad+i);
            estate = XOR(estate, tmp);
            AES128_encrypt(estate, estate);
            i += STATE_LEN;
        }
        
        /* process the last (partial) ad block */
        load_partial_block(&tmp, ad+i, adlen - i, ONE_ZERO_PADDING);
        estate = XOR(estate, tmp);
        AES128_encrypt(estate, estate);
    }
    
    
    /* process the nonce */
    load_partial_block(&tmp, npub, CRYPTO_NPUBBYTES, PARAM_OZP);
    estate = XOR(estate, tmp);
    
    if((adlen % STATE_LEN) || (adlen == 0)){
        /* apply f2 */
        f2(estate);
    }
    else{
        /* apply f1 */
        f1(estate);
    }
    
    /* process ciphertext */
    
    tstate = estate;
    AES128_encrypt(estate, estate);
    
    if(*mlen){
        /* apply g2 to tag state */
        g2(tstate);
    }
    else{
        /* apply g1 to tag state */
        g1(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);
        print_state("after xoring last partial message block\n", estate);
        store_partial_block(m+i, estate, lastblocklen);
        unsigned char shift_bytes = (STATE_LEN - (unsigned char)lastblocklen);
        tmp = AND(SHR(_mm_set1_epi8(0xff), shift_bytes), tmp);
        tstate = XOR(tstate, tmp);
        /* add the one zero padding */
        tstate = XOR(tstate, SHL(_mm_set_epi8(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0x80), lastblocklen));
        
        if((*mlen) % STATE_LEN){
            /* apply f2 */
            f2(tstate);
        }
        else{
            /* apply f1 */
            f1(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 %rdi, 0x18(%rsp)
movq 0x140(%rsp), %r15
movq 0x150(%rsp), %rdi
addq $-0x8, %r8
movq %r8, 0x78(%rsp)
movq %rsi, 0x20(%rsp)
movq %r8, (%rsi)
callq 0x1060
pushq $0x10
popq %r12
cmpq %r12, %r15
movq %r12, %r13
cmovbq %r15, %r13
movq %rsp, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x1080
leaq (%rsp,%r13), %r14
movq %r12, %rdx
subq %r13, %rdx
movq %r14, %rdi
xorl %esi, %esi
callq 0x1030
movq %r15, %rsi
cmpq $0xf, %r15
ja 0x1af2
movb $-0x80, (%r14)
vmovdqa (%rsp), %xmm0
movq 0x34f2(%rip), %r15      # 0x4ff0
vmovdqa (%r15), %xmm3
vmovdqa 0x10(%r15), %xmm4
vmovdqa 0x20(%r15), %xmm5
vmovdqa 0x30(%r15), %xmm6
vpternlogq $0x6c, 0x1500(%rip), %xmm3, %xmm0 # 0x3020
vaesenc %xmm4, %xmm0, %xmm0
vaesenc %xmm5, %xmm0, %xmm0
vaesenc %xmm6, %xmm0, %xmm0
vmovdqa 0x40(%r15), %xmm7
vmovdqa 0x50(%r15), %xmm8
vaesenc %xmm7, %xmm0, %xmm0
vaesenc %xmm8, %xmm0, %xmm0
vmovdqa 0x60(%r15), %xmm9
vmovdqa 0x70(%r15), %xmm10
vaesenc %xmm9, %xmm0, %xmm0
vaesenc %xmm10, %xmm0, %xmm0
vmovdqa 0x80(%r15), %xmm11
vmovdqa 0x90(%r15), %xmm12
vaesenc %xmm11, %xmm0, %xmm0
vaesenc %xmm12, %xmm0, %xmm0
vmovdqa 0xa0(%r15), %xmm13
vaesenclast %xmm13, %xmm0, %xmm2
testq %rsi, %rsi
je 0x1b90
cmpb $0x0, (%rbp)
jns 0x1bb7
vpshufd $0x24, %xmm2, %xmm0     # xmm0 = xmm2[0,1,2,0]
vpshufd $0x79, %xmm2, %xmm1     # xmm1 = xmm2[1,2,3,1]
vpxor %xmm1, %xmm0, %xmm0
movq 0x3453(%rip), %rax      # 0x4ff8
vmovdqa %xmm0, (%rax)
vpxor %xmm1, %xmm1, %xmm1
vpblendd $0x8, %xmm2, %xmm1, %xmm1 # xmm1 = xmm1[0,1,2],xmm2[3]
vpxor %xmm1, %xmm0, %xmm2
movq 0x148(%rsp), %rax
movabsq $0x1000000000, %r8      # imm = 0x1000000000
cmpq $0x11, %rsi
jb 0x1c40
movabsq $-0x1000000000, %rax    # imm = 0xFFFFFFF000000000
movq %rsi, %r13
shlq $0x20, %r13
movabsq $0x2000000000, %r14     # imm = 0x2000000000
subq %r13, %r14
addq %rax, %r13
leaq 0x10(%r12), %rcx
cmpq %rsi, %rcx
jae 0x1c4a
vpternlogq $0x96, (%rbp,%r12), %xmm3, %xmm2
vaesenc %xmm4, %xmm2, %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, %xmm2
addq %r8, %r14
addq %rax, %r13
movq %rcx, %r12
jmp 0x1bf0
movq 0x18(%rsp), %r14
jmp 0x1da1
vmovdqa %xmm2, 0x80(%rsp)
vmovdqa %xmm13, 0x90(%rsp)
vmovdqa %xmm12, 0xa0(%rsp)
vmovdqa %xmm11, 0xb0(%rsp)
vmovdqa %xmm10, 0xc0(%rsp)
vmovdqa %xmm9, 0xd0(%rsp)
vmovdqa %xmm8, 0xe0(%rsp)
vmovdqa %xmm7, 0xf0(%rsp)
vmovdqa %xmm6, 0x40(%rsp)
vmovdqa %xmm5, 0x50(%rsp)
vmovdqa %xmm4, 0x60(%rsp)
vmovdqa %xmm3, 0x30(%rsp)
movl %esi, %eax
subl %r12d, %eax
movl %eax, 0x2c(%rsp)
sarq $0x20, %r13
addq %r12, %rbp
movq %rsp, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x1080
addq %rsp, %r13
sarq $0x20, %r14
movq %r13, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x1030
cmpl $0xf, 0x2c(%rsp)
jg 0x1ce8
movb $-0x80, (%r13)
vmovdqa 0x30(%rsp), %xmm3
vmovdqa 0x80(%rsp), %xmm0
vpternlogq $0x96, (%rsp), %xmm3, %xmm0
vmovdqa 0x60(%rsp), %xmm4
vaesenc %xmm4, %xmm0, %xmm0
vmovdqa 0x50(%rsp), %xmm5
vaesenc %xmm5, %xmm0, %xmm0
vmovdqa 0x40(%rsp), %xmm6
vaesenc %xmm6, %xmm0, %xmm0
vmovdqa 0xf0(%rsp), %xmm7
vaesenc %xmm7, %xmm0, %xmm0
vmovdqa 0xe0(%rsp), %xmm8
vaesenc %xmm8, %xmm0, %xmm0
vmovdqa 0xd0(%rsp), %xmm9
vaesenc %xmm9, %xmm0, %xmm0
vmovdqa 0xc0(%rsp), %xmm10
vaesenc %xmm10, %xmm0, %xmm0
vmovdqa 0xb0(%rsp), %xmm11
vaesenc %xmm11, %xmm0, %xmm0
vmovdqa 0xa0(%rsp), %xmm12
vaesenc %xmm12, %xmm0, %xmm0
vmovdqa 0x90(%rsp), %xmm13
vaesenclast %xmm13, %xmm0, %xmm2
movq 0x18(%rsp), %r14
movabsq $0x1000000000, %r8      # imm = 0x1000000000
movq 0x140(%rsp), %rsi
movq 0x148(%rsp), %rax
testq %rsi, %rsi
vmovq (%rax), %xmm0
vpinsrd $0x2, 0x8(%rax), %xmm0, %xmm0
setne %al
vpslldq $0x1, %xmm0, %xmm0      # xmm0 = zero,xmm0[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14]
movl $0xc0, %ecx
vpinsrb $0x0, %ecx, %xmm0, %xmm0
movw $0xe000, %cx            # imm = 0xE000
kmovd %ecx, %k1
vmovdqu8 0x125c(%rip), %xmm0 {%k1} # 0x3030
testb $0xf, %sil
sete %cl
vpxor %xmm2, %xmm0, %xmm0
testb %cl, %al
jne 0x1df3
vpshufd $0x39, %xmm0, %xmm1     # xmm1 = xmm0[1,2,3,0]
vpshufb 0x124f(%rip), %xmm0, %xmm0 # 0x3040
jmp 0x1e16
vpbroadcastq %xmm0, %xmm1
vpshufd $0x9e, %xmm0, %xmm2     # xmm2 = xmm0[2,3,1,2]
vpxor %xmm2, %xmm1, %xmm1
movq 0x31f0(%rip), %rax      # 0x4ff8
vmovdqa %xmm1, (%rax)
vpxor %xmm2, %xmm2, %xmm2
vpblendd $0xc, %xmm0, %xmm2, %xmm0 # xmm0 = xmm2[0,1],xmm0[2,3]
vpxor %xmm0, %xmm1, %xmm0
movq 0x20(%rsp), %rcx
vpxor %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, %xmm14
movq (%rcx), %r12
testq %r12, %r12
je 0x1e6d
vpshufd $0x39, %xmm0, %xmm1     # xmm1 = xmm0[1,2,3,0]
vpshufb 0x11d5(%rip), %xmm0, %xmm0 # 0x3040
jmp 0x1e7b
vpshufd $0x4e, %xmm0, %xmm1     # xmm1 = xmm0[2,3,0,1]
vpxor %xmm2, %xmm2, %xmm2
vshufps $0x94, %xmm0, %xmm2, %xmm0 # xmm0 = xmm2[0,1],xmm0[1,2]
vpxor %xmm0, %xmm1, %xmm0
vpxor %xmm0, %xmm3, %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
vaesenc %xmm11, %xmm0, %xmm0
vaesenc %xmm12, %xmm0, %xmm0
vaesenclast %xmm13, %xmm0, %xmm6
xorl %ebp, %ebp
vpmovzxbq 0x1152(%rip), %xmm0 # 0x3012
leaq 0x10(%rbp), %rax
cmpq %r12, %rax
jae 0x1fa3
vmovdqa (%rbx,%rbp), %xmm1
vpxor %xmm1, %xmm14, %xmm2
vmovdqa %xmm2, (%r14,%rbp)
vmovdqa (%r15), %xmm2
vmovdqa 0x10(%r15), %xmm3
vmovdqa 0x20(%r15), %xmm4
vmovdqa 0x30(%r15), %xmm5
vpternlogq $0x96, %xmm2, %xmm1, %xmm6
vaesenc %xmm3, %xmm6, %xmm6
vaesenc %xmm4, %xmm6, %xmm6
vaesenc %xmm5, %xmm6, %xmm6
vmovdqa 0x40(%r15), %xmm7
vaesenc %xmm7, %xmm6, %xmm6
vmovdqa 0x50(%r15), %xmm8
vaesenc %xmm8, %xmm6, %xmm6
vmovdqa 0x60(%r15), %xmm9
vaesenc %xmm9, %xmm6, %xmm6
vmovdqa 0x70(%r15), %xmm10
vaesenc %xmm10, %xmm6, %xmm6
vmovdqa 0x80(%r15), %xmm11
vaesenc %xmm11, %xmm6, %xmm6
vmovdqa 0x90(%r15), %xmm12
vaesenc %xmm12, %xmm6, %xmm6
vmovdqa 0xa0(%r15), %xmm13
vaesenclast %xmm13, %xmm6, %xmm6
vpternlogq $0x1e, %xmm0, %xmm1, %xmm2
vaesenc %xmm3, %xmm2, %xmm1
vaesenc %xmm4, %xmm1, %xmm1
vaesenc %xmm5, %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, %xmm14
movq (%rcx), %r12
movq %rax, %rbp
jmp 0x1ec0
subq %rbp, %r12
je 0x20ef
leaq (%rbx,%rbp), %rsi
movq %r12, %r13
shlq $0x20, %r13
movslq %r12d, %rdx
movq %rdx, 0x30(%rsp)
movq %rsp, %rdi
movq %r8, %r14
vmovdqa %xmm6, 0x60(%rsp)
vmovdqa %xmm14, 0x40(%rsp)
callq 0x1080
movq 0x30(%rsp), %rax
leaq (%rsp,%rax), %rdi
subq %r13, %r14
sarq $0x20, %r14
xorl %esi, %esi
movq %r14, %rdx
callq 0x1030
movq %rsp, %rax
vmovaps (%rax), %xmm0
vmovaps %xmm0, 0x50(%rsp)
vxorps 0x40(%rsp), %xmm0, %xmm0
movq 0x18(%rsp), %rdi
addq %rbp, %rdi
vmovaps %xmm0, (%rax)
movq %rsp, %rsi
movq 0x30(%rsp), %rdx
callq 0x1080
vmovdqa 0x60(%rsp), %xmm3
vpbroadcastb %r12d, %xmm0
vmovdqa 0x1020(%rip), %xmm1     # 0x3050
vpsubb %xmm0, %xmm1, %xmm1
vpcmpeqd %xmm2, %xmm2, %xmm2
vpshufb %xmm1, %xmm2, %xmm1
vpand 0x50(%rsp), %xmm1, %xmm1
vmovdqa 0x1015(%rip), %xmm2     # 0x3060
vpsubb %xmm0, %xmm2, %xmm0
vpmovzxbq 0xfba(%rip), %xmm2 # 0x3012
vpshufb %xmm0, %xmm2, %xmm0
vpternlogq $0x96, %xmm0, %xmm1, %xmm3
movq 0x20(%rsp), %rax
testb $0xf, (%rax)
je 0x207e
vpshufd $0x39, %xmm3, %xmm0     # xmm0 = xmm3[1,2,3,0]
vpshufb 0xfc4(%rip), %xmm3, %xmm1 # 0x3040
jmp 0x20a1
vpbroadcastq %xmm3, %xmm0
vpshufd $0x9e, %xmm3, %xmm1     # xmm1 = xmm3[2,3,1,2]
vpxor %xmm1, %xmm0, %xmm0
movq 0x2f65(%rip), %rax      # 0x4ff8
vmovdqa %xmm0, (%rax)
vpxor %xmm1, %xmm1, %xmm1
vpblendd $0xc, %xmm3, %xmm1, %xmm1 # xmm1 = xmm1[0,1],xmm3[2,3]
vpxor %xmm1, %xmm0, %xmm0
vpxor (%r15), %xmm0, %xmm0
vaesenc 0x10(%r15), %xmm0, %xmm0
vaesenc 0x20(%r15), %xmm0, %xmm0
vaesenc 0x30(%r15), %xmm0, %xmm0
vaesenc 0x40(%r15), %xmm0, %xmm0
vaesenc 0x50(%r15), %xmm0, %xmm0
vaesenc 0x60(%r15), %xmm0, %xmm0
vaesenc 0x70(%r15), %xmm0, %xmm0
vaesenc 0x80(%r15), %xmm0, %xmm0
vaesenc 0x90(%r15), %xmm0, %xmm0
vaesenclast 0xa0(%r15), %xmm0, %xmm6
vmovdqa %xmm6, (%rsp)
addq 0x78(%rsp), %rbx
xorl %eax, %eax
pushq $-0x1
popq %rcx
xorl %edx, %edx
cmpq $0x8, %rdx
je 0x2119
movb (%rsp,%rdx), %sil
leaq 0x1(%rdx), %rdi
cmpb (%rbx,%rdx), %sil
movq %rdi, %rdx
je 0x2100
movl %ecx, %eax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop