// File: /workspace/github_c_makefile_star2_O2/Carton32[P]quectel-CM/util.c
// Function: getpid_by_pdp
pid_t getpid_by_pdp(int pdp, const char* program_name)
{
    glob_t gt;
    int ret;
    char filter[16];
    pid_t pid;

    snprintf(filter, sizeof(filter), "-n %d", pdp);
    ret = glob("/proc/*/cmdline", GLOB_NOSORT, NULL, &gt);
    if (ret != 0) {
        dbg_time("glob error, errno = %d(%s)", errno, strerror(errno));
		return -1;
    } else {
        int i = 0, fd = -1;
        ssize_t nreads;
        char cmdline[512] = {0};

		for (i = 0; i < (int)gt.gl_pathc; i++) {
            fd = open(gt.gl_pathv[i], O_RDONLY);
            if (fd == -1) {
                dbg_time("open %s failed, errno = %d(%s)", gt.gl_pathv[i], errno, strerror(errno));
                globfree(&gt);
                return -1;
            }
            
            nreads = read(fd, cmdline, sizeof(cmdline));
            if (nreads > 0) {
                int pos = 0;
                while (pos < nreads-1) {
                    if (cmdline[pos] == '\0')
                        cmdline[pos] = ' ';  // space
                    pos++;
                }
                // printf("%s\n", cmdline);
            }

            if (strstr(cmdline, program_name) && strstr(cmdline, filter)) {
            	char path[64] = {0};
            	char pidstr[64] = {0};
            	char *p;
            	
                dbg_time("%s: %s", gt.gl_pathv[i], cmdline);
            	strcpy(path, gt.gl_pathv[i]);
            	p = strstr(gt.gl_pathv[i], "/cmdline");
            	*p = '\0';
            	while (*(--p) != '/') ;
            		
            	strcpy(pidstr, p+1);
            	pid = atoi(pidstr);
            	globfree(&gt);
                
                return pid;
            }
        }
    }

    globfree(&gt);
    return -1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x2a0, %rsp            # imm = 0x2A0
movq %rsi, %rbx
movl %edi, %ecx
leaq 0x1036e(%rip), %rdx     # 0x1edd4
leaq 0x50(%rsp), %rdi
pushq $0x10
popq %rsi
xorl %eax, %eax
callq 0x51c0
leaq 0x1035e(%rip), %rdi     # 0x1edda
pushq $0x4
popq %rsi
leaq 0x8(%rsp), %rcx
xorl %edx, %edx
callq 0x54e0
testl %eax, %eax
je 0xeb14
movq 0x1a51e(%rip), %rax     # 0x28fb8
movq (%rax), %rbx
callq 0xe343
callq 0x5070
movq %rax, %r14
movl (%rax), %ebp
movl %ebp, %edi
callq 0x5540
leaq 0x10330(%rip), %rsi     # 0x1edea
leaq 0x1b1ef(%rip), %rdx     # 0x29cb0
movq %rbx, %rdi
movl %ebp, %ecx
movq %rax, %r8
xorl %eax, %eax
callq 0x52c0
movq 0x1b1d1(%rip), %r15     # 0x29ca8
pushq $-0x1
popq %rbx
testq %r15, %r15
je 0xed31
callq 0xe343
movl (%r14), %ebp
movl %ebp, %edi
callq 0x5540
leaq 0x102f1(%rip), %rsi     # 0x1edea
leaq 0x1b1b0(%rip), %rdx     # 0x29cb0
movq %r15, %rdi
movl %ebp, %ecx
movq %rax, %r8
xorl %eax, %eax
callq 0x52c0
jmp 0xed31
leaq 0xa0(%rsp), %r14
movl $0x200, %edx            # imm = 0x200
movq %r14, %rdi
xorl %esi, %esi
callq 0x5200
leaq 0x50(%rsp), %r15
xorl %r12d, %r12d
movslq 0x8(%rsp), %rax
cmpq %rax, %r12
jge 0xec47
movq 0x10(%rsp), %rax
movq (%rax,%r12,8), %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x5450
cmpl $-0x1, %eax
je 0xebb9
movl $0x200, %edx            # imm = 0x200
movl %eax, %edi
movq %r14, %rsi
callq 0x5250
testq %rax, %rax
jle 0xeb8d
decq %rax
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0xeb8d
cmpb $0x0, 0xa0(%rsp,%rcx)
jne 0xeb88
movb $0x20, 0xa0(%rsp,%rcx)
incq %rcx
jmp 0xeb71
movq %r14, %rdi
movq %rbx, %rsi
callq 0x5560
testq %rax, %rax
je 0xebb1
movq %r14, %rdi
movq %r15, %rsi
callq 0x5560
testq %rax, %rax
jne 0xec59
incq %r12
jmp 0xeb33
movq 0x1a3f8(%rip), %rax     # 0x28fb8
movq (%rax), %r14
callq 0xe343
movq 0x10(%rsp), %rax
movq (%rax,%r12,8), %r15
callq 0x5070
movq %rax, %rbx
movl (%rax), %ebp
movl %ebp, %edi
callq 0x5540
leaq 0x10222(%rip), %rsi     # 0x1ee0b
leaq 0x1b0c0(%rip), %rdx     # 0x29cb0
movq %r14, %rdi
movq %r15, %rcx
movl %ebp, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0x52c0
movq 0x1b09e(%rip), %r14     # 0x29ca8
testq %r14, %r14
je 0xec47
callq 0xe343
movq 0x10(%rsp), %rax
movq (%rax,%r12,8), %r15
movl (%rbx), %ebx
movl %ebx, %edi
callq 0x5540
leaq 0x101de(%rip), %rsi     # 0x1ee0b
leaq 0x1b07c(%rip), %rdx     # 0x29cb0
movq %r14, %rdi
movq %r15, %rcx
movl %ebx, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0x52c0
leaq 0x8(%rsp), %rdi
callq 0x5030
pushq $-0x1
popq %rbx
jmp 0xed31
xorps %xmm0, %xmm0
movaps %xmm0, 0x90(%rsp)
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movq 0x1a33b(%rip), %rax     # 0x28fb8
movq (%rax), %rbx
callq 0xe343
movq 0x10(%rsp), %rax
movq (%rax,%r12,8), %rcx
leaq 0x1019b(%rip), %rsi     # 0x1ee30
leaq 0x1b014(%rip), %rdx     # 0x29cb0
leaq 0xa0(%rsp), %r8
movq %rbx, %rdi
xorl %eax, %eax
callq 0x52c0
movq 0x1aff3(%rip), %rbx     # 0x29ca8
testq %rbx, %rbx
je 0xece8
callq 0xe343
movq 0x10(%rsp), %rax
movq (%rax,%r12,8), %rcx
leaq 0x10161(%rip), %rsi     # 0x1ee30
leaq 0x1afda(%rip), %rdx     # 0x29cb0
leaq 0xa0(%rsp), %r8
movq %rbx, %rdi
xorl %eax, %eax
callq 0x52c0
movq 0x10(%rsp), %rax
movq (%rax,%r12,8), %rdi
leaq 0x100e9(%rip), %rsi     # 0x1ede1
callq 0x5560
movb $0x0, (%rax)
incq %rax
cmpb $0x2f, -0x2(%rax)
leaq -0x1(%rax), %rax
jne 0xed03
leaq 0x60(%rsp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x50a0
movq %rbx, %rdi
callq 0x54c0
movl %eax, %ebx
leaq 0x8(%rsp), %rdi
callq 0x5030
movl %ebx, %eax
addq $0x2a0, %rsp            # imm = 0x2A0
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq