// File: /workspace/github_cpp_makefile_star2_O2/chenhn123[P]wdt_util_src/wdt_ct/wdt_dev_api.cpp
// Function: find_device_name(char*, char*, char*)
int find_device_name(char *hid_dev_name, char *driver_name, char *driver_path)
{
	char dev_path[] = "/sys/bus/i2c/devices/";

	struct dirent * devs_dir_entry;
	DIR * devs_dir;
	struct dirent * dev_dir_entry;
	DIR * dev_dir;
	int 	device_found = 0;
	ssize_t sz;
	char	tmp_buf[256];
	char	tmp_path[277];

	devs_dir = opendir(dev_path);
	if (!devs_dir) {
		printf("can not open device path: %s\n", dev_path);
		return 0;
	}


	while((devs_dir_entry = readdir(devs_dir)) != NULL) {
		if (devs_dir_entry->d_type != DT_LNK)
			continue;

		sz = readlinkat(dirfd(devs_dir), devs_dir_entry->d_name, tmp_buf, 256);
		if (sz < 0)
			continue;

		tmp_buf[sz] = 0;

		sprintf(tmp_path, "%s%s", dev_path, tmp_buf);

		dev_dir = opendir(tmp_path);
		if (!dev_dir) 
			continue;

		while ((dev_dir_entry = readdir(dev_dir)) != NULL) {
			if (!strcmp(dev_dir_entry->d_name, hid_dev_name)) {
				strcpy(driver_name, devs_dir_entry->d_name);
				device_found = 1;
				break;
			}
		}
		closedir(dev_dir);

		if (device_found){
			strcat(tmp_path, "/uevent");
			FILE *stream;
                        char line[64];

                	stream = fopen(tmp_path, "r");
                	if (stream == NULL) {
            	        	printf("can not open driver path: %s\n", tmp_path);
		        	return 0;
               		}

                	if(fgets (line, 64, stream)!=NULL) {
            			line[strcspn(line, "\n")] = 0;
                        	char *modulename;
                		modulename = strchr(line, '=') +1;
                        	strcat(driver_path, modulename);
                        	strcat(driver_path, "/");
                	}
                	fclose(stream);
                	break;
		}
	}
	closedir(devs_dir);
	return device_found;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x298, %rsp            # imm = 0x298
movq %rdx, %r14
movq %rsi, 0x8(%rsp)
movq %rdi, %r12
movaps 0x3e72(%rip), %xmm0     # 0xb740
leaq 0x10(%rsp), %rdi
movaps %xmm0, (%rdi)
movabsq $0x2f736563697665, %rax # imm = 0x2F736563697665
movq %rax, 0xe(%rdi)
callq 0x2250
testq %rax, %rax
je 0x7a5e
movq %rax, %rbx
movq %r14, (%rsp)
leaq 0x190(%rsp), %r13
movq %rbx, %rdi
callq 0x2260
testq %rax, %rax
je 0x7a52
movq %rax, %rbp
cmpb $0xa, 0x12(%rax)
jne 0x7901
movq %rbx, %rdi
callq 0x2100
addq $0x13, %rbp
movl $0x100, %ecx            # imm = 0x100
movl %eax, %edi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x2090
testq %rax, %rax
js 0x7901
movb $0x0, 0x190(%rsp,%rax)
leaq 0x70(%rsp), %r14
movq %r14, %rdi
leaq 0x3b10(%rip), %rsi      # 0xb465
leaq 0x10(%rsp), %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x2080
movq %r14, %rdi
callq 0x2250
testq %rax, %rax
je 0x7901
movq %rax, %r14
movq %r14, %rdi
callq 0x2260
movq %rax, %r15
testq %rax, %rax
je 0x79a6
leaq 0x13(%r15), %rdi
movq %r12, %rsi
callq 0x2210
testl %eax, %eax
jne 0x7974
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
callq 0x2180
pushq $0x1
popq %rbp
jmp 0x79a8
xorl %ebp, %ebp
movq %r14, %rdi
callq 0x22a0
testq %r15, %r15
je 0x7901
leaq 0x70(%rsp), %r14
movq %r14, %rdi
callq 0x20d0
movabsq $0x746e657665752f, %rcx # imm = 0x746E657665752F
movq %rcx, 0x70(%rsp,%rax)
leaq 0x4038(%rip), %rsi      # 0xba14
movq %r14, %rdi
callq 0x21d0
testq %rax, %rax
je 0x7a87
movq %rax, %r14
leaq 0x30(%rsp), %rdi
pushq $0x40
popq %rsi
movq %rax, %rdx
callq 0x2280
testq %rax, %rax
je 0x7a48
leaq 0x3283(%rip), %rsi      # 0xac8f
leaq 0x30(%rsp), %r15
movq %r15, %rdi
callq 0x2060
movb $0x0, 0x30(%rsp,%rax)
pushq $0x3d
popq %rsi
movq %r15, %rdi
callq 0x20c0
leaq 0x1(%rax), %rsi
movq (%rsp), %r15
movq %r15, %rdi
callq 0x22c0
movq %r15, %rdi
callq 0x20d0
movw $0x2f, (%r15,%rax)
movq %r14, %rdi
callq 0x21a0
jmp 0x7a54
xorl %ebp, %ebp
movq %rbx, %rdi
callq 0x22a0
jmp 0x7a73
leaq 0x39e2(%rip), %rdi      # 0xb447
xorl %ebp, %ebp
leaq 0x10(%rsp), %rsi
xorl %eax, %eax
callq 0x2050
movl %ebp, %eax
addq $0x298, %rsp            # imm = 0x298
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x39dc(%rip), %rdi      # 0xb46a
xorl %ebp, %ebp
leaq 0x70(%rsp), %rsi
jmp 0x7a6c