// File: /workspace/github_cpp_makefile_star2_O2/merelin[P]mediasmartserverd/src/mediasmartserverd.cpp
// Function: run_light_show(std::tr1::shared_ptr<LedControlBase> const&, int)
int run_light_show( const LedControlPtr& leds, int light_show ) {
	
	int light_leds = 0;
	size_t show_mode = 0;
	if ( 1 == light_show ) {
		// holiday lights
		srand( time(0) );
	} else {
		show_mode = (light_show - 2) % 4 + 1;
		switch ( (light_show - 2) / 4 ) {
		default:
		case 0: light_leds = LED_BLUE; break;
		case 1: light_leds = LED_RED;  break;
		case 2: light_leds = LED_BLUE | LED_RED; break;
		}
	}
	
	
	size_t state = 0;
	
	sigset_t sigempty;
	sigemptyset( &sigempty );
	while ( true ) {
		
		switch ( show_mode ) {
		case 0: // holiday lights
		{
			for ( size_t i = 0; i < 4; ++i ) {
				switch ( rand() % 4 ) {
				default:
				case 0: light_leds = 0; break;
				case 1: light_leds = LED_BLUE; break;
				case 2: light_leds = LED_RED;  break;
				case 3: light_leds = LED_BLUE | LED_RED; break;
				}
				
				leds->Set(  light_leds, i, true );
				leds->Set( ~light_leds, i, false );
			}
			break;
		}
		case 1: // descending chasers
		{
			for ( size_t i = 0; i < 4; ++i ) leds->Set( light_leds, i, (i == (3 - state)) );
			if ( ++state >= 4 ) state = 0;
			break;
		}
		case 2: // ascending chasers
		{
			for ( size_t i = 0; i < 4; ++i ) leds->Set( light_leds, i, (i == state) );
			if ( ++state >= 4 ) state = 0;
			break;
		}
		case 3: // knight rider
		{
			const size_t sel = ( state < 3 ) ? state : 6 - state;
			for ( size_t i = 0; i < 4; ++i ) leds->Set( light_leds, i, (i == sel) );
			if ( ++state >= 6 ) state = 0;
			break;
		}
		case 4: // pulsing
		{
			for ( size_t i = 0; i < 4; ++i ) leds->Set( light_leds, i, true );
			const size_t sel = 1 + ( ( state < 9 ) ? state : 16 - state );
			leds->SetBrightness( sel );
			if ( ++state >= 16 ) state = 0;
			break;
		}
		default:
			cout << "Unsupported light show\n";
			return 1;
		}
		
		
		// wait a bit
		struct timespec timeout = { 0, 200000000 };
		int res = pselect( 0, 0, 0, 0, &timeout, &sigempty );
		if ( res < 0 ) {
			if ( EINTR != errno ) throw ErrnoException( "select" );
			cout << "Exiting on signal\n";
			break; // signalled
		}
	}
	
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rdi, %rbx
pushq $0x4
popq %r15
cmpl $0x1, %esi
jne 0x69d6
xorl %r13d, %r13d
xorl %edi, %edi
callq 0x42b0
movl %eax, %edi
callq 0x42c0
xorl %r14d, %r14d
jmp 0x6a03
addl $-0x2, %esi
movl %esi, %eax
cltd
idivl %r15d
leal (,%rax,4), %ecx
subl %ecx, %esi
incl %esi
movslq %esi, %r13
cmpl $0x2, %eax
je 0x69fb
cmpl $0x1, %eax
jne 0x69ff
pushq $0x2
jmp 0x6a01
pushq $0x3
jmp 0x6a01
pushq $0x1
popq %r14
leaq 0x48(%rsp), %rdi
callq 0x43d0
cmpq $0x4, %r13
ja 0x6bd0
xorl %r12d, %r12d
pushq $0x1
popq %rbp
movq %r13, 0x8(%rsp)
cmpq $0x3, %r13
ja 0x6b37
leaq 0x3bad(%rip), %rcx      # 0xa5e0
movslq (%rcx,%r13,4), %rax
addq %rcx, %rax
jmpq *%rax
cmpq $0x4, %r13
je 0x6b7f
callq 0x4080
cltd
idivl %r15d
movl %edx, %r14d
leal -0x1(%r14), %eax
cmpl $0x3, %eax
jb 0x6a5e
xorl %r14d, %r14d
movq (%rbx), %rdi
movq (%rdi), %rax
movl %r14d, %esi
movq %r13, %rdx
movl %ebp, %ecx
callq *0x28(%rax)
movq (%rbx), %rdi
movl %r14d, %esi
notl %esi
movq (%rdi), %rax
movq %r13, %rdx
xorl %ecx, %ecx
callq *0x28(%rax)
incq %r13
jmp 0x6a3c
xorl %r13d, %r13d
cmpq $0x4, %r13
je 0x6aac
movq (%rbx), %rdi
xorl %ecx, %ecx
cmpq %r13, %r12
sete %cl
movq (%rdi), %rax
movl %r14d, %esi
movq %r13, %rdx
callq *0x28(%rax)
incq %r13
jmp 0x6a8a
incq %r12
cmpq $0x4, %r12
jmp 0x6b76
pushq $0x6
popq %r15
subq %r12, %r15
cmpq $0x3, %r12
cmovbq %r12, %r15
xorl %r13d, %r13d
cmpq $0x4, %r13
je 0x6aec
movq (%rbx), %rdi
xorl %ecx, %ecx
cmpq %r13, %r15
sete %cl
movq (%rdi), %rax
movl %r14d, %esi
movq %r13, %rdx
callq *0x28(%rax)
incq %r13
jmp 0x6aca
incq %r12
cmpq $0x6, %r12
jmp 0x6b28
pushq $0x3
popq %r15
subq %r12, %r15
xorl %r13d, %r13d
cmpq $0x4, %r13
je 0x6b21
movq (%rbx), %rdi
xorl %ecx, %ecx
cmpq %r13, %r15
sete %cl
movq (%rdi), %rax
movl %r14d, %esi
movq %r13, %rdx
callq *0x28(%rax)
incq %r13
jmp 0x6aff
incq %r12
cmpq $0x4, %r12
movl $0x0, %eax
cmovaeq %rax, %r12
pushq $0x4
popq %r15
jmp 0x6b7f
xorl %r13d, %r13d
cmpq $0x4, %r13
je 0x6b56
movq (%rbx), %rdi
movq (%rdi), %rax
movl %r14d, %esi
movq %r13, %rdx
movl %ebp, %ecx
callq *0x28(%rax)
incq %r13
jmp 0x6b3a
pushq $0x10
popq %rsi
subl %r12d, %esi
cmpq $0x9, %r12
movq (%rbx), %rdi
cmovbl %r12d, %esi
incl %esi
movq (%rdi), %rax
callq *0x30(%rax)
incq %r12
cmpq $0x10, %r12
movl $0x0, %eax
cmovaeq %rax, %r12
movups 0x429a(%rip), %xmm0     # 0xae20
movaps %xmm0, 0x10(%rsp)
xorl %edi, %edi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
leaq 0x10(%rsp), %r8
leaq 0x48(%rsp), %r9
callq 0x4330
testl %eax, %eax
movq 0x8(%rsp), %r13
jns 0x6a22
callq 0x4030
cmpl $0x4, (%rax)
jne 0x6bf8
movq 0x93f0(%rip), %rdi      # 0xffb0
leaq 0x350e(%rip), %rsi      # 0xa0d5
callq 0x42d0
xorl %eax, %eax
jmp 0x6be6
movq 0x93d9(%rip), %rdi      # 0xffb0
leaq 0x3e27(%rip), %rsi      # 0xaa05
callq 0x42d0
pushq $0x1
popq %rax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
pushq $0x10
popq %rdi
callq 0x4160
movq %rax, %rbx
leaq 0x34c1(%rip), %rsi      # 0xa0ce
leaq 0x28(%rsp), %rdi
leaq 0x7(%rsp), %rdx
callq 0x5694
movl (%r14), %edx
movb $0x1, %bpl
leaq 0x28(%rsp), %rsi
movq %rbx, %rdi
callq 0x56d0
xorl %ebp, %ebp
leaq 0x8aa8(%rip), %rsi      # 0xf6e0
movq 0x9361(%rip), %rdx      # 0xffa0
movq %rbx, %rdi
callq 0x4530
movq %rax, %r14
leaq 0x28(%rsp), %rdi
callq 0x4150
testb %bpl, %bpl
jne 0x6c5e
jmp 0x6c66
movq %rax, %r14
movq %rbx, %rdi
callq 0x4220
movq %r14, %rdi
callq 0x4550