// File: 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
movq %rsp, %rbp
subq $0x170, %rsp            # imm = 0x170
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl $0x0, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movl $0x1, %eax
cmpl -0x14(%rbp), %eax
jne 0xa11d
xorl %eax, %eax
movl %eax, %edi
callq 0x5360
movl %eax, %edi
callq 0x5370
jmp 0xa196
movl -0x14(%rbp), %edx
movl %edx, %eax
addl $-0x2, %eax
movl %edx, %ecx
incl %ecx
testl %eax, %eax
cmovsl %ecx, %eax
andl $-0x4, %eax
negl %eax
movl %eax, %ecx
movl %edx, %eax
leal -0x1(%rax,%rcx), %eax
cltq
movq %rax, -0x20(%rbp)
movl -0x14(%rbp), %ecx
movl %ecx, %eax
addl $-0x2, %eax
incl %ecx
testl %eax, %eax
cmovsl %ecx, %eax
sarl $0x2, %eax
movl %eax, -0x134(%rbp)
testl %eax, %eax
je 0xa17b
jmp 0xa15f
movl -0x134(%rbp), %eax
subl $0x1, %eax
je 0xa184
jmp 0xa16c
movl -0x134(%rbp), %eax
subl $0x2, %eax
je 0xa18d
jmp 0xa179
jmp 0xa17b
movl $0x1, -0x18(%rbp)
jmp 0xa194
movl $0x2, -0x18(%rbp)
jmp 0xa194
movl $0x3, -0x18(%rbp)
jmp 0xa196
movq $0x0, -0x28(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x54f0
movq -0x20(%rbp), %rax
movq %rax, -0x140(%rbp)
subq $0x4, %rax
ja 0xa511
movq -0x140(%rbp), %rax
leaq 0x8413(%rip), %rcx      # 0x125e0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq $0x0, -0xb0(%rbp)
cmpq $0x4, -0xb0(%rbp)
jae 0xa2a7
callq 0x50a0
movl %eax, %edx
addl $0x3, %edx
testl %eax, %eax
movl %eax, %ecx
cmovsl %edx, %ecx
andl $-0x4, %ecx
subl %ecx, %eax
movl %eax, %ecx
movq %rcx, -0x148(%rbp)
subl $0x3, %eax
ja 0xa22a
movq -0x148(%rbp), %rax
leaq 0x83d3(%rip), %rcx      # 0x125f4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
jmp 0xa22c
movl $0x0, -0x18(%rbp)
jmp 0xa24e
movl $0x1, -0x18(%rbp)
jmp 0xa24e
movl $0x2, -0x18(%rbp)
jmp 0xa24e
movl $0x3, -0x18(%rbp)
movq -0x10(%rbp), %rdi
callq 0x7880
movq %rax, %rdi
movl -0x18(%rbp), %esi
movq -0xb0(%rbp), %rdx
movq (%rdi), %rax
movl $0x1, %ecx
callq *0x28(%rax)
movq -0x10(%rbp), %rdi
callq 0x7880
movq %rax, %rdi
movl -0x18(%rbp), %esi
xorl $-0x1, %esi
movq -0xb0(%rbp), %rdx
movq (%rdi), %rax
xorl %ecx, %ecx
callq *0x28(%rax)
movq -0xb0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xb0(%rbp)
jmp 0xa1e1
jmp 0xa530
movq $0x0, -0xb8(%rbp)
cmpq $0x4, -0xb8(%rbp)
jae 0xa310
movq -0x10(%rbp), %rdi
callq 0x7880
movq %rax, %rdi
movl -0x18(%rbp), %esi
movq -0xb8(%rbp), %rdx
movq -0xb8(%rbp), %rax
movl $0x3, %ecx
subq -0x28(%rbp), %rcx
cmpq %rcx, %rax
sete %cl
movq (%rdi), %rax
movq 0x28(%rax), %rax
movzbl %cl, %ecx
andl $0x1, %ecx
callq *%rax
movq -0xb8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xb8(%rbp)
jmp 0xa2b7
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
cmpq $0x4, %rax
jb 0xa32a
movq $0x0, -0x28(%rbp)
jmp 0xa530
movq $0x0, -0xc0(%rbp)
cmpq $0x4, -0xc0(%rbp)
jae 0xa38b
movq -0x10(%rbp), %rdi
callq 0x7880
movq %rax, %rdi
movl -0x18(%rbp), %esi
movq -0xc0(%rbp), %rdx
movq -0xc0(%rbp), %rax
cmpq -0x28(%rbp), %rax
sete %cl
movq (%rdi), %rax
movq 0x28(%rax), %rax
movzbl %cl, %ecx
andl $0x1, %ecx
callq *%rax
movq -0xc0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xc0(%rbp)
jmp 0xa33a
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
cmpq $0x4, %rax
jb 0xa3a5
movq $0x0, -0x28(%rbp)
jmp 0xa530
cmpq $0x3, -0x28(%rbp)
jae 0xa3be
movq -0x28(%rbp), %rax
movq %rax, -0x150(%rbp)
jmp 0xa3ce
movl $0x6, %eax
subq -0x28(%rbp), %rax
movq %rax, -0x150(%rbp)
movq -0x150(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq $0x0, -0xd0(%rbp)
cmpq $0x4, -0xd0(%rbp)
jae 0xa43b
movq -0x10(%rbp), %rdi
callq 0x7880
movq %rax, %rdi
movl -0x18(%rbp), %esi
movq -0xd0(%rbp), %rdx
movq -0xd0(%rbp), %rax
cmpq -0xc8(%rbp), %rax
sete %cl
movq (%rdi), %rax
movq 0x28(%rax), %rax
movzbl %cl, %ecx
andl $0x1, %ecx
callq *%rax
movq -0xd0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xd0(%rbp)
jmp 0xa3e7
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
cmpq $0x6, %rax
jb 0xa455
movq $0x0, -0x28(%rbp)
jmp 0xa530
movq $0x0, -0xd8(%rbp)
cmpq $0x4, -0xd8(%rbp)
jae 0xa4a4
movq -0x10(%rbp), %rdi
callq 0x7880
movq %rax, %rdi
movl -0x18(%rbp), %esi
movq -0xd8(%rbp), %rdx
movq (%rdi), %rax
movl $0x1, %ecx
callq *0x28(%rax)
movq -0xd8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xd8(%rbp)
jmp 0xa465
cmpq $0x9, -0x28(%rbp)
jae 0xa4b8
movq -0x28(%rbp), %rax
movq %rax, -0x158(%rbp)
jmp 0xa4c8
movl $0x10, %eax
subq -0x28(%rbp), %rax
movq %rax, -0x158(%rbp)
movq -0x158(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xe0(%rbp)
movq -0x10(%rbp), %rdi
callq 0x7880
movq %rax, %rdi
movq -0xe0(%rbp), %rax
movl %eax, %esi
movq (%rdi), %rax
callq *0x30(%rax)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
cmpq $0x10, %rax
jb 0xa50f
movq $0x0, -0x28(%rbp)
jmp 0xa530
movq 0xda98(%rip), %rdi      # 0x17fb0
leaq 0x85c2(%rip), %rsi      # 0x12ae1
callq 0x5380
movl $0x1, -0x4(%rbp)
jmp 0xa694
movq 0x89c9(%rip), %rax      # 0x12f00
movq %rax, -0xf0(%rbp)
movq 0x89c3(%rip), %rax      # 0x12f08
movq %rax, -0xe8(%rbp)
xorl %edi, %edi
xorl %eax, %eax
movl %eax, %ecx
leaq -0xf0(%rbp), %r8
leaq -0xa8(%rbp), %r9
movq %rcx, %rsi
movq %rcx, %rdx
callq 0x5410
movl %eax, -0xf4(%rbp)
cmpl $0x0, -0xf4(%rbp)
jge 0xa688
callq 0x5030
movq %rax, %rcx
movl $0x4, %eax
cmpl (%rcx), %eax
je 0xa673
movb $0x1, -0x12d(%rbp)
movl $0x10, %edi
callq 0x51b0
movq %rax, -0x168(%rbp)
leaq -0x119(%rbp), %rdi
movq %rdi, -0x160(%rbp)
callq 0x56c0
movq -0x160(%rbp), %rdx
leaq 0x7b02(%rip), %rsi      # 0x120ce
leaq -0x118(%rbp), %rdi
callq 0x7600
jmp 0xa5da
callq 0x5030
movq -0x168(%rbp), %rdi
movl (%rax), %edx
leaq -0x118(%rbp), %rsi
callq 0x76b0
jmp 0xa5f6
movq -0x168(%rbp), %rdi
movb $0x0, -0x12d(%rbp)
leaq 0xcff5(%rip), %rsi      # 0x17600
leaq -0x2e92(%rip), %rdx     # 0x7780
callq 0x5680
jmp 0xa6ac
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x128(%rbp)
movl %eax, -0x12c(%rbp)
jmp 0xa64e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x128(%rbp)
movl %eax, -0x12c(%rbp)
leaq -0x118(%rbp), %rdi
callq 0x5230
leaq -0x119(%rbp), %rdi
callq 0x53f0
testb $0x1, -0x12d(%rbp)
jne 0xa665
jmp 0xa671
movq -0x168(%rbp), %rdi
callq 0x52a0
jmp 0xa6a0
movq 0xd936(%rip), %rdi      # 0x17fb0
leaq 0x7a54(%rip), %rsi      # 0x120d5
callq 0x5380
jmp 0xa68d
jmp 0xa1aa
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x170, %rsp            # imm = 0x170
popq %rbp
retq
movq -0x128(%rbp), %rdi
callq 0x56b0
nopl (%rax)