// File: /workspace/github_cpp_makefile_star2_O3/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 0x746e
xorl %r14d, %r14d
xorl %edi, %edi
callq 0x4260
movl %eax, %edi
callq 0x4270
leaq 0x48(%rsp), %rdi
callq 0x4390
xorl %ebp, %ebp
jmp 0x74b4
leal -0x2(%rsi), %eax
leal 0x1(%rsi), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
andl $-0x4, %ecx
negl %ecx
leal (%rsi,%rcx), %ebp
decl %ebp
leal -0x6(%rsi), %eax
xorl %ecx, %ecx
cmpl $0x4, %eax
adcl $0x1, %ecx
addl $-0xa, %esi
cmpl $0x4, %esi
movl $0x3, %r14d
cmovael %ecx, %r14d
leaq 0x48(%rsp), %rdi
callq 0x4390
cmpl $0x4, %ebp
ja 0x767d
movslq %ebp, %rbp
xorl %r15d, %r15d
xorl %r12d, %r12d
movq %rbp, 0x8(%rsp)
cmpq $0x3, %rbp
ja 0x75e1
leaq 0x4128(%rip), %rcx      # 0xb5f8
movslq (%rcx,%rbp,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %rbp, %r13
callq 0x4070
leal 0x3(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
andl $-0x4, %ecx
movl %eax, %r14d
subl %ecx, %r14d
negl %ecx
addl %ecx, %eax
decl %eax
cmpl $0x3, %eax
cmovael %r15d, %r14d
movq (%rbx), %rdi
movq (%rdi), %rax
movl %r14d, %esi
movq %r13, %rdx
movl $0x1, %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
cmpq $0x4, %r13
jne 0x74dc
jmp 0x762c
xorl %r13d, %r13d
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
cmpq $0x4, %r13
jne 0x7537
incq %r12
cmpq $0x4, %r12
jmp 0x7623
movl $0x6, %ebp
subq %r12, %rbp
cmpq $0x3, %r12
cmovbq %r12, %rbp
xorl %r13d, %r13d
movq (%rbx), %rdi
xorl %ecx, %ecx
cmpq %r13, %rbp
sete %cl
movq (%rdi), %rax
movl %r14d, %esi
movq %r13, %rdx
callq *0x28(%rax)
incq %r13
cmpq $0x4, %r13
jne 0x7576
incq %r12
cmpq $0x6, %r12
jmp 0x75d1
movl $0x3, %ebp
subq %r12, %rbp
xorl %r13d, %r13d
movq (%rbx), %rdi
xorl %ecx, %ecx
cmpq %r13, %rbp
sete %cl
movq (%rdi), %rax
movl %r14d, %esi
movq %r13, %rdx
callq *0x28(%rax)
incq %r13
cmpq $0x4, %r13
jne 0x75aa
incq %r12
cmpq $0x4, %r12
movl $0x0, %eax
cmovaeq %rax, %r12
movq 0x8(%rsp), %rbp
jmp 0x762c
xorl %r13d, %r13d
movq (%rbx), %rdi
movq (%rdi), %rax
movl %r14d, %esi
movq %r13, %rdx
movl $0x1, %ecx
callq *0x28(%rax)
incq %r13
cmpq $0x4, %r13
jne 0x75e4
movl $0x10, %esi
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 0x45ed(%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
jns 0x74bf
callq 0x4030
cmpl $0x4, (%rax)
jne 0x76ac
movq 0x8948(%rip), %rdi      # 0xffb0
leaq 0x3a66(%rip), %rsi      # 0xb0d5
movl $0x12, %edx
callq 0x4310
xorl %eax, %eax
jmp 0x769a
movq 0x892c(%rip), %rdi      # 0xffb0
leaq 0x4392(%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, %r14
movl $0x10, %edi
callq 0x4140
movq %rax, %rbx
leaq 0x20(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x3a02(%rip), %rsi      # 0xb0ce
leaq 0x3a01(%rip), %rdx      # 0xb0d4
leaq 0x10(%rsp), %rdi
callq 0x5e90
movl (%r14), %edx
movb $0x1, %bpl
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x5be6
xorl %ebp, %ebp
leaq 0x8087(%rip), %rsi      # 0xf780
movq 0x88a0(%rip), %rdx      # 0xffa0
movq %rbx, %rdi
callq 0x4520
movq %rax, %r14
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
je 0x7722
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x42c0
testb %bpl, %bpl
jne 0x772c
jmp 0x7734
movq %rax, %r14
movq %rbx, %rdi
callq 0x41e0
movq %r14, %rdi
callq 0x4550