// File: /workspace/github_c_makefile_star2_O3/mjosaarinen[P]brutus/crypto_aead/aes128n8t8clocv2/ref/cloc.c
// Function: ae_encrypt
int ae_encrypt(ae_cxt* cxt, byte* pt, unsigned long long mlen, byte* ct, byte* tag, unsigned long long tlen, int enc_dec)
{
	cxt->pt = pt;
	cxt->ptlen = mlen;
	cxt->ct = ct;
	cxt->ctlen = mlen;
	cxt->tag = tag;
	cxt->tlen = tlen;

	byte* es = cxt->es;
	byte* ts = cxt->ts;
	AES_KEY *ekey = cxt->pt_ekey;
    
    u32* wd = (u32*) ts;
    if(mlen != 0){
        pstate2("g2: ", ts, STATE_LEN);
    	g2(wd[0], wd[1], wd[2], wd[3]);
        pstate2("", ts, STATE_LEN);
        AES_encrypt(cxt->ts, cxt->ts, ekey);
    }
    else{
        pstate2("g1: ", ts, STATE_LEN);
        g1(wd[0], wd[1], wd[2], wd[3]);
        pstate2("", ts, STATE_LEN);
        AES_encrypt(cxt->ts, cxt->ts, ekey);
        memcpy(tag, ts, tlen);
        return SUCCESS;
    }
    

    
	unsigned long long pc = 0;
	while((pc + STATE_LEN) < mlen){
        if(enc_dec == ENC){ // encryption
            xor_block((block*)es, (block*)((byte*)(pt+pc)));
            memcpy(ct+pc, es, STATE_LEN);
        }
        else{ // decryption
            xor_bytes2(pt+pc, ct+pc, es, STATE_LEN);
            memcpy(es, ct+pc, STATE_LEN);
        }
		xor_block((block*)ts, (block*)es);
		AES_encrypt(ts, ts, ekey);
		/* apply fix1 */
		es[0] |= 0x80;
		AES_encrypt(es, es, ekey);
		pc += STATE_LEN;
	}

	/* process the last block */
	unsigned long long lastblocklen = mlen - pc;
    if(enc_dec == ENC){ // encryption
        xor_bytes(es, pt+pc, lastblocklen);
        memcpy(ct+pc, es, lastblocklen);
    }
    else{ // decryption
        xor_bytes2(pt+pc, ct+pc, es, lastblocklen);
        memcpy(es, ct+pc, lastblocklen);
    }
	xor_bytes(ts, es, lastblocklen);
	if(lastblocklen != STATE_LEN){ // apply f2
        /* apply padding only when last message block is not full */
        ts[lastblocklen] ^= 0x80;
		u32* wd = (u32*) ts;
        pstate2("f2: ", ts, STATE_LEN);
		f2(wd[0], wd[1], wd[2], wd[3]);
        pstate2("", ts, STATE_LEN);
	}
	else{ // apply f1
		u32* wd = (u32*) ts;
        pstate2("f1: ", ts, STATE_LEN);
		f1(wd[0], wd[1], wd[2], wd[3]);
        pstate2("", ts, STATE_LEN);
	}

	AES_encrypt(ts, ts, ekey);
	memcpy(tag, ts, tlen);

	return SUCCESS;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r9, %r14
movq %rdi, %r13
movq %rsi, 0x150(%rdi)
movq %rdx, 0x158(%rdi)
movq %rcx, 0x160(%rdi)
movq %rdx, 0x168(%rdi)
movq %r8, 0x170(%rdi)
movl %r14d, 0x178(%rdi)
leaq 0x10(%rdi), %r15
movq 0x118(%rdi), %r12
testq %rdx, %rdx
movq %r8, 0x20(%rsp)
je 0x345b
movq %rcx, %rbx
movq %rdx, %rbp
movq %rsi, 0x8(%rsp)
movq %r14, 0x18(%rsp)
leaq 0x34dc(%rip), %rdi      # 0x6846
movq %r15, %rsi
movl $0x10, %edx
callq 0x10f0
movq 0x14(%r13), %rax
movl 0x10(%r13), %ecx
xorl %eax, %ecx
movq %rax, 0x10(%r13)
movl 0x1c(%r13), %eax
movl %eax, 0x18(%r13)
movl %ecx, 0x1c(%r13)
leaq 0x354b(%rip), %rdi      # 0x68e3
movq %r15, %rsi
movl $0x10, %edx
callq 0x10f0
movq %r15, %rdi
movq %r15, %rsi
movq %r12, 0x10(%rsp)
movq %r12, %rdx
callq 0x1070
cmpq $0x11, %rbp
jb 0x349d
xorl %r12d, %r12d
movq 0x8(%rsp), %rsi
leaq (%rsi,%r12), %rax
cmpl $0x1, 0x60(%rsp)
jne 0x33ee
movq %r13, %rdi
movq %rax, %rsi
callq 0x10c0
vmovdqu (%r13), %xmm0
vmovdqu %xmm0, (%rbx,%r12)
jmp 0x340e
leaq (%rbx,%r12), %rsi
movq %rax, %rdi
movq %r13, %rdx
movl $0x10, %ecx
callq 0x1100
vmovdqu (%rbx,%r12), %xmm0
vmovdqu %xmm0, (%r13)
movq %r15, %rdi
movq %r13, %rsi
callq 0x10c0
movq %r15, %rdi
movq %r15, %rsi
movq 0x10(%rsp), %r14
movq %r14, %rdx
callq 0x1070
orb $-0x80, (%r13)
movq %r13, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x1070
leaq 0x10(%r12), %r14
addq $0x20, %r12
cmpq %rbp, %r12
movq %r14, %r12
movq 0x8(%rsp), %rsi
jb 0x33ca
jmp 0x34a5
leaq 0x33e9(%rip), %rdi      # 0x684b
movq %r15, %rsi
movl $0x10, %edx
callq 0x10f0
vmovq 0x10(%r13), %xmm0
vmovq 0x18(%r13), %xmm1
vpbroadcastq 0x33ac(%rip), %xmm2 # 0x6830
vpermi2d %xmm1, %xmm0, %xmm2
vpxor %xmm2, %xmm0, %xmm0
vpunpcklqdq %xmm0, %xmm1, %xmm0 # xmm0 = xmm1[0],xmm0[0]
vmovdqu %xmm0, 0x10(%r13)
jmp 0x3577
xorl %r14d, %r14d
movq 0x8(%rsp), %rsi
subq %r14, %rbp
addq %r14, %rsi
cmpl $0x1, 0x60(%rsp)
jne 0x34c7
movq %r13, %rdi
movl %ebp, %edx
callq 0x10d0
addq %r14, %rbx
movq %rbx, %rdi
movq %r13, %rsi
jmp 0x34e0
addq %r14, %rbx
movq %rsi, %rdi
movq %rbx, %rsi
movq %r13, %rdx
movl %ebp, %ecx
callq 0x1100
movq %r13, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x10e0
movq %r15, %rdi
movq %r13, %rsi
movl %ebp, %edx
callq 0x10d0
cmpq $0x10, %rbp
movq 0x18(%rsp), %r14
movq 0x10(%rsp), %r12
jne 0x3543
leaq 0x3335(%rip), %rdi      # 0x6841
movq %r15, %rsi
movl $0x10, %edx
callq 0x10f0
movl 0x14(%r13), %eax
movl 0x18(%r13), %ecx
movl 0x10(%r13), %edx
xorl %ecx, %edx
movl %edx, 0x10(%r13)
movl 0x1c(%r13), %esi
xorl %eax, %esi
movl %esi, 0x14(%r13)
xorl %ecx, %esi
movl %esi, 0x1c(%r13)
xorl %eax, %edx
movl %edx, 0x18(%r13)
jmp 0x3577
xorb $-0x80, 0x10(%r13,%rbp)
leaq 0x32ec(%rip), %rdi      # 0x683c
movq %r15, %rsi
movl $0x10, %edx
callq 0x10f0
movq 0x14(%r13), %rax
movl 0x10(%r13), %ecx
xorl %eax, %ecx
movq %rax, 0x10(%r13)
movl 0x1c(%r13), %eax
movl %eax, 0x18(%r13)
movl %ecx, 0x1c(%r13)
leaq 0x3365(%rip), %rdi      # 0x68e3
movq %r15, %rsi
movl $0x10, %edx
callq 0x10f0
movq %r15, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x1070
movq 0x20(%rsp), %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x10e0
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop