// File: /workspace/github_c_makefile_star2_O0/mjosaarinen[P]brutus/crypto_aead/present80n6t4silcv2/ref/silc.c
// Function: ae_encrypt
int ae_encrypt(ae_cxt* cxt, byte* pt, unsigned long long mlen, byte* ct, byte* tag, unsigned long long tlen, int enc_dec)
{
	cxt->pt = pt;
	cxt->ptlen = mlen;
	cxt->ct = ct;
	cxt->ctlen = mlen;
	cxt->tag = tag;
	cxt->tlen = tlen;

	byte* es = cxt->es;
	byte* ts = cxt->ts;


	unsigned long long pc = 0;
	while((pc + STATE_LEN) < mlen){
		if(enc_dec == ENC){ // encryption
			xor_bytes(es, pt+pc, STATE_LEN);
			memcpy(ct+pc, es, STATE_LEN);
		}
		else{ // decryption
			xor_bytes2(pt+pc, ct+pc, es, STATE_LEN);
			memcpy(es, ct+pc, STATE_LEN);
		}
		xor_bytes(ts, es, STATE_LEN);
		present_enc(ts, cxt->userkey);
		/* apply fix1 */
		es[0] |= 0x80;
		present_enc(es, cxt->userkey);
		pc += STATE_LEN;
	}

	/* process the last block */
	unsigned long long lastblocklen = mlen - pc;

	if(lastblocklen > 0){
		if(enc_dec == ENC){ // encryption
			xor_bytes(es, pt+pc, lastblocklen);
			memcpy(ct+pc, es, lastblocklen);
		}
		else{ // decryption
			xor_bytes2(pt+pc, ct+pc, es, lastblocklen);
			memcpy(es, ct+pc, lastblocklen);
		}
		xor_bytes(ts, es, lastblocklen);
		present_enc(ts, cxt->userkey);
	}
	/* encode the adlen and process */
	unsigned long long t_adlen = mlen;
	unsigned long long i;
	for(i = 0; i < sizeof(unsigned long long); i++) {
		ts[STATE_LEN-1-i] ^= t_adlen & 0xff;
		t_adlen >>= 8;
	}
	g(ts);
	present_enc(ts, cxt->userkey);

	memcpy(tag, ts, tlen);
	return SUCCESS;
}

// Assembly:
subq $0x68, %rsp
movl 0x70(%rsp), %eax
movq %rdi, 0x60(%rsp)
movq %rsi, 0x58(%rsp)
movq %rdx, 0x50(%rsp)
movq %rcx, 0x48(%rsp)
movq %r8, 0x40(%rsp)
movq %r9, 0x38(%rsp)
movq 0x58(%rsp), %rcx
movq 0x60(%rsp), %rax
movq %rcx, 0x40(%rax)
movq 0x50(%rsp), %rcx
movq 0x60(%rsp), %rax
movq %rcx, 0x48(%rax)
movq 0x48(%rsp), %rcx
movq 0x60(%rsp), %rax
movq %rcx, 0x50(%rax)
movq 0x50(%rsp), %rcx
movq 0x60(%rsp), %rax
movq %rcx, 0x58(%rax)
movq 0x40(%rsp), %rcx
movq 0x60(%rsp), %rax
movq %rcx, 0x60(%rax)
movq 0x38(%rsp), %rax
movl %eax, %ecx
movq 0x60(%rsp), %rax
movl %ecx, 0x68(%rax)
movq 0x60(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x60(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x28(%rsp)
movq $0x0, 0x20(%rsp)
movq 0x20(%rsp), %rax
addq $0x8, %rax
cmpq 0x50(%rsp), %rax
jae 0x1cbe
cmpl $0x1, 0x70(%rsp)
jne 0x1c28
movq 0x30(%rsp), %rdi
movq 0x58(%rsp), %rsi
addq 0x20(%rsp), %rsi
movl $0x8, %edx
callq 0x10d0
movq 0x48(%rsp), %rax
movq 0x20(%rsp), %rcx
movq 0x30(%rsp), %rdx
movq (%rdx), %rdx
movq %rdx, (%rax,%rcx)
jmp 0x1c61
movq 0x58(%rsp), %rdi
addq 0x20(%rsp), %rdi
movq 0x48(%rsp), %rsi
addq 0x20(%rsp), %rsi
movq 0x30(%rsp), %rdx
movl $0x8, %ecx
callq 0x10f0
movq 0x30(%rsp), %rax
movq 0x48(%rsp), %rcx
movq 0x20(%rsp), %rdx
movq (%rcx,%rdx), %rcx
movq %rcx, (%rax)
movq 0x28(%rsp), %rdi
movq 0x30(%rsp), %rsi
movl $0x8, %edx
callq 0x10d0
movq 0x28(%rsp), %rdi
movq 0x60(%rsp), %rax
movq 0x10(%rax), %rsi
callq 0x1070
movq 0x30(%rsp), %rax
movzbl (%rax), %ecx
orl $0x80, %ecx
movb %cl, (%rax)
movq 0x30(%rsp), %rdi
movq 0x60(%rsp), %rax
movq 0x10(%rax), %rsi
callq 0x1070
movq 0x20(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x20(%rsp)
jmp 0x1bdc
movq 0x50(%rsp), %rax
subq 0x20(%rsp), %rax
movq %rax, 0x18(%rsp)
cmpq $0x0, 0x18(%rsp)
jbe 0x1d7d
cmpl $0x1, 0x70(%rsp)
jne 0x1d16
movq 0x30(%rsp), %rdi
movq 0x58(%rsp), %rsi
addq 0x20(%rsp), %rsi
movq 0x18(%rsp), %rax
movl %eax, %edx
callq 0x10d0
movq 0x48(%rsp), %rdi
addq 0x20(%rsp), %rdi
movq 0x30(%rsp), %rsi
movq 0x18(%rsp), %rdx
callq 0x10e0
jmp 0x1d54
movq 0x58(%rsp), %rdi
addq 0x20(%rsp), %rdi
movq 0x48(%rsp), %rsi
addq 0x20(%rsp), %rsi
movq 0x30(%rsp), %rdx
movq 0x18(%rsp), %rax
movl %eax, %ecx
callq 0x10f0
movq 0x30(%rsp), %rdi
movq 0x48(%rsp), %rsi
addq 0x20(%rsp), %rsi
movq 0x18(%rsp), %rdx
callq 0x10e0
movq 0x28(%rsp), %rdi
movq 0x30(%rsp), %rsi
movq 0x18(%rsp), %rax
movl %eax, %edx
callq 0x10d0
movq 0x28(%rsp), %rdi
movq 0x60(%rsp), %rax
movq 0x10(%rax), %rsi
callq 0x1070
movq 0x50(%rsp), %rax
movq %rax, 0x10(%rsp)
movq $0x0, 0x8(%rsp)
cmpq $0x8, 0x8(%rsp)
jae 0x1ddb
movq 0x10(%rsp), %rsi
andq $0xff, %rsi
movq 0x28(%rsp), %rax
movl $0x7, %ecx
subq 0x8(%rsp), %rcx
movzbl (%rax,%rcx), %edx
xorq %rsi, %rdx
movb %dl, (%rax,%rcx)
movq 0x10(%rsp), %rax
shrq $0x8, %rax
movq %rax, 0x10(%rsp)
movq 0x8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x8(%rsp)
jmp 0x1d90
jmp 0x1ddd
movq 0x28(%rsp), %rax
movzbl (%rax), %eax
movq 0x28(%rsp), %rcx
movzbl 0x1(%rcx), %ecx
xorl %ecx, %eax
movb %al, 0x3(%rsp)
movl $0x0, 0x4(%rsp)
cmpl $0x7, 0x4(%rsp)
jge 0x1e2f
movq 0x28(%rsp), %rax
movl 0x4(%rsp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %dl
movq 0x28(%rsp), %rax
movslq 0x4(%rsp), %rcx
movb %dl, (%rax,%rcx)
movl 0x4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4(%rsp)
jmp 0x1dfc
movb 0x3(%rsp), %dl
movq 0x28(%rsp), %rax
movslq 0x4(%rsp), %rcx
movb %dl, (%rax,%rcx)
movq 0x28(%rsp), %rdi
movq 0x60(%rsp), %rax
movq 0x10(%rax), %rsi
callq 0x1070
movq 0x40(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq 0x38(%rsp), %rdx
callq 0x10e0
xorl %eax, %eax
addq $0x68, %rsp
retq
nop