// File: /workspace/github_cpp_makefile_star2_O3/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, %r15
movq %rdi, %r12
movaps 0x4461(%rip), %xmm0     # 0xc740
leaq 0x10(%rsp), %rdi
movaps %xmm0, (%rdi)
movabsq $0x2f736563697665, %rax # imm = 0x2F736563697665
movq %rax, 0xe(%rdi)
callq 0x2250
testq %rax, %rax
je 0x83cf
movq %rax, %rbx
movq %rax, %rdi
callq 0x2260
testq %rax, %rax
je 0x83c8
movq %rax, %rbp
movq %r15, 0x8(%rsp)
movq %r14, (%rsp)
leaq 0x190(%rsp), %r13
leaq 0x70(%rsp), %r14
cmpb $0xa, 0x12(%rbp)
jne 0x83b4
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 0x83b4
movb $0x0, 0x190(%rsp,%rax)
movq %r14, %rdi
leaq 0x40fa(%rip), %rsi      # 0xc465
leaq 0x10(%rsp), %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x2080
movq %r14, %rdi
callq 0x2250
testq %rax, %rax
je 0x83b4
movq %rax, %r15
movq %r15, %rdi
callq 0x2260
testq %rax, %rax
je 0x83ac
addq $0x13, %rax
movq %rax, %rdi
movq %r12, %rsi
callq 0x2210
testl %eax, %eax
jne 0x838a
jmp 0x83e2
movq %r15, %rdi
callq 0x22a0
movq %rbx, %rdi
callq 0x2260
movq %rax, %rbp
testq %rax, %rax
jne 0x8330
xorl %ebp, %ebp
jmp 0x8493
leaq 0x4071(%rip), %rdi      # 0xc447
xorl %ebp, %ebp
leaq 0x10(%rsp), %rsi
jmp 0x84ab
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
callq 0x2180
movq %r15, %rdi
callq 0x22a0
leaq 0x70(%rsp), %r14
movq %r14, %rdi
callq 0x20d0
movabsq $0x746e657665752f, %rcx # imm = 0x746E657665752F
movq %rcx, 0x70(%rsp,%rax)
leaq 0x45fa(%rip), %rsi      # 0xca14
movq %r14, %rdi
callq 0x21d0
testq %rax, %rax
je 0x849d
movq %rax, %r14
leaq 0x30(%rsp), %rdi
movl $0x40, %esi
movq %rax, %rdx
callq 0x2280
testq %rax, %rax
je 0x8486
leaq 0x3847(%rip), %rsi      # 0xbc8f
leaq 0x30(%rsp), %r15
movq %r15, %rdi
callq 0x2060
movb $0x0, 0x30(%rsp,%rax)
movq %r15, %rdi
movl $0x3d, %esi
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
movl $0x1, %ebp
movq %rbx, %rdi
callq 0x22a0
jmp 0x84b2
leaq 0x3fc6(%rip), %rdi      # 0xc46a
xorl %ebp, %ebp
leaq 0x70(%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