// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/aes128n8t8silcv2/ref/silc.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;


	unsigned long long pc = 0;
	while((pc + STATE_LEN) < mlen){
		if(enc_dec == ENC){ // encryption
			xor_bytes(es, pt+pc, STATE_LEN);
			pstate2("After xoring message block:", es);
			memcpy(ct+pc, es, STATE_LEN);
		}
		else{ // decryption
			xor_bytes2(pt+pc, ct+pc, es, STATE_LEN);
			pstate2("After xoring ciphertext block:", es);
			memcpy(es, ct+pc, STATE_LEN);
		}
		xor_bytes(ts, es, STATE_LEN);
		pstate2("tag state:", ts);
		pstate2("Enc:", NULL);
		AES_encrypt(ts, ts, ekey);
		/* apply fix1 */
		es[0] |= 0x80;
		pstate2("After applying fix1:", es);
		pstate2("Enc:", NULL);
		AES_encrypt(es, es, ekey);
		pc += STATE_LEN;
	}

	/* process the last block */
	unsigned long long lastblocklen = mlen - pc;

	if(lastblocklen > 0){
		if(enc_dec == ENC){ // encryption
			xor_bytes(es, pt+pc, lastblocklen);
			pstate2("After xoring last partial message block:", es);
			memcpy(ct+pc, es, lastblocklen);
		}
		else{ // decryption
			xor_bytes2(pt+pc, ct+pc, es, lastblocklen);
			pstate2("After xoring last partial ciphertext block:", es);
			memcpy(es, ct+pc, lastblocklen);
		}
		xor_bytes(ts, es, lastblocklen);
		pstate2("tag state:", ts);
		pstate2("Enc:", NULL);
		AES_encrypt(ts, ts, ekey);
	}
	/* encode the adlen and process */
	unsigned long long t_adlen = mlen;
	unsigned long long i;
	for(i = 0; i < sizeof(unsigned long long); i++) {
		ts[STATE_LEN-1-i] ^= t_adlen & 0xff;
		t_adlen >>= 8;
	}
	pstate2("After xoring the legnth of message:", ts);
	g(ts);
	pstate2("After applying g:", ts);
	pstate2("Enc:", NULL);
	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 %rdx, %rbp
movq %rsi, %r12
movq %rdi, %r13
movq %rsi, 0x150(%rdi)
movq %rdx, 0x158(%rdi)
movq %rcx, 0x8(%rsp)
movq %rcx, 0x160(%rdi)
movq %rdx, 0x168(%rdi)
movq %r8, 0x28(%rsp)
movq %r8, 0x170(%rdi)
movq %r9, 0x30(%rsp)
movl %r9d, 0x178(%rdi)
leaq 0x10(%rdi), %rax
movq %rax, 0x10(%rsp)
movq 0x118(%rdi), %rax
movq %rax, 0x18(%rsp)
xorl %ebx, %ebx
movq %rsi, 0x20(%rsp)
leaq 0x10(%rbx), %r14
cmpq %rbp, %r14
jae 0x354f
leaq (%r12,%rbx), %rax
cmpl $0x1, 0x70(%rsp)
jne 0x349a
movq %r13, %rdi
movq %rax, %rsi
pushq $0x10
popq %r15
movl %r15d, %edx
callq 0x10b0
leaq 0x3477(%rip), %rdi      # 0x68f7
movq %r13, %rsi
callq 0x10d0
vmovups (%r13), %xmm0
movq 0x8(%rsp), %rax
vmovups %xmm0, (%rax,%rbx)
jmp 0x34d5
movq 0x8(%rsp), %r12
leaq (%r12,%rbx), %rsi
movq %rax, %rdi
movq %r13, %rdx
pushq $0x10
popq %r15
movl %r15d, %ecx
callq 0x10e0
leaq 0x3457(%rip), %rdi      # 0x6913
movq %r13, %rsi
callq 0x10d0
vmovups (%r12,%rbx), %xmm0
movq 0x20(%rsp), %r12
vmovups %xmm0, (%r13)
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
movl %r15d, %edx
callq 0x10b0
leaq 0x33fd(%rip), %rdi      # 0x68ec
movq %rbx, %rsi
callq 0x10d0
leaq 0x3362(%rip), %r15      # 0x6860
movq %r15, %rdi
xorl %esi, %esi
callq 0x10d0
movq %rbx, %rdi
movq %rbx, %rsi
movq 0x18(%rsp), %rbx
movq %rbx, %rdx
callq 0x1060
orb $-0x80, (%r13)
leaq 0x340b(%rip), %rdi      # 0x6932
movq %r13, %rsi
callq 0x10d0
movq %r15, %rdi
xorl %esi, %esi
callq 0x10d0
movq %r13, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x1060
movq %r14, %rbx
jmp 0x344f
movq %rbp, %r12
subq %rbx, %r12
je 0x3613
movq 0x20(%rsp), %rsi
addq %rbx, %rsi
cmpl $0x1, 0x70(%rsp)
jne 0x3597
movl %ebp, %r15d
subl %ebx, %r15d
movq %r13, %rdi
movl %r15d, %edx
callq 0x10b0
leaq 0x33c5(%rip), %rdi      # 0x6947
movq %r13, %rsi
callq 0x10d0
movq 0x8(%rsp), %rdi
addq %rbx, %rdi
movq %r13, %rsi
jmp 0x35cb
movq 0x8(%rsp), %r14
addq %rbx, %r14
movl %ebp, %r15d
subl %ebx, %r15d
movq %rsi, %rdi
movq %r14, %rsi
movq %r13, %rdx
movl %r15d, %ecx
callq 0x10e0
leaq 0x33b3(%rip), %rdi      # 0x6970
movq %r13, %rsi
callq 0x10d0
movq %r13, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x10c0
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
movl %r15d, %edx
callq 0x10b0
leaq 0x32ff(%rip), %rdi      # 0x68ec
movq %rbx, %rsi
callq 0x10d0
leaq 0x3264(%rip), %rdi      # 0x6860
xorl %esi, %esi
callq 0x10d0
movq %rbx, %rdi
movq %rbx, %rsi
movq 0x18(%rsp), %rdx
callq 0x1060
xorl %eax, %eax
cmpq $-0x8, %rax
je 0x3629
xorb %bpl, 0x1f(%r13,%rax)
shrq $0x8, %rbp
decq %rax
jmp 0x3615
leaq 0x336c(%rip), %rdi      # 0x699c
movq 0x10(%rsp), %rsi
callq 0x10d0
movb 0x10(%r13), %al
movb 0x11(%r13), %cl
xorl %edx, %edx
cmpq $0xf, %rdx
je 0x3659
movb 0x11(%r13,%rdx), %sil
movb %sil, 0x10(%r13,%rdx)
incq %rdx
jmp 0x3644
xorb %al, %cl
movb %cl, 0x1f(%r13)
leaq 0x3260(%rip), %rdi      # 0x68c6
movq 0x10(%rsp), %rbx
movq %rbx, %rsi
callq 0x10d0
leaq 0x31e6(%rip), %rdi      # 0x6860
xorl %esi, %esi
callq 0x10d0
movq %rbx, %rdi
movq %rbx, %rsi
movq 0x18(%rsp), %rdx
callq 0x1060
movq 0x28(%rsp), %rdi
movq %rbx, %rsi
movq 0x30(%rsp), %rdx
callq 0x10c0
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq