// File: /workspace/github_c_makefile_star2_O0/adsr[P]phpspy/phpspy.c
// Function: get_php_version
static int get_php_version(trace_target_t *target) {
    struct _zend_module_entry basic_functions_module;
    char version_cmd[1024];
    char phpv[4];
    pid_t pid;
    FILE *pcmd;

    pid = target->pid;
    phpv[0] = '\0';

    /* Try reading basic_functions_module */
    if (target->basic_functions_module_addr) {
        if (copy_proc_mem(pid, "basic_functions_module", (void*)target->basic_functions_module_addr, &basic_functions_module, sizeof(basic_functions_module)) == 0) {
            copy_proc_mem(pid, "basic_functions_module.version", (void*)basic_functions_module.version, phpv, 3);
        }
    }

    /* Try greping binary */
    if (phpv[0] == '\0') {
        char libname[PHPSPY_STR_SIZE];
        if (shell_escape(opt_libname_awk_patt, libname, sizeof(libname), "opt_libname_awk_patt")) {
            return PHPSPY_ERR;
        }
        int n = snprintf(
            version_cmd,
            sizeof(version_cmd),
            "{ echo -n /proc/%d/root/; "
            "  awk -ve=1 -vp=%s '$0~p{print $NF; e=0; exit} END{exit e}' /proc/%d/maps "
            "  || readlink /proc/%d/exe; } "
            "| { xargs stat --printf=%%n 2>/dev/null || echo /proc/%d/exe; } "
            "| xargs strings "
            "| grep -Eo 'X-Powered-By: PHP/[0-9]+\\.[0-9]+' "
            "| grep -Eo '[0-9]+\\.[0-9]+' ",
            pid, libname, pid, pid, pid
        );
        if ((size_t)n >= sizeof(version_cmd) - 1) {
            log_error("get_php_version: snprintf overflow\n");
            return PHPSPY_ERR;
        }

        if ((pcmd = popen(version_cmd, "r")) == NULL) {
            perror("get_php_version: popen");
            return PHPSPY_ERR;
        } else if (fread(&phpv, sizeof(char), 3, pcmd) != 3) {
            log_error("get_php_version: Could not detect PHP version\n");
            pclose(pcmd);
            return PHPSPY_ERR;
        }
        pclose(pcmd);
    }

    if      (strncmp(phpv, "7.0", 3) == 0) opt_phpv = "70";
    else if (strncmp(phpv, "7.1", 3) == 0) opt_phpv = "71";
    else if (strncmp(phpv, "7.2", 3) == 0) opt_phpv = "72";
    else if (strncmp(phpv, "7.3", 3) == 0) opt_phpv = "73";
    else if (strncmp(phpv, "7.4", 3) == 0) opt_phpv = "74";
    else if (strncmp(phpv, "8.0", 3) == 0) opt_phpv = "80";
    else if (strncmp(phpv, "8.1", 3) == 0) opt_phpv = "81";
    else if (strncmp(phpv, "8.2", 3) == 0) opt_phpv = "82";
    else if (strncmp(phpv, "8.3", 3) == 0) opt_phpv = "83";
    else if (strncmp(phpv, "8.4", 3) == 0) opt_phpv = "84";
    else {
        log_error("get_php_version: Unrecognized PHP version (%s)\n", phpv);
        return PHPSPY_ERR;
    }

    return PHPSPY_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x5a0, %rsp            # imm = 0x5A0
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x478(%rbp)
movb $0x0, -0x474(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x20(%rax)
je 0xa61b
movl -0x478(%rbp), %edi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdx
leaq 0x2a6be(%rip), %rsi     # 0x34ca0
leaq -0x70(%rbp), %rcx
movl $0x60, %r8d
callq 0x12680
cmpl $0x0, %eax
jne 0xa619
movl -0x478(%rbp), %edi
movq -0x18(%rbp), %rdx
leaq -0x474(%rbp), %rcx
leaq 0x2a6f3(%rip), %rsi     # 0x34d01
movl $0x3, %r8d
callq 0x12680
jmp 0xa61b
movsbl -0x474(%rbp), %eax
cmpl $0x0, %eax
jne 0xa75c
movq 0x2fd36(%rip), %rdi     # 0x3a368
leaq -0x580(%rbp), %rsi
movl $0x100, %edx            # imm = 0x100
leaq 0x2a6db(%rip), %rcx     # 0x34d20
callq 0x2c610
cmpl $0x0, %eax
je 0xa65b
movl $0x1, -0x4(%rbp)
jmp 0xa96a
leaq -0x470(%rbp), %rdi
movl -0x478(%rbp), %ecx
leaq -0x580(%rbp), %r8
movl -0x478(%rbp), %r9d
movl -0x478(%rbp), %r10d
movl -0x478(%rbp), %eax
movl $0x400, %esi            # imm = 0x400
leaq 0x2a6a6(%rip), %rdx     # 0x34d35
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
movb $0x0, %al
callq 0x61d0
movl %eax, -0x584(%rbp)
movslq -0x584(%rbp), %rax
cmpq $0x3ff, %rax            # imm = 0x3FF
jb 0xa6cd
leaq 0x2a798(%rip), %rdi     # 0x34e52
movb $0x0, %al
callq 0x9cc0
movl $0x1, -0x4(%rbp)
jmp 0xa96a
leaq -0x470(%rbp), %rdi
leaq 0x2a2e1(%rip), %rsi     # 0x349bc
callq 0x6500
movq %rax, -0x480(%rbp)
cmpq $0x0, %rax
jne 0xa705
leaq 0x2a782(%rip), %rdi     # 0x34e76
callq 0x6520
movl $0x1, -0x4(%rbp)
jmp 0xa96a
movq -0x480(%rbp), %rcx
leaq -0x474(%rbp), %rdi
movl $0x1, %esi
movl $0x3, %edx
callq 0x60e0
cmpq $0x3, %rax
je 0xa74e
leaq 0x2a75e(%rip), %rdi     # 0x34e8d
movb $0x0, %al
callq 0x9cc0
movq -0x480(%rbp), %rdi
callq 0x61c0
movl $0x1, -0x4(%rbp)
jmp 0xa96a
jmp 0xa750
movq -0x480(%rbp), %rdi
callq 0x61c0
leaq -0x474(%rbp), %rdi
leaq 0x2a367(%rip), %rsi     # 0x34ad1
movl $0x3, %edx
callq 0x6080
cmpl $0x0, %eax
jne 0xa78c
leaq 0x297ff(%rip), %rax     # 0x33f7f
movq %rax, 0x2fbc9(%rip)     # 0x3a350
jmp 0xa963
leaq -0x474(%rbp), %rdi
leaq 0x2a722(%rip), %rsi     # 0x34ebc
movl $0x3, %edx
callq 0x6080
cmpl $0x0, %eax
jne 0xa7bc
leaq 0x297d2(%rip), %rax     # 0x33f82
movq %rax, 0x2fb99(%rip)     # 0x3a350
jmp 0xa961
leaq -0x474(%rbp), %rdi
leaq 0x2a6f6(%rip), %rsi     # 0x34ec0
movl $0x3, %edx
callq 0x6080
cmpl $0x0, %eax
jne 0xa7ec
leaq 0x297a5(%rip), %rax     # 0x33f85
movq %rax, 0x2fb69(%rip)     # 0x3a350
jmp 0xa95f
leaq -0x474(%rbp), %rdi
leaq 0x2a6ca(%rip), %rsi     # 0x34ec4
movl $0x3, %edx
callq 0x6080
cmpl $0x0, %eax
jne 0xa81c
leaq 0x29778(%rip), %rax     # 0x33f88
movq %rax, 0x2fb39(%rip)     # 0x3a350
jmp 0xa95d
leaq -0x474(%rbp), %rdi
leaq 0x2a69e(%rip), %rsi     # 0x34ec8
movl $0x3, %edx
callq 0x6080
cmpl $0x0, %eax
jne 0xa84c
leaq 0x2974b(%rip), %rax     # 0x33f8b
movq %rax, 0x2fb09(%rip)     # 0x3a350
jmp 0xa95b
leaq -0x474(%rbp), %rdi
leaq 0x2a672(%rip), %rsi     # 0x34ecc
movl $0x3, %edx
callq 0x6080
cmpl $0x0, %eax
jne 0xa87c
leaq 0x2971e(%rip), %rax     # 0x33f8e
movq %rax, 0x2fad9(%rip)     # 0x3a350
jmp 0xa959
leaq -0x474(%rbp), %rdi
leaq 0x2a646(%rip), %rsi     # 0x34ed0
movl $0x3, %edx
callq 0x6080
cmpl $0x0, %eax
jne 0xa8ac
leaq 0x296f1(%rip), %rax     # 0x33f91
movq %rax, 0x2faa9(%rip)     # 0x3a350
jmp 0xa957
leaq -0x474(%rbp), %rdi
leaq 0x2a61a(%rip), %rsi     # 0x34ed4
movl $0x3, %edx
callq 0x6080
cmpl $0x0, %eax
jne 0xa8d9
leaq 0x296c4(%rip), %rax     # 0x33f94
movq %rax, 0x2fa79(%rip)     # 0x3a350
jmp 0xa955
leaq -0x474(%rbp), %rdi
leaq 0x2a5f1(%rip), %rsi     # 0x34ed8
movl $0x3, %edx
callq 0x6080
cmpl $0x0, %eax
jne 0xa906
leaq 0x2969a(%rip), %rax     # 0x33f97
movq %rax, 0x2fa4c(%rip)     # 0x3a350
jmp 0xa953
leaq -0x474(%rbp), %rdi
leaq 0x2a5c8(%rip), %rsi     # 0x34edc
movl $0x3, %edx
callq 0x6080
cmpl $0x0, %eax
jne 0xa933
leaq 0x29670(%rip), %rax     # 0x33f9a
movq %rax, 0x2fa1f(%rip)     # 0x3a350
jmp 0xa951
leaq -0x474(%rbp), %rsi
leaq 0x2a59f(%rip), %rdi     # 0x34ee0
movb $0x0, %al
callq 0x9cc0
movl $0x1, -0x4(%rbp)
jmp 0xa96a
jmp 0xa953
jmp 0xa955
jmp 0xa957
jmp 0xa959
jmp 0xa95b
jmp 0xa95d
jmp 0xa95f
jmp 0xa961
jmp 0xa963
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x5a0, %rsp            # imm = 0x5A0
popq %rbp
retq
nopw %cs:(%rax,%rax)