// File: /workspace/github_c_makefile_star2_O1/mjosaarinen[P]brutus/crypto_aead/twine80n6t4clocv2/ref/cloc.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;

	cxt->ad = (byte*) ad;
	cxt->adlen = adlen;
	cxt->nonce = (byte*) nonce;
	cxt->nlen = nlen;
	/* process the first block */
	int ozp = 0;
	if(adlen < STATE_LEN){       // less than one block
		memcpy(state, ad, adlen);
		memset(state+adlen, 0, STATE_LEN-adlen);
		state[adlen] = 0x80;
		ozp = 1;                // one-zero padding works only if the adlen < 16
	}
	else{                       // full first block
		memcpy(state, ad, STATE_LEN);
	}

	/* apply fix0 and the E_k */
	int fix0 = state[0] & 0x80;    /* test if the MSB is zero */
	state[0] &= 0x7f;

	/* apply the first encryption */
	Encode(state, state);
	/* when fix0 works, apply h */
	if(fix0){
		word* wd = (word*) state;
		h(wd[0], wd[1], wd[2], wd[3]);
	}
	else{
		// do nothing
	}

	/* process the middle normal blocks of ad */
	unsigned long long i;
	for(i = 1; i < (unsigned long long)adlen/STATE_LEN; i++){
		xor_bytes(state, ad+i*STATE_LEN, STATE_LEN);
		Encode(state, state);
	}
	/* process the last block partial block if any */
	unsigned long long lastblocklen = adlen % STATE_LEN;
	if((adlen > STATE_LEN) && lastblocklen){
		xor_bytes(state, ad+i*STATE_LEN, lastblocklen);
		state[lastblocklen] ^= 0x80;
		Encode(state, state);
		ozp = 1;
	}

	/* process the nonce 
	 * 1. first byte is: PARAM
	 * 2. then the nonce value
	 * 3. padding if any (at the moment, the parameter set contains padding for all choices)
	 */
	state[0] ^= PARAM;
	xor_bytes(state+1, nonce, nlen);

	/* apply padding to nonce */
	if((nlen+1) != STATE_LEN)
		state[nlen+1] ^= 0x80;

	/* apply f1 or f2 to get V */
	if(ozp){ // apply f2
		word* wd = (word*) state;
		f2(wd[0], wd[1], wd[2], wd[3]);
	}
	else{ // apply f1
		word* wd = (word*) state;
		f1(wd[0], wd[1], wd[2], wd[3]);
	}

	memcpy(cxt->ts, state, STATE_LEN);
	Encode(state, state);

	return SUCCESS;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %r8, %r14
movq %rdx, %rbp
movq %rsi, %r13
movq %rdi, %rbx
movq %rsi, 0x20(%rdi)
movq %rdx, 0x28(%rdi)
movq %rcx, 0x10(%rsp)
movq %rcx, 0x30(%rdi)
movq %r8, 0x38(%rdi)
cmpq $0x7, %rdx
ja 0x12ae
movq %rbx, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x1120
leaq (%rbx,%rbp), %rdi
movl $0x8, %edx
subq %rbp, %rdx
xorl %esi, %esi
callq 0x1040
movb $-0x80, (%rbx,%rbp)
movl $0x1, %eax
jmp 0x12b7
movq (%r13), %rax
movq %rax, (%rbx)
xorl %eax, %eax
movl %eax, 0xc(%rsp)
movb (%rbx), %r15b
movl %r15d, %eax
andb $0x7f, %al
movb %al, (%rbx)
movq %rbx, %rdi
movq %rbx, %rsi
callq 0x1110
testb %r15b, %r15b
jns 0x12fc
movzwl 0x2(%rbx), %eax
movzwl (%rbx), %ecx
xorw %ax, %cx
movw %cx, (%rbx)
movzwl 0x4(%rbx), %edx
xorl %edx, %eax
movw %ax, 0x2(%rbx)
movzwl 0x6(%rbx), %eax
xorl %eax, %edx
movw %dx, 0x4(%rbx)
xorl %eax, %ecx
movw %cx, 0x6(%rbx)
movl $0x8, %r15d
cmpq $0x10, %rbp
jb 0x1337
movq %rbp, %r12
shrq $0x3, %r12
decq %r12
leaq (%r15,%r13), %rsi
movq %rbx, %rdi
movl $0x8, %edx
callq 0x1100
movq %rbx, %rdi
movq %rbx, %rsi
callq 0x1110
addq $0x8, %r15
decq %r12
jne 0x1312
cmpq $0x9, %rbp
setb %al
andq $0x7, %rbp
sete %cl
orb %al, %cl
movl 0xc(%rsp), %r12d
jne 0x1373
addq %r15, %r13
movq %rbx, %rdi
movq %r13, %rsi
movl %ebp, %edx
callq 0x1100
xorb $-0x80, (%rbx,%rbp)
movq %rbx, %rdi
movq %rbx, %rsi
callq 0x1110
movl $0x1, %r12d
xorb $-0x34, (%rbx)
leaq 0x1(%rbx), %rdi
movq 0x10(%rsp), %rsi
movl %r14d, %edx
callq 0x1100
incq %r14
cmpq $0x8, %r14
je 0x1395
xorb $-0x80, (%rbx,%r14)
testl %r12d, %r12d
je 0x13b3
movl 0x2(%rbx), %eax
movzwl (%rbx), %ecx
xorw %ax, %cx
movl %eax, (%rbx)
movzwl 0x6(%rbx), %eax
movw %ax, 0x4(%rbx)
movw %cx, 0x6(%rbx)
jmp 0x13db
movzwl 0x4(%rbx), %eax
movzwl (%rbx), %ecx
xorw %ax, %cx
movw %cx, (%rbx)
movzwl 0x2(%rbx), %edx
movzwl 0x6(%rbx), %esi
xorw %dx, %si
movw %si, 0x2(%rbx)
xorl %esi, %eax
movw %ax, 0x6(%rbx)
xorl %edx, %ecx
movw %cx, 0x4(%rbx)
movq (%rbx), %rax
movq %rax, 0x8(%rbx)
movq %rbx, %rdi
movq %rbx, %rsi
callq 0x1110
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq