// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x78, %rsp
movq %rdi, 0x70(%rsp)
movq %rsi, 0x68(%rsp)
movq %rdx, 0x60(%rsp)
movq %rcx, 0x58(%rsp)
movq %r8, 0x50(%rsp)
movq 0x70(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0x68(%rsp), %rcx
movq 0x70(%rsp), %rax
movq %rcx, 0x20(%rax)
movq 0x60(%rsp), %rcx
movq 0x70(%rsp), %rax
movq %rcx, 0x28(%rax)
movq 0x58(%rsp), %rcx
movq 0x70(%rsp), %rax
movq %rcx, 0x30(%rax)
movq 0x50(%rsp), %rcx
movq 0x70(%rsp), %rax
movq %rcx, 0x38(%rax)
movl $0x0, 0x44(%rsp)
cmpq $0x8, 0x60(%rsp)
jae 0x1346
movq 0x48(%rsp), %rdi
movq 0x68(%rsp), %rsi
movq 0x60(%rsp), %rdx
callq 0x1120
movq 0x48(%rsp), %rdi
addq 0x60(%rsp), %rdi
movl $0x8, %edx
subq 0x60(%rsp), %rdx
xorl %esi, %esi
callq 0x1040
movq 0x48(%rsp), %rax
movq 0x60(%rsp), %rcx
movb $-0x80, (%rax,%rcx)
movl $0x1, 0x44(%rsp)
jmp 0x1356
movq 0x48(%rsp), %rax
movq 0x68(%rsp), %rcx
movq (%rcx), %rcx
movq %rcx, (%rax)
movq 0x48(%rsp), %rax
movzbl (%rax), %eax
andl $0x80, %eax
movl %eax, 0x40(%rsp)
movq 0x48(%rsp), %rax
movzbl (%rax), %ecx
andl $0x7f, %ecx
movb %cl, (%rax)
movq 0x48(%rsp), %rdi
movq 0x48(%rsp), %rsi
callq 0x1110
cmpl $0x0, 0x40(%rsp)
je 0x13f3
movq 0x48(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rax
movzwl 0x2(%rax), %edx
movq 0x38(%rsp), %rax
movzwl (%rax), %ecx
xorl %edx, %ecx
movw %cx, (%rax)
movq 0x38(%rsp), %rax
movzwl 0x4(%rax), %edx
movq 0x38(%rsp), %rax
movzwl 0x2(%rax), %ecx
xorl %edx, %ecx
movw %cx, 0x2(%rax)
movq 0x38(%rsp), %rax
movzwl 0x6(%rax), %edx
movq 0x38(%rsp), %rax
movzwl 0x4(%rax), %ecx
xorl %edx, %ecx
movw %cx, 0x4(%rax)
movq 0x38(%rsp), %rax
movzwl (%rax), %edx
movq 0x38(%rsp), %rax
movzwl 0x6(%rax), %ecx
xorl %edx, %ecx
movw %cx, 0x6(%rax)
jmp 0x13f5
jmp 0x13f5
movq $0x1, 0x30(%rsp)
movq 0x30(%rsp), %rax
movq 0x60(%rsp), %rcx
shrq $0x3, %rcx
cmpq %rcx, %rax
jae 0x1450
movq 0x48(%rsp), %rdi
movq 0x68(%rsp), %rsi
movq 0x30(%rsp), %rax
shlq $0x3, %rax
addq %rax, %rsi
movl $0x8, %edx
callq 0x1100
movq 0x48(%rsp), %rdi
movq 0x48(%rsp), %rsi
callq 0x1110
movq 0x30(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x30(%rsp)
jmp 0x13fe
movq 0x60(%rsp), %rax
andq $0x7, %rax
movq %rax, 0x28(%rsp)
cmpq $0x8, 0x60(%rsp)
jbe 0x14be
cmpq $0x0, 0x28(%rsp)
je 0x14be
movq 0x48(%rsp), %rdi
movq 0x68(%rsp), %rsi
movq 0x30(%rsp), %rax
shlq $0x3, %rax
addq %rax, %rsi
movq 0x28(%rsp), %rax
movl %eax, %edx
callq 0x1100
movq 0x48(%rsp), %rax
movq 0x28(%rsp), %rcx
movzbl (%rax,%rcx), %edx
xorl $0x80, %edx
movb %dl, (%rax,%rcx)
movq 0x48(%rsp), %rdi
movq 0x48(%rsp), %rsi
callq 0x1110
movl $0x1, 0x44(%rsp)
movq 0x48(%rsp), %rax
movzbl (%rax), %ecx
xorl $0xcc, %ecx
movb %cl, (%rax)
movq 0x48(%rsp), %rdi
addq $0x1, %rdi
movq 0x58(%rsp), %rsi
movq 0x50(%rsp), %rax
movl %eax, %edx
callq 0x1100
movq 0x50(%rsp), %rax
addq $0x1, %rax
cmpq $0x8, %rax
je 0x1510
movq 0x48(%rsp), %rax
movq 0x50(%rsp), %rcx
movzbl 0x1(%rax,%rcx), %edx
xorl $0x80, %edx
movb %dl, 0x1(%rax,%rcx)
cmpl $0x0, 0x44(%rsp)
je 0x1581
movq 0x48(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rax
movzwl (%rax), %eax
movq 0x20(%rsp), %rcx
movzwl 0x2(%rcx), %ecx
xorl %ecx, %eax
movw %ax, 0x1e(%rsp)
movq 0x20(%rsp), %rax
movw 0x2(%rax), %cx
movq 0x20(%rsp), %rax
movw %cx, (%rax)
movq 0x20(%rsp), %rax
movw 0x4(%rax), %cx
movq 0x20(%rsp), %rax
movw %cx, 0x2(%rax)
movq 0x20(%rsp), %rax
movw 0x6(%rax), %cx
movq 0x20(%rsp), %rax
movw %cx, 0x4(%rax)
movw 0x1e(%rsp), %cx
movq 0x20(%rsp), %rax
movw %cx, 0x6(%rax)
jmp 0x1604
movq 0x48(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
movzwl 0x4(%rax), %edx
movq 0x10(%rsp), %rax
movzwl (%rax), %ecx
xorl %edx, %ecx
movw %cx, (%rax)
movq 0x10(%rsp), %rax
movw 0x2(%rax), %ax
movw %ax, 0xe(%rsp)
movq 0x10(%rsp), %rax
movzwl 0x6(%rax), %edx
movq 0x10(%rsp), %rax
movzwl 0x2(%rax), %ecx
xorl %edx, %ecx
movw %cx, 0x2(%rax)
movq 0x10(%rsp), %rax
movzwl 0x4(%rax), %eax
movq 0x10(%rsp), %rcx
movzwl 0x2(%rcx), %ecx
xorl %ecx, %eax
movw %ax, %cx
movq 0x10(%rsp), %rax
movw %cx, 0x6(%rax)
movzwl 0xe(%rsp), %eax
movq 0x10(%rsp), %rcx
movzwl (%rcx), %ecx
xorl %ecx, %eax
movw %ax, %cx
movq 0x10(%rsp), %rax
movw %cx, 0x4(%rax)
jmp 0x1604
movq 0x70(%rsp), %rax
movq 0x48(%rsp), %rcx
movq (%rcx), %rcx
movq %rcx, 0x8(%rax)
movq 0x48(%rsp), %rdi
movq 0x48(%rsp), %rsi
callq 0x1110
xorl %eax, %eax
addq $0x78, %rsp
retq
nopl (%rax,%rax)