// File: /workspace/github_c_makefile_star2_O3/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
movq %rdi, 0x18(%rsp)
vmovq (%rdi), %xmm0
vpbroadcastb 0xa52(%rip), %xmm6 # 0x2000
vpunpcklbw %xmm0, %xmm0, %xmm0 # xmm0 = xmm0[0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7]
vpand %xmm6, %xmm0, %xmm1
vpsrlw $0x4, %xmm0, %xmm0
vpand %xmm6, %xmm0, %xmm0
movw $0xaaaa, %ax            # imm = 0xAAAA
kmovd %eax, %k1
vmovdqu8 %xmm1, %xmm0 {%k1}
vmovdqa %xmm0, (%rsp)
vpxor %xmm0, %xmm0, %xmm0
vmovdqu %ymm0, 0x30(%rsp)
cmpl $0x4, %edx
jl 0x1671
movl %ebp, %eax
shrl $0x2, %eax
leaq -0x1(%rax), %rcx
incl %eax
andl $0x3ffffffe, %eax       # imm = 0x3FFFFFFE
vpmovsxbq 0xa03(%rip), %xmm0 # 0x2001
vpbroadcastq %rcx, %xmm1
xorl %ecx, %ecx
vpbroadcastq 0xa71(%rip), %xmm2 # 0x2080
vpbroadcastq 0xa70(%rip), %xmm3 # 0x2088
vpcmpleuq %xmm1, %xmm0, %k1
kmovd %k1, %edx
testb $0x1, %dl
je 0x162d
vpbroadcastb (%rsi), %xmm4
kmovd %k1, %edx
testb $0x2, %dl
je 0x163c
vpinsrb $0x1, (%rsi), %xmm4, %xmm4
vpand %xmm2, %xmm0, %xmm5
vptestnmq %xmm5, %xmm5, %k2
vpsrlw $0x4, %xmm4, %xmm5
vpand %xmm6, %xmm5, %xmm5
vpand %xmm6, %xmm4, %xmm4
vmovdqu8 %xmm5, %xmm4 {%k2}
vmovdqu8 %xmm4, 0x30(%rsp,%rcx) {%k1}
addq $0x2, %rcx
vpaddq %xmm3, %xmm0, %xmm0
incq %rsi
cmpq %rcx, %rax
jne 0x1618
movq 0x2978(%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 0x167c
xorl %eax, %eax
cmpl $0x41, %ebp
setge %al
leaq 0x8(,%rax,4), %rax
movq %rax, 0x20(%rsp)
movl %ebp, %ebx
shrb $0x4, %bl
andb $0xf, %bpl
xorl %r12d, %r12d
movq 0x2944(%rip), %r14      # 0x3ff8
movq %rsp, %r15
movq %r12, 0x28(%rsp)
leaq (,%r12,4), %r12
xorl %r13d, %r13d
leaq (%r12,%r13), %rax
xorb %bl, (%rsp)
movb 0x4(%rsp), %cl
xorb %bl, %cl
xorb $0x1, %cl
movb %cl, 0x4(%rsp)
movb 0x8(%rsp), %cl
xorb %bpl, %cl
xorb $0x2, %cl
movb %cl, 0x8(%rsp)
movb 0xc(%rsp), %cl
xorb %bpl, %cl
xorb $0x3, %cl
movb %cl, 0xc(%rsp)
leaq 0x942(%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 0x171d
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 0x1781
incq %rax
incq %rdx
addq $0x4, %rcx
cmpq $0x3, %rdx
jne 0x1777
movq %r15, %rdi
vzeroupper
callq 0x10a0
incq %r13
cmpq $0x4, %r13
jne 0x16c7
movq 0x28(%rsp), %r12
incq %r12
movq %r15, %rdi
leaq 0x30(%rsp), %rsi
movl %r12d, %edx
callq 0x1080
cmpq 0x20(%rsp), %r12
jne 0x16b7
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
vpsllw $0x4, %xmm1, %xmm1
vpinsrb $0x7, 0xf(%rsp), %xmm0, %xmm0
vpbroadcastb 0x794(%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