// File: 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
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
callq 0x51e0
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rdi
callq 0x5650
movq %rax, -0x108(%rbp)
leaq -0x39(%rbp), %rdi
movq %rdi, -0x100(%rbp)
callq 0x56c0
movq -0x108(%rbp), %rsi
movq -0x100(%rbp), %rdx
leaq -0x38(%rbp), %rdi
callq 0x7600
jmp 0x71d9
leaq -0x39(%rbp), %rdi
callq 0x53f0
leaq -0x71(%rbp), %rdi
movq %rdi, -0x110(%rbp)
callq 0x56c0
movq -0x110(%rbp), %rdx
leaq 0xb5d0(%rip), %rsi      # 0x127d0
leaq -0x70(%rbp), %rdi
callq 0x7600
jmp 0x720b
leaq -0x71(%rbp), %rdi
callq 0x53f0
movb $0x0, -0x72(%rbp)
movl $0x0, -0x78(%rbp)
movl $0x0, -0x7c(%rbp)
leaq -0x38(%rbp), %rdi
leaq 0xaf4e(%rip), %rsi      # 0x1217f
xorl %eax, %eax
movl %eax, %edx
callq 0x5790
movq %rax, -0x88(%rbp)
cmpq $-0x1, -0x88(%rbp)
je 0x7356
movq -0x88(%rbp), %rcx
addq $0x4, %rcx
xorl %eax, %eax
movl %eax, %edx
leaq -0xa8(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x53c0
jmp 0x7270
leaq -0x70(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x5770
jmp 0x7282
leaq -0xa8(%rbp), %rdi
callq 0x5230
movq -0x88(%rbp), %rsi
addq $0xa, %rsi
leaq -0x38(%rbp), %rdi
callq 0x5760
movq %rax, -0x118(%rbp)
jmp 0x72ab
movq -0x118(%rbp), %rax
movsbl (%rax), %eax
addl $-0x30, %eax
movl %eax, -0x78(%rbp)
movq -0x88(%rbp), %rsi
addq $0x4, %rsi
leaq -0x38(%rbp), %rdi
callq 0x5760
movq %rax, -0x120(%rbp)
jmp 0x72d8
movq -0x120(%rbp), %rax
movsbl (%rax), %eax
subl $0x30, %eax
subl -0x78(%rbp), %eax
movl %eax, -0x7c(%rbp)
movb $0x1, -0x72(%rbp)
jmp 0x7408
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x39(%rbp), %rdi
callq 0x53f0
jmp 0x7572
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x71(%rbp), %rdi
callq 0x53f0
jmp 0x7569
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x7560
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x5230
jmp 0x7560
leaq 0xae27(%rip), %rsi      # 0x12184
xorl %eax, %eax
movl %eax, %edx
movq %rdx, -0x128(%rbp)
leaq -0x38(%rbp), %rdi
movq %rdi, -0x130(%rbp)
callq 0x5790
movq -0x130(%rbp), %rsi
movq -0x128(%rbp), %rdx
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rcx
leaq -0xc8(%rbp), %rdi
callq 0x53c0
jmp 0x73a2
leaq -0x70(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x5770
jmp 0x73b4
leaq -0xc8(%rbp), %rdi
callq 0x5230
movq -0x88(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x5760
movq %rax, -0x138(%rbp)
jmp 0x73d9
movq -0x138(%rbp), %rax
movsbl (%rax), %eax
subl $0x30, %eax
movl %eax, -0x78(%rbp)
jmp 0x7408
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x5230
jmp 0x7560
movl $0xffffffff, -0xcc(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0xd0(%rbp)
movl -0xd0(%rbp), %eax
cmpl -0x78(%rbp), %eax
jg 0x7533
leaq -0xf0(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x5180
jmp 0x743d
testb $0x1, -0x72(%rbp)
je 0x7489
movl -0x7c(%rbp), %edx
movl -0xd0(%rbp), %eax
movl %eax, %ecx
movl %edx, %eax
leal 0x30(%rax,%rcx), %eax
movsbl %al, %edx
leaq -0xf0(%rbp), %rdi
movl $0x1, %esi
callq 0x53e0
jmp 0x746a
jmp 0x7489
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0x5230
jmp 0x7560
leaq 0xacf4(%rip), %rsi      # 0x12184
leaq -0xf0(%rbp), %rdi
callq 0x5750
jmp 0x749e
movl -0xd0(%rbp), %eax
addl $0x30, %eax
movsbl %al, %edx
leaq -0xf0(%rbp), %rdi
movl $0x1, %esi
callq 0x53e0
jmp 0x74bd
movq -0x18(%rbp), %rax
movq %rax, -0x148(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0x5170
movq -0x148(%rbp), %rdi
movq %rax, %rsi
callq 0x52c0
movq %rax, -0x140(%rbp)
jmp 0x74ec
movq -0x140(%rbp), %rax
movq %rax, -0xf8(%rbp)
cmpq $0x0, -0xf8(%rbp)
je 0x7513
movl -0xcc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xcc(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0x5230
movl -0xd0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xd0(%rbp)
jmp 0x741c
movl -0xcc(%rbp), %eax
movl %eax, -0x14c(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x5230
leaq -0x38(%rbp), %rdi
callq 0x5230
movl -0x14c(%rbp), %eax
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
leaq -0x70(%rbp), %rdi
callq 0x5230
leaq -0x38(%rbp), %rdi
callq 0x5230
movq -0x48(%rbp), %rdi
callq 0x56b0
nopl (%rax,%rax)