// File: /workspace/github_c_makefile_star2_O2/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 $0x38, %rsp
movq %r9, %r14
movq %r8, %r12
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), %rbp
testq %rdx, %rdx
je 0x345c
movq %rcx, %rbx
movq %rdx, %rax
movq %rsi, 0x30(%rsp)
movq %r12, 0x20(%rsp)
movq %r14, 0x28(%rsp)
leaq 0x351d(%rip), %rdi      # 0x6846
pushq $0x10
popq %r14
movq %r15, %rsi
movl %r14d, %edx
movq %rax, 0x8(%rsp)
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 0x3585(%rip), %rdi      # 0x68e3
movq %r15, %rsi
movl %r14d, %edx
callq 0x10f0
movq %r15, %rdi
movq %r15, %rsi
movq %rbp, 0x10(%rsp)
movq %rbp, %rdx
callq 0x1070
movq 0x8(%rsp), %rax
leaq (%rax,%r13), %r14
addq $0x10, %r14
xorl %r12d, %r12d
leaq 0x10(%r12), %rbp
cmpq %rax, %rbp
movq 0x30(%rsp), %rsi
jae 0x3423
leaq (%rsi,%r12), %rax
cmpl $0x1, 0x70(%rsp)
jne 0x33c3
movq %r13, %rdi
movq %rax, %rsi
callq 0x10c0
vmovdqu (%r13), %xmm0
vmovdqu %xmm0, (%rbx,%r12)
jmp 0x33e1
leaq (%rbx,%r12), %rsi
movq %rax, %rdi
movq %r13, %rdx
pushq $0x10
popq %rcx
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), %r12
movq %r12, %rdx
callq 0x1070
orb $-0x80, (%r13)
movq %r13, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x1070
addq $-0x10, %r14
movq %rbp, %r12
movq 0x8(%rsp), %rax
jmp 0x338c
movq %rax, %rcx
subq %r12, %rcx
movq %rcx, 0x18(%rsp)
leaq -0x10(%rax), %rcx
movq %rcx, 0x8(%rsp)
addq %r12, %rsi
cmpl $0x1, 0x70(%rsp)
jne 0x349c
subl %r12d, %eax
movq %r13, %rdi
movl %eax, %edx
movq %rax, %rbp
callq 0x10d0
addq %r12, %rbx
movq %rbx, %rdi
movq %r13, %rsi
jmp 0x34bb
leaq 0x33e8(%rip), %rdi      # 0x684b
pushq $0x10
popq %rdx
movq %r15, %rsi
callq 0x10f0
vmovq 0x10(%r13), %xmm0
vmovq 0x18(%r13), %xmm1
vpbroadcastq 0x33ad(%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 0x3557
addq %r12, %rbx
subl %r12d, %eax
movq %rsi, %rdi
movq %rbx, %rsi
movq %r13, %rdx
movq %rax, %rbp
movl %ebp, %ecx
callq 0x1100
movq %r13, %rdi
movq %rbx, %rsi
movq 0x18(%rsp), %rdx
callq 0x10e0
movq 0x10(%rsp), %rbx
movq %r15, %rdi
movq %r13, %rsi
movl %ebp, %edx
callq 0x10d0
cmpq %r12, 0x8(%rsp)
jne 0x351a
leaq 0x335c(%rip), %rdi      # 0x6841
pushq $0x10
popq %rdx
movq %r15, %rsi
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 0x354a
xorb $-0x80, (%r14)
leaq 0x3317(%rip), %rdi      # 0x683c
pushq $0x10
popq %rdx
movq %r15, %rsi
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)
movq 0x28(%rsp), %r14
movq 0x20(%rsp), %r12
movq %rbx, %rbp
leaq 0x3385(%rip), %rdi      # 0x68e3
pushq $0x10
popq %rdx
movq %r15, %rsi
callq 0x10f0
movq %r15, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x1070
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x10e0
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop