// File: /workspace/github_c_makefile_star2_O1/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 $0x28, %rsp
movq %rdx, %rbp
movq %rdi, %r13
movq %rsi, 0x150(%rdi)
movq %rdx, 0x158(%rdi)
movq %rcx, (%rsp)
movq %rcx, 0x160(%rdi)
movq %rdx, 0x168(%rdi)
movq %r8, 0x18(%rsp)
movq %r8, 0x170(%rdi)
movq %r9, 0x20(%rsp)
movl %r9d, 0x178(%rdi)
leaq 0x10(%rdi), %rax
movq %rax, 0x8(%rsp)
movq 0x118(%rdi), %rax
movq %rax, 0x10(%rsp)
cmpq $0x11, %rdx
jb 0x3566
xorl %r12d, %r12d
movq %rsi, %rbx
leaq (%rsi,%r12), %rax
cmpl $0x1, 0x60(%rsp)
jne 0x34a5
movq %r13, %rdi
movq %rax, %rsi
movl $0x10, %edx
callq 0x10b0
leaq 0x346c(%rip), %rdi      # 0x68f7
movq %r13, %rsi
callq 0x10d0
vmovups (%r13), %xmm0
movq (%rsp), %rax
vmovups %xmm0, (%rax,%r12)
jmp 0x34d8
movq (%rsp), %r14
leaq (%r14,%r12), %rsi
movq %rax, %rdi
movq %r13, %rdx
movl $0x10, %ecx
callq 0x10e0
leaq 0x344f(%rip), %rdi      # 0x6913
movq %r13, %rsi
callq 0x10d0
vmovups (%r14,%r12), %xmm0
vmovups %xmm0, (%r13)
movq 0x8(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
movl $0x10, %edx
callq 0x10b0
leaq 0x33f8(%rip), %rdi      # 0x68ec
movq %r14, %rsi
callq 0x10d0
leaq 0x335d(%rip), %r15      # 0x6860
movq %r15, %rdi
xorl %esi, %esi
callq 0x10d0
movq %r14, %rdi
movq %r14, %rsi
movq 0x10(%rsp), %r14
movq %r14, %rdx
callq 0x1060
orb $-0x80, (%r13)
leaq 0x3406(%rip), %rdi      # 0x6932
movq %r13, %rsi
callq 0x10d0
movq %r15, %rdi
xorl %esi, %esi
callq 0x10d0
movq %r13, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x1060
leaq 0x10(%r12), %r14
addq $0x20, %r12
cmpq %rbp, %r12
movq %r14, %r12
movq %rbx, %rsi
jb 0x3466
jmp 0x3569
xorl %r14d, %r14d
movq %rbp, %r12
subq %r14, %r12
je 0x361a
addq %r14, %rsi
cmpl $0x1, 0x60(%rsp)
jne 0x35a5
movq %r13, %rdi
movl %r12d, %edx
callq 0x10b0
leaq 0x33b6(%rip), %rdi      # 0x6947
movq %r13, %rsi
callq 0x10d0
movq (%rsp), %rdi
addq %r14, %rdi
movq %r13, %rsi
jmp 0x35d2
movq (%rsp), %rbx
addq %r14, %rbx
movq %rsi, %rdi
movq %rbx, %rsi
movq %r13, %rdx
movl %r12d, %ecx
callq 0x10e0
leaq 0x33ac(%rip), %rdi      # 0x6970
movq %r13, %rsi
callq 0x10d0
movq %r13, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x10c0
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
movl %r12d, %edx
callq 0x10b0
leaq 0x32f8(%rip), %rdi      # 0x68ec
movq %rbx, %rsi
callq 0x10d0
leaq 0x325d(%rip), %rdi      # 0x6860
xorl %esi, %esi
callq 0x10d0
movq %rbx, %rdi
movq %rbx, %rsi
movq 0x10(%rsp), %rdx
callq 0x1060
xorl %eax, %eax
xorb %bpl, 0x1f(%r13,%rax)
shrq $0x8, %rbp
decq %rax
cmpq $-0x8, %rax
jne 0x361c
leaq 0x3367(%rip), %rdi      # 0x699c
movq 0x8(%rsp), %rbx
movq %rbx, %rsi
callq 0x10d0
movb 0x11(%r13), %al
movq 0x11(%r13), %rcx
movl 0x19(%r13), %edx
movzwl 0x1d(%r13), %esi
xorb 0x10(%r13), %al
movb 0x1f(%r13), %dil
movb %dil, 0xe(%rbx)
movw %si, 0xc(%rbx)
movl %edx, 0x8(%rbx)
movq %rcx, (%rbx)
movb %al, 0x1f(%r13)
leaq 0x3252(%rip), %rdi      # 0x68c6
movq %rbx, %rsi
callq 0x10d0
leaq 0x31dd(%rip), %rdi      # 0x6860
xorl %esi, %esi
callq 0x10d0
movq %rbx, %rdi
movq %rbx, %rsi
movq 0x10(%rsp), %rdx
callq 0x1060
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
movq 0x20(%rsp), %rdx
callq 0x10c0
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)