// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe28, %rsp            # imm = 0xE28
movl %edi, %ebx
leaq 0x578(%rsp), %r14
movl $0x8b0, %edx            # imm = 0x8B0
movq %r14, %rdi
xorl %esi, %esi
callq 0x6230
movl %ebx, (%r14)
movq 0x1b7ca(%rip), %rax     # 0x25368
movq %rax, 0x798(%r14)
movq 0x1c55c(%rip), %rax     # 0x26108
movq %rax, 0x7a0(%r14)
leaq 0x170(%rsp), %rdi
movl $0x208, %edx            # imm = 0x208
xorl %esi, %esi
callq 0x6230
movq 0x1c4e2(%rip), %rax     # 0x260b0
testq %rax, %rax
je 0x9e18
movq %rax, 0x580(%rsp)
movq 0x1c4d2(%rip), %rax     # 0x260b8
testq %rax, %rax
je 0x9e3f
movq %rax, 0x588(%rsp)
cmpl $0x0, 0x1c4d6(%rip)     # 0x260d4
movl 0x578(%rsp), %esi
je 0x9c32
leaq 0x590(%rsp), %rcx
leaq 0x13f18(%rip), %rdx     # 0x1db2e
leaq 0x170(%rsp), %rdi
callq 0x17e37
testl %eax, %eax
jne 0x9e73
movl 0x578(%rsp), %esi
andl $0x0, 0x1b747(%rip)     # 0x25380
leaq 0x598(%rsp), %rcx
leaq 0x13ef4(%rip), %rdx     # 0x1db3c
leaq 0x170(%rsp), %rdi
callq 0x17e37
testl %eax, %eax
je 0x9c62
andq $0x0, 0x598(%rsp)
movl $0x1, 0x1b714(%rip)     # 0x25380
leaq 0x578(%rsp), %rdi
xorl %esi, %esi
callq *0x798(%rdi)
movl %eax, %ebp
testl %eax, %eax
jne 0x9e75
movq 0x1b6d3(%rip), %r14     # 0x25360
leaq 0x11d20(%rip), %rsi     # 0x1b9b4
movq %r14, %rdi
callq 0x6310
pushq $0x1
popq %rbp
testl %eax, %eax
jne 0x9fbd
movl 0x578(%rsp), %r14d
movb $0x0, (%rsp)
movq 0x598(%rsp), %rdx
testq %rdx, %rdx
je 0x9e89
leaq 0x13e71(%rip), %rsi     # 0x1db3c
leaq 0x10(%rsp), %rcx
pushq $0x60
popq %r8
movl %r14d, %edi
callq 0x140e4
testl %eax, %eax
jne 0x9cfb
movq 0x68(%rsp), %rdx
leaq 0x13eb1(%rip), %rsi     # 0x1db9d
movq %rsp, %rcx
pushq $0x3
popq %r8
movl %r14d, %edi
callq 0x140e4
cmpb $0x0, (%rsp)
je 0x9e89
leaq 0x13c61(%rip), %rsi     # 0x1d96d
movq %rsp, %rdi
pushq $0x3
popq %rdx
callq 0x6410
testl %eax, %eax
je 0x9eef
leaq 0x14032(%rip), %rsi     # 0x1dd58
movq %rsp, %rdi
pushq $0x3
popq %rdx
callq 0x6410
testl %eax, %eax
je 0x9f3b
leaq 0x1401c(%rip), %rsi     # 0x1dd5c
movq %rsp, %rdi
pushq $0x3
popq %rdx
callq 0x6410
testl %eax, %eax
je 0x9f44
leaq 0x14006(%rip), %rsi     # 0x1dd60
movq %rsp, %rdi
pushq $0x3
popq %rdx
callq 0x6410
testl %eax, %eax
je 0x9f68
leaq 0x13ff0(%rip), %rsi     # 0x1dd64
movq %rsp, %rdi
pushq $0x3
popq %rdx
callq 0x6410
testl %eax, %eax
je 0x9f71
leaq 0x13fda(%rip), %rsi     # 0x1dd68
movq %rsp, %rdi
pushq $0x3
popq %rdx
callq 0x6410
testl %eax, %eax
je 0x9f7a
leaq 0x13fc4(%rip), %rsi     # 0x1dd6c
movq %rsp, %rdi
pushq $0x3
popq %rdx
callq 0x6410
testl %eax, %eax
je 0x9f83
leaq 0x13fae(%rip), %rsi     # 0x1dd70
movq %rsp, %rdi
pushq $0x3
popq %rdx
callq 0x6410
testl %eax, %eax
je 0x9f9d
leaq 0x13f98(%rip), %rsi     # 0x1dd74
movq %rsp, %rdi
pushq $0x3
popq %rdx
callq 0x6410
testl %eax, %eax
je 0x9fa6
leaq 0x13f82(%rip), %rsi     # 0x1dd78
movq %rsp, %rdi
pushq $0x3
popq %rdx
callq 0x6410
testl %eax, %eax
je 0x9faf
leaq 0x13f6c(%rip), %rdi     # 0x1dd7c
movq %rsp, %rsi
jmp 0xa105
leaq 0x580(%rsp), %rcx
leaq 0x13ce9(%rip), %rdx     # 0x1db10
leaq 0x170(%rsp), %rdi
movl %ebx, %esi
callq 0x17e37
testl %eax, %eax
jne 0x9e73
jmp 0x9bdf
cmpl $0x0, 0x1c27a(%rip)     # 0x260c0
je 0x9bf7
leaq 0x588(%rsp), %rcx
movl -0x10(%rcx), %esi
leaq 0x13cc3(%rip), %rdx     # 0x1db21
leaq 0x170(%rsp), %rdi
callq 0x17e37
testl %eax, %eax
je 0x9bf7
movl %eax, %ebp
movl %ebp, %eax
addq $0xe28, %rsp            # imm = 0xE28
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1b4e8(%rip), %rdi     # 0x25378
leaq 0x13d25(%rip), %rcx     # 0x1dbbc
leaq 0x70(%rsp), %rsi
movl $0x100, %edx            # imm = 0x100
callq 0x17dac
testl %eax, %eax
jne 0x9e75
leaq 0x13d20(%rip), %rdx     # 0x1dbd1
leaq 0x170(%rsp), %rdi
leaq 0x70(%rsp), %r8
movl $0x400, %esi            # imm = 0x400
movl %r14d, %ecx
movl %r14d, %r9d
xorl %eax, %eax
pushq %r14
pushq %r14
callq 0x61d0
addq $0x10, %rsp
cmpl $0x3ff, %eax            # imm = 0x3FF
jb 0x9efb
leaq 0x13e08(%rip), %rdi     # 0x1dcee
xorl %eax, %eax
callq 0xa442
jmp 0x9e75
leaq 0x12f25(%rip), %r14     # 0x1ce1b
jmp 0x9fb6
leaq 0x13956(%rip), %rsi     # 0x1d858
leaq 0x170(%rsp), %rdi
callq 0x6510
testq %rax, %rax
je 0x9f8c
movq %rax, %r14
movq %rsp, %rdi
pushq $0x1
popq %rsi
pushq $0x3
popq %rdx
movq %rax, %rcx
callq 0x60e0
cmpq $0x3, %rax
jne 0x9f4d
movq %r14, %rdi
callq 0x61c0
jmp 0x9d05
leaq 0x12edc(%rip), %r14     # 0x1ce1e
jmp 0x9fb6
leaq 0x12ed6(%rip), %r14     # 0x1ce21
jmp 0x9fb6
leaq 0x13dd5(%rip), %rdi     # 0x1dd29
xorl %eax, %eax
callq 0xa442
movq %r14, %rdi
callq 0x61c0
jmp 0x9e75
leaq 0x12eb5(%rip), %r14     # 0x1ce24
jmp 0x9fb6
leaq 0x12eaf(%rip), %r14     # 0x1ce27
jmp 0x9fb6
leaq 0x12ea9(%rip), %r14     # 0x1ce2a
jmp 0x9fb6
leaq 0x12ea3(%rip), %r14     # 0x1ce2d
jmp 0x9fb6
leaq 0x13d7f(%rip), %rdi     # 0x1dd12
callq 0x6530
jmp 0x9e75
leaq 0x12e8c(%rip), %r14     # 0x1ce30
jmp 0x9fb6
leaq 0x12e86(%rip), %r14     # 0x1ce33
jmp 0x9fb6
leaq 0x12e80(%rip), %r14     # 0x1ce36
movq %r14, 0x1b3a3(%rip)     # 0x25360
movzbl (%r14), %eax
leal -0x37(%rax), %ecx
negl %ecx
movl %ecx, %edx
cmpl $0x37, %eax
jne 0x9ffe
movzbl 0x1(%r14), %edx
subl $0x30, %edx
jne 0x9fdc
movzbl 0x2(%r14), %edx
negl %edx
testl %edx, %edx
je 0xa11a
movl %ecx, %edx
cmpl $0x37, %eax
jne 0x9ffe
movzbl 0x1(%r14), %edx
subl $0x31, %edx
jne 0x9ffc
movzbl 0x2(%r14), %edx
negl %edx
testl %edx, %edx
je 0xa111
movl %ecx, %edx
cmpl $0x37, %eax
jne 0xa03e
movzbl 0x1(%r14), %edx
subl $0x32, %edx
jne 0xa01c
movzbl 0x2(%r14), %edx
negl %edx
testl %edx, %edx
je 0xa135
movl %ecx, %edx
cmpl $0x37, %eax
jne 0xa03e
movzbl 0x1(%r14), %edx
subl $0x33, %edx
jne 0xa03c
movzbl 0x2(%r14), %edx
negl %edx
testl %edx, %edx
je 0xa123
cmpl $0x37, %eax
jne 0xa05c
movzbl 0x1(%r14), %ecx
subl $0x34, %ecx
jne 0xa05a
movzbl 0x2(%r14), %ecx
negl %ecx
testl %ecx, %ecx
je 0xa12c
leal -0x38(%rax), %ecx
negl %ecx
movl %ecx, %edx
cmpl $0x38, %eax
jne 0xa0a0
movzbl 0x1(%r14), %edx
subl $0x30, %edx
jne 0xa07f
movzbl 0x2(%r14), %edx
negl %edx
testl %edx, %edx
je 0xa147
movl %ecx, %edx
cmpb $0x38, %al
jne 0xa0a0
movzbl 0x1(%r14), %edx
subl $0x31, %edx
jne 0xa09e
movzbl 0x2(%r14), %edx
negl %edx
testl %edx, %edx
je 0xa13e
movl %ecx, %edx
cmpb $0x38, %al
jne 0xa0de
movzbl 0x1(%r14), %edx
subl $0x32, %edx
jne 0xa0bd
movzbl 0x2(%r14), %edx
negl %edx
testl %edx, %edx
je 0xa162
movl %ecx, %edx
cmpb $0x38, %al
jne 0xa0de
movzbl 0x1(%r14), %edx
subl $0x33, %edx
jne 0xa0dc
movzbl 0x2(%r14), %edx
negl %edx
testl %edx, %edx
je 0xa150
cmpb $0x38, %al
jne 0xa0f7
movzbl 0x1(%r14), %ecx
subl $0x34, %ecx
jne 0xa0f5
movzbl 0x2(%r14), %ecx
negl %ecx
testl %ecx, %ecx
je 0xa159
leaq 0x12d37(%rip), %rdi     # 0x1ce39
movq %r14, %rsi
xorl %eax, %eax
callq 0xa442
jmp 0x9e75
leaq 0x18bd(%rip), %rax      # 0xb9d5
jmp 0xa169
leaq 0xb72(%rip), %rax       # 0xac93
jmp 0xa169
leaq 0x331d(%rip), %rax      # 0xd447
jmp 0xa169
leaq 0x404d(%rip), %rax      # 0xe180
jmp 0xa169
leaq 0x25d2(%rip), %rax      # 0xc70e
jmp 0xa169
leaq 0x5aad(%rip), %rax      # 0xfbf2
jmp 0xa169
leaq 0x4d6b(%rip), %rax      # 0xeeb9
jmp 0xa169
leaq 0x750d(%rip), %rax      # 0x11664
jmp 0xa169
leaq 0x823d(%rip), %rax      # 0x1239d
jmp 0xa169
leaq 0x67c2(%rip), %rax      # 0x1092b
movq %rax, 0x1bfa8(%rip)     # 0x26118
cmpl $0x0, 0x1bfb9(%rip)     # 0x26130
sete %cl
movq 0x1bf5f(%rip), %rax     # 0x260e0
testq %rax, %rax
setg %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0xa1df
movl $0x3e8, %ecx            # imm = 0x3E8
xorl %edx, %edx
divq %rcx
movq %rax, %r15
imulq $0xf4240, %rdx, %r12    # imm = 0xF4240
movq %rsp, %r14
movq %r14, %rdi
callq 0x130d6
addq (%r14), %r15
movq %r15, (%r14)
addq 0x8(%r14), %r12
movq %r12, 0x8(%r14)
cmpq $0x3b9aca00, %r12       # imm = 0x3B9ACA00
jl 0xa1e2
movl $0x3b9aca00, %ecx       # imm = 0x3B9ACA00
movq %r12, %rax
xorl %edx, %edx
divq %rcx
addq %r15, %rax
movq %rax, (%rsp)
movq %rdx, 0x8(%rsp)
jmp 0xa1e2
xorl %r14d, %r14d
leaq 0x170(%rsp), %r15
leaq 0x578(%rsp), %r12
cmpl $0x0, 0x1bf1b(%rip)     # 0x26114
jne 0xa3f7
movq %r15, %rdi
callq 0x130d6
cmpl $0x0, 0x1beda(%rip)     # 0x260e8
je 0xa24e
pushq $0x10
popq %rdi
movl %ebx, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x6420
cmpq $-0x1, %rax
je 0xa3a9
xorl %ebp, %ebp
movl %ebx, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x64b0
testl %eax, %eax
jns 0xa250
leaq 0x138c6(%rip), %rdi     # 0x1db08
callq 0x6530
pushq $0x1
popq %rax
movl %eax, %ebp
jmp 0xa250
xorl %ebp, %ebp
movq %r12, %rdi
callq *0x1bebf(%rip)          # 0x26118
movl %eax, %r13d
orl %ebp, %r13d
cmpl $0x0, 0x1be82(%rip)     # 0x260e8
je 0xa287
xorl %ebp, %ebp
pushq $0x11
popq %rdi
movl %ebx, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x6420
cmpq $-0x1, %rax
je 0xa3d0
orl %ebp, %r13d
testb $0x2, %r13b
jne 0xa3f7
movq 0x1be40(%rip), %rax     # 0x260d8
testq %rax, %rax
sete %cl
testl %r13d, %r13d
setne %dl
orb %cl, %dl
jne 0xa2e3
cmpl $0x0, 0x1be81(%rip)     # 0x26130
je 0xa2c9
lock
incq 0x1be7f(%rip)           # 0x26138
movq 0x1be78(%rip), %rcx     # 0x26138
movq 0x1be11(%rip), %rax     # 0x260d8
jmp 0xa2da
movq 0x1be68(%rip), %rcx     # 0x26138
incq %rcx
movq %rcx, 0x1be5e(%rip)     # 0x26138
cmpq %rax, %rcx
jae 0xa3f7
testq %r14, %r14
je 0xa307
movq (%r14), %rax
cmpq %rax, 0x170(%rsp)
jne 0xa301
movq 0x178(%rsp), %rax
cmpq 0x8(%r14), %rax
jg 0xa3f7
leaq 0x70(%rsp), %rdi
callq 0x130d6
movq 0x170(%rsp), %rax
subq 0x70(%rsp), %rax
jne 0xa336
movq 0x1b011(%rip), %rcx     # 0x25338
subq 0x78(%rsp), %rcx
addq 0x178(%rsp), %rcx
jmp 0xa351
imulq $0x3b9aca00, %rax, %rcx # imm = 0x3B9ACA00
subq 0x78(%rsp), %rcx
addq 0x178(%rsp), %rcx
addq 0x1afe7(%rip), %rcx     # 0x25338
testq %rcx, %rcx
js 0xa37d
cmpq $0x3b9ac9ff, %rcx       # imm = 0x3B9AC9FF
jbe 0xa38d
movq %rcx, %rax
xorl %edx, %edx
movl $0x3b9aca00, %esi       # imm = 0x3B9ACA00
divq %rsi
movq %rax, 0x10(%rsp)
imulq $-0x3b9aca00, %rax, %rax # imm = 0xC4653600
addq %rax, %rcx
jmp 0xa393
leaq 0x137dd(%rip), %rdi     # 0x1db61
xorl %eax, %eax
callq 0xa442
xorl %ecx, %ecx
andq $0x0, 0x10(%rsp)
movq %rcx, 0x18(%rsp)
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
callq 0x61e0
jmp 0xa1f2
callq 0x6060
movl (%rax), %ebp
leaq 0x1374a(%rip), %rdi     # 0x1db01
callq 0x6530
xorl %eax, %eax
cmpl $0x3, %ebp
sete %al
leal 0x1(,%rax,2), %ebp
jmp 0xa250
callq 0x6060
movl (%rax), %ebp
leaq 0x13723(%rip), %rdi     # 0x1db01
callq 0x6530
xorl %eax, %eax
cmpl $0x3, %ebp
sete %al
leal 0x1(,%rax,2), %ebp
jmp 0xa284
leaq 0x578(%rsp), %rdi
pushq $0x9
popq %rsi
callq *0x798(%rdi)
xorl %ebp, %ebp
cmpl $0x0, 0x1bd1f(%rip)     # 0x26130
je 0x9e75
movq 0x1bcba(%rip), %rax     # 0x260d8
testq %rax, %rax
je 0x9e75
cmpq %rax, 0x1bd0a(%rip)     # 0x26138
jb 0x9e75
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x16bd8
jmp 0x9e75