// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x3b0, %rsp            # imm = 0x3B0
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x70(%rbp), %rdi
movl -0x8(%rbp), %ecx
movl $0x10, %esi
leaq 0x199ff(%rip), %rdx     # 0x2dfb4
movb $0x0, %al
callq 0x51c0
leaq 0x199f7(%rip), %rdi     # 0x2dfba
movl $0x4, %esi
xorl %eax, %eax
movl %eax, %edx
leaq -0x58(%rbp), %rcx
callq 0x54e0
movl %eax, -0x5c(%rbp)
cmpl $0x0, -0x5c(%rbp)
je 0x146af
jmp 0x145e4
movq 0x229f5(%rip), %rax     # 0x36fe0
movq (%rax), %rax
movq %rax, -0x340(%rbp)
callq 0x13930
movq %rax, -0x338(%rbp)
callq 0x5070
movl (%rax), %eax
movl %eax, -0x32c(%rbp)
callq 0x5070
movl (%rax), %edi
callq 0x5540
movq -0x340(%rbp), %rdi
movq -0x338(%rbp), %rdx
movl -0x32c(%rbp), %ecx
movq %rax, %r8
leaq 0x19992(%rip), %rsi     # 0x2dfca
movb $0x0, %al
callq 0x52c0
cmpq $0x0, 0x23821(%rip)     # 0x37e68
je 0x146a1
movq 0x23818(%rip), %rax     # 0x37e68
movq %rax, -0x358(%rbp)
callq 0x13930
movq %rax, -0x350(%rbp)
callq 0x5070
movl (%rax), %eax
movl %eax, -0x344(%rbp)
callq 0x5070
movl (%rax), %edi
callq 0x5540
movq -0x358(%rbp), %rdi
movq -0x350(%rbp), %rdx
movl -0x344(%rbp), %ecx
movq %rax, %r8
leaq 0x19930(%rip), %rsi     # 0x2dfca
movb $0x0, %al
callq 0x52c0
jmp 0x146a3
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x14a2f
movl $0x0, -0x78(%rbp)
movl $0xffffffff, -0x7c(%rbp) # imm = 0xFFFFFFFF
leaq -0x290(%rbp), %rdi
xorl %esi, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x5200
movl $0x0, -0x78(%rbp)
movl -0x78(%rbp), %eax
movq -0x58(%rbp), %rcx
cmpl %ecx, %eax
jge 0x14a1d
movq -0x50(%rbp), %rax
movslq -0x78(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x5450
movl %eax, -0x7c(%rbp)
cmpl $-0x1, -0x7c(%rbp)
jne 0x14814
jmp 0x1470a
movq 0x228cf(%rip), %rax     # 0x36fe0
movq (%rax), %rax
movq %rax, -0x378(%rbp)
callq 0x13930
movq %rax, -0x370(%rbp)
movq -0x50(%rbp), %rax
movslq -0x78(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x368(%rbp)
callq 0x5070
movl (%rax), %eax
movl %eax, -0x35c(%rbp)
callq 0x5070
movl (%rax), %edi
callq 0x5540
movq -0x378(%rbp), %rdi
movq -0x370(%rbp), %rdx
movq -0x368(%rbp), %rcx
movl -0x35c(%rbp), %r8d
movq %rax, %r9
leaq 0x19872(%rip), %rsi     # 0x2dfeb
movb $0x0, %al
callq 0x52c0
cmpq $0x0, 0x236e0(%rip)     # 0x37e68
je 0x147fd
movq 0x236d7(%rip), %rax     # 0x37e68
movq %rax, -0x398(%rbp)
callq 0x13930
movq %rax, -0x390(%rbp)
movq -0x50(%rbp), %rax
movslq -0x78(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x388(%rbp)
callq 0x5070
movl (%rax), %eax
movl %eax, -0x37c(%rbp)
callq 0x5070
movl (%rax), %edi
callq 0x5540
movq -0x398(%rbp), %rdi
movq -0x390(%rbp), %rdx
movq -0x388(%rbp), %rcx
movl -0x37c(%rbp), %r8d
movq %rax, %r9
leaq 0x197f5(%rip), %rsi     # 0x2dfeb
movb $0x0, %al
callq 0x52c0
jmp 0x147ff
leaq -0x58(%rbp), %rdi
callq 0x5030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x14a2f
movl -0x7c(%rbp), %edi
leaq -0x290(%rbp), %rsi
movl $0x200, %edx            # imm = 0x200
callq 0x5250
movq %rax, -0x88(%rbp)
cmpq $0x0, -0x88(%rbp)
jle 0x14890
movl $0x0, -0x294(%rbp)
movslq -0x294(%rbp), %rax
movq -0x88(%rbp), %rcx
subq $0x1, %rcx
cmpq %rcx, %rax
jge 0x1488e
movslq -0x294(%rbp), %rax
movsbl -0x290(%rbp,%rax), %eax
cmpl $0x0, %eax
jne 0x1487d
movslq -0x294(%rbp), %rax
movb $0x20, -0x290(%rbp,%rax)
movl -0x294(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x294(%rbp)
jmp 0x14843
jmp 0x14890
leaq -0x290(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x5560
cmpq $0x0, %rax
je 0x14a0d
leaq -0x290(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x5560
cmpq $0x0, %rax
je 0x14a0d
leaq -0x2e0(%rbp), %rdi
xorl %esi, %esi
movl $0x40, %edx
callq 0x5200
leaq -0x320(%rbp), %rdi
xorl %esi, %esi
movl $0x40, %edx
callq 0x5200
movq 0x226ef(%rip), %rax     # 0x36fe0
movq (%rax), %rax
movq %rax, -0x3a0(%rbp)
callq 0x13930
movq -0x3a0(%rbp), %rdi
movq %rax, %rdx
movq -0x50(%rbp), %rax
movslq -0x78(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
leaq -0x290(%rbp), %r8
leaq 0x196ec(%rip), %rsi     # 0x2e010
movb $0x0, %al
callq 0x52c0
cmpq $0x0, 0x23535(%rip)     # 0x37e68
je 0x14973
movq 0x2352c(%rip), %rax     # 0x37e68
movq %rax, -0x3a8(%rbp)
callq 0x13930
movq -0x3a8(%rbp), %rdi
movq %rax, %rdx
movq -0x50(%rbp), %rax
movslq -0x78(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
leaq -0x290(%rbp), %r8
leaq 0x196a4(%rip), %rsi     # 0x2e010
movb $0x0, %al
callq 0x52c0
jmp 0x14975
leaq -0x2e0(%rbp), %rdi
movq -0x50(%rbp), %rax
movslq -0x78(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x50a0
movq -0x50(%rbp), %rax
movslq -0x78(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x19621(%rip), %rsi     # 0x2dfc1
callq 0x5560
movq %rax, -0x328(%rbp)
movq -0x328(%rbp), %rax
movb $0x0, (%rax)
movq -0x328(%rbp), %rax
movq %rax, %rcx
addq $-0x1, %rcx
movq %rcx, -0x328(%rbp)
movsbl -0x1(%rax), %eax
cmpl $0x2f, %eax
je 0x149d6
jmp 0x149b6
leaq -0x320(%rbp), %rdi
movq -0x328(%rbp), %rsi
addq $0x1, %rsi
callq 0x50a0
leaq -0x320(%rbp), %rdi
callq 0x54c0
movl %eax, -0x74(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x5030
movl -0x74(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x14a2f
jmp 0x14a0f
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x146d7
jmp 0x14a1f
leaq -0x58(%rbp), %rdi
callq 0x5030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x3b0, %rsp            # imm = 0x3B0
popq %rbp
retq
nopl (%rax,%rax)