// File: /workspace/github_cpp_makefile_star2_O1/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, %r14
movq %rsi, %rdi
callq 0x4160
movq %rax, 0x70(%rsp)
movq %r14, %rdi
callq 0x44e0
leaq 0x50(%rsp), %rdi
leaq 0x28(%rsp), %rdx
movq %rax, %rsi
callq 0x5baa
leaq 0x38(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x5e36(%rip), %rdx      # 0xb70c
leaq 0x28(%rsp), %rdi
movq %rdx, %rsi
callq 0x5e88
leaq 0x5895(%rip), %rsi      # 0xb17f
leaq 0x50(%rsp), %rdi
movl $0x4, %ecx
xorl %edx, %edx
callq 0x4540
movq %rax, %rbx
cmpq $-0x1, %rax
je 0x597d
leaq 0x4(%rbx), %r12
leaq 0x8(%rsp), %rdi
leaq 0x50(%rsp), %rsi
xorl %edx, %edx
movq %r12, %rcx
callq 0x42d0
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
leaq 0x28(%rsp), %rdi
callq 0x40c0
leaq 0x18(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x594b
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x42c0
leaq 0xa(%rbx), %r15
movq 0x58(%rsp), %rdx
cmpq %r15, %rdx
jbe 0x5b0d
cmpq %r12, %rdx
jbe 0x5b0a
movq 0x50(%rsp), %rax
movsbl (%rax,%r15), %r13d
movzbl (%rax,%r12), %eax
subl %r13d, %eax
addl $0x30, %eax
jmp 0x59f9
leaq 0x5800(%rip), %rsi      # 0xb184
leaq 0x50(%rsp), %r12
movl $0x5, %ecx
movq %r12, %rdi
xorl %edx, %edx
callq 0x4540
movq %rax, %r15
leaq 0x8(%rsp), %rdi
movq %r12, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x42d0
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
leaq 0x28(%rsp), %rdi
callq 0x40c0
leaq 0x18(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x59dc
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x42c0
movq 0x58(%rsp), %rdx
cmpq %r15, %rdx
jbe 0x5b0d
movq 0x50(%rsp), %rax
movsbl (%rax,%r15), %r13d
movl $0x30, %eax
movq %rax, 0x48(%rsp)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x30, %r13d
jl 0x5ac2
leaq 0x18(%rsp), %r12
addl $-0x2f, %r13d
xorl %r14d, %r14d
leaq 0x8(%rsp), %r15
movq %r12, 0x8(%rsp)
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
addq %rsi, %rdx
movq %r15, %rdi
callq 0x5df8
cmpq $-0x1, %rbx
je 0x5a5e
movq 0x48(%rsp), %rax
addl %r14d, %eax
movq 0x10(%rsp), %rsi
movsbl %al, %r8d
movl $0x1, %ecx
movq %r15, %rdi
xorl %edx, %edx
callq 0x45d0
movq %r15, %rdi
leaq 0x571c(%rip), %rsi      # 0xb184
callq 0x45f0
leal 0x30(%r14), %eax
movq 0x10(%rsp), %rsi
movsbl %al, %r8d
movl $0x1, %ecx
movq %r15, %rdi
xorl %edx, %edx
callq 0x45d0
movq 0x8(%rsp), %rsi
movq 0x70(%rsp), %rdi
callq 0x4200
cmpq $0x1, %rax
sbbl $-0x1, %ebp
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x5ab6
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x42c0
incl %r14d
cmpl %r14d, %r13d
jne 0x5a1e
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x5ade
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x42c0
leaq 0x60(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5af9
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x42c0
movl %ebp, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, %r15
leaq 0x56ad(%rip), %rdi      # 0xb1c1
movq %r15, %rsi
xorl %eax, %eax
callq 0x4450
jmp 0x5b22
jmp 0x5b40
movq %rax, %rbx
leaq 0x18(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
jne 0x5b52
jmp 0x5b5f
jmp 0x5b40
movq %rax, %rbx
jmp 0x5b7b
jmp 0x5b40
jmp 0x5b45
movq %rax, %rbx
jmp 0x5b5f
movq %rax, %rbx
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x5b5f
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x42c0
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x5b7b
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x42c0
leaq 0x60(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5b96
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x42c0
movq %rbx, %rdi
callq 0x4550