// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x78, %rsp
movl 0x80(%rsp), %eax
movq %rdi, 0x70(%rsp)
movq %rsi, 0x68(%rsp)
movq %rdx, 0x60(%rsp)
movq %rcx, 0x58(%rsp)
movq %r8, 0x50(%rsp)
movq %r9, 0x48(%rsp)
movq 0x68(%rsp), %rcx
movq 0x70(%rsp), %rax
movq %rcx, 0x150(%rax)
movq 0x60(%rsp), %rcx
movq 0x70(%rsp), %rax
movq %rcx, 0x158(%rax)
movq 0x58(%rsp), %rcx
movq 0x70(%rsp), %rax
movq %rcx, 0x160(%rax)
movq 0x60(%rsp), %rcx
movq 0x70(%rsp), %rax
movq %rcx, 0x168(%rax)
movq 0x50(%rsp), %rcx
movq 0x70(%rsp), %rax
movq %rcx, 0x170(%rax)
movq 0x48(%rsp), %rax
movl %eax, %ecx
movq 0x70(%rsp), %rax
movl %ecx, 0x178(%rax)
movq 0x70(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x70(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x38(%rsp)
movq 0x70(%rsp), %rax
movq 0x118(%rax), %rax
movq %rax, 0x30(%rsp)
movq $0x0, 0x28(%rsp)
movq 0x28(%rsp), %rax
addq $0x10, %rax
cmpq 0x60(%rsp), %rax
jae 0x5e2f
cmpl $0x1, 0x80(%rsp)
jne 0x5d3b
movq 0x40(%rsp), %rdi
movq 0x68(%rsp), %rsi
addq 0x28(%rsp), %rsi
movl $0x10, %edx
callq 0x10d0
movq 0x40(%rsp), %rsi
leaq 0x3be2(%rip), %rdi      # 0x98f7
callq 0x10f0
movq 0x58(%rsp), %rax
movq 0x28(%rsp), %rcx
movq 0x40(%rsp), %rdx
movq (%rdx), %rsi
movq %rsi, (%rax,%rcx)
movq 0x8(%rdx), %rdx
movq %rdx, 0x8(%rax,%rcx)
jmp 0x5d8e
movq 0x68(%rsp), %rdi
addq 0x28(%rsp), %rdi
movq 0x58(%rsp), %rsi
addq 0x28(%rsp), %rsi
movq 0x40(%rsp), %rdx
movl $0x10, %ecx
callq 0x1100
movq 0x40(%rsp), %rsi
leaq 0x3ba9(%rip), %rdi      # 0x9913
callq 0x10f0
movq 0x40(%rsp), %rax
movq 0x58(%rsp), %rcx
movq 0x28(%rsp), %rdx
movq (%rcx,%rdx), %rsi
movq %rsi, (%rax)
movq 0x8(%rcx,%rdx), %rcx
movq %rcx, 0x8(%rax)
movq 0x38(%rsp), %rdi
movq 0x40(%rsp), %rsi
movl $0x10, %edx
callq 0x10d0
movq 0x38(%rsp), %rsi
leaq 0x3b3e(%rip), %rdi      # 0x98ec
callq 0x10f0
leaq 0x3aa6(%rip), %rdi      # 0x9860
xorl %eax, %eax
movl %eax, %esi
callq 0x10f0
movq 0x38(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq 0x30(%rsp), %rdx
callq 0x1070
movq 0x40(%rsp), %rax
movzbl (%rax), %ecx
orl $0x80, %ecx
movb %cl, (%rax)
movq 0x40(%rsp), %rsi
leaq 0x3b3f(%rip), %rdi      # 0x9932
callq 0x10f0
leaq 0x3a61(%rip), %rdi      # 0x9860
xorl %eax, %eax
movl %eax, %esi
callq 0x10f0
movq 0x40(%rsp), %rdi
movq 0x40(%rsp), %rsi
movq 0x30(%rsp), %rdx
callq 0x1070
movq 0x28(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x28(%rsp)
jmp 0x5cd2
movq 0x60(%rsp), %rax
subq 0x28(%rsp), %rax
movq %rax, 0x20(%rsp)
cmpq $0x0, 0x20(%rsp)
jbe 0x5f35
cmpl $0x1, 0x80(%rsp)
jne 0x5e9b
movq 0x40(%rsp), %rdi
movq 0x68(%rsp), %rsi
addq 0x28(%rsp), %rsi
movq 0x20(%rsp), %rax
movl %eax, %edx
callq 0x10d0
movq 0x40(%rsp), %rsi
leaq 0x3acc(%rip), %rdi      # 0x9947
callq 0x10f0
movq 0x58(%rsp), %rdi
addq 0x28(%rsp), %rdi
movq 0x40(%rsp), %rsi
movq 0x20(%rsp), %rdx
callq 0x10e0
jmp 0x5eea
movq 0x68(%rsp), %rdi
addq 0x28(%rsp), %rdi
movq 0x58(%rsp), %rsi
addq 0x28(%rsp), %rsi
movq 0x40(%rsp), %rdx
movq 0x20(%rsp), %rax
movl %eax, %ecx
callq 0x1100
movq 0x40(%rsp), %rsi
leaq 0x3aa4(%rip), %rdi      # 0x9970
callq 0x10f0
movq 0x40(%rsp), %rdi
movq 0x58(%rsp), %rsi
addq 0x28(%rsp), %rsi
movq 0x20(%rsp), %rdx
callq 0x10e0
movq 0x38(%rsp), %rdi
movq 0x40(%rsp), %rsi
movq 0x20(%rsp), %rax
movl %eax, %edx
callq 0x10d0
movq 0x38(%rsp), %rsi
leaq 0x39e0(%rip), %rdi      # 0x98ec
callq 0x10f0
leaq 0x3948(%rip), %rdi      # 0x9860
xorl %eax, %eax
movl %eax, %esi
callq 0x10f0
movq 0x38(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq 0x30(%rsp), %rdx
callq 0x1070
movq 0x60(%rsp), %rax
movq %rax, 0x18(%rsp)
movq $0x0, 0x10(%rsp)
cmpq $0x8, 0x10(%rsp)
jae 0x5f93
movq 0x18(%rsp), %rsi
andq $0xff, %rsi
movq 0x38(%rsp), %rax
movl $0xf, %ecx
subq 0x10(%rsp), %rcx
movzbl (%rax,%rcx), %edx
xorq %rsi, %rdx
movb %dl, (%rax,%rcx)
movq 0x18(%rsp), %rax
shrq $0x8, %rax
movq %rax, 0x18(%rsp)
movq 0x10(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x10(%rsp)
jmp 0x5f48
movq 0x38(%rsp), %rsi
leaq 0x39fd(%rip), %rdi      # 0x999c
callq 0x10f0
movq 0x38(%rsp), %rax
movzbl (%rax), %eax
movq 0x38(%rsp), %rcx
movzbl 0x1(%rcx), %ecx
xorl %ecx, %eax
movb %al, 0xb(%rsp)
movl $0x0, 0xc(%rsp)
cmpl $0xf, 0xc(%rsp)
jge 0x5ff6
movq 0x38(%rsp), %rax
movl 0xc(%rsp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %dl
movq 0x38(%rsp), %rax
movslq 0xc(%rsp), %rcx
movb %dl, (%rax,%rcx)
movl 0xc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc(%rsp)
jmp 0x5fc3
movb 0xb(%rsp), %dl
movq 0x38(%rsp), %rax
movslq 0xc(%rsp), %rcx
movb %dl, (%rax,%rcx)
movq 0x38(%rsp), %rsi
leaq 0x38b3(%rip), %rdi      # 0x98c6
callq 0x10f0
leaq 0x3841(%rip), %rdi      # 0x9860
xorl %eax, %eax
movl %eax, %esi
callq 0x10f0
movq 0x38(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq 0x30(%rsp), %rdx
callq 0x1070
movq 0x50(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq 0x48(%rsp), %rdx
callq 0x10e0
xorl %eax, %eax
addq $0x78, %rsp
retq
nopw (%rax,%rax)