// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/led80n6t4silcv2/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);
		LED80_enc(ts, cxt->userkey);
		/* apply fix1 */
		es[0] |= 0x80;
		LED80_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);
		LED80_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);
	LED80_enc(ts, cxt->userkey);

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, %r13
movq %rdi, %r14
movq %rsi, 0x40(%rdi)
movq %rdx, 0x48(%rdi)
movq %rcx, 0x8(%rsp)
movq %rcx, 0x50(%rdi)
movq %rdx, 0x58(%rdi)
movq %r8, 0x18(%rsp)
movq %r8, 0x60(%rdi)
movq %r9, 0x20(%rsp)
movl %r9d, 0x68(%rdi)
leaq 0x8(%rdi), %rax
movq %rax, 0x10(%rsp)
xorl %r15d, %r15d
pushq $0x8
popq %rbp
leaq 0x8(%r15), %rbx
cmpq %r13, %rbx
jae 0x192b
movq %rsi, %r12
addq %r15, %rsi
cmpl $0x1, 0x60(%rsp)
jne 0x18d1
movq %r14, %rdi
movl %ebp, %edx
callq 0x10f0
addq 0x8(%rsp), %r15
movq %r14, %rax
jmp 0x18ec
addq 0x8(%rsp), %r15
movq %rsi, %rdi
movq %r15, %rsi
movq %r14, %rdx
movl %ebp, %ecx
callq 0x1140
movq %r15, %rax
movq %r14, %r15
movq (%rax), %rax
movq %rax, (%r15)
movq 0x10(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0x10f0
movq 0x10(%r14), %rsi
movq %r15, %rdi
callq 0x1100
orb $-0x80, (%r14)
movq 0x10(%r14), %rsi
movq %r14, %rdi
callq 0x1100
movq %rbx, %r15
movq %r12, %rsi
jmp 0x18a7
movq %r13, %rbp
subq %r15, %rbp
je 0x19a7
addq %r15, %rsi
cmpl $0x1, 0x60(%rsp)
jne 0x195b
movl %r13d, %r12d
subl %r15d, %r12d
movq %r14, %rdi
movl %r12d, %edx
callq 0x10f0
movq 0x8(%rsp), %rdi
addq %r15, %rdi
movq %r14, %rsi
jmp 0x1980
movq 0x8(%rsp), %rbx
addq %r15, %rbx
movl %r13d, %r12d
subl %r15d, %r12d
movq %rsi, %rdi
movq %rbx, %rsi
movq %r14, %rdx
movl %r12d, %ecx
callq 0x1140
movq %r14, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x1120
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
movl %r12d, %edx
callq 0x10f0
movq 0x10(%r14), %rsi
movq %rbx, %rdi
callq 0x1100
pushq $0xf
popq %rax
cmpq $0x7, %rax
je 0x19bd
xorb %r13b, (%r14,%rax)
shrq $0x8, %r13
decq %rax
jmp 0x19aa
movb 0x8(%r14), %al
movb 0x9(%r14), %cl
xorl %edx, %edx
cmpq $0x7, %rdx
je 0x19dc
movb 0x9(%r14,%rdx), %sil
movb %sil, 0x8(%r14,%rdx)
incq %rdx
jmp 0x19c7
xorb %al, %cl
movb %cl, 0xf(%r14)
movq 0x10(%r14), %rsi
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x1100
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
movq 0x20(%rsp), %rdx
callq 0x1120
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop