// File: /workspace/github_c_makefile_star2_O2/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 $0x108, %rsp            # imm = 0x108
movq %r9, %rbp
movq %r8, %rbx
movq %rcx, %r12
movq %rdx, %r15
movq %rdi, %r14
movq 0x148(%rsp), %r13
movq 0x150(%rsp), %rdi
leaq 0x8(%rcx), %rax
movq %rax, (%rsi)
callq 0x1060
movq %rbp, %rsi
vmovq (%r13), %xmm0
vpinsrd $0x2, 0x8(%r13), %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 0x3b87(%rip), %r13      # 0x4ff8
vmovdqa (%r13), %xmm1
vmovdqa 0x10(%r13), %xmm2
vmovdqa 0x20(%r13), %xmm3
vmovdqa 0x30(%r13), %xmm4
vpxor %xmm0, %xmm1, %xmm0
vaesenc %xmm2, %xmm0, %xmm0
vaesenc %xmm3, %xmm0, %xmm0
vaesenc %xmm4, %xmm0, %xmm0
vmovdqa 0x40(%r13), %xmm5
vmovdqa 0x50(%r13), %xmm6
vaesenc %xmm5, %xmm0, %xmm0
vaesenc %xmm6, %xmm0, %xmm0
vmovdqa 0x60(%r13), %xmm7
vmovdqa 0x70(%r13), %xmm8
vaesenc %xmm7, %xmm0, %xmm0
vaesenc %xmm8, %xmm0, %xmm0
vmovdqa 0x80(%r13), %xmm9
vmovdqa 0x90(%r13), %xmm10
vaesenc %xmm9, %xmm0, %xmm0
vaesenc %xmm10, %xmm0, %xmm0
vmovdqa 0xa0(%r13), %xmm11
vaesenclast %xmm11, %xmm0, %xmm0
movq %rbp, %rax
shrq $0x4, %rax
movq %rbx, %rcx
subq $0x1, %rax
jb 0x1541
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 0x14fc
vmovdqa %xmm0, 0xe0(%rsp)
movq %rsi, %rbp
andq $0xf, %rbp
vmovdqa %xmm1, 0x10(%rsp)
vmovdqa %xmm2, (%rsp)
vmovdqa %xmm3, 0xb0(%rsp)
vmovdqa %xmm4, 0xa0(%rsp)
vmovdqa %xmm5, 0x90(%rsp)
vmovdqa %xmm6, 0x80(%rsp)
vmovdqa %xmm7, 0x70(%rsp)
vmovdqa %xmm8, 0x60(%rsp)
vmovdqa %xmm9, 0x50(%rsp)
vmovdqa %xmm10, 0x40(%rsp)
vmovdqa %xmm11, 0x30(%rsp)
je 0x1661
movq %rsi, %rax
andq $-0x10, %rax
addq %rax, %rbx
leaq 0x20(%rsp), %rdi
movq %rsi, 0xc8(%rsp)
movq %rbx, %rsi
movq %rbp, %rdx
vmovdqa %xmm0, 0xf0(%rsp)
callq 0x1090
leaq (%rsp,%rbp), %rdi
addq $0x20, %rdi
pushq $0x10
popq %rdx
subq %rbp, %rdx
xorl %esi, %esi
callq 0x1030
movq 0xc8(%rsp), %rsi
vmovdqa 0x10(%rsp), %xmm0
leaq 0x20(%rsp), %rax
vmovdqa 0xf0(%rsp), %xmm1
vpternlogq $0x96, (%rax), %xmm0, %xmm1
vaesenc (%rsp), %xmm1, %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
vaesenc 0x40(%rsp), %xmm0, %xmm0
vaesenclast 0x30(%rsp), %xmm0, %xmm0
vmovdqa %xmm0, 0xe0(%rsp)
leaq 0xe0(%rsp), %rbx
movq %rbx, %rdi
callq 0x1080
vmovdqa (%rbx), %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 0x197d(%rip), %xmm2     # 0x3000
vpshufb %xmm2, %xmm0, %xmm0
vpxor %xmm0, %xmm1, %xmm0
vmovdqa64 0x10(%rsp), %xmm16
vpxorq %xmm0, %xmm16, %xmm3
vmovdqa (%rsp), %xmm4
vaesenc %xmm4, %xmm3, %xmm3
vmovdqa 0xb0(%rsp), %xmm5
vaesenc %xmm5, %xmm3, %xmm3
vmovdqa 0xa0(%rsp), %xmm6
vaesenc %xmm6, %xmm3, %xmm3
vmovdqa 0x90(%rsp), %xmm7
vaesenc %xmm7, %xmm3, %xmm3
vmovdqa 0x80(%rsp), %xmm8
vaesenc %xmm8, %xmm3, %xmm3
vmovdqa 0x70(%rsp), %xmm9
vaesenc %xmm9, %xmm3, %xmm3
vmovdqa 0x60(%rsp), %xmm10
vaesenc %xmm10, %xmm3, %xmm3
vmovdqa 0x50(%rsp), %xmm11
vaesenc %xmm11, %xmm3, %xmm3
vmovdqa 0x40(%rsp), %xmm12
vaesenc %xmm12, %xmm3, %xmm3
vmovdqa 0x30(%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 %xmm2, %xmm1, %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
movq %r12, %rbx
shlq $0x20, %rbx
xorl %ebp, %ebp
vpmovzxbq 0x18d7(%rip), %xmm0 # 0x3040
movabsq $-0x1000000000, %rax    # imm = 0xFFFFFFF000000000
vmovdqa64 %xmm16, %xmm3
leaq 0x10(%rbp), %rcx
cmpq %r12, %rcx
jae 0x185e
vpxor (%r15,%rbp), %xmm15, %xmm1
vmovdqa %xmm1, (%r14,%rbp)
vmovdqa (%r13), %xmm3
vmovdqa 0x10(%r13), %xmm4
vmovdqa 0x20(%r13), %xmm5
vmovdqa 0x30(%r13), %xmm6
vmovdqa %xmm3, %xmm2
vpternlogq $0x1e, %xmm0, %xmm1, %xmm2
vaesenc %xmm4, %xmm2, %xmm2
vaesenc %xmm5, %xmm2, %xmm2
vaesenc %xmm6, %xmm2, %xmm2
vmovdqa 0x40(%r13), %xmm7
vaesenc %xmm7, %xmm2, %xmm2
vmovdqa 0x50(%r13), %xmm8
vaesenc %xmm8, %xmm2, %xmm2
vmovdqa 0x60(%r13), %xmm9
vaesenc %xmm9, %xmm2, %xmm2
vmovdqa 0x70(%r13), %xmm10
vaesenc %xmm10, %xmm2, %xmm2
vmovdqa 0x80(%r13), %xmm11
vaesenc %xmm11, %xmm2, %xmm2
vmovdqa 0x90(%r13), %xmm12
vaesenc %xmm12, %xmm2, %xmm2
vmovdqa 0xa0(%r13), %xmm13
vaesenclast %xmm13, %xmm2, %xmm15
vpternlogq $0x96, %xmm1, %xmm3, %xmm14
vaesenc %xmm4, %xmm14, %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
addq %rax, %rbx
movq %rcx, %rbp
jmp 0x1779
vmovdqa %xmm14, 0xd0(%rsp)
cmpq %rbp, %r12
je 0x1994
addq %rbp, %r15
movq %rbx, %rax
sarq $0x20, %rax
leaq 0x20(%rsp), %rdi
movq %r15, %rsi
movq %rax, %r15
movq %rax, %rdx
vmovdqa %xmm14, 0x10(%rsp)
vmovdqa %xmm15, (%rsp)
callq 0x1090
leaq (%rsp,%r15), %rdi
addq $0x20, %rdi
movabsq $0x1000000000, %rdx     # imm = 0x1000000000
subq %rbx, %rdx
sarq $0x20, %rdx
xorl %esi, %esi
callq 0x1030
leaq 0x20(%rsp), %rax
vmovaps (%rsp), %xmm0
vxorps (%rax), %xmm0, %xmm0
vmovaps %xmm0, (%rsp)
leaq (%r14,%rbp), %rdi
vmovaps %xmm0, (%rax)
leaq 0x20(%rsp), %rsi
movq %r15, %rdx
callq 0x1090
movl %r12d, %eax
subl %ebp, %eax
vpbroadcastb %eax, %xmm0
vmovdqa 0x171d(%rip), %xmm1     # 0x3010
vpsubb %xmm0, %xmm1, %xmm0
vpcmpeqd %xmm1, %xmm1, %xmm1
vpshufb %xmm0, %xmm1, %xmm0
vmovdqa (%r13), %xmm3
vmovdqa 0x10(%r13), %xmm4
vmovdqa 0x20(%r13), %xmm5
vpand (%rsp), %xmm0, %xmm0
vmovdqa 0x30(%r13), %xmm6
vpternlogq $0x96, 0x10(%rsp), %xmm3, %xmm0
vaesenc %xmm4, %xmm0, %xmm0
vaesenc %xmm5, %xmm0, %xmm0
vaesenc %xmm6, %xmm0, %xmm0
vmovdqa 0x40(%r13), %xmm7
vaesenc %xmm7, %xmm0, %xmm0
vmovdqa 0x50(%r13), %xmm8
vaesenc %xmm8, %xmm0, %xmm0
vmovdqa 0x60(%r13), %xmm9
vaesenc %xmm9, %xmm0, %xmm0
vmovdqa 0x70(%r13), %xmm10
vaesenc %xmm10, %xmm0, %xmm0
vmovdqa 0x80(%r13), %xmm11
vaesenc %xmm11, %xmm0, %xmm0
vmovdqa 0x90(%r13), %xmm12
vaesenc %xmm12, %xmm0, %xmm0
vmovdqa 0xa0(%r13), %xmm13
vaesenclast %xmm13, %xmm0, %xmm0
vmovdqa %xmm0, 0xd0(%rsp)
vmovdqa %xmm3, 0x10(%rsp)
vmovdqa %xmm4, (%rsp)
vmovdqa %xmm5, 0xb0(%rsp)
vmovdqa %xmm6, 0xa0(%rsp)
vmovdqa %xmm7, 0x90(%rsp)
vmovdqa %xmm8, 0x80(%rsp)
vmovdqa %xmm9, 0x70(%rsp)
vmovdqa %xmm10, 0x60(%rsp)
vmovdqa %xmm11, 0x50(%rsp)
vmovdqa %xmm12, 0x40(%rsp)
vmovdqa %xmm13, 0x30(%rsp)
leaq 0xd0(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x1080
vmovdqa (%r15), %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 0x15f8(%rip), %xmm0, %xmm0 # 0x3000
vpternlogq $0x96, 0x10(%rsp), %xmm1, %xmm0
vaesenc (%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
vaesenc 0x40(%rsp), %xmm0, %xmm0
vaesenclast 0x30(%rsp), %xmm0, %xmm0
vmovq %xmm0, (%r14,%r12)
xorl %eax, %eax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq