// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x88, %rsp
movq %rdi, 0x80(%rsp)
movq %rsi, 0x78(%rsp)
movl %edx, 0x74(%rsp)
movl $0x0, 0x3c(%rsp)
cmpl $0x10, 0x3c(%rsp)
jge 0x1b65
movl 0x3c(%rsp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
je 0x1af7
movq 0x80(%rsp), %rax
movl 0x3c(%rsp), %ecx
sarl %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
andl $0xf, %eax
movb %al, 0x33(%rsp)
movl 0x3c(%rsp), %eax
movl $0x4, %ecx
cltd
idivl %ecx
movslq %eax, %rcx
leaq 0x60(%rsp), %rax
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, 0x28(%rsp)
movl 0x3c(%rsp), %eax
movl $0x4, %ecx
cltd
idivl %ecx
movq 0x28(%rsp), %rax
movl %edx, %ecx
movb 0x33(%rsp), %dl
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
jmp 0x1b53
movq 0x80(%rsp), %rax
movl 0x3c(%rsp), %ecx
sarl %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
sarl $0x4, %eax
andl $0xf, %eax
movb %al, 0x27(%rsp)
movl 0x3c(%rsp), %eax
movl $0x4, %ecx
cltd
idivl %ecx
movslq %eax, %rcx
leaq 0x60(%rsp), %rax
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, 0x18(%rsp)
movl 0x3c(%rsp), %eax
movl $0x4, %ecx
cltd
idivl %ecx
movq 0x18(%rsp), %rax
movl %edx, %ecx
movb 0x27(%rsp), %dl
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
jmp 0x1b55
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0x1a80
leaq 0x40(%rsp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x1040
movl $0x0, 0x3c(%rsp)
movl 0x3c(%rsp), %eax
movl %eax, 0x14(%rsp)
movl 0x74(%rsp), %eax
movl $0x4, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl 0x14(%rsp), %eax
cmpl %ecx, %eax
jge 0x1c04
movl 0x3c(%rsp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
je 0x1bcf
movq 0x78(%rsp), %rax
movl 0x3c(%rsp), %ecx
sarl %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
andl $0xf, %eax
movb %al, %cl
movslq 0x3c(%rsp), %rax
movb %cl, 0x40(%rsp,%rax)
jmp 0x1bf2
movq 0x78(%rsp), %rax
movl 0x3c(%rsp), %ecx
sarl %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
sarl $0x4, %eax
andl $0xf, %eax
movb %al, %cl
movslq 0x3c(%rsp), %rax
movb %cl, 0x40(%rsp,%rax)
jmp 0x1bf4
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0x1b7e
movl 0x74(%rsp), %ecx
movq 0x33e1(%rip), %rax      # 0x4ff0
movl %ecx, (%rax)
movl $0x30, 0x38(%rsp)
movq 0x33d0(%rip), %rax      # 0x4ff0
cmpl $0x40, (%rax)
jg 0x1c2d
movl $0x20, 0x38(%rsp)
leaq 0x60(%rsp), %rdi
leaq 0x40(%rsp), %rsi
xorl %edx, %edx
callq 0x10c0
movl $0x0, 0x3c(%rsp)
movl 0x3c(%rsp), %eax
movl %eax, 0x10(%rsp)
movl 0x38(%rsp), %eax
movl $0x4, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl 0x10(%rsp), %eax
cmpl %ecx, %eax
jge 0x1cd9
movl $0x0, 0x34(%rsp)
cmpl $0x4, 0x34(%rsp)
jge 0x1cb3
leaq 0x60(%rsp), %rdi
movl 0x3c(%rsp), %esi
shll $0x2, %esi
addl 0x34(%rsp), %esi
callq 0x1080
leaq 0x60(%rsp), %rdi
callq 0x1050
leaq 0x60(%rsp), %rdi
callq 0x1150
leaq 0x60(%rsp), %rdi
callq 0x10e0
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x34(%rsp)
jmp 0x1c6c
leaq 0x60(%rsp), %rdi
leaq 0x40(%rsp), %rsi
movl 0x3c(%rsp), %edx
addl $0x1, %edx
callq 0x10c0
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0x1c46
movl $0x0, 0x3c(%rsp)
cmpl $0x8, 0x3c(%rsp)
jge 0x1d92
movl 0x3c(%rsp), %eax
shll %eax
movl $0x4, %ecx
cltd
idivl %ecx
movslq %eax, %rcx
leaq 0x60(%rsp), %rax
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, (%rsp)
movl 0x3c(%rsp), %eax
shll %eax
movl $0x4, %ecx
cltd
idivl %ecx
movq (%rsp), %rax
movslq %edx, %rcx
movzbl (%rax,%rcx), %eax
andl $0xf, %eax
shll $0x4, %eax
movl %eax, 0xc(%rsp)
movl 0x3c(%rsp), %eax
shll %eax
addl $0x1, %eax
movl $0x4, %ecx
cltd
idivl %ecx
cltq
leaq 0x60(%rsp), %rcx
shlq $0x2, %rax
addq %rax, %rcx
movl 0x3c(%rsp), %eax
shll %eax
addl $0x1, %eax
movl $0x4, %esi
cltd
idivl %esi
movl 0xc(%rsp), %eax
movslq %edx, %rdx
movzbl (%rcx,%rdx), %ecx
andl $0xf, %ecx
orl %ecx, %eax
movb %al, %dl
movq 0x80(%rsp), %rax
movslq 0x3c(%rsp), %rcx
movb %dl, (%rax,%rcx)
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0x1ce1
addq $0x88, %rsp
retq
nopw (%rax,%rax)