// File: /workspace/github_c_makefile_star2_O3/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 %rcx, %rbp
movq %rdx, %r15
movq %rsi, %r13
movq %rdi, %rbx
movq %rsi, 0x20(%rdi)
movq %rdx, 0x28(%rdi)
movq %rcx, 0x30(%rdi)
movq %r8, 0x38(%rdi)
cmpq $0x7, %rdx
ja 0x12b3
movq %rbx, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x1120
leaq (%rbx,%r15), %rdi
movl $0x8, %edx
subq %r15, %rdx
xorl %esi, %esi
callq 0x1040
movb $-0x80, (%rbx,%r15)
movb (%rbx), %r12b
movl $0x1, 0xc(%rsp)
jmp 0x12c2
movq (%r13), %r12
movq %r12, (%rbx)
movl $0x0, 0xc(%rsp)
movl %r12d, %eax
andb $0x7f, %al
movb %al, (%rbx)
movq %rbx, %rdi
movq %rbx, %rsi
callq 0x1110
testb %r12b, %r12b
jns 0x1300
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, %eax
cmpq $0x10, %r15
jb 0x134e
movq %rbp, 0x10(%rsp)
movq %r15, %r12
shrq $0x3, %r12
decq %r12
leaq 0x8(%r13), %rbp
movq %rbx, %rdi
movq %rbp, %rsi
movl $0x8, %edx
callq 0x1100
movq %rbx, %rdi
movq %rbx, %rsi
callq 0x1110
addq $0x8, %rbp
decq %r12
jne 0x131e
movq %r15, %rax
andq $-0x8, %rax
movq 0x10(%rsp), %rbp
cmpq $0x9, %r15
setb %cl
andq $0x7, %r15
sete %dl
orb %cl, %dl
movl 0xc(%rsp), %r12d
jne 0x138c
addq %rax, %r13
movq %rbx, %rdi
movq %r13, %rsi
movl %r15d, %edx
callq 0x1100
xorb $-0x80, (%rbx,%r15)
movq %rbx, %rdi
movq %rbx, %rsi
callq 0x1110
movl $0x1, %r12d
xorb $-0x34, (%rbx)
leaq 0x1(%rbx), %rdi
movq %rbp, %rsi
movl %r14d, %edx
callq 0x1100
incq %r14
cmpq $0x8, %r14
je 0x13ac
xorb $-0x80, (%rbx,%r14)
testl %r12d, %r12d
je 0x13ca
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 0x13f2
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