// File: /workspace/github_c_makefile_star2_O1/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
pushq %rax
movq %rdx, %rbp
movq %rsi, %r13
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, (%rsp)
subq %r8, %rdi
movq %rcx, %rsi
movq %r8, %rdx
callq 0x10c0
leaq 0x35d7(%rip), %rdi      # 0x6830
xorl %ebx, %ebx
xorl %esi, %esi
callq 0x10d0
movq %r15, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x1060
cmpq $0x10, %rbp
jb 0x32c4
movq %rbp, %r12
shrq $0x4, %r12
xorl %ebx, %ebx
leaq (%rbx,%r13), %rsi
movq %r15, %rdi
movl $0x10, %edx
callq 0x10b0
leaq 0x35ab(%rip), %rdi      # 0x6842
movq %r15, %rsi
callq 0x10d0
leaq 0x35ba(%rip), %rdi      # 0x6860
xorl %esi, %esi
callq 0x10d0
movq %r15, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x1060
addq $0x10, %rbx
decq %r12
jne 0x327f
movq %rbp, %rdx
andq $0xf, %rdx
movq %r14, %r12
je 0x3309
addq %rbx, %r13
movq %r15, %rdi
movq %r13, %rsi
callq 0x10b0
leaq 0x3580(%rip), %rdi      # 0x6865
movq %r15, %rsi
callq 0x10d0
leaq 0x356c(%rip), %rdi      # 0x6860
xorl %esi, %esi
callq 0x10d0
movq %r15, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x1060
movl $0xf, %eax
xorb %bpl, (%r15,%rax)
shrq $0x8, %rbp
decq %rax
cmpq $0x7, %rax
jne 0x330e
leaq 0x3574(%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 0x3565(%rip), %rdi      # 0x68c6
movq %r15, %rsi
callq 0x10d0
vmovups (%r15), %xmm0
movq (%rsp), %rbx
vmovups %xmm0, (%rbx)
leaq 0x34e3(%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(%rbx)
movw %si, 0xc(%rbx)
movl %edx, 0x8(%rbx)
movq %rcx, (%rbx)
movb %al, 0x1f(%r15)
leaq 0x3511(%rip), %rdi      # 0x68d8
movq %rbx, %rsi
callq 0x10d0
movq %r14, %rdi
xorl %esi, %esi
callq 0x10d0
movq %rbx, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x1060
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq