// File: /workspace/github_c_makefile_star2_O2/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, %rbp
movq %rsi, %rbx
movq %rdi, %r15
movq 0x118(%rdi), %r14
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)
addq $0x10, %rdi
movq %rdi, 0x10(%rsp)
subq %r8, %rdi
movq %rcx, %rsi
movq %r8, %rdx
callq 0x10c0
leaq 0x35f0(%rip), %rdi      # 0x6830
xorl %esi, %esi
callq 0x10d0
movq %r15, %rdi
movq %r15, %rsi
movq %r14, (%rsp)
movq %r14, %rdx
callq 0x1060
movq %rbp, %r12
shrq $0x4, %r12
leaq 0x35db(%rip), %r14      # 0x6842
leaq 0x35f2(%rip), %r13      # 0x6860
movq %rbx, 0x8(%rsp)
subq $0x1, %r12
jb 0x32b1
movq %r15, %rdi
movq %rbx, %rsi
pushq $0x10
popq %rdx
callq 0x10b0
movq %r14, %rdi
movq %r15, %rsi
callq 0x10d0
movq %r13, %rdi
xorl %esi, %esi
callq 0x10d0
movq %r15, %rdi
movq %r15, %rsi
movq (%rsp), %rdx
callq 0x1060
addq $0x10, %rbx
jmp 0x3273
movq %rbp, %rdx
andq $0xf, %rdx
je 0x32fd
movq %rbp, %rax
andq $-0x10, %rax
movq 0x8(%rsp), %rsi
addq %rax, %rsi
movq %r15, %rdi
callq 0x10b0
leaq 0x358d(%rip), %rdi      # 0x6865
movq %r15, %rsi
callq 0x10d0
leaq 0x3579(%rip), %rdi      # 0x6860
xorl %esi, %esi
callq 0x10d0
movq %r15, %rdi
movq %r15, %rsi
movq (%rsp), %rdx
callq 0x1060
pushq $0xf
popq %rax
movq 0x10(%rsp), %r14
cmpq $0x7, %rax
je 0x3318
xorb %bpl, (%r15,%rax)
shrq $0x8, %rbp
decq %rax
jmp 0x3305
leaq 0x357b(%rip), %rdi      # 0x689a
movq %r15, %rsi
callq 0x10d0
movb (%r15), %al
movb 0x1(%r15), %cl
pushq $0x1
popq %rdx
cmpq $0x10, %rdx
je 0x3345
movb (%r15,%rdx), %sil
movb %sil, -0x1(%r15,%rdx)
incq %rdx
jmp 0x3331
xorb %al, %cl
movb %cl, 0xf(%r15)
leaq 0x3574(%rip), %rdi      # 0x68c6
movq %r15, %rsi
callq 0x10d0
vmovups (%r15), %xmm0
vmovups %xmm0, (%r14)
leaq 0x34f5(%rip), %rdi      # 0x6860
xorl %ebx, %ebx
xorl %esi, %esi
callq 0x10d0
movq %r15, %rdi
movq %r15, %rsi
movq (%rsp), %rdx
callq 0x1060
movb 0x10(%r15), %al
movb 0x11(%r15), %cl
cmpq $0xf, %rbx
je 0x33a0
movb 0x11(%r15,%rbx), %dl
movb %dl, 0x10(%r15,%rbx)
incq %rbx
jmp 0x338b
xorb %al, %cl
movb %cl, 0x1f(%r15)
leaq 0x352b(%rip), %rdi      # 0x68d8
movq %r14, %rsi
callq 0x10d0
leaq 0x34a4(%rip), %rdi      # 0x6860
xorl %esi, %esi
callq 0x10d0
movq %r14, %rdi
movq %r14, %rsi
movq (%rsp), %rdx
callq 0x1060
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq