// File: /workspace/github_c_makefile_star2_O3/mjosaarinen[P]brutus/crypto_aead/twine80n6t4clocv2/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;

	word* wd = (word*) ts;
	if(mlen != 0){
		g2(wd[0], wd[1], wd[2], wd[3]);
		Encode(cxt->ts, cxt->ts);
	}
	else{
		g1(wd[0], wd[1], wd[2], wd[3]);
		Encode(cxt->ts, cxt->ts);
		memcpy(tag, ts, tlen);
		return SUCCESS;
	}


	unsigned long long pc = 0;
	while((pc + STATE_LEN) < mlen){
		if(enc_dec == ENC){ // encryption
			xor_bytes(es, pt+pc, STATE_LEN);
			memcpy(ct+pc, es, STATE_LEN);
		}
		else{ // decryption
			xor_bytes2(pt+pc, ct+pc, es, STATE_LEN);
			memcpy(es, ct+pc, STATE_LEN);
		}
		xor_bytes(ts, es, STATE_LEN);
		Encode(ts, ts);
		/* apply fix1 */
		es[0] |= 0x80;
		Encode(es, es);
		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;
		word* wd = (word*) ts;
		f2(wd[0], wd[1], wd[2], wd[3]);
	}
	else{ // apply f1
		word* wd = (word*) ts;
		f1(wd[0], wd[1], wd[2], wd[3]);
	}

	Encode(ts, ts);
	memcpy(tag, ts, tlen);

	return SUCCESS;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %r9, %r12
movq %r8, %rbp
movq %rdi, %r14
movq %rsi, 0x40(%rdi)
movq %rdx, 0x48(%rdi)
movq %rcx, (%rsp)
movq %rcx, 0x50(%rdi)
movq %rdx, 0x58(%rdi)
movq %r8, 0x60(%rdi)
movl %r12d, 0x68(%rdi)
leaq 0x8(%rdi), %r15
movzwl 0x8(%rdi), %eax
testq %rdx, %rdx
je 0x1522
movq %rdx, %r13
movq %rsi, %rbx
movq %rbp, 0x8(%rsp)
movq %r12, 0x10(%rsp)
movl 0xa(%r14), %ecx
xorl %ecx, %eax
movl %ecx, 0x8(%r14)
movzwl 0xe(%r14), %ecx
movw %cx, 0xc(%r14)
movw %ax, 0xe(%r14)
movq %r15, %rdi
movq %r15, %rsi
callq 0x1110
cmpq $0x9, %r13
jb 0x1542
xorl %ebp, %ebp
leaq (%rbx,%rbp), %rsi
cmpl $0x1, 0x50(%rsp)
jne 0x14bd
movq %r14, %rdi
movl $0x8, %edx
callq 0x1100
movq (%rsp), %rax
addq %rbp, %rax
movq %r14, %r12
jmp 0x14db
movq (%rsp), %rax
leaq (%rax,%rbp), %r12
movq %rsi, %rdi
movq %r12, %rsi
movq %r14, %rdx
movl $0x8, %ecx
callq 0x1130
movq %r14, %rax
movq (%r12), %rcx
movq %rcx, (%rax)
movq %r15, %rdi
movq %r14, %rsi
movl $0x8, %edx
callq 0x1100
movq %r15, %rdi
movq %r15, %rsi
callq 0x1110
orb $-0x80, (%r14)
movq %r14, %rdi
movq %r14, %rsi
callq 0x1110
leaq 0x8(%rbp), %r12
addq $0x10, %rbp
cmpq %r13, %rbp
movq %r12, %rbp
jb 0x1499
jmp 0x1545
movzwl 0xa(%r14), %ecx
xorl %ecx, %eax
movl 0xc(%r14), %edx
movw %ax, 0xc(%r14)
movl %edx, 0x8(%r14)
xorl %edx, %ecx
movw %cx, 0xe(%r14)
jmp 0x1603
xorl %r12d, %r12d
subq %r12, %r13
addq %r12, %rbx
cmpl $0x1, 0x50(%rsp)
jne 0x156c
movq %r14, %rdi
movq %rbx, %rsi
movl %r13d, %edx
callq 0x1100
movq (%rsp), %rdi
addq %r12, %rdi
movq %r14, %rsi
jmp 0x158a
movq (%rsp), %rbp
addq %r12, %rbp
movq %rbx, %rdi
movq %rbp, %rsi
movq %r14, %rdx
movl %r13d, %ecx
callq 0x1130
movq %r14, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x1120
movq 0x8(%rsp), %rbp
movq %r15, %rdi
movq %r14, %rsi
movl %r13d, %edx
callq 0x1100
cmpq $0x8, %r13
movq 0x10(%rsp), %r12
jne 0x15d2
movzwl 0xc(%r14), %eax
movzwl 0x8(%r14), %ecx
xorw %ax, %cx
movzwl 0xa(%r14), %edi
movzwl 0xe(%r14), %edx
xorw %di, %dx
xorl %edx, %eax
movl %ecx, %esi
xorl %edi, %esi
jmp 0x15ef
xorb $-0x80, 0x8(%r14,%r13)
movzwl 0xa(%r14), %ecx
movzwl 0xc(%r14), %edx
movzwl 0x8(%r14), %eax
xorw %cx, %ax
movzwl 0xe(%r14), %esi
movw %cx, 0x8(%r14)
movw %dx, 0xa(%r14)
movw %ax, 0xe(%r14)
movw %si, 0xc(%r14)
movq %r15, %rdi
movq %r15, %rsi
callq 0x1110
movq %rbp, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x1120
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)