// File: /workspace/github_cpp_makefile_star2_O1/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 0x43ec(%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 0x8461
movq %rax, %rbx
movq %rax, %rdi
callq 0x2260
testq %rax, %rax
je 0x8455
movq %rax, %rbp
movq %r14, (%rsp)
leaq 0x190(%rsp), %r13
cmpb $0xa, 0x12(%rbp)
jne 0x8441
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 0x8441
movb $0x0, 0x190(%rsp,%rax)
leaq 0x70(%rsp), %r14
movq %r14, %rdi
leaq 0x4086(%rip), %rsi      # 0xc465
leaq 0x10(%rsp), %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x2080
movq %r14, %rdi
callq 0x2250
testq %rax, %rax
je 0x8441
movq %rax, %r14
movq %r14, %rdi
callq 0x2260
movq %rax, %r15
testq %rax, %rax
je 0x8432
leaq 0x13(%r15), %rdi
movq %r12, %rsi
callq 0x2210
testl %eax, %eax
jne 0x83fe
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
callq 0x2180
movl $0x1, %ebp
jmp 0x8434
xorl %ebp, %ebp
movq %r14, %rdi
callq 0x22a0
testq %r15, %r15
jne 0x848a
movq %rbx, %rdi
callq 0x2260
movq %rax, %rbp
testq %rax, %rax
jne 0x839b
xorl %ebp, %ebp
movq %rbx, %rdi
callq 0x22a0
jmp 0x8476
leaq 0x3fdf(%rip), %rdi      # 0xc447
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 0x70(%rsp), %r14
movq %r14, %rdi
callq 0x20d0
movabsq $0x746e657665752f, %rcx # imm = 0x746E657665752F
movq %rcx, 0x70(%rsp,%rax)
leaq 0x456b(%rip), %rsi      # 0xca18
movq %r14, %rdi
callq 0x21d0
movq %rax, %r14
testq %rax, %rax
je 0x8523
leaq 0x30(%rsp), %rdi
movl $0x40, %esi
movq %r14, %rdx
callq 0x2280
testq %rax, %rax
je 0x8519
leaq 0x37b4(%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
jmp 0x8536
leaq 0x3f40(%rip), %rdi      # 0xc46a
leaq 0x70(%rsp), %rsi
xorl %eax, %eax
callq 0x2050
testq %r14, %r14
jne 0x8457
xorl %ebp, %ebp
jmp 0x8476