// File: /workspace/github_c_makefile_star2_O3/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 0x11c08(%rip), %rdx     # 0x20dbe
leaq 0x50(%rsp), %rdi
movl $0x10, %esi
xorl %eax, %eax
callq 0x51c0
leaq 0x11bf6(%rip), %rdi     # 0x20dc4
leaq 0x8(%rsp), %rcx
movl $0x4, %esi
xorl %edx, %edx
callq 0x54d0
testl %eax, %eax
je 0xf26a
movq 0x1add2(%rip), %rax     # 0x29fc0
movq (%rax), %rbx
callq 0xea3b
callq 0x5070
movq %rax, %r14
movl (%rax), %ebp
movl %ebp, %edi
callq 0x5530
leaq 0x11bc6(%rip), %rsi     # 0x20dd4
leaq 0x1ba9b(%rip), %rdx     # 0x2acb0
movq %rbx, %rdi
movl %ebp, %ecx
movq %rax, %r8
xorl %eax, %eax
callq 0x52c0
movq 0x1ba7d(%rip), %r15     # 0x2aca8
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testq %r15, %r15
je 0xf3b8
callq 0xea3b
movl (%r14), %ebp
movl %ebp, %edi
callq 0x5530
leaq 0x11b85(%rip), %rsi     # 0x20dd4
leaq 0x1ba5a(%rip), %rdx     # 0x2acb0
movq %r15, %rdi
movl %ebp, %ecx
movq %rax, %r8
xorl %eax, %eax
callq 0x52c0
jmp 0xf3b8
leaq 0xa0(%rsp), %rdi
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
callq 0x5200
cmpl $0x0, 0x8(%rsp)
jle 0xf3a9
leaq 0xa0(%rsp), %r14
leaq 0x50(%rsp), %r15
xorl %r12d, %r12d
movq 0x10(%rsp), %rax
movq (%rax,%r12,8), %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x5440
cmpl $-0x1, %eax
je 0xf31b
movl $0x200, %edx            # imm = 0x200
movl %eax, %edi
movq %r14, %rsi
callq 0x5250
testq %rax, %rax
jle 0xf2e5
decq %rax
je 0xf2e5
xorl %ecx, %ecx
cmpb $0x0, 0xa0(%rsp,%rcx)
jne 0xf2dd
movb $0x20, 0xa0(%rsp,%rcx)
incq %rcx
cmpq %rcx, %rax
jne 0xf2cb
movq %r14, %rdi
movq %rbx, %rsi
callq 0x5550
testq %rax, %rax
je 0xf309
movq %r14, %rdi
movq %r15, %rsi
callq 0x5550
testq %rax, %rax
jne 0xf3ca
incq %r12
movslq 0x8(%rsp), %rax
cmpq %rax, %r12
jl 0xf299
jmp 0xf3a9
movq 0x1ac9e(%rip), %rax     # 0x29fc0
movq (%rax), %r14
callq 0xea3b
movq 0x10(%rsp), %rax
movq (%rax,%r12,8), %r15
callq 0x5070
movq %rax, %rbx
movl (%rax), %ebp
movl %ebp, %edi
callq 0x5530
leaq 0x11aaa(%rip), %rsi     # 0x20df5
leaq 0x1b95e(%rip), %rdx     # 0x2acb0
movq %r14, %rdi
movq %r15, %rcx
movl %ebp, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0x52c0
movq 0x1b93c(%rip), %r14     # 0x2aca8
testq %r14, %r14
je 0xf3a9
callq 0xea3b
movq 0x10(%rsp), %rax
movq (%rax,%r12,8), %r15
movl (%rbx), %ebx
movl %ebx, %edi
callq 0x5530
leaq 0x11a66(%rip), %rsi     # 0x20df5
leaq 0x1b91a(%rip), %rdx     # 0x2acb0
movq %r14, %rdi
movq %r15, %rcx
movl %ebx, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0x52c0
leaq 0x8(%rsp), %rdi
callq 0x5030
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %ebx, %eax
addq $0x2a0, %rsp            # imm = 0x2A0
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
xorps %xmm0, %xmm0
movaps %xmm0, 0x90(%rsp)
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movq 0x1abd2(%rip), %rax     # 0x29fc0
movq (%rax), %rbx
callq 0xea3b
movq 0x10(%rsp), %rax
movq (%rax,%r12,8), %rcx
leaq 0x11a14(%rip), %rsi     # 0x20e1a
leaq 0x1b8a3(%rip), %rdx     # 0x2acb0
leaq 0xa0(%rsp), %r8
movq %rbx, %rdi
xorl %eax, %eax
callq 0x52c0
movq 0x1b882(%rip), %rbx     # 0x2aca8
testq %rbx, %rbx
je 0xf459
callq 0xea3b
movq 0x10(%rsp), %rax
movq (%rax,%r12,8), %rcx
leaq 0x119da(%rip), %rsi     # 0x20e1a
leaq 0x1b869(%rip), %rdx     # 0x2acb0
leaq 0xa0(%rsp), %r8
movq %rbx, %rdi
xorl %eax, %eax
callq 0x52c0
movq 0x10(%rsp), %rax
movq (%rax,%r12,8), %rdi
leaq 0x11962(%rip), %rsi     # 0x20dcb
callq 0x5550
movb $0x0, (%rax)
incq %rax
cmpb $0x2f, -0x2(%rax)
leaq -0x1(%rax), %rax
jne 0xf474
leaq 0x60(%rsp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x50a0
movq %rbx, %rdi
callq 0x54b0
movl %eax, %ebx
leaq 0x8(%rsp), %rdi
callq 0x5030
jmp 0xf3b8