// File: /workspace/github_c_makefile_star2_O2/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 $0x28, %rsp
movq %r9, %rbx
movq %r8, %r14
movq %rdi, %r13
movq %rsi, 0x40(%rdi)
movq %rdx, 0x48(%rdi)
movq %rcx, 0x8(%rsp)
movq %rcx, 0x50(%rdi)
movq %rdx, 0x58(%rdi)
movq %r8, 0x60(%rdi)
movl %ebx, 0x68(%rdi)
leaq 0x8(%rdi), %r15
movzwl 0x8(%rdi), %eax
testq %rdx, %rdx
je 0x153c
movq %rsi, 0x10(%rsp)
movq %r14, 0x18(%rsp)
movq %rbx, 0x20(%rsp)
movq %rdx, (%rsp)
movl 0xa(%r13), %ecx
xorl %ecx, %eax
movl %ecx, 0x8(%r13)
movzwl 0xe(%r13), %ecx
movw %cx, 0xc(%r13)
movw %ax, 0xe(%r13)
movq %r15, %rdi
movq %r15, %rsi
callq 0x1110
movq (%rsp), %rax
leaq (%rax,%r13), %rbx
addq $0x8, %rbx
xorl %r14d, %r14d
pushq $0x8
popq %rbp
leaq 0x8(%r14), %r12
cmpq %rax, %r12
movq 0x10(%rsp), %rsi
jae 0x150a
addq %r14, %rsi
cmpl $0x1, 0x60(%rsp)
jne 0x14b1
movq %r13, %rdi
movl %ebp, %edx
callq 0x1100
addq 0x8(%rsp), %r14
movq %r13, %rax
jmp 0x14cc
addq 0x8(%rsp), %r14
movq %rsi, %rdi
movq %r14, %rsi
movq %r13, %rdx
movl %ebp, %ecx
callq 0x1130
movq %r14, %rax
movq %r13, %r14
movq (%rax), %rax
movq %rax, (%r14)
movq %r15, %rdi
movq %r13, %rsi
movl %ebp, %edx
callq 0x1100
movq %r15, %rdi
movq %r15, %rsi
callq 0x1110
orb $-0x80, (%r13)
movq %r13, %rdi
movq %r13, %rsi
callq 0x1110
addq $-0x8, %rbx
movq %r12, %r14
movq (%rsp), %rax
jmp 0x1485
movq %rax, %rbp
subq %r14, %rbp
leaq -0x8(%rax), %r12
addq %r14, %rsi
cmpl $0x1, 0x60(%rsp)
jne 0x155c
subl %r14d, %eax
movq %rax, (%rsp)
movq %r13, %rdi
movl %eax, %edx
callq 0x1100
movq 0x8(%rsp), %rdi
addq %r14, %rdi
movq %r13, %rsi
jmp 0x158b
movzwl 0xa(%r13), %ecx
xorl %ecx, %eax
movl 0xc(%r13), %edx
movw %ax, 0xc(%r13)
movl %edx, 0x8(%r13)
xorl %edx, %ecx
movw %cx, 0xe(%r13)
jmp 0x1601
movq %r12, 0x10(%rsp)
movq 0x8(%rsp), %r12
addq %r14, %r12
subl %r14d, %eax
movq %rsi, %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %rax, (%rsp)
movl %eax, %ecx
callq 0x1130
movq %r13, %rdi
movq %r12, %rsi
movq 0x10(%rsp), %r12
movq %rbp, %rdx
callq 0x1120
movq %r15, %rdi
movq %r13, %rsi
movq (%rsp), %rdx
callq 0x1100
cmpq %r14, %r12
jne 0x15c9
movzwl 0xc(%r13), %eax
movzwl 0x8(%r13), %ecx
xorw %ax, %cx
movzwl 0xa(%r13), %edi
movzwl 0xe(%r13), %edx
xorw %di, %dx
xorl %edx, %eax
movl %ecx, %esi
xorl %edi, %esi
jmp 0x15e3
xorb $-0x80, (%rbx)
movzwl 0xa(%r13), %ecx
movzwl 0xc(%r13), %edx
movzwl 0x8(%r13), %eax
xorw %cx, %ax
movzwl 0xe(%r13), %esi
movq 0x20(%rsp), %rbx
movq 0x18(%rsp), %r14
movw %cx, 0x8(%r13)
movw %dx, 0xa(%r13)
movw %ax, 0xe(%r13)
movw %si, 0xc(%r13)
movq %r15, %rdi
movq %r15, %rsi
callq 0x1110
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x1120
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop