// File: /workspace/github_c_makefile_star2_O3/mjosaarinen[P]brutus/crypto_aead/aes128n12t8silcv2/aesni/encrypt.c
// Function: crypto_aead_encrypt
int crypto_aead_encrypt(
                        unsigned char *c, unsigned long long *clen,         // ciphertext
                        const unsigned char *m, unsigned long long mlen,    // message
                        const unsigned char *ad, unsigned long long adlen,  // associated data
                        const unsigned char *nsec,                          // not relavent to CLOC or SLIC
                        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();
    /* set ciphertext length */
    *clen = mlen + 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(m+i);
        estate = XOR(estate, tmp);
        STORE(c+i, estate);
        tstate = XOR(estate, tstate);
        fix1(estate);
        print_state("after applying fix1\n", estate);
        AES128_encrypt(estate, estate);
        AES128_encrypt(tstate, tstate);
        i += STATE_LEN;
    }
    
    /* process the last block of the message/ciphetext */
    lastblocklen = mlen - i;
    
    if(lastblocklen > 0){
        load_partial_block(&tmp, m+i, lastblocklen, ZERO_APPEND);
        estate = XOR(estate, tmp);
        print_state("after xoring last partial message block\n", estate);
        store_partial_block(c+i, estate, lastblocklen);
        unsigned char shift_bytes = (STATE_LEN - (unsigned char)lastblocklen);
        tmp = AND(SHR(_mm_set1_epi8(0xff), shift_bytes), estate);
        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);
    
    /* output the tag */
    store_partial_block(c+i+lastblocklen, tstate, CRYPTO_ABYTES);
    
    return RETURN_SUCCESS;
    
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %r9, %r12
movq %r8, %rbx
movq %rcx, %r13
movq %rdx, %r15
movq %rdi, 0x18(%rsp)
movq 0x128(%rsp), %r14
movq 0x130(%rsp), %rdi
leaq 0x8(%rcx), %rax
movq %rax, (%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 0x3b9d(%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, %r12
jae 0x14e5
xorl %eax, %eax
jmp 0x1538
movq %r12, %rax
shrq $0x4, %rax
movq %rbx, %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 0x14ef
movq %r12, %rax
andq $-0x10, %rax
movq %r12, %rbp
andq $0xf, %rbp
je 0x1659
addq %rax, %rbx
movq %rsp, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
vmovdqa %xmm4, 0x30(%rsp)
vmovdqa %xmm5, 0x20(%rsp)
vmovdqa %xmm6, 0xd0(%rsp)
vmovdqa %xmm7, 0xc0(%rsp)
vmovdqa %xmm8, 0xb0(%rsp)
vmovdqa %xmm9, 0xa0(%rsp)
vmovdqa %xmm10, 0x90(%rsp)
vmovdqa %xmm11, 0x80(%rsp)
vmovdqa %xmm12, 0x70(%rsp)
vmovdqa %xmm13, 0x60(%rsp)
vmovdqa %xmm14, 0x50(%rsp)
vmovdqa %xmm1, 0x40(%rsp)
callq 0x1080
leaq (%rsp,%rbp), %rdi
movl $0x10, %edx
subq %rbp, %rdx
xorl %esi, %esi
callq 0x1030
vmovdqa 0x50(%rsp), %xmm14
vmovdqa 0x60(%rsp), %xmm13
vmovdqa 0x70(%rsp), %xmm12
vmovdqa 0x80(%rsp), %xmm11
vmovdqa 0x90(%rsp), %xmm10
vmovdqa 0xa0(%rsp), %xmm9
vmovdqa 0xb0(%rsp), %xmm8
vmovdqa 0xc0(%rsp), %xmm7
vmovdqa 0xd0(%rsp), %xmm6
vmovdqa 0x20(%rsp), %xmm5
vmovdqa 0x30(%rsp), %xmm4
movq %rsp, %rax
vmovdqa 0x40(%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
movq 0x18(%rsp), %rcx
vpxor %xmm0, %xmm0, %xmm0
vmovdqa %xmm0, (%rsp)
movl $0x8, %eax
movb %r12b, 0x7(%rsp,%rax)
shrq $0x8, %r12
decq %rax
jne 0x166c
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 0x1973(%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
cmpq $0x11, %r13
jb 0x1806
xorl %eax, %eax
vpmovzxbq 0x191b(%rip), %xmm0 # 0x3040
vpxor (%r15,%rax), %xmm15, %xmm1
vmovdqa %xmm1, (%rcx,%rax)
vmovdqa (%r14), %xmm4
vmovdqa 0x10(%r14), %xmm5
vmovdqa 0x20(%r14), %xmm6
vmovdqa 0x30(%r14), %xmm7
vmovdqa %xmm4, %xmm2
vpternlogq $0x1e, %xmm0, %xmm1, %xmm2
vaesenc %xmm5, %xmm2, %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, %xmm15
vpternlogq $0x96, %xmm1, %xmm4, %xmm3
vaesenc %xmm5, %xmm3, %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, %xmm3
leaq 0x10(%rax), %r12
addq $0x20, %rax
cmpq %r13, %rax
movq %r12, %rax
jb 0x1725
jmp 0x1809
xorl %r12d, %r12d
movq %r13, %rbx
subq %r12, %rbx
je 0x191e
addq %r12, %r15
movslq %ebx, %rbp
movq %rsp, %rdi
movq %r15, %rsi
movq %rbp, %rdx
vmovdqa %xmm3, 0x30(%rsp)
vmovdqa %xmm15, 0x20(%rsp)
callq 0x1080
leaq (%rsp,%rbp), %rdi
movl $0x10, %eax
subl %ebp, %eax
movslq %eax, %rdx
xorl %esi, %esi
callq 0x1030
movq %rsp, %rax
vmovaps 0x20(%rsp), %xmm0
vxorps (%rax), %xmm0, %xmm0
vmovaps %xmm0, 0x20(%rsp)
addq 0x18(%rsp), %r12
vmovaps %xmm0, (%rax)
movq %rsp, %rsi
movq %r12, %rdi
movq %rbp, %rdx
callq 0x1080
vpbroadcastb %ebx, %xmm0
vmovdqa 0x179e(%rip), %xmm1     # 0x3020
vpsubb %xmm0, %xmm1, %xmm0
vpcmpeqd %xmm1, %xmm1, %xmm1
vpshufb %xmm0, %xmm1, %xmm0
vmovdqa (%r14), %xmm4
vmovdqa 0x10(%r14), %xmm5
vpand 0x20(%rsp), %xmm0, %xmm0
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
vaesenc %xmm8, %xmm0, %xmm0
vmovdqa 0x50(%r14), %xmm9
vaesenc %xmm9, %xmm0, %xmm0
vmovdqa 0x60(%r14), %xmm10
vaesenc %xmm10, %xmm0, %xmm0
vmovdqa 0x70(%r14), %xmm11
vaesenc %xmm11, %xmm0, %xmm0
vmovdqa 0x80(%r14), %xmm12
vaesenc %xmm12, %xmm0, %xmm0
vmovdqa 0x90(%r14), %xmm13
vaesenc %xmm13, %xmm0, %xmm0
vmovdqa 0xa0(%r14), %xmm14
vaesenclast %xmm14, %xmm0, %xmm3
vpxor %xmm0, %xmm0, %xmm0
vmovdqa %xmm0, (%rsp)
movl $0x8, %eax
movq %r13, %rcx
movb %cl, 0x7(%rsp,%rax)
shrq $0x8, %rcx
decq %rax
jne 0x192f
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 0x16b0(%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
movq 0x18(%rsp), %rax
vmovq %xmm0, (%rax,%r13)
xorl %eax, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq