// File: /workspace/github_c_makefile_star2_O0/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:
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 0x224e
cmpl $0x1, 0x70(%rsp)
jne 0x21b8
movq 0x30(%rsp), %rdi
movq 0x58(%rsp), %rsi
addq 0x20(%rsp), %rsi
movl $0x8, %edx
callq 0x1110
movq 0x48(%rsp), %rax
movq 0x20(%rsp), %rcx
movq 0x30(%rsp), %rdx
movq (%rdx), %rdx
movq %rdx, (%rax,%rcx)
jmp 0x21f1
movq 0x58(%rsp), %rdi
addq 0x20(%rsp), %rdi
movq 0x48(%rsp), %rsi
addq 0x20(%rsp), %rsi
movq 0x30(%rsp), %rdx
movl $0x8, %ecx
callq 0x1160
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 0x1110
movq 0x28(%rsp), %rdi
movq 0x60(%rsp), %rax
movq 0x10(%rax), %rsi
callq 0x1120
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 0x1120
movq 0x20(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x20(%rsp)
jmp 0x216c
movq 0x50(%rsp), %rax
subq 0x20(%rsp), %rax
movq %rax, 0x18(%rsp)
cmpq $0x0, 0x18(%rsp)
jbe 0x230d
cmpl $0x1, 0x70(%rsp)
jne 0x22a6
movq 0x30(%rsp), %rdi
movq 0x58(%rsp), %rsi
addq 0x20(%rsp), %rsi
movq 0x18(%rsp), %rax
movl %eax, %edx
callq 0x1110
movq 0x48(%rsp), %rdi
addq 0x20(%rsp), %rdi
movq 0x30(%rsp), %rsi
movq 0x18(%rsp), %rdx
callq 0x1140
jmp 0x22e4
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 0x1160
movq 0x30(%rsp), %rdi
movq 0x48(%rsp), %rsi
addq 0x20(%rsp), %rsi
movq 0x18(%rsp), %rdx
callq 0x1140
movq 0x28(%rsp), %rdi
movq 0x30(%rsp), %rsi
movq 0x18(%rsp), %rax
movl %eax, %edx
callq 0x1110
movq 0x28(%rsp), %rdi
movq 0x60(%rsp), %rax
movq 0x10(%rax), %rsi
callq 0x1120
movq 0x50(%rsp), %rax
movq %rax, 0x10(%rsp)
movq $0x0, 0x8(%rsp)
cmpq $0x8, 0x8(%rsp)
jae 0x236b
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 0x2320
jmp 0x236d
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 0x23bf
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 0x238c
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 0x1120
movq 0x40(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq 0x38(%rsp), %rdx
callq 0x1140
xorl %eax, %eax
addq $0x68, %rsp
retq
nop