// File: /workspace/github_c_makefile_star2_O0/mjosaarinen[P]brutus/crypto_aead/aes128n8t8clocv2/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;
	AES_KEY *ekey = cxt->pt_ekey;

	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 */
	AES_encrypt(state, state, ekey);
	/* when fix0 works, apply h */
	if(fix0){
		u32* wd = (u32*) state;
        pstate2("h: ", state, STATE_LEN);
		h(wd[0], wd[1], wd[2], wd[3]);
        pstate2("", state, STATE_LEN);
	}
	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_block((block*)state, (block*)ad+i);
		AES_encrypt(state, state, ekey);
	}
	/* 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;
		AES_encrypt(state, state, ekey);
		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
		u32* wd = (u32*) state;
        pstate2("f2: ", state, STATE_LEN);
		f2(wd[0], wd[1], wd[2], wd[3]);
        pstate2("", state, STATE_LEN);
	}
	else{ // apply f1
		u32* wd = (u32*) state;
        pstate2("f1: ", state, STATE_LEN);
		f1(wd[0], wd[1], wd[2], wd[3]);
        pstate2("", state, STATE_LEN);
	}

	memcpy(cxt->ts, state, STATE_LEN);
	AES_encrypt(state, state, ekey);

	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 0x70(%rsp), %rax
movq 0x118(%rax), %rax
movq %rax, 0x40(%rsp)
movq 0x68(%rsp), %rcx
movq 0x70(%rsp), %rax
movq %rcx, 0x130(%rax)
movq 0x60(%rsp), %rcx
movq 0x70(%rsp), %rax
movq %rcx, 0x138(%rax)
movq 0x58(%rsp), %rcx
movq 0x70(%rsp), %rax
movq %rcx, 0x140(%rax)
movq 0x50(%rsp), %rcx
movq 0x70(%rsp), %rax
movq %rcx, 0x148(%rax)
movl $0x0, 0x3c(%rsp)
cmpq $0x10, 0x60(%rsp)
jae 0x56a3
movq 0x48(%rsp), %rdi
movq 0x68(%rsp), %rsi
movq 0x60(%rsp), %rdx
callq 0x10f0
movq 0x48(%rsp), %rdi
addq 0x60(%rsp), %rdi
movl $0x10, %edx
subq 0x60(%rsp), %rdx
xorl %esi, %esi
callq 0x1040
movq 0x48(%rsp), %rax
movq 0x60(%rsp), %rcx
movb $-0x80, (%rax,%rcx)
movl $0x1, 0x3c(%rsp)
jmp 0x56bb
movq 0x48(%rsp), %rax
movq 0x68(%rsp), %rcx
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rcx
movq %rcx, 0x8(%rax)
movq 0x48(%rsp), %rax
movzbl (%rax), %eax
andl $0x80, %eax
movl %eax, 0x38(%rsp)
movq 0x48(%rsp), %rax
movzbl (%rax), %ecx
andl $0x7f, %ecx
movb %cl, (%rax)
movq 0x48(%rsp), %rdi
movq 0x48(%rsp), %rsi
movq 0x40(%rsp), %rdx
callq 0x1070
cmpl $0x0, 0x38(%rsp)
je 0x5779
movq 0x48(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x48(%rsp), %rsi
leaq 0x4122(%rip), %rdi      # 0x9830
movl $0x10, %edx
callq 0x1100
movq 0x30(%rsp), %rax
movl 0x4(%rax), %ecx
movq 0x30(%rsp), %rax
xorl (%rax), %ecx
movl %ecx, (%rax)
movq 0x30(%rsp), %rax
movl 0x8(%rax), %ecx
movq 0x30(%rsp), %rax
xorl 0x4(%rax), %ecx
movl %ecx, 0x4(%rax)
movq 0x30(%rsp), %rax
movl 0xc(%rax), %ecx
movq 0x30(%rsp), %rax
xorl 0x8(%rax), %ecx
movl %ecx, 0x8(%rax)
movq 0x30(%rsp), %rax
movl (%rax), %ecx
movq 0x30(%rsp), %rax
xorl 0xc(%rax), %ecx
movl %ecx, 0xc(%rax)
movq 0x48(%rsp), %rsi
leaq 0x416e(%rip), %rdi      # 0x98db
movl $0x10, %edx
callq 0x1100
jmp 0x577b
jmp 0x577b
movq $0x1, 0x28(%rsp)
movq 0x28(%rsp), %rax
movq 0x60(%rsp), %rcx
shrq $0x4, %rcx
cmpq %rcx, %rax
jae 0x57d6
movq 0x48(%rsp), %rdi
movq 0x68(%rsp), %rsi
movq 0x28(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rsi
callq 0x10d0
movq 0x48(%rsp), %rdi
movq 0x48(%rsp), %rsi
movq 0x40(%rsp), %rdx
callq 0x1070
movq 0x28(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x28(%rsp)
jmp 0x5784
movq 0x60(%rsp), %rax
andq $0xf, %rax
movq %rax, 0x20(%rsp)
cmpq $0x10, 0x60(%rsp)
jbe 0x5849
cmpq $0x0, 0x20(%rsp)
je 0x5849
movq 0x48(%rsp), %rdi
movq 0x68(%rsp), %rsi
movq 0x28(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rsi
movq 0x20(%rsp), %rax
movl %eax, %edx
callq 0x10e0
movq 0x48(%rsp), %rax
movq 0x20(%rsp), %rcx
movzbl (%rax,%rcx), %edx
xorl $0x80, %edx
movb %dl, (%rax,%rcx)
movq 0x48(%rsp), %rdi
movq 0x48(%rsp), %rsi
movq 0x40(%rsp), %rdx
callq 0x1070
movl $0x1, 0x3c(%rsp)
movq 0x48(%rsp), %rax
movzbl (%rax), %ecx
xorl $0xd0, %ecx
movb %cl, (%rax)
movq 0x48(%rsp), %rdi
addq $0x1, %rdi
movq 0x58(%rsp), %rsi
movq 0x50(%rsp), %rax
movl %eax, %edx
callq 0x10e0
movq 0x50(%rsp), %rax
addq $0x1, %rax
cmpq $0x10, %rax
je 0x589b
movq 0x48(%rsp), %rax
movq 0x50(%rsp), %rcx
movzbl 0x1(%rax,%rcx), %edx
xorl $0x80, %edx
movb %dl, 0x1(%rax,%rcx)
cmpl $0x0, 0x3c(%rsp)
je 0x592f
movq 0x48(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x48(%rsp), %rsi
leaq 0x3f78(%rip), %rdi      # 0x9834
movl $0x10, %edx
callq 0x1100
movq 0x18(%rsp), %rax
movl (%rax), %eax
movq 0x18(%rsp), %rcx
xorl 0x4(%rcx), %eax
movl %eax, 0x14(%rsp)
movq 0x18(%rsp), %rax
movl 0x4(%rax), %ecx
movq 0x18(%rsp), %rax
movl %ecx, (%rax)
movq 0x18(%rsp), %rax
movl 0x8(%rax), %ecx
movq 0x18(%rsp), %rax
movl %ecx, 0x4(%rax)
movq 0x18(%rsp), %rax
movl 0xc(%rax), %ecx
movq 0x18(%rsp), %rax
movl %ecx, 0x8(%rax)
movl 0x14(%rsp), %ecx
movq 0x18(%rsp), %rax
movl %ecx, 0xc(%rax)
movq 0x48(%rsp), %rsi
leaq 0x3fbb(%rip), %rdi      # 0x98db
movl $0x10, %edx
callq 0x1100
jmp 0x59c0
movq 0x48(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x48(%rsp), %rsi
leaq 0x3ef4(%rip), %rdi      # 0x9839
movl $0x10, %edx
callq 0x1100
movq 0x8(%rsp), %rax
movl 0x8(%rax), %ecx
movq 0x8(%rsp), %rax
xorl (%rax), %ecx
movl %ecx, (%rax)
movq 0x8(%rsp), %rax
movl 0x4(%rax), %eax
movl %eax, 0x4(%rsp)
movq 0x8(%rsp), %rax
movl 0xc(%rax), %ecx
movq 0x8(%rsp), %rax
xorl 0x4(%rax), %ecx
movl %ecx, 0x4(%rax)
movq 0x8(%rsp), %rax
movl 0x8(%rax), %ecx
movq 0x8(%rsp), %rax
xorl 0x4(%rax), %ecx
movq 0x8(%rsp), %rax
movl %ecx, 0xc(%rax)
movl 0x4(%rsp), %ecx
movq 0x8(%rsp), %rax
xorl (%rax), %ecx
movq 0x8(%rsp), %rax
movl %ecx, 0x8(%rax)
movq 0x48(%rsp), %rsi
leaq 0x3f25(%rip), %rdi      # 0x98db
movl $0x10, %edx
callq 0x1100
movq 0x70(%rsp), %rax
movq 0x48(%rsp), %rcx
movq (%rcx), %rdx
movq %rdx, 0x10(%rax)
movq 0x8(%rcx), %rcx
movq %rcx, 0x18(%rax)
movq 0x48(%rsp), %rdi
movq 0x48(%rsp), %rsi
movq 0x40(%rsp), %rdx
callq 0x1070
xorl %eax, %eax
addq $0x78, %rsp
retq
nopw %cs:(%rax,%rax)