// File: /workspace/github_cpp_makefile_star2_O1/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
cmpl $0x1, %esi
jne 0x7467
xorl %r12d, %r12d
xorl %edi, %edi
callq 0x4260
movl %eax, %edi
callq 0x4270
xorl %ebp, %ebp
jmp 0x749e
leal -0x2(%rsi), %ecx
leal 0x1(%rsi), %eax
testl %ecx, %ecx
cmovnsl %ecx, %eax
movl %eax, %ecx
andl $-0x4, %ecx
negl %ecx
addl %esi, %ecx
decl %ecx
movslq %ecx, %r12
sarl $0x2, %eax
cmpl $0x2, %eax
je 0x7499
movl $0x1, %ebp
cmpl $0x1, %eax
jne 0x749e
movl $0x2, %ebp
jmp 0x749e
movl $0x3, %ebp
leaq 0x48(%rsp), %rdi
callq 0x4390
cmpq $0x4, %r12
ja 0x7671
xorl %r15d, %r15d
xorl %r14d, %r14d
movq %r12, 0x8(%rsp)
cmpq $0x3, %r12
ja 0x75bf
leaq 0x412a(%rip), %rcx      # 0xb5f8
movslq (%rcx,%r12,4), %rax
addq %rcx, %rax
jmpq *%rax
callq 0x4070
leal 0x3(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
andl $-0x4, %ecx
movl %eax, %ebp
subl %ecx, %ebp
negl %ecx
addl %ecx, %eax
decl %eax
cmpl $0x3, %eax
cmovael %r15d, %ebp
movq (%rbx), %rdi
movq (%rdi), %rax
movl %ebp, %esi
movq %r12, %rdx
movl $0x1, %ecx
callq *0x28(%rax)
movq (%rbx), %rdi
movl %ebp, %esi
notl %esi
movq (%rdi), %rax
movq %r12, %rdx
xorl %ecx, %ecx
callq *0x28(%rax)
incq %r12
cmpq $0x4, %r12
jne 0x74d7
jmp 0x7609
xorl %r12d, %r12d
movq (%rbx), %rdi
xorl %ecx, %ecx
cmpq %r12, %r14
sete %cl
movq (%rdi), %rax
movl %ebp, %esi
movq %r12, %rdx
callq *0x28(%rax)
incq %r12
cmpq $0x4, %r12
jne 0x752e
jmp 0x75b6
movl $0x6, %r13d
subq %r14, %r13
cmpq $0x3, %r14
cmovbq %r14, %r13
xorl %r12d, %r12d
movq (%rbx), %rdi
xorl %ecx, %ecx
cmpq %r12, %r13
sete %cl
movq (%rdi), %rax
movl %ebp, %esi
movq %r12, %rdx
callq *0x28(%rax)
incq %r12
cmpq $0x4, %r12
jne 0x7563
incq %r14
cmpq $0x6, %r14
jmp 0x7600
movl $0x3, %r13d
subq %r14, %r13
xorl %r12d, %r12d
movq (%rbx), %rdi
xorl %ecx, %ecx
cmpq %r12, %r13
sete %cl
movq (%rdi), %rax
movl %ebp, %esi
movq %r12, %rdx
callq *0x28(%rax)
incq %r12
cmpq $0x4, %r12
jne 0x7597
incq %r14
cmpq $0x4, %r14
jmp 0x7600
xorl %r12d, %r12d
movq (%rbx), %rdi
movq (%rdi), %rax
movl %ebp, %esi
movq %r12, %rdx
movl $0x1, %ecx
callq *0x28(%rax)
incq %r12
cmpq $0x4, %r12
jne 0x75c2
movl $0x10, %esi
subl %r14d, %esi
cmpq $0x9, %r14
movq (%rbx), %rdi
cmovbl %r14d, %esi
incl %esi
movq (%rdi), %rax
callq *0x30(%rax)
incq %r14
cmpq $0x10, %r14
movl $0x0, %eax
cmovaeq %rax, %r14
movups 0x4610(%rip), %xmm0     # 0xbc20
movaps %xmm0, 0x30(%rsp)
xorl %edi, %edi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
leaq 0x30(%rsp), %r8
leaq 0x48(%rsp), %r9
callq 0x42e0
testl %eax, %eax
js 0x763a
movq 0x8(%rsp), %r12
jmp 0x74bd
movl %eax, %r13d
callq 0x4030
cmpl $0x4, (%rax)
jne 0x76a0
movl $0x12, %edx
movq 0x895d(%rip), %rdi      # 0xffb0
leaq 0x3a7b(%rip), %rsi      # 0xb0d5
callq 0x4310
testl %r13d, %r13d
movq 0x8(%rsp), %r12
jns 0x74bd
xorl %eax, %eax
jmp 0x768e
movq 0x8938(%rip), %rdi      # 0xffb0
leaq 0x439e(%rip), %rsi      # 0xba1d
movl $0x17, %edx
callq 0x4310
movl $0x1, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r12
movl $0x10, %edi
callq 0x4140
movq %rax, %rbx
leaq 0x20(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x3a0e(%rip), %rsi      # 0xb0ce
leaq 0x3a0d(%rip), %rdx      # 0xb0d4
leaq 0x10(%rsp), %rdi
callq 0x5e88
movl (%r12), %edx
movb $0x1, %bpl
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x5be8
xorl %ebp, %ebp
leaq 0x8002(%rip), %rsi      # 0xf6f0
movq 0x88ab(%rip), %rdx      # 0xffa0
movq %rbx, %rdi
callq 0x4520
movq %rax, %r14
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
je 0x771f
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x42c0
jmp 0x771f
movq %rax, %r14
movb $0x1, %bpl
testb %bpl, %bpl
je 0x772c
movq %rbx, %rdi
callq 0x41e0
movq %r14, %rdi
callq 0x4550