// File: /workspace/github_cpp_makefile_star2_O2/merelin[P]mediasmartserverd/src/device_monitor.cpp
// Function: DeviceMonitor::Main()
void DeviceMonitor::Main( ) {
	assert( dev_monitor_ );
	
	const int fd_mon = udev_monitor_get_fd( dev_monitor_ );
	const int nfds = fd_mon + 1;
        int queue_tok = 8;
	
	sigset_t sigempty;
	sigemptyset( &sigempty );

        struct timespec timeout;

        if( activity )
        {
            timeout.tv_sec = 0;
            //timeout.tv_sec = 1;
            timeout.tv_nsec = 100000000;
            //timeout.tv_nsec = 0;
        }
        else
        {
            timeout.tv_sec = 999;
            timeout.tv_nsec = 0;
        }

	while ( true ) {
		fd_set fds_read;
		FD_ZERO( &fds_read );
		FD_SET( fd_mon, &fds_read );
		
		// block for something interesting to happen
		int res = pselect( nfds, &fds_read, 0, 0, &timeout, &sigempty );
		if ( res < 0 ) {
			if ( EINTR != errno ) throw ErrnoException( "select" );
			std::cout << "Exiting on signal\n";
			return; // signalled
		}
		
		// udev monitor notification?
		if ( FD_ISSET( fd_mon, &fds_read ) ) {
			std::tr1::shared_ptr< udev_device > device( udev_monitor_receive_device( dev_monitor_ ), &udev_device_unref );
                        // make sure this is a device we want to monitor
			if (!acceptDevice_(device.get())) continue;
	
			const char* str = udev_device_get_action( device.get() );
			if ( !str ) {
			} else if ( 0 == strcasecmp( str, "add" ) ) {
				deviceAdded_( device.get() );
			} else if ( 0 == strcasecmp( str, "remove" ) ) {
				deviceRemove_( device.get() );
			} else {
				if ( debug ) {
					std::cout << "action: " << str << '\n';
					std::cout << ' ' << udev_device_get_syspath(device.get()) << "' (" << udev_device_get_subsystem(device.get()) << ")\n";
				}
			}
		}

                if( activity )
                {
                    for( int i = 0; i < num_disks_; ++i )
                    {
                        std::ifstream stats;
                        stats.open( statsFile(i).c_str() );

                        char buf[256];
                        stats.getline( &(buf[0]), 255 );
                        std::string s(&(buf[0]));

                        // tokenize the string
                        std::string::size_type last = s.find_first_not_of( " ", 0 );
                        std::string::size_type pos = s.find_first_of( " ", last );

                        int tok = 0;
                        int queue_length = 0;
                        while( std::string::npos != pos || std::string::npos != last )
                        {
                            last = s.find_first_not_of(" ", pos );
                            pos = s.find_first_of( " ", last );
                            ++tok;

                            if( tok == queue_tok )
                            {
                                queue_length = atoi(s.substr( last, pos - last ).c_str());
                                if( debug )
                                    std::cout << " " << i << " " << queue_length;
                                break;
                            }
                        }

                        int led_idx = ledIndex(i);

                        if( led_enabled_[led_idx] && leds_ )
                        {
                            if( queue_length > 0 )
                            {
                                leds_->Set( LED_BLUE, led_idx, true );
                                leds_->Set( LED_RED, led_idx, true );
                            }
                            else
                            {
                                leds_->Set( LED_BLUE, led_idx, true );
                                leds_->Set( LED_RED, led_idx, false );
                            }
                        }
                    }
                }
                if( debug )
                    std::cout << "\n";
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x488, %rsp            # imm = 0x488
movq %rdi, %rbx
movq 0x8(%rdi), %rdi
testq %rdi, %rdi
je 0x519c
callq 0x4360
movl %eax, %r14d
leal 0x1(%r14), %eax
movl %eax, 0x14(%rsp)
leaq 0x300(%rsp), %rdi
callq 0x43d0
leaq 0xb5e1(%rip), %rcx      # 0x10368
xorl %eax, %eax
cmpb $0x0, (%rcx)
movl $0x3e7, %ecx            # imm = 0x3E7
cmovneq %rax, %rcx
movl $0x5f5e100, %edx        # imm = 0x5F5E100
cmoveq %rax, %rdx
leaq 0x28(%rsp), %rax
movq %rcx, (%rax)
movq %rdx, 0x8(%rax)
pushq $0x40
popq %rcx
movl %r14d, %eax
cltd
idivl %ecx
movslq %eax, %rdx
shll $0x6, %eax
subl %eax, %r14d
pushq $0x1
popq %rsi
movl %r14d, %ecx
movq %rdx, %r14
shlq %cl, %rsi
leaq 0x28(%rbx), %rax
movq %rax, 0x20(%rsp)
leaq 0x58(%rsp), %r15
leaq 0x56c2(%rip), %rbp      # 0xa49f
movq %rsi, 0x8(%rsp)
movq %rdx, 0x18(%rsp)
xorl %eax, %eax
cmpq $0x10, %rax
je 0x4dfa
andq $0x0, 0x78(%rsp,%rax,8)
incq %rax
jmp 0x4de9
orq %rsi, 0x78(%rsp,%r14,8)
movl 0x14(%rsp), %edi
movq %rsi, %r12
leaq 0x78(%rsp), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
leaq 0x28(%rsp), %r8
leaq 0x300(%rsp), %r9
callq 0x4330
testl %eax, %eax
js 0x516d
testq %r12, 0x78(%rsp,%r14,8)
movq %r12, %rsi
je 0x4fa3
movq 0x8(%rbx), %rdi
callq 0x40f0
leaq 0xf8(%rsp), %rdi
movq %rax, %rsi
movq 0xb176(%rip), %rdx      # 0xffc8
callq 0x5946
movq 0xf8(%rsp), %rsi
callq 0x5278
movl %eax, %r14d
testb %al, %al
je 0x4f54
movq 0xf8(%rsp), %rdi
callq 0x4050
movq %rax, %r13
testq %rax, %rax
je 0x4f88
movq %r13, %rdi
leaq 0x5256(%rip), %rsi      # 0xa0e8
callq 0x4340
testl %eax, %eax
je 0x4f66
movq %r13, %rdi
leaq 0x5243(%rip), %rsi      # 0xa0ec
callq 0x4340
testl %eax, %eax
je 0x4f78
leaq 0xb4a3(%rip), %rax      # 0x10360
cmpl $0x0, (%rax)
je 0x4f88
movq 0xb0e3(%rip), %rdi      # 0xffb0
leaq 0x521f(%rip), %rsi      # 0xa0f3
callq 0x42d0
movq %rax, %rdi
movq %r13, %rsi
callq 0x42d0
movq %rax, %rdi
pushq $0xa
popq %rsi
callq 0x4370
movq 0xb0ba(%rip), %rdi      # 0xffb0
pushq $0x20
popq %rsi
callq 0x4370
movq %rax, %r12
movq 0xf8(%rsp), %rdi
callq 0x4510
movq %r12, %rdi
movq %rax, %rsi
callq 0x42d0
movq %rax, %rdi
leaq 0x51d9(%rip), %rsi      # 0xa0fc
callq 0x42d0
movq %rax, %r12
movq 0xf8(%rsp), %rdi
callq 0x40d0
movq %r12, %rdi
movq %rax, %rsi
callq 0x42d0
movq %rax, %rdi
leaq 0x58fb(%rip), %rsi      # 0xa848
callq 0x42d0
jmp 0x4f88
leaq 0x100(%rsp), %rdi
callq 0x5758
jmp 0x515e
movq 0xf8(%rsp), %rsi
movq %rbx, %rdi
callq 0x52c8
jmp 0x4f88
movq 0xf8(%rsp), %rsi
movq %rbx, %rdi
callq 0x5344
leaq 0x100(%rsp), %rdi
callq 0x5758
testb %r14b, %r14b
movq 0x8(%rsp), %rsi
je 0x5163
leaq 0xb3be(%rip), %rax      # 0x10368
cmpb $0x1, (%rax)
leaq 0xf8(%rsp), %r12
jne 0x513f
xorl %r14d, %r14d
movslq 0x20(%rbx), %rax
cmpq %rax, %r14
jge 0x513f
movq %r12, %rdi
callq 0x4350
movq %r14, %rax
shlq $0x5, %rax
movq 0x20(%rsp), %rcx
movq (%rcx,%rax), %rsi
movq %r12, %rdi
pushq $0x8
popq %rdx
callq 0x41c0
movl $0xff, %edx
movq %r12, %rdi
leaq 0x380(%rsp), %r12
movq %r12, %rsi
callq 0x40b0
movq %r15, %rdi
movq %r12, %rsi
leaq 0x38(%rsp), %rdx
callq 0x5694
movq %r15, %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq 0x41a0
movq %rax, %r13
movq %r15, %rdi
movq %rbp, %rsi
movq %rax, %rdx
callq 0x4180
pushq $0x8
popq %r12
andq %rax, %r13
cmpq $-0x1, %r13
je 0x50d5
movq %r15, %rdi
movq %rbp, %rsi
movq %rax, %rdx
callq 0x41a0
movq %rax, %r13
movq %r15, %rdi
movq %rbp, %rsi
movq %rax, %rdx
callq 0x4180
decl %r12d
jne 0x5038
subq %r13, %rax
leaq 0x38(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
movq %r13, %rdx
movq %rax, %rcx
callq 0x4310
movq 0x38(%rsp), %rdi
callq 0x44d0
movl %eax, %r13d
movq %r12, %rdi
callq 0x4150
leaq 0xb2c2(%rip), %rax      # 0x10360
cmpl $0x0, (%rax)
je 0x50d8
movq 0xaf06(%rip), %rdi      # 0xffb0
movq %rbp, %rsi
callq 0x42d0
movq %rax, %rdi
movl %r14d, %esi
callq 0x4540
movq %rax, %rdi
movq %rbp, %rsi
callq 0x42d0
movq %rax, %rdi
movl %r13d, %esi
callq 0x4540
jmp 0x50d8
xorl %r13d, %r13d
movslq 0x174(%rbx,%r14,4), %r12
cmpb $0x1, 0x168(%rbx,%r12)
jne 0x511a
movq 0x10(%rbx), %rdi
testq %rdi, %rdi
je 0x511a
movq (%rdi), %rax
pushq $0x1
popq %rcx
movl %ecx, %esi
movq %r12, %rdx
callq *0x28(%rax)
xorl %ecx, %ecx
testl %r13d, %r13d
setg %cl
movq 0x10(%rbx), %rdi
movq (%rdi), %rax
pushq $0x2
popq %rsi
movq %r12, %rdx
callq *0x28(%rax)
movq %r15, %rdi
callq 0x4150
leaq 0xf8(%rsp), %r12
movq %r12, %rdi
callq 0x4070
incq %r14
movq 0x8(%rsp), %rsi
jmp 0x4fbe
leaq 0xb21a(%rip), %rax      # 0x10360
cmpl $0x0, (%rax)
je 0x5163
movq 0xae5e(%rip), %rdi      # 0xffb0
leaq 0x559a(%rip), %rsi      # 0xa6f3
callq 0x42d0
movq 0x8(%rsp), %rsi
movq 0x18(%rsp), %r14
jmp 0x4de7
callq 0x4030
cmpl $0x4, (%rax)
jne 0x51b9
movq 0xae32(%rip), %rdi      # 0xffb0
leaq 0x4f50(%rip), %rsi      # 0xa0d5
callq 0x42d0
addq $0x488, %rsp            # imm = 0x488
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4eec(%rip), %rdi      # 0xa08f
leaq 0x4ef2(%rip), %rsi      # 0xa09c
leaq 0x4f02(%rip), %rcx      # 0xa0b3
pushq $0x5b
popq %rdx
callq 0x41d0
movq %rax, %rbx
pushq $0x10
popq %rdi
callq 0x4160
movq %rax, %r14
leaq 0x4f00(%rip), %rsi      # 0xa0ce
leaq 0xf8(%rsp), %rdi
leaq 0x380(%rsp), %rdx
callq 0x5694
movl (%rbx), %edx
movb $0x1, %bpl
leaq 0xf8(%rsp), %rsi
movq %r14, %rdi
callq 0x56d0
xorl %ebp, %ebp
leaq 0xa4df(%rip), %rsi      # 0xf6e0
movq 0xad98(%rip), %rdx      # 0xffa0
movq %r14, %rdi
callq 0x4530
movq %rax, %rbx
leaq 0xf8(%rsp), %rdi
callq 0x4150
testb %bpl, %bpl
jne 0x522a
jmp 0x526f
movq %rax, %rbx
movq %r14, %rdi
callq 0x4220
jmp 0x526f
jmp 0x5236
movq %rax, %rbx
leaq 0x100(%rsp), %rdi
callq 0x5758
jmp 0x526f
jmp 0x5255
jmp 0x5255
jmp 0x5250
jmp 0x5250
movq %rax, %rbx
jmp 0x5262
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x4150
leaq 0xf8(%rsp), %rdi
callq 0x4070
movq %rbx, %rdi
callq 0x4550
nop