// File: /workspace/github_c_makefile_star2_O1/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 %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 %r9d, 0x178(%rdi)
leaq 0x10(%rdi), %r15
movq 0x118(%rdi), %r14
testq %rdx, %rdx
movq %r9, 0x20(%rsp)
je 0x3441
movq %rcx, %rbx
movq %rdx, %rbp
movq %rsi, 0x8(%rsp)
movq %r12, 0x18(%rsp)
leaq 0x34f5(%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 0x3564(%rip), %rdi      # 0x68e3
movq %r15, %rsi
movl $0x10, %edx
callq 0x10f0
movq %r15, %rdi
movq %r15, %rsi
movq %r14, 0x10(%rsp)
movq %r14, %rdx
callq 0x1070
cmpq $0x11, %rbp
jb 0x3483
xorl %r14d, %r14d
movq 0x8(%rsp), %rsi
leaq (%rsi,%r14), %rax
cmpl $0x1, 0x60(%rsp)
jne 0x33d5
movq %r13, %rdi
movq %rax, %rsi
callq 0x10c0
vmovdqu (%r13), %xmm0
vmovdqu %xmm0, (%rbx,%r14)
jmp 0x33f5
leaq (%rbx,%r14), %rsi
movq %rax, %rdi
movq %r13, %rdx
movl $0x10, %ecx
callq 0x1100
vmovdqu (%rbx,%r14), %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
leaq 0x10(%r14), %r12
addq $0x20, %r14
cmpq %rbp, %r14
movq %r12, %r14
movq 0x8(%rsp), %rsi
jb 0x33b1
jmp 0x348b
leaq 0x3403(%rip), %rdi      # 0x684b
movq %r15, %rsi
movl $0x10, %edx
callq 0x10f0
vmovq 0x10(%r13), %xmm0
vmovq 0x18(%r13), %xmm1
vpbroadcastq 0x33c6(%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 0x355d
xorl %r12d, %r12d
movq 0x8(%rsp), %rsi
subq %r12, %rbp
addq %r12, %rsi
cmpl $0x1, 0x60(%rsp)
jne 0x34ad
movq %r13, %rdi
movl %ebp, %edx
callq 0x10d0
addq %r12, %rbx
movq %rbx, %rdi
movq %r13, %rsi
jmp 0x34c6
addq %r12, %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), %r12
movq 0x10(%rsp), %r14
jne 0x3529
leaq 0x334f(%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 0x355d
xorb $-0x80, 0x10(%r13,%rbp)
leaq 0x3306(%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 0x337f(%rip), %rdi      # 0x68e3
movq %r15, %rsi
movl $0x10, %edx
callq 0x10f0
movq %r15, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x1070
movq %r12, %rdi
movq %r15, %rsi
movq 0x20(%rsp), %rdx
callq 0x10e0
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq