// File: /workspace/github_c_makefile_star2_O0/adsr[P]phpspy/phpspy.c
// Function: main_pid
int main_pid(pid_t pid) {
    int rv;
    trace_context_t context;
    struct timespec start_time, end_time, sleep_time, _stop_time, limit_time;
    struct timespec *stop_time;

    memset(&context, 0, sizeof(trace_context_t));
    context.target.pid = pid;
    context.event_handler = opt_event_handler;
    context.event_handler_opts = opt_event_handler_opts;
    try(rv, find_addresses(&context.target));
    try(rv, context.event_handler(&context, PHPSPY_TRACE_EVENT_INIT));

    #ifdef USE_ZEND
    do_trace_ptr = do_trace;
    #else

    if (strcmp(opt_phpv, "auto") == 0) {
        try(rv, get_php_version(&context.target));
    }

    if (strcmp("70", opt_phpv) == 0) {
        do_trace_ptr = do_trace_70;
    } else if (strcmp("71", opt_phpv) == 0) {
        do_trace_ptr = do_trace_71;
    } else if (strcmp("72", opt_phpv) == 0) {
        do_trace_ptr = do_trace_72;
    } else if (strcmp("73", opt_phpv) == 0) {
        do_trace_ptr = do_trace_73;
    } else if (strcmp("74", opt_phpv) == 0) {
        do_trace_ptr = do_trace_74;
    } else if (strcmp("80", opt_phpv) == 0) {
        do_trace_ptr = do_trace_80;
    } else if (strcmp("81", opt_phpv) == 0) {
        do_trace_ptr = do_trace_81;
    } else if (strcmp("82", opt_phpv) == 0) {
        do_trace_ptr = do_trace_82;
    } else if (strcmp("83", opt_phpv) == 0) {
        do_trace_ptr = do_trace_83;
    } else if (strcmp("84", opt_phpv) == 0) {
        do_trace_ptr = do_trace_84; /* TODO verify 8.4 structs */
    } else {
        log_error("main_pid: Unrecognized PHP version (%s)\n", opt_phpv);
        return PHPSPY_ERR;
    }
    #endif

    /* calc stop_time */
    stop_time = NULL;
    if (in_pgrep_mode) {
        /* in pgrep mode, we use a SIGALRM (see main_pgrep) */
    } else if (opt_time_limit_ms > 0) {
        stop_time = &_stop_time;
        limit_time.tv_sec = opt_time_limit_ms / 1000L;
        limit_time.tv_nsec = (opt_time_limit_ms % 1000L) * 1000000L;
        clock_get(stop_time);
        clock_add(stop_time, &limit_time, stop_time);
    }

    while (!done) {
        /* record start_time */
        clock_get(&start_time);

        /* trace (maybe with PTRACE_ATTACH) */
        rv = 0;
        if (opt_pause) rv |= pause_pid(pid);
        rv |= do_trace_ptr(&context);
        if (opt_pause) rv |= unpause_pid(pid);

        /* bail if pid died */
        if ((rv & PHPSPY_ERR_PID_DEAD) != 0) break;

        /* maybe apply trace limit */
        if (opt_trace_limit > 0 && rv == PHPSPY_OK) {
            if (in_pgrep_mode) {
                __atomic_add_fetch(&trace_count, 1, __ATOMIC_SEQ_CST);
            } else {
                trace_count += 1;
            }
            /* in pgrep mode, it is possible for each thread to sneak in one
               extra trace even after the limit is reached but it should exit
               after that. */
            if (trace_count >= opt_trace_limit) break;
        }

        /* maybe apply time limit */
        if (stop_time && clock_diff(&start_time, stop_time) >= 1) {
            break;
        }

        /* calc sleep_time and sleep */
        clock_get(&end_time);
        calc_sleep_time(&end_time, &start_time, &sleep_time);
        nanosleep(&sleep_time, NULL);
    }

    context.event_handler(&context, PHPSPY_TRACE_EVENT_DEINIT);

    /* in pgrep mode, trigger done condition if we went over the trace limit.
       it is ok for multiple threads to call this. */
    if (in_pgrep_mode && opt_trace_limit > 0 && trace_count >= opt_trace_limit) {
        write_done_pipe();
    }

    /* TODO proper signal handling for non-pgrep modes */
    return PHPSPY_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x930, %rsp            # imm = 0x930
movl %edi, -0x8(%rbp)
leaq -0x8c0(%rbp), %rdi
xorl %esi, %esi
movl $0x8b0, %edx            # imm = 0x8B0
callq 0x6230
movl -0x8(%rbp), %eax
movl %eax, -0x8c0(%rbp)
movq 0x30c67(%rip), %rax     # 0x3a358
movq %rax, -0x128(%rbp)
movq 0x32e39(%rip), %rax     # 0x3c538
movq %rax, -0x120(%rbp)
leaq -0x8c0(%rbp), %rdi
callq 0xa430
movl %eax, -0xc(%rbp)
cmpl $0x0, %eax
je 0x9725
movl -0xc(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x9b75
jmp 0x9727
jmp 0x9729
leaq -0x8c0(%rbp), %rdi
xorl %esi, %esi
callq *-0x128(%rbp)
movl %eax, -0xc(%rbp)
cmpl $0x0, %eax
je 0x974b
movl -0xc(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x9b75
jmp 0x974d
movq 0x30bfc(%rip), %rdi     # 0x3a350
leaq 0x293bd(%rip), %rsi     # 0x32b18
callq 0x6310
cmpl $0x0, %eax
jne 0x978a
jmp 0x9767
leaq -0x8c0(%rbp), %rdi
callq 0xa5a0
movl %eax, -0xc(%rbp)
cmpl $0x0, %eax
je 0x9786
movl -0xc(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x9b75
jmp 0x9788
jmp 0x978a
movq 0x30bbf(%rip), %rsi     # 0x3a350
leaq 0x2a7e7(%rip), %rdi     # 0x33f7f
callq 0x6310
cmpl $0x0, %eax
jne 0x97b5
leaq 0x11d7(%rip), %rax      # 0xa980
movq %rax, 0x32d98(%rip)     # 0x3c548
jmp 0x9962
movq 0x30b94(%rip), %rsi     # 0x3a350
leaq 0x2a7bf(%rip), %rdi     # 0x33f82
callq 0x6310
cmpl $0x0, %eax
jne 0x97e0
leaq 0x13bc(%rip), %rax      # 0xab90
movq %rax, 0x32d6d(%rip)     # 0x3c548
jmp 0x9960
movq 0x30b69(%rip), %rsi     # 0x3a350
leaq 0x2a797(%rip), %rdi     # 0x33f85
callq 0x6310
cmpl $0x0, %eax
jne 0x980b
leaq 0x15a1(%rip), %rax      # 0xada0
movq %rax, 0x32d42(%rip)     # 0x3c548
jmp 0x995e
movq 0x30b3e(%rip), %rsi     # 0x3a350
leaq 0x2a76f(%rip), %rdi     # 0x33f88
callq 0x6310
cmpl $0x0, %eax
jne 0x9836
leaq 0x1786(%rip), %rax      # 0xafb0
movq %rax, 0x32d17(%rip)     # 0x3c548
jmp 0x995c
movq 0x30b13(%rip), %rsi     # 0x3a350
leaq 0x2a747(%rip), %rdi     # 0x33f8b
callq 0x6310
cmpl $0x0, %eax
jne 0x9861
leaq 0x196b(%rip), %rax      # 0xb1c0
movq %rax, 0x32cec(%rip)     # 0x3c548
jmp 0x995a
movq 0x30ae8(%rip), %rsi     # 0x3a350
leaq 0x2a71f(%rip), %rdi     # 0x33f8e
callq 0x6310
cmpl $0x0, %eax
jne 0x988c
leaq 0x1b50(%rip), %rax      # 0xb3d0
movq %rax, 0x32cc1(%rip)     # 0x3c548
jmp 0x9958
movq 0x30abd(%rip), %rsi     # 0x3a350
leaq 0x2a6f7(%rip), %rdi     # 0x33f91
callq 0x6310
cmpl $0x0, %eax
jne 0x98b7
leaq 0x1d35(%rip), %rax      # 0xb5e0
movq %rax, 0x32c96(%rip)     # 0x3c548
jmp 0x9956
movq 0x30a92(%rip), %rsi     # 0x3a350
leaq 0x2a6cf(%rip), %rdi     # 0x33f94
callq 0x6310
cmpl $0x0, %eax
jne 0x98df
leaq 0x1f1a(%rip), %rax      # 0xb7f0
movq %rax, 0x32c6b(%rip)     # 0x3c548
jmp 0x9954
movq 0x30a6a(%rip), %rsi     # 0x3a350
leaq 0x2a6aa(%rip), %rdi     # 0x33f97
callq 0x6310
cmpl $0x0, %eax
jne 0x9907
leaq 0x2102(%rip), %rax      # 0xba00
movq %rax, 0x32c43(%rip)     # 0x3c548
jmp 0x9952
movq 0x30a42(%rip), %rsi     # 0x3a350
leaq 0x2a685(%rip), %rdi     # 0x33f9a
callq 0x6310
cmpl $0x0, %eax
jne 0x992f
leaq 0x22ea(%rip), %rax      # 0xbc10
movq %rax, 0x32c1b(%rip)     # 0x3c548
jmp 0x9950
movq 0x30a1a(%rip), %rsi     # 0x3a350
leaq 0x2a660(%rip), %rdi     # 0x33f9d
movb $0x0, %al
callq 0x9cc0
movl $0x1, -0x4(%rbp)
jmp 0x9b75
jmp 0x9952
jmp 0x9954
jmp 0x9956
jmp 0x9958
jmp 0x995a
jmp 0x995c
jmp 0x995e
jmp 0x9960
jmp 0x9962
movq $0x0, -0x918(%rbp)
cmpl $0x0, 0x32bec(%rip)     # 0x3c560
je 0x9978
jmp 0x99ef
cmpq $0x0, 0x32b90(%rip)     # 0x3c510
jle 0x99ed
leaq -0x900(%rbp), %rax
movq %rax, -0x918(%rbp)
movq 0x32b79(%rip), %rax     # 0x3c510
movl $0x3e8, %ecx            # imm = 0x3E8
cqto
idivq %rcx
movq %rax, -0x910(%rbp)
movq 0x32b61(%rip), %rax     # 0x3c510
movl $0x3e8, %ecx            # imm = 0x3E8
cqto
idivq %rcx
imulq $0xf4240, %rdx, %rax    # imm = 0xF4240
movq %rax, -0x908(%rbp)
movq -0x918(%rbp), %rdi
callq 0xbe20
movq -0x918(%rbp), %rdi
movq -0x918(%rbp), %rdx
leaq -0x910(%rbp), %rsi
callq 0xbe70
jmp 0x99ef
jmp 0x99f1
cmpl $0x0, 0x32b4c(%rip)     # 0x3c544
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x9a06
jmp 0x9b32
leaq -0x8d0(%rbp), %rdi
callq 0xbe20
movl $0x0, -0xc(%rbp)
cmpl $0x0, 0x32af8(%rip)     # 0x3c518
je 0x9a30
movl -0x8(%rbp), %edi
callq 0xbf00
orl -0xc(%rbp), %eax
movl %eax, -0xc(%rbp)
leaq -0x8c0(%rbp), %rdi
callq *0x32b0b(%rip)          # 0x3c548
orl -0xc(%rbp), %eax
movl %eax, -0xc(%rbp)
cmpl $0x0, 0x32ace(%rip)     # 0x3c518
je 0x9a5a
movl -0x8(%rbp), %edi
callq 0xbf90
orl -0xc(%rbp), %eax
movl %eax, -0xc(%rbp)
movl -0xc(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x9a6a
jmp 0x9b32
cmpq $0x0, 0x32a96(%rip)     # 0x3c508
jbe 0x9ad3
cmpl $0x0, -0xc(%rbp)
jne 0x9ad3
cmpl $0x0, 0x32adf(%rip)     # 0x3c560
je 0x9aad
movq $0x1, -0x920(%rbp)
movq -0x920(%rbp), %rcx
movq %rcx, %rax
lock
xaddq %rax, 0x32ac7(%rip)     # 0x3c568
addq %rcx, %rax
movq %rax, -0x928(%rbp)
jmp 0x9abf
movq 0x32ab4(%rip), %rax     # 0x3c568
addq $0x1, %rax
movq %rax, 0x32aa9(%rip)     # 0x3c568
movq 0x32aa2(%rip), %rax     # 0x3c568
cmpq 0x32a3b(%rip), %rax     # 0x3c508
jb 0x9ad1
jmp 0x9b32
jmp 0x9ad3
cmpq $0x0, -0x918(%rbp)
je 0x9af7
movq -0x918(%rbp), %rsi
leaq -0x8d0(%rbp), %rdi
callq 0xc000
cmpl $0x1, %eax
jl 0x9af7
jmp 0x9b32
leaq -0x8e0(%rbp), %rdi
callq 0xbe20
leaq -0x8e0(%rbp), %rdi
leaq -0x8d0(%rbp), %rsi
leaq -0x8f0(%rbp), %rdx
callq 0xc090
leaq -0x8f0(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x61e0
jmp 0x99f1
leaq -0x8c0(%rbp), %rdi
movl $0x9, %esi
callq *-0x128(%rbp)
cmpl $0x0, 0x32a15(%rip)     # 0x3c560
je 0x9b6e
cmpq $0x0, 0x329b3(%rip)     # 0x3c508
jbe 0x9b6e
movq 0x32a0a(%rip), %rax     # 0x3c568
cmpq 0x329a3(%rip), %rax     # 0x3c508
jb 0x9b6e
movb $0x0, %al
callq 0x29f70
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x930, %rsp            # imm = 0x930
popq %rbp
retq
nopw %cs:(%rax,%rax)