// File: /workspace/github_c_makefile_star2_O1/mjosaarinen[P]brutus/crypto_aead/led80n6t4silcv2/ref/led.c
// Function: LED_enc
void LED_enc(unsigned char* input, const unsigned char* userkey, int ksbits)
{
#ifdef _PRINT
	printf("LED  input: ");
	pstate(input, 8);
#endif
	unsigned char state[4][4];
	unsigned char keyNibbles[32];
	int i;
	for(i = 0; i < 16; i++) {
		if(i%2) state[i/4][i%4] = input[i>>1]&0xF;
		else state[i/4][i%4] = (input[i>>1]>>4)&0xF;
	}

	/* break the key into 4-bit nibbles */
	memset(keyNibbles, 0, 32);
	for(i = 0; i < ksbits/4; i++){
		if(i%2) keyNibbles[i] = userkey[i>>1]&0xF;
		else keyNibbles[i] = (userkey[i>>1]>>4)&0xF;
	}
	LED = ksbits;
	int RN = 48;
	if(LED <= 64)
		RN = 32;
	int j;
	AddKey(state, keyNibbles, 0);
	for(i = 0; i < RN/4; i++){
		for(j = 0; j < 4; j++)
		{
			AddConstants(state, i*4+j);
			SubCell(state);
			ShiftRow(state);
			MixColumn(state);
		}
		AddKey(state, keyNibbles, i+1);
	}
	for(i = 0; i < 8; i++)
		input[i] = ((state[(2*i)/4][(2*i)%4] & 0xF) << 4) | (state[(2*i+1)/4][(2*i+1)%4] & 0xF);
#ifdef _PRINT
	printf("LED output: ");
	pstate(input, 8);
#endif
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %edx, %ebp
xorl %eax, %eax
movl %eax, %ecx
shrl %ecx
movl %eax, %edx
shrl $0x2, %edx
movb (%rdi,%rcx), %cl
leaq (%rsp,%rdx,4), %rdx
testb $0x1, %al
jne 0x15d8
shrb $0x4, %cl
movl %eax, %r8d
andl $0x2, %r8d
jmp 0x15e2
andb $0xf, %cl
movl %eax, %r8d
andl $0x3, %r8d
movb %cl, (%r8,%rdx)
incl %eax
cmpl $0x10, %eax
jne 0x15b8
vpxor %xmm0, %xmm0, %xmm0
vmovdqu %ymm0, 0x30(%rsp)
cmpl $0x4, %ebp
jl 0x162f
movl %ebp, %eax
shrl $0x2, %eax
xorl %ecx, %ecx
movl %ecx, %edx
shrl %edx
movb (%rsi,%rdx), %dl
movl %edx, %r8d
andb $0xf, %r8b
shrb $0x4, %dl
testb $0x1, %cl
movzbl %dl, %edx
movzbl %r8b, %r8d
cmovel %edx, %r8d
movb %r8b, 0x30(%rsp,%rcx)
incq %rcx
cmpq %rcx, %rax
jne 0x1603
movq %rdi, 0x18(%rsp)
movq 0x29b5(%rip), %rax      # 0x3ff0
movl %ebp, (%rax)
xorl %eax, %eax
movl 0x30(%rsp,%rax,4), %ecx
xorl %ecx, (%rsp,%rax,4)
incq %rax
cmpq $0x4, %rax
jne 0x163f
xorl %eax, %eax
cmpl $0x41, %ebp
setge %al
leaq 0x8(,%rax,4), %rax
movq %rax, 0x20(%rsp)
movl %ebp, %r12d
shrb $0x4, %r12b
andb $0xf, %bpl
xorl %ebx, %ebx
movq 0x2980(%rip), %r14      # 0x3ff8
movq %rsp, %r15
movq %rbx, 0x28(%rsp)
leaq (,%rbx,4), %rbx
xorl %r13d, %r13d
leaq (%rbx,%r13), %rax
movb 0x4(%rsp), %cl
xorb %r12b, %cl
movb 0x8(%rsp), %dl
xorb %bpl, %dl
movb 0xc(%rsp), %sil
xorb %bpl, %sil
xorb %r12b, (%rsp)
xorb $0x1, %cl
movb %cl, 0x4(%rsp)
xorb $0x2, %dl
movb %dl, 0x8(%rsp)
xorb $0x3, %sil
movb %sil, 0xc(%rsp)
leaq 0x979(%rip), %rcx       # 0x2040
movb (%rcx,%rax), %al
movl %eax, %ecx
shrb $0x3, %cl
andb $0x7, %cl
xorb %cl, 0x1(%rsp)
xorb %cl, 0x9(%rsp)
andb $0x7, %al
xorb %al, 0x5(%rsp)
xorb %al, 0xd(%rsp)
xorl %eax, %eax
vpmovzxbq (%rsp,%rax,4), %ymm0
vmovq %xmm0, %rcx
vpextrq $0x1, %xmm0, %rdx
vextracti128 $0x1, %ymm0, %xmm0
vmovq %xmm0, %rsi
vpextrq $0x1, %xmm0, %rdi
movzbl (%r14,%rcx), %ecx
vmovd %ecx, %xmm0
vpinsrb $0x1, (%r14,%rdx), %xmm0, %xmm0
vpinsrb $0x2, (%r14,%rsi), %xmm0, %xmm0
vpinsrb $0x3, (%r14,%rdi), %xmm0, %xmm0
vmovd %xmm0, (%rsp,%rax,4)
incq %rax
cmpq $0x4, %rax
jne 0x16e6
movl $0x1, %eax
leaq 0x4(%rsp), %rcx
xorl %edx, %edx
movl 0x4(%rsp,%rdx,4), %esi
movl %esi, 0x14(%rsp)
xorl %esi, %esi
leal (%rax,%rsi), %edi
andl $0x3, %edi
movb 0x14(%rsp,%rdi), %dil
movb %dil, (%rcx,%rsi)
incq %rsi
cmpq $0x4, %rsi
jne 0x174a
incq %rax
incq %rdx
addq $0x4, %rcx
cmpq $0x3, %rdx
jne 0x1740
movq %r15, %rdi
vzeroupper
callq 0x10a0
incq %r13
cmpq $0x4, %r13
jne 0x168b
movq 0x28(%rsp), %rbx
incq %rbx
movq %r15, %rdi
leaq 0x30(%rsp), %rsi
movl %ebx, %edx
callq 0x1080
cmpq 0x20(%rsp), %rbx
jne 0x167b
vmovdqa (%rsp), %xmm0
vpinsrb $0x1, 0x2(%rsp), %xmm0, %xmm1
vpinsrb $0x2, 0x4(%rsp), %xmm1, %xmm1
vpinsrb $0x3, 0x6(%rsp), %xmm1, %xmm1
vpinsrb $0x4, 0x8(%rsp), %xmm1, %xmm1
vpinsrb $0x5, 0xa(%rsp), %xmm1, %xmm1
vpinsrb $0x6, 0xc(%rsp), %xmm1, %xmm1
vpinsrb $0x7, 0xe(%rsp), %xmm1, %xmm1
vpsrlw $0x8, %xmm0, %xmm0
vpinsrb $0x1, 0x3(%rsp), %xmm0, %xmm0
vpinsrb $0x2, 0x5(%rsp), %xmm0, %xmm0
vpinsrb $0x3, 0x7(%rsp), %xmm0, %xmm0
vpinsrb $0x4, 0x9(%rsp), %xmm0, %xmm0
vpinsrb $0x5, 0xb(%rsp), %xmm0, %xmm0
vpinsrb $0x6, 0xd(%rsp), %xmm0, %xmm0
vpinsrb $0x7, 0xf(%rsp), %xmm0, %xmm0
vpsllw $0x4, %xmm1, %xmm1
vpbroadcastb 0x7cc(%rip), %xmm2 # 0x2000
vpternlogd $0xca, %xmm1, %xmm0, %xmm2
movq 0x18(%rsp), %rax
vmovq %xmm2, (%rax)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop