// File: /workspace/github_c_makefile_star2_O1/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 %r13
pushq %r12
pushq %rbx
subq $0x2a8, %rsp            # imm = 0x2A8
movq %rsi, %rbx
movl %edi, %ecx
leaq 0x11b1c(%rip), %rdx     # 0x20dc2
leaq 0x50(%rsp), %rdi
movl $0x10, %esi
xorl %eax, %eax
callq 0x51c0
leaq 0x11b0a(%rip), %rdi     # 0x20dc8
leaq 0x8(%rsp), %rcx
movl $0x4, %esi
xorl %edx, %edx
callq 0x54d0
testl %eax, %eax
je 0xf359
movq 0x1ace2(%rip), %rax     # 0x29fc0
movq (%rax), %r14
callq 0xeb2f
callq 0x5070
movq %rax, %rbx
movl (%rax), %ebp
movl %ebp, %edi
callq 0x5530
leaq 0x11ada(%rip), %rsi     # 0x20dd8
leaq 0x1b9ab(%rip), %rdx     # 0x2acb0
movq %r14, %rdi
movl %ebp, %ecx
movq %rax, %r8
xorl %eax, %eax
callq 0x52c0
movq 0x1b98d(%rip), %r14     # 0x2aca8
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %r14, %r14
je 0xf5a5
callq 0xeb2f
movl (%rbx), %ebx
movl %ebx, %edi
callq 0x5530
leaq 0x11a9a(%rip), %rsi     # 0x20dd8
leaq 0x1b96b(%rip), %rdx     # 0x2acb0
movq %r14, %rdi
movl %ebx, %ecx
movq %rax, %r8
xorl %eax, %eax
callq 0x52c0
jmp 0xf5a5
leaq 0xa0(%rsp), %rdi
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
callq 0x5200
cmpl $0x0, 0x8(%rsp)
setg %r12b
jle 0xf411
leaq 0xa0(%rsp), %r14
leaq 0x50(%rsp), %r15
xorl %r13d, %r13d
movq 0x10(%rsp), %rax
movq (%rax,%r13,8), %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x5440
cmpl $-0x1, %eax
je 0xf416
movl $0x200, %edx            # imm = 0x200
movl %eax, %edi
movq %r14, %rsi
callq 0x5250
cmpq $0x2, %rax
jl 0xf3d7
decq %rax
xorl %ecx, %ecx
cmpb $0x0, 0xa0(%rsp,%rcx)
jne 0xf3cf
movb $0x20, 0xa0(%rsp,%rcx)
incq %rcx
cmpq %rcx, %rax
jne 0xf3bd
movq %r14, %rdi
movq %rbx, %rsi
callq 0x5550
testq %rax, %rax
je 0xf3fb
movq %r14, %rdi
movq %r15, %rsi
callq 0x5550
testq %rax, %rax
jne 0xf4b8
incq %r13
movslq 0x8(%rsp), %rax
cmpq %rax, %r13
setl %r12b
jl 0xf38c
jmp 0xf590
jmp 0xf590
movq 0x1aba3(%rip), %rax     # 0x29fc0
movq (%rax), %r14
callq 0xeb2f
movq 0x10(%rsp), %rax
movq (%rax,%r13,8), %r15
callq 0x5070
movq %rax, %rbx
movl (%rax), %ebp
movl %ebp, %edi
callq 0x5530
leaq 0x119b3(%rip), %rsi     # 0x20df9
leaq 0x1b863(%rip), %rdx     # 0x2acb0
movq %r14, %rdi
movq %r15, %rcx
movl %ebp, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0x52c0
movq 0x1b841(%rip), %r14     # 0x2aca8
testq %r14, %r14
je 0xf4a4
callq 0xeb2f
movq 0x10(%rsp), %rax
movq (%rax,%r13,8), %r15
movl (%rbx), %ebx
movl %ebx, %edi
callq 0x5530
leaq 0x1196f(%rip), %rsi     # 0x20df9
leaq 0x1b81f(%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, %ebp       # imm = 0xFFFFFFFF
jmp 0xf590
xorps %xmm0, %xmm0
movaps %xmm0, 0x90(%rsp)
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movq 0x1aae4(%rip), %rax     # 0x29fc0
movq (%rax), %rbx
callq 0xeb2f
movq 0x10(%rsp), %rax
movq (%rax,%r13,8), %rcx
leaq 0x1192a(%rip), %rsi     # 0x20e1e
leaq 0x1b7b5(%rip), %rdx     # 0x2acb0
leaq 0xa0(%rsp), %r8
movq %rbx, %rdi
xorl %eax, %eax
callq 0x52c0
movq 0x1b794(%rip), %rbx     # 0x2aca8
testq %rbx, %rbx
je 0xf547
callq 0xeb2f
movq 0x10(%rsp), %rax
movq (%rax,%r13,8), %rcx
leaq 0x118f0(%rip), %rsi     # 0x20e1e
leaq 0x1b77b(%rip), %rdx     # 0x2acb0
leaq 0xa0(%rsp), %r8
movq %rbx, %rdi
xorl %eax, %eax
callq 0x52c0
movq 0x10(%rsp), %rax
movq (%rax,%r13,8), %rdi
leaq 0x11878(%rip), %rsi     # 0x20dcf
callq 0x5550
movb $0x0, (%rax)
incq %rax
cmpb $0x2f, -0x2(%rax)
leaq -0x1(%rax), %rax
jne 0xf562
leaq 0x60(%rsp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x50a0
movq %rbx, %rdi
callq 0x54b0
movl %eax, %ebp
leaq 0x8(%rsp), %rdi
callq 0x5030
testb $0x1, %r12b
jne 0xf5a5
leaq 0x8(%rsp), %rdi
callq 0x5030
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x2a8, %rsp            # imm = 0x2A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq