// File: /workspace/github_c_makefile_star2_O2/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 %rdx, %r15
movq %rsi, %r13
movq %rdi, %rbx
movq %rsi, 0x20(%rdi)
movq %rdx, 0x28(%rdi)
movq %rcx, 0x8(%rsp)
movq %rcx, 0x30(%rdi)
movq %r8, 0x10(%rsp)
movq %r8, 0x38(%rdi)
cmpq $0x7, %rdx
ja 0x12af
movq %rbx, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x1120
leaq (%rbx,%r15), %rdi
pushq $0x8
popq %rdx
subq %r15, %rdx
xorl %esi, %esi
callq 0x1040
movb $-0x80, (%rbx,%r15)
movb (%rbx), %r14b
pushq $0x1
popq %rax
jmp 0x12b8
movq (%r13), %r14
movq %r14, (%rbx)
xorl %eax, %eax
movl %eax, 0x4(%rsp)
movl %r14d, %eax
andb $0x7f, %al
movb %al, (%rbx)
movq %rbx, %rdi
movq %rbx, %rsi
callq 0x1110
testb %r14b, %r14b
jns 0x12fa
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)
movq %r15, %r12
shrq $0x3, %r12
addq $0x8, %r13
pushq $0x1
popq %r14
pushq $0x8
popq %rbp
cmpq %r12, %r14
jae 0x1332
movq %rbx, %rdi
movq %r13, %rsi
movl %ebp, %edx
callq 0x1100
movq %rbx, %rdi
movq %rbx, %rsi
callq 0x1110
incq %r14
addq $0x8, %r13
jmp 0x130c
cmpq $0x9, %r15
setb %al
andq $0x7, %r15
sete %cl
orb %al, %cl
movl 0x4(%rsp), %ebp
jne 0x1369
movq %rbx, %rdi
movq %r13, %rsi
movl %r15d, %edx
callq 0x1100
xorb $-0x80, (%rbx,%r15)
movq %rbx, %rdi
movq %rbx, %rsi
callq 0x1110
pushq $0x1
popq %rbp
xorb $-0x34, (%rbx)
leaq 0x1(%rbx), %rdi
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %r14
movl %r14d, %edx
callq 0x1100
incq %r14
cmpq $0x8, %r14
je 0x1390
xorb $-0x80, (%rbx,%r14)
testl %ebp, %ebp
je 0x13ad
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 0x13d5
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