// File: /workspace/github_c_makefile_star2_O3/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 $0x18, %rsp
movq %rdx, %r13
movq %rsi, %rbx
movq %rdi, %r15
movq %rsi, 0x40(%rdi)
movq %rdx, 0x48(%rdi)
movq %rcx, (%rsp)
movq %rcx, 0x50(%rdi)
movq %rdx, 0x58(%rdi)
movq %r8, 0x8(%rsp)
movq %r8, 0x60(%rdi)
movq %r9, 0x10(%rsp)
movl %r9d, 0x68(%rdi)
leaq 0x8(%rdi), %r12
cmpq $0x9, %rdx
jb 0x1ab2
xorl %ebp, %ebp
leaq (%rbx,%rbp), %rsi
cmpl $0x1, 0x50(%rsp)
jne 0x1a4c
movq %r15, %rdi
movl $0x8, %edx
callq 0x10c0
movq (%rsp), %rax
addq %rbp, %rax
movq %r15, %r14
jmp 0x1a6a
movq (%rsp), %rax
leaq (%rax,%rbp), %r14
movq %rsi, %rdi
movq %r14, %rsi
movq %r15, %rdx
movl $0x8, %ecx
callq 0x1100
movq %r15, %rax
movq (%r14), %rcx
movq %rcx, (%rax)
movq %r12, %rdi
movq %r15, %rsi
movl $0x8, %edx
callq 0x10c0
movq 0x10(%r15), %rsi
movq %r12, %rdi
callq 0x10d0
orb $-0x80, (%r15)
movq 0x10(%r15), %rsi
movq %r15, %rdi
callq 0x10d0
leaq 0x8(%rbp), %r14
addq $0x10, %rbp
cmpq %r13, %rbp
movq %r14, %rbp
jb 0x1a28
jmp 0x1ab5
xorl %r14d, %r14d
movq %r13, %rbp
subq %r14, %rbp
je 0x1b1e
addq %r14, %rbx
cmpl $0x1, 0x50(%rsp)
jne 0x1ae0
movq %r15, %rdi
movq %rbx, %rsi
movl %ebp, %edx
callq 0x10c0
movq (%rsp), %rdi
addq %r14, %rdi
movq %r15, %rsi
jmp 0x1afd
movq (%rsp), %rsi
addq %r14, %rsi
movq %rbx, %rdi
movq %rsi, %rbx
movq %r15, %rdx
movl %ebp, %ecx
callq 0x1100
movq %r15, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x10f0
movq %r12, %rdi
movq %r15, %rsi
movl %ebp, %edx
callq 0x10c0
movq 0x10(%r15), %rsi
movq %r12, %rdi
callq 0x10d0
movl $0xf, %eax
xorb %r13b, (%r15,%rax)
shrq $0x8, %r13
decq %rax
cmpq $0x7, %rax
jne 0x1b23
movb 0x9(%r15), %al
movl 0x9(%r15), %ecx
movzwl 0xd(%r15), %edx
xorb 0x8(%r15), %al
movb 0xf(%r15), %sil
movb %sil, 0x6(%r12)
movw %dx, 0x4(%r12)
movl %ecx, (%r12)
movb %al, 0xf(%r15)
movq 0x10(%r15), %rsi
movq %r12, %rdi
callq 0x10d0
movq 0x8(%rsp), %rdi
movq %r12, %rsi
movq 0x10(%rsp), %rdx
callq 0x10f0
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop