// File: /workspace/github_cpp_makefile_star2_O2/merelin[P]mediasmartserverd/src/device_monitor.cpp
// Function: DeviceMonitor::scsiHostIndex_(udev_device*)
int DeviceMonitor::scsiHostIndex_( udev_device* device ) {
	udev* udev = udev_device_get_udev(device);
	std::string device_sys_path (udev_device_get_syspath(device));
	std::string bus_prefix ("");

	bool use_ata = false;
	int host_index = 0;
	int bus_index_correction = 0;

	size_t index_of = device_sys_path.find("/ata");
	if (index_of != std::string::npos) {
		bus_prefix.append(device_sys_path.substr(0, index_of + 4));
		host_index = device_sys_path.at(index_of + 10) - '0';
		bus_index_correction = device_sys_path.at(index_of + 4) - '0' - host_index;
		use_ata = true;
	} else {
		index_of = device_sys_path.find("/host");
		bus_prefix.append(device_sys_path.substr(0, index_of));
		host_index = device_sys_path.at(index_of) - '0';
	}

	int scsi_host_index = -1;
	for (int i = 0; i <= host_index; i++) {
		std::string dev_path (bus_prefix);

		if (use_ata) {
			dev_path.append(1, '0' + bus_index_correction + i);
		}

		dev_path.append("/host");
		dev_path.append(1, '0' + i);

		udev_device* dev_host = udev_device_new_from_syspath(udev, dev_path.c_str());
		if (dev_host) {
			scsi_host_index++;
		}
	}

	return scsi_host_index;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rsi, %rbx
movq %rsi, %rdi
callq 0x4190
movq %rax, 0x50(%rsp)
movq %rbx, %rdi
callq 0x4510
leaq 0x30(%rsp), %rdi
leaq 0x58(%rsp), %rdx
movq %rax, %rsi
callq 0x5694
leaq 0x523f(%rip), %rsi      # 0xa6f4
leaq 0x58(%rsp), %rdi
leaq 0x10(%rsp), %rdx
callq 0x5694
leaq 0x4cb4(%rip), %rsi      # 0xa17f
leaq 0x30(%rsp), %rdi
xorl %edx, %edx
callq 0x4620
movq %rax, %r14
cmpq $-0x1, %rax
je 0x5539
leaq 0x4(%r14), %rbx
leaq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rsi
xorl %edx, %edx
movq %rbx, %rcx
callq 0x4310
leaq 0x58(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x4610
leaq 0x10(%rsp), %rdi
callq 0x4150
leaq 0xa(%r14), %rsi
leaq 0x30(%rsp), %rdi
callq 0x4600
movsbl (%rax), %ebp
leaq 0x30(%rsp), %rdi
movq %rbx, %rsi
callq 0x4600
movzbl (%rax), %eax
subl %ebp, %eax
addl $0x30, %eax
jmp 0x5590
leaq 0x4c44(%rip), %rsi      # 0xa184
leaq 0x30(%rsp), %r15
movq %r15, %rdi
xorl %edx, %edx
callq 0x4620
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x4310
leaq 0x58(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x4610
leaq 0x10(%rsp), %rdi
callq 0x4150
leaq 0x30(%rsp), %rdi
movq %rbx, %rsi
callq 0x4600
movsbl (%rax), %ebp
pushq $0x30
popq %rax
movq %rax, 0x8(%rsp)
cmpl $0x30, %ebp
pushq $0x2f
popq %r12
cmovgel %ebp, %r12d
addl $-0x2f, %r12d
xorl %r13d, %r13d
pushq $-0x1
popq %rbp
leaq 0x10(%rsp), %r15
pushq $0x1
popq %rbx
cmpl %r13d, %r12d
je 0x5624
movq %r15, %rdi
leaq 0x58(%rsp), %rsi
callq 0x4420
cmpq $-0x1, %r14
je 0x55e0
movq 0x8(%rsp), %rax
addl %r13d, %eax
movsbl %al, %edx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x4320
movq %r15, %rdi
leaq 0x4b9a(%rip), %rsi      # 0xa184
callq 0x45e0
leal 0x30(%r13), %eax
movsbl %al, %edx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x4320
movq 0x10(%rsp), %rsi
movq 0x50(%rsp), %rdi
callq 0x4240
cmpq $0x1, %rax
sbbl $-0x1, %ebp
movq %r15, %rdi
callq 0x4150
incl %r13d
jmp 0x55b2
leaq 0x58(%rsp), %rdi
callq 0x4150
leaq 0x30(%rsp), %rdi
callq 0x4150
movl %ebp, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x565f
jmp 0x5658
jmp 0x565f
jmp 0x5658
movq %rax, %rbx
jmp 0x5676
jmp 0x5658
movq %rax, %rbx
jmp 0x566c
jmp 0x565f
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x4150
leaq 0x58(%rsp), %rdi
callq 0x4150
leaq 0x30(%rsp), %rdi
callq 0x4150
movq %rbx, %rdi
callq 0x4550