// File: /workspace/github_c_makefile_star2_O3/mjosaarinen[P]brutus/crypto_aead/aes128n8t8silcv2/ref/silc.c
// Function: process_ad
int process_ad(ae_cxt* cxt, const byte* ad, unsigned long long adlen, const byte* nonce, unsigned long long nlen)
{
	byte* state = cxt->es;
	AES_KEY *ekey = cxt->pt_ekey;

	cxt->ad = (byte*) ad;
	cxt->adlen = adlen;
	cxt->nonce = (byte*) nonce;
	cxt->nlen = nlen;

	/* process the nonce */
	memset(state, 0, STATE_LEN);
	state[STATE_LEN-nlen-1] = PARAM;
	memcpy(state+STATE_LEN-nlen, nonce, nlen);
	//xor_bytes(state, nonce, nlen);	// xor to the last nlen bytes
	pstate2("processing nonce:", NULL);
	AES_encrypt(state, state, ekey);

	/* process the middle normal blocks of ad */
	unsigned long long i;
	for(i = 0; i < (unsigned long long)adlen/STATE_LEN; i++){
		xor_bytes(state, ad+STATE_LEN*i, STATE_LEN);
		pstate2("After xoring associated data:", state);
		pstate2("Enc:", NULL);
		AES_encrypt(state, state, ekey);
	}
	/* process the last block partial block if any */
	unsigned long long lastblocklen = adlen % STATE_LEN;
	if(lastblocklen){
		xor_bytes(state, ad+i*STATE_LEN, lastblocklen);
		pstate2("After processing last partial associated data block:", state);
		pstate2("Enc:", NULL);
		AES_encrypt(state, state, ekey);
	}

	/* encode the adlen and process */
	unsigned long long t_adlen = adlen;
	for(i = 0; i < sizeof(unsigned long long); i++) {
		state[STATE_LEN-1-i] ^= t_adlen & 0xff;
		t_adlen >>= 8;
	}
	pstate2("After xoring the length of associated data:", state);

	g(state);
	pstate2("After applying g:", state);
	memcpy(cxt->ts, state, STATE_LEN);
	pstate2("Enc:", NULL);
	AES_encrypt(state, state, ekey);

	g(cxt->ts);
	pstate2("After applying g to tag state:", cxt->ts);
	pstate2("Enc:", NULL);
	AES_encrypt(cxt->ts, cxt->ts, ekey);
	return SUCCESS;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, %rbx
movq %rsi, %rbp
movq %rdi, %r15
movq 0x118(%rdi), %r12
movq %rsi, 0x130(%rdi)
movq %rdx, 0x138(%rdi)
movq %rcx, 0x140(%rdi)
movq %r8, 0x148(%rdi)
vxorps %xmm0, %xmm0, %xmm0
vmovups %xmm0, (%rdi)
movq %rdi, %rax
subq %r8, %rax
movb $-0x30, 0xf(%rax)
leaq 0x10(%rdi), %r13
movq %r13, %rdi
subq %r8, %rdi
movq %rcx, %rsi
movq %r8, %rdx
callq 0x10c0
leaq 0x35cd(%rip), %rdi      # 0x6830
xorl %r14d, %r14d
xorl %esi, %esi
callq 0x10d0
movq %r15, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x1060
cmpq $0x10, %rbx
jb 0x32f3
movq %r13, 0x10(%rsp)
movq %rbp, %r14
movq %r12, %rbp
movq %rbx, %r12
shrq $0x4, %r12
leaq 0x35c6(%rip), %r13      # 0x6860
movq %r14, 0x8(%rsp)
movq %r15, %rdi
movq %r14, %rsi
movl $0x10, %edx
callq 0x10b0
leaq 0x358c(%rip), %rdi      # 0x6842
movq %r15, %rsi
callq 0x10d0
movq %r13, %rdi
xorl %esi, %esi
callq 0x10d0
movq %r15, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x1060
addq $0x10, %r14
decq %r12
jne 0x329f
movq %rbx, %r14
andq $-0x10, %r14
movq %rbp, %r12
movq 0x10(%rsp), %r13
movq 0x8(%rsp), %rbp
movq %rbx, %rdx
andq $0xf, %rdx
je 0x3335
addq %r14, %rbp
movq %r15, %rdi
movq %rbp, %rsi
callq 0x10b0
leaq 0x3554(%rip), %rdi      # 0x6865
movq %r15, %rsi
callq 0x10d0
leaq 0x3540(%rip), %rdi      # 0x6860
xorl %esi, %esi
callq 0x10d0
movq %r15, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x1060
movl $0xf, %eax
xorb %bl, (%r15,%rax)
shrq $0x8, %rbx
decq %rax
cmpq $0x7, %rax
jne 0x333a
leaq 0x3548(%rip), %rdi      # 0x689a
movq %r15, %rsi
callq 0x10d0
movb 0x1(%r15), %al
movq 0x1(%r15), %rcx
movl 0x9(%r15), %edx
movl %edx, 0x8(%r15)
movzwl 0xd(%r15), %edx
movw %dx, 0xc(%r15)
movb 0xf(%r15), %dl
movb %dl, 0xe(%r15)
xorb (%r15), %al
movq %rcx, (%r15)
movb %al, 0xf(%r15)
leaq 0x3539(%rip), %rdi      # 0x68c6
movq %r15, %rsi
callq 0x10d0
vmovups (%r15), %xmm0
vmovups %xmm0, (%r13)
leaq 0x34b9(%rip), %r14      # 0x6860
movq %r14, %rdi
xorl %esi, %esi
callq 0x10d0
movq %r15, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x1060
movb 0x11(%r15), %al
movq 0x11(%r15), %rcx
movl 0x19(%r15), %edx
movzwl 0x1d(%r15), %esi
xorb 0x10(%r15), %al
movb 0x1f(%r15), %dil
movb %dil, 0xe(%r13)
movw %si, 0xc(%r13)
movl %edx, 0x8(%r13)
movq %rcx, (%r13)
movb %al, 0x1f(%r15)
leaq 0x34e4(%rip), %rdi      # 0x68d8
movq %r13, %rsi
callq 0x10d0
movq %r14, %rdi
xorl %esi, %esi
callq 0x10d0
movq %r13, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x1060
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq