// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x98, %rsp
movl 0xa0(%rsp), %eax
movq %rdi, 0x88(%rsp)
movq %rsi, 0x80(%rsp)
movq %rdx, 0x78(%rsp)
movq %rcx, 0x70(%rsp)
movq %r8, 0x68(%rsp)
movq %r9, 0x60(%rsp)
movq 0x80(%rsp), %rcx
movq 0x88(%rsp), %rax
movq %rcx, 0x150(%rax)
movq 0x78(%rsp), %rcx
movq 0x88(%rsp), %rax
movq %rcx, 0x158(%rax)
movq 0x70(%rsp), %rcx
movq 0x88(%rsp), %rax
movq %rcx, 0x160(%rax)
movq 0x78(%rsp), %rcx
movq 0x88(%rsp), %rax
movq %rcx, 0x168(%rax)
movq 0x68(%rsp), %rcx
movq 0x88(%rsp), %rax
movq %rcx, 0x170(%rax)
movq 0x60(%rsp), %rax
movl %eax, %ecx
movq 0x88(%rsp), %rax
movl %ecx, 0x178(%rax)
movq 0x88(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0x88(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x50(%rsp)
movq 0x88(%rsp), %rax
movq 0x118(%rax), %rax
movq %rax, 0x48(%rsp)
movq 0x50(%rsp), %rax
movq %rax, 0x40(%rsp)
cmpq $0x0, 0x78(%rsp)
je 0x5b97
movq 0x50(%rsp), %rsi
leaq 0x3d3c(%rip), %rdi      # 0x983e
movl $0x10, %edx
callq 0x1100
movq 0x40(%rsp), %rax
movl (%rax), %eax
movq 0x40(%rsp), %rcx
xorl 0x4(%rcx), %eax
movl %eax, 0x3c(%rsp)
movq 0x40(%rsp), %rax
movl 0x4(%rax), %ecx
movq 0x40(%rsp), %rax
movl %ecx, (%rax)
movq 0x40(%rsp), %rax
movl 0x8(%rax), %ecx
movq 0x40(%rsp), %rax
movl %ecx, 0x4(%rax)
movq 0x40(%rsp), %rax
movl 0xc(%rax), %ecx
movq 0x40(%rsp), %rax
movl %ecx, 0x8(%rax)
movl 0x3c(%rsp), %ecx
movq 0x40(%rsp), %rax
movl %ecx, 0xc(%rax)
movq 0x50(%rsp), %rsi
leaq 0x3d75(%rip), %rdi      # 0x98db
movl $0x10, %edx
callq 0x1100
movq 0x88(%rsp), %rdi
addq $0x10, %rdi
movq 0x88(%rsp), %rsi
addq $0x10, %rsi
movq 0x48(%rsp), %rdx
callq 0x1070
jmp 0x5c66
movq 0x50(%rsp), %rsi
leaq 0x3ca0(%rip), %rdi      # 0x9843
movl $0x10, %edx
callq 0x1100
movq 0x40(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x38(%rsp)
movq 0x40(%rsp), %rax
movl 0x8(%rax), %ecx
movq 0x40(%rsp), %rax
movl %ecx, (%rax)
movl 0x38(%rsp), %ecx
movq 0x40(%rsp), %rax
xorl 0x4(%rax), %ecx
movq 0x40(%rsp), %rax
movl %ecx, 0x8(%rax)
movq 0x40(%rsp), %rax
movl 0x4(%rax), %eax
movl %eax, 0x38(%rsp)
movq 0x40(%rsp), %rax
movl 0xc(%rax), %ecx
movq 0x40(%rsp), %rax
movl %ecx, 0x4(%rax)
movq 0x40(%rsp), %rax
movl (%rax), %ecx
xorl 0x38(%rsp), %ecx
movq 0x40(%rsp), %rax
movl %ecx, 0xc(%rax)
movq 0x50(%rsp), %rsi
leaq 0x3cc5(%rip), %rdi      # 0x98db
movl $0x10, %edx
callq 0x1100
movq 0x88(%rsp), %rdi
addq $0x10, %rdi
movq 0x88(%rsp), %rsi
addq $0x10, %rsi
movq 0x48(%rsp), %rdx
callq 0x1070
movq 0x68(%rsp), %rdi
movq 0x50(%rsp), %rsi
movq 0x60(%rsp), %rdx
callq 0x10f0
movl $0x0, 0x94(%rsp)
jmp 0x5f7d
movq $0x0, 0x30(%rsp)
movq 0x30(%rsp), %rax
addq $0x10, %rax
cmpq 0x78(%rsp), %rax
jae 0x5d64
cmpl $0x1, 0xa0(%rsp)
jne 0x5cc5
movq 0x58(%rsp), %rdi
movq 0x80(%rsp), %rsi
addq 0x30(%rsp), %rsi
callq 0x10d0
movq 0x70(%rsp), %rax
movq 0x30(%rsp), %rcx
movq 0x58(%rsp), %rdx
movq (%rdx), %rsi
movq %rsi, (%rax,%rcx)
movq 0x8(%rdx), %rdx
movq %rdx, 0x8(%rax,%rcx)
jmp 0x5d0a
movq 0x80(%rsp), %rdi
addq 0x30(%rsp), %rdi
movq 0x70(%rsp), %rsi
addq 0x30(%rsp), %rsi
movq 0x58(%rsp), %rdx
movl $0x10, %ecx
callq 0x1110
movq 0x58(%rsp), %rax
movq 0x70(%rsp), %rcx
movq 0x30(%rsp), %rdx
movq (%rcx,%rdx), %rsi
movq %rsi, (%rax)
movq 0x8(%rcx,%rdx), %rcx
movq %rcx, 0x8(%rax)
movq 0x50(%rsp), %rdi
movq 0x58(%rsp), %rsi
callq 0x10d0
movq 0x50(%rsp), %rdi
movq 0x50(%rsp), %rsi
movq 0x48(%rsp), %rdx
callq 0x1070
movq 0x58(%rsp), %rax
movzbl (%rax), %ecx
orl $0x80, %ecx
movb %cl, (%rax)
movq 0x58(%rsp), %rdi
movq 0x58(%rsp), %rsi
movq 0x48(%rsp), %rdx
callq 0x1070
movq 0x30(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x30(%rsp)
jmp 0x5c6f
movq 0x78(%rsp), %rax
subq 0x30(%rsp), %rax
movq %rax, 0x28(%rsp)
cmpl $0x1, 0xa0(%rsp)
jne 0x5db6
movq 0x58(%rsp), %rdi
movq 0x80(%rsp), %rsi
addq 0x30(%rsp), %rsi
movq 0x28(%rsp), %rax
movl %eax, %edx
callq 0x10e0
movq 0x70(%rsp), %rdi
addq 0x30(%rsp), %rdi
movq 0x58(%rsp), %rsi
movq 0x28(%rsp), %rdx
callq 0x10f0
jmp 0x5df7
movq 0x80(%rsp), %rdi
addq 0x30(%rsp), %rdi
movq 0x70(%rsp), %rsi
addq 0x30(%rsp), %rsi
movq 0x58(%rsp), %rdx
movq 0x28(%rsp), %rax
movl %eax, %ecx
callq 0x1110
movq 0x58(%rsp), %rdi
movq 0x70(%rsp), %rsi
addq 0x30(%rsp), %rsi
movq 0x28(%rsp), %rdx
callq 0x10f0
movq 0x50(%rsp), %rdi
movq 0x58(%rsp), %rsi
movq 0x28(%rsp), %rax
movl %eax, %edx
callq 0x10e0
cmpq $0x10, 0x28(%rsp)
je 0x5eb9
movq 0x50(%rsp), %rax
movq 0x28(%rsp), %rcx
movzbl (%rax,%rcx), %edx
xorl $0x80, %edx
movb %dl, (%rax,%rcx)
movq 0x50(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x50(%rsp), %rsi
leaq 0x39ee(%rip), %rdi      # 0x9834
movl $0x10, %edx
callq 0x1100
movq 0x20(%rsp), %rax
movl (%rax), %eax
movq 0x20(%rsp), %rcx
xorl 0x4(%rcx), %eax
movl %eax, 0x1c(%rsp)
movq 0x20(%rsp), %rax
movl 0x4(%rax), %ecx
movq 0x20(%rsp), %rax
movl %ecx, (%rax)
movq 0x20(%rsp), %rax
movl 0x8(%rax), %ecx
movq 0x20(%rsp), %rax
movl %ecx, 0x4(%rax)
movq 0x20(%rsp), %rax
movl 0xc(%rax), %ecx
movq 0x20(%rsp), %rax
movl %ecx, 0x8(%rax)
movl 0x1c(%rsp), %ecx
movq 0x20(%rsp), %rax
movl %ecx, 0xc(%rax)
movq 0x50(%rsp), %rsi
leaq 0x3a31(%rip), %rdi      # 0x98db
movl $0x10, %edx
callq 0x1100
jmp 0x5f4a
movq 0x50(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x50(%rsp), %rsi
leaq 0x396a(%rip), %rdi      # 0x9839
movl $0x10, %edx
callq 0x1100
movq 0x10(%rsp), %rax
movl 0x8(%rax), %ecx
movq 0x10(%rsp), %rax
xorl (%rax), %ecx
movl %ecx, (%rax)
movq 0x10(%rsp), %rax
movl 0x4(%rax), %eax
movl %eax, 0xc(%rsp)
movq 0x10(%rsp), %rax
movl 0xc(%rax), %ecx
movq 0x10(%rsp), %rax
xorl 0x4(%rax), %ecx
movl %ecx, 0x4(%rax)
movq 0x10(%rsp), %rax
movl 0x8(%rax), %ecx
movq 0x10(%rsp), %rax
xorl 0x4(%rax), %ecx
movq 0x10(%rsp), %rax
movl %ecx, 0xc(%rax)
movl 0xc(%rsp), %ecx
movq 0x10(%rsp), %rax
xorl (%rax), %ecx
movq 0x10(%rsp), %rax
movl %ecx, 0x8(%rax)
movq 0x50(%rsp), %rsi
leaq 0x399b(%rip), %rdi      # 0x98db
movl $0x10, %edx
callq 0x1100
movq 0x50(%rsp), %rdi
movq 0x50(%rsp), %rsi
movq 0x48(%rsp), %rdx
callq 0x1070
movq 0x68(%rsp), %rdi
movq 0x50(%rsp), %rsi
movq 0x60(%rsp), %rdx
callq 0x10f0
movl $0x0, 0x94(%rsp)
movl 0x94(%rsp), %eax
addq $0x98, %rsp
retq
nopl (%rax)