// File: /workspace/github_cpp_makefile_star2_O3/chenhn123[P]wdt_util_src/wdt_ct/wdt_dev_api.cpp
// Function: init_n_scan_device(WdtDevice*, ExecParam*, unsigned int)
int init_n_scan_device(WDT_DEV *pdev, EXEC_PARAM *pparam, unsigned int flag)
{
	WDT_DEVICE_INFO	wdtDevInfo;

	if (!pdev || !pparam)
		return 0;

	pdev->intf_index = pparam->interface_num;

	if (!pdev->func_wh_get_device_access_func(pdev->intf_index, &pdev->funcs_device)) {
		wh_printf("Get device funcs error");
		return 0;		
	}

	if (!strlen(pparam->dev_path)) {
		memset(&wdtDevInfo, 0, sizeof(WDT_DEVICE_INFO));	
		
		/* num of devices */
		int num = pdev->funcs_device.p_wh_scan_device(pdev);
		
		if (num == 0) {
			wh_printf("Open device error");
			return 0;
		}

		/* just use index 0 to get the device info */
		if (!pdev->funcs_device.p_wh_get_device(pdev, &wdtDevInfo, 0))
			return 0;
			
		strcpy(pdev->dev_path, wdtDevInfo.path);
	}else{
        	struct dirent *dir;
        	pdev->board_info.i2c_address = DEFAULT_I2C_ADDR;
        	wh_printf("%s\n", pparam->dev_path);
        	char i2c_sysfs_path[64] = "/sys/bus/i2c/devices";	
        	strcat(i2c_sysfs_path, strdup(&pparam->dev_path[4]));
        	DIR* d = opendir(i2c_sysfs_path);
        	wh_printf("%s\n", i2c_sysfs_path);
		if (d) {	
			while ((dir = readdir(d)) != NULL) {
				if (memcmp(dir->d_name, ACPI_NAME_HID, strlen(ACPI_NAME_HID)) == 0) {
					char* reg_gen_hid  = strdup(&dir->d_name[4]);
					wh_printf("current reg_gen_hid:%s\n", reg_gen_hid);
					pdev->board_info.i2c_address = get_i2c_address_map(reg_gen_hid);
					wh_printf("i2c_address %x\n", pdev->board_info.i2c_address);
					break;	
				}				
			}
            		closedir(d);
		}
        
		strcpy(pdev->dev_path, pparam->dev_path);
    	}
	if (pdev->funcs_device.p_wh_open_device(pdev)) {
		if (pdev->funcs_device.p_wh_prepare_data(pdev, &pdev->board_info)) {		
			if (pdev->is_legacy)
				return 1;
			
			if (!pdev->func_wh_get_device_private_access_func(pdev, &pdev->funcs_device_private)) {
				wh_printf("Get device private funcs error");
				return 0;		
			}
			return 1;
		}else
			wh_printf("Get system info error");
	}
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
testq %rdi, %rdi
sete %al
testq %rsi, %rsi
sete %cl
xorl %ebp, %ebp
orb %al, %cl
jne 0x7493
movq %rsi, %r15
movq %rdi, %rbx
movl 0x4(%rsi), %edi
movl %edi, (%rbx)
leaq 0x288(%rbx), %rsi
callq *0x320(%rbx)
testl %eax, %eax
je 0x73ae
cmpb $0x0, 0x18(%r15)
je 0x73ba
leaq 0x18(%r15), %r14
movb $0x2c, 0x1d6(%rbx)
leaq 0x5615(%rip), %r12      # 0xc8d6
movq %r12, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x2506
movaps 0x4b7b(%rip), %xmm0     # 0xbe50
movq %rsp, %r13
movaps %xmm0, 0x30(%r13)
movaps 0x4b5c(%rip), %xmm0     # 0xbe40
movaps %xmm0, 0x20(%r13)
movaps 0x4b40(%rip), %xmm0     # 0xbe30
movaps %xmm0, 0x10(%r13)
movaps 0x4b24(%rip), %xmm0     # 0xbe20
movaps %xmm0, (%r13)
addq $0x1c, %r15
movq %r15, %rdi
callq 0x22d0
movq %r13, %rdi
movq %rax, %rsi
callq 0x22c0
movq %r13, %rdi
callq 0x2250
movq %rax, %r15
movq %r12, %rdi
movq %r13, %rsi
xorl %eax, %eax
callq 0x2506
testq %r15, %r15
je 0x7429
movabsq $0x544844572d633269, %r12 # imm = 0x544844572D633269
movq %r15, %rdi
callq 0x2260
testq %rax, %rax
je 0x7421
cmpq %r12, 0x13(%rax)
jne 0x7343
addq $0x17, %rax
movq %rax, %rdi
callq 0x22d0
movq %rax, %r12
leaq 0x4b50(%rip), %rdi      # 0xbec0
movq %rax, %rsi
xorl %eax, %eax
callq 0x2506
movl $0x8, %r13d
leaq 0x9909(%rip), %rbp      # 0x10c90
movq -0x8(%r13,%rbp), %rsi
movl $0x8, %edx
movq %r12, %rdi
callq 0x20e0
testl %eax, %eax
je 0x7405
addq $0x10, %r13
cmpq $0x98, %r13
jne 0x7387
movb $0x2c, %al
jmp 0x740a
leaq 0x4ad9(%rip), %rdi      # 0xbe8e
jmp 0x748a
movq %rsp, %rdi
xorl %ebp, %ebp
movl $0x108, %edx            # imm = 0x108
xorl %esi, %esi
callq 0x2110
movq %rbx, %rdi
callq *0x288(%rbx)
testl %eax, %eax
je 0x73f9
movq %rsp, %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq *0x290(%rbx)
testl %eax, %eax
je 0x7493
leaq 0xc(%rbx), %rdi
leaq 0x8(%rsp), %rsi
jmp 0x7430
leaq 0x4aa5(%rip), %rdi      # 0xbea5
jmp 0x748a
movb (%r13,%rbp), %al
movb %al, 0x1d6(%rbx)
movzbl %al, %esi
leaq 0x4abe(%rip), %rdi      # 0xbed8
xorl %eax, %eax
callq 0x2506
movq %r15, %rdi
callq 0x22a0
leaq 0xc(%rbx), %rdi
movq %r14, %rsi
callq 0x2180
movq %rbx, %rdi
callq *0x298(%rbx)
movl $0x0, %ebp
testl %eax, %eax
je 0x7493
leaq 0x188(%rbx), %rsi
movq %rbx, %rdi
callq *0x2a8(%rbx)
testl %eax, %eax
je 0x7483
movl $0x1, %ebp
cmpl $0x0, 0x4(%rbx)
jne 0x7493
leaq 0x2b0(%rbx), %rsi
movq %rbx, %rdi
callq *0x328(%rbx)
testl %eax, %eax
jne 0x7493
leaq 0x4a67(%rip), %rdi      # 0xbee8
jmp 0x748a
leaq 0x4a7d(%rip), %rdi      # 0xbf07
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x2506
movl %ebp, %eax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq