// File: /workspace/github_c_makefile_star2_O1/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 0x1ae62(%rip), %rax     # 0x25368
movq %rax, 0x798(%r14)
movq 0x1bbf4(%rip), %rax     # 0x26108
movq %rax, 0x7a0(%r14)
leaq 0x170(%rsp), %rdi
movl $0x208, %edx            # imm = 0x208
xorl %esi, %esi
callq 0x6230
movq 0x1bb7a(%rip), %rax     # 0x260b0
testq %rax, %rax
je 0xa5d6
movq %rax, 0x580(%rsp)
movq 0x1bb6a(%rip), %rax     # 0x260b8
testq %rax, %rax
je 0xa5fd
movq %rax, 0x588(%rsp)
cmpl $0x0, 0x1bb6e(%rip)     # 0x260d4
je 0xa58f
leaq 0x590(%rsp), %rcx
movl -0x18(%rcx), %esi
leaq 0x145b4(%rip), %rdx     # 0x1eb2e
leaq 0x170(%rsp), %rdi
callq 0x19612
testl %eax, %eax
jne 0xa631
movl $0x0, 0x1ade7(%rip)     # 0x25380
leaq 0x598(%rsp), %rcx
movl -0x20(%rcx), %esi
leaq 0x14591(%rip), %rdx     # 0x1eb3c
leaq 0x170(%rsp), %rdi
callq 0x19612
testl %eax, %eax
je 0xa5c8
movq $0x0, 0x598(%rsp)
movl $0x1, 0x1adae(%rip)     # 0x25380
xorl %ebp, %ebp
jmp 0xa633
leaq 0x580(%rsp), %rcx
leaq 0x1452b(%rip), %rdx     # 0x1eb10
leaq 0x170(%rsp), %rdi
movl %ebx, %esi
callq 0x19612
testl %eax, %eax
jne 0xa631
jmp 0xa547
cmpl $0x0, 0x1babc(%rip)     # 0x260c0
je 0xa55f
leaq 0x588(%rsp), %rcx
movl -0x10(%rcx), %esi
leaq 0x14505(%rip), %rdx     # 0x1eb21
leaq 0x170(%rsp), %rdi
callq 0x19612
testl %eax, %eax
je 0xa55f
movl %eax, %ebp
testl %ebp, %ebp
jne 0xa64d
leaq 0x578(%rsp), %rdi
xorl %esi, %esi
callq *0x798(%rdi)
movl %eax, %ebp
testl %eax, %eax
je 0xa661
movl %ebp, %eax
addq $0xe28, %rsp            # imm = 0xE28
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1acf8(%rip), %rdi     # 0x25360
leaq 0x12345(%rip), %rsi     # 0x1c9b4
callq 0x6310
testl %eax, %eax
jne 0xa955
movl 0x578(%rsp), %r14d
movb $0x0, (%rsp)
movq 0x598(%rsp), %rdx
testq %rdx, %rdx
je 0xa6d0
leaq 0x144a0(%rip), %rsi     # 0x1eb3c
leaq 0x10(%rsp), %rcx
movl $0x60, %r8d
movl %r14d, %edi
callq 0x1459f
testl %eax, %eax
jne 0xa6d0
movq 0x68(%rsp), %rdx
leaq 0x144de(%rip), %rsi     # 0x1eb9d
movq %rsp, %rcx
movl $0x3, %r8d
movl %r14d, %edi
callq 0x1459f
cmpb $0x0, (%rsp)
je 0xa7fc
movzwl (%rsp), %eax
xorl $0x2e37, %eax           # imm = 0x2E37
movzbl 0x2(%rsp), %ecx
xorl $0x30, %ecx
orw %ax, %cx
je 0xa869
movzwl (%rsp), %eax
xorl $0x2e37, %eax           # imm = 0x2E37
movzbl 0x2(%rsp), %ecx
xorl $0x31, %ecx
orw %ax, %cx
je 0xa875
movzwl (%rsp), %eax
xorl $0x2e37, %eax           # imm = 0x2E37
movzbl 0x2(%rsp), %ecx
xorl $0x32, %ecx
orw %ax, %cx
je 0xa881
movzwl (%rsp), %eax
xorl $0x2e37, %eax           # imm = 0x2E37
movzbl 0x2(%rsp), %ecx
xorl $0x33, %ecx
orw %ax, %cx
je 0xa8d5
movzwl (%rsp), %eax
xorl $0x2e37, %eax           # imm = 0x2E37
movzbl 0x2(%rsp), %ecx
xorl $0x34, %ecx
orw %ax, %cx
je 0xa8de
movzwl (%rsp), %eax
xorl $0x2e38, %eax           # imm = 0x2E38
movzbl 0x2(%rsp), %ecx
xorl $0x30, %ecx
orw %ax, %cx
je 0xa907
movzwl (%rsp), %eax
xorl $0x2e38, %eax           # imm = 0x2E38
movzbl 0x2(%rsp), %ecx
xorl $0x31, %ecx
orw %ax, %cx
je 0xa910
movzwl (%rsp), %eax
xorl $0x2e38, %eax           # imm = 0x2E38
movzbl 0x2(%rsp), %ecx
xorl $0x32, %ecx
orw %ax, %cx
je 0xa919
movzwl (%rsp), %eax
xorl $0x2e38, %eax           # imm = 0x2E38
movzbl 0x2(%rsp), %ecx
xorl $0x33, %ecx
orw %ax, %cx
je 0xa922
movzwl (%rsp), %eax
xorl $0x2e38, %eax           # imm = 0x2E38
movzbl 0x2(%rsp), %ecx
xorl $0x34, %ecx
orw %ax, %cx
je 0xa939
leaq 0x14597(%rip), %rdi     # 0x1ed7c
xorl %r14d, %r14d
movq %rsp, %rsi
xorl %eax, %eax
callq 0xadcf
movl $0x1, %ebp
jmp 0xa94c
movq 0x1ab75(%rip), %rdi     # 0x25378
leaq 0x143b2(%rip), %rcx     # 0x1ebbc
leaq 0x70(%rsp), %rsi
movl $0x100, %edx            # imm = 0x100
callq 0x19570
testl %eax, %eax
jne 0xa8fd
leaq 0x143a9(%rip), %rdx     # 0x1ebd1
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 0xa88d
leaq 0x14491(%rip), %rdi     # 0x1ecee
xorl %eax, %eax
callq 0xadcf
jmp 0xa8fd
leaq 0x135ab(%rip), %rax     # 0x1de1b
jmp 0xa940
leaq 0x135a2(%rip), %rax     # 0x1de1e
jmp 0xa940
leaq 0x13599(%rip), %rax     # 0x1de21
jmp 0xa940
leaq 0x13fc4(%rip), %rsi     # 0x1e858
leaq 0x170(%rsp), %rdi
callq 0x6510
testq %rax, %rax
je 0xa92b
movq %rax, %r14
movq %rsp, %rdi
movl $0x1, %esi
movl $0x3, %edx
movq %rax, %rcx
callq 0x60e0
cmpq $0x3, %rax
jne 0xa8e7
movq %r14, %rdi
callq 0x61c0
jmp 0xa6da
leaq 0x13548(%rip), %rax     # 0x1de24
jmp 0xa940
leaq 0x13542(%rip), %rax     # 0x1de27
jmp 0xa940
leaq 0x1443b(%rip), %rdi     # 0x1ed29
xorl %eax, %eax
callq 0xadcf
movq %r14, %rdi
callq 0x61c0
movl $0x1, %ebp
xorl %r14d, %r14d
jmp 0xa94c
leaq 0x1351c(%rip), %rax     # 0x1de2a
jmp 0xa940
leaq 0x13516(%rip), %rax     # 0x1de2d
jmp 0xa940
leaq 0x13510(%rip), %rax     # 0x1de30
jmp 0xa940
leaq 0x1350a(%rip), %rax     # 0x1de33
jmp 0xa940
leaq 0x143e0(%rip), %rdi     # 0x1ed12
callq 0x6530
jmp 0xa8fd
leaq 0x134f6(%rip), %rax     # 0x1de36
movq %rax, 0x1aa19(%rip)     # 0x25360
xorl %ebp, %ebp
movb $0x1, %r14b
testb %r14b, %r14b
je 0xa64d
movq 0x1aa04(%rip), %r14     # 0x25360
leaq 0x134b8(%rip), %rdi     # 0x1de1b
movq %r14, %rsi
callq 0x6310
testl %eax, %eax
je 0xaa55
leaq 0x134a4(%rip), %rdi     # 0x1de1e
movq %r14, %rsi
callq 0x6310
testl %eax, %eax
je 0xaa5e
leaq 0x13490(%rip), %rdi     # 0x1de21
movq %r14, %rsi
callq 0x6310
testl %eax, %eax
je 0xaa67
leaq 0x1347c(%rip), %rdi     # 0x1de24
movq %r14, %rsi
callq 0x6310
testl %eax, %eax
je 0xaa70
leaq 0x13468(%rip), %rdi     # 0x1de27
movq %r14, %rsi
callq 0x6310
testl %eax, %eax
je 0xaa79
leaq 0x13454(%rip), %rdi     # 0x1de2a
movq %r14, %rsi
callq 0x6310
testl %eax, %eax
je 0xaa82
leaq 0x13440(%rip), %rdi     # 0x1de2d
movq %r14, %rsi
callq 0x6310
testl %eax, %eax
je 0xaa8b
leaq 0x1342c(%rip), %rdi     # 0x1de30
movq %r14, %rsi
callq 0x6310
testl %eax, %eax
je 0xaa94
leaq 0x13418(%rip), %rdi     # 0x1de33
movq %r14, %rsi
callq 0x6310
testl %eax, %eax
je 0xaa9d
leaq 0x13408(%rip), %rdi     # 0x1de36
movq %r14, %rsi
callq 0x6310
testl %eax, %eax
je 0xaaa6
leaq 0x133f8(%rip), %rdi     # 0x1de39
movq %r14, %rsi
xorl %eax, %eax
callq 0xadcf
movl $0x1, %ebp
jmp 0xa64d
leaq 0xca7(%rip), %rax       # 0xb703
jmp 0xaaad
leaq 0x1919(%rip), %rax      # 0xc37e
jmp 0xaaad
leaq 0x257f(%rip), %rax      # 0xcfed
jmp 0xaaad
leaq 0x31e5(%rip), %rax      # 0xdc5c
jmp 0xaaad
leaq 0x3e4b(%rip), %rax      # 0xe8cb
jmp 0xaaad
leaq 0x4ab1(%rip), %rax      # 0xf53a
jmp 0xaaad
leaq 0x5717(%rip), %rax      # 0x101a9
jmp 0xaaad
leaq 0x637d(%rip), %rax      # 0x10e18
jmp 0xaaad
leaq 0x6fe3(%rip), %rax      # 0x11a87
jmp 0xaaad
leaq 0x7c49(%rip), %rax      # 0x126f6
movq %rax, 0x1b664(%rip)     # 0x26118
cmpl $0x0, 0x1b675(%rip)     # 0x26130
sete %al
movq 0x1b61b(%rip), %rcx     # 0x260e0
testq %rcx, %rcx
setg %dl
andb %al, %dl
cmpb $0x1, %dl
jne 0xab50
movq %rcx, %rax
shrq $0x3, %rax
movabsq $0x20c49ba5e353f7cf, %rdx # imm = 0x20C49BA5E353F7CF
mulq %rdx
movq %rdx, %r15
shrq $0x4, %r15
imulq $0x3e8, %r15, %rax      # imm = 0x3E8
subq %rax, %rcx
imulq $0xf4240, %rcx, %r12    # imm = 0xF4240
movq %rsp, %r14
movq %r14, %rdi
callq 0x13365
addq (%r14), %r15
movq %r15, (%r14)
addq 0x8(%r14), %r12
movq %r12, 0x8(%r14)
cmpq $0x3b9aca00, %r12       # imm = 0x3B9ACA00
jl 0xab53
movq %r12, %rax
shrq $0x9, %rax
movabsq $0x44b82fa09b5a53, %rcx # imm = 0x44B82FA09B5A53
mulq %rcx
shrq $0xb, %rdx
addq %rdx, %r15
movq %r15, (%rsp)
imulq $0x3b9aca00, %rdx, %rax # imm = 0x3B9ACA00
subq %rax, %r12
movq %r12, 0x8(%rsp)
jmp 0xab53
xorl %r14d, %r14d
cmpl $0x0, 0x1b5ba(%rip)     # 0x26114
je 0xaba9
leaq 0x578(%rsp), %rdi
movl $0x9, %esi
callq *0x798(%rdi)
xorl %ebp, %ebp
cmpl $0x0, 0x1b5b8(%rip)     # 0x26130
je 0xa64d
movq 0x1b553(%rip), %rax     # 0x260d8
testq %rax, %rax
je 0xa64d
cmpq %rax, 0x1b5a3(%rip)     # 0x26138
jb 0xa64d
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x18296
jmp 0xa64d
leaq 0x170(%rsp), %r15
leaq 0x578(%rsp), %r12
movq %r15, %rdi
callq 0x13365
cmpl $0x0, 0x1b520(%rip)     # 0x260e8
movl $0x0, %ebp
je 0xac00
movl $0x10, %edi
movl %ebx, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x6420
cmpq $-0x1, %rax
je 0xad6b
xorl %ebp, %ebp
movl %ebx, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x64b0
testl %eax, %eax
js 0xadb9
movq %r12, %rdi
callq *0x1b50f(%rip)          # 0x26118
movl %eax, %r13d
orl %ebp, %r13d
cmpl $0x0, 0x1b4d2(%rip)     # 0x260e8
je 0xac39
xorl %ebp, %ebp
movl $0x11, %edi
movl %ebx, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x6420
cmpq $-0x1, %rax
je 0xad92
orl %ebp, %r13d
testb $0x2, %r13b
jne 0xab5c
testl %r13d, %r13d
jne 0xac80
cmpq $0x0, 0x1b488(%rip)     # 0x260d8
je 0xac80
cmpl $0x0, 0x1b4d7(%rip)     # 0x26130
je 0xac65
lock
incq 0x1b4d5(%rip)           # 0x26138
jmp 0xac6c
incq 0x1b4cc(%rip)           # 0x26138
movq 0x1b4c5(%rip), %rax     # 0x26138
cmpq 0x1b45e(%rip), %rax     # 0x260d8
jae 0xab5c
testq %r14, %r14
je 0xaca9
movq (%r14), %rax
cmpq %rax, 0x170(%rsp)
jne 0xac9e
movq 0x178(%rsp), %rax
cmpq 0x8(%r14), %rax
setg %al
testb %al, %al
jne 0xab5c
leaq 0x70(%rsp), %rdi
callq 0x13365
movq 0x170(%rsp), %rax
subq 0x70(%rsp), %rax
jne 0xacd8
movq 0x1a66f(%rip), %rcx     # 0x25338
subq 0x78(%rsp), %rcx
addq 0x178(%rsp), %rcx
jmp 0xacf3
imulq $0x3b9aca00, %rax, %rcx # imm = 0x3B9ACA00
subq 0x78(%rsp), %rcx
addq 0x178(%rsp), %rcx
addq 0x1a645(%rip), %rcx     # 0x25338
testq %rcx, %rcx
jns 0xad08
leaq 0x13e62(%rip), %rdi     # 0x1eb61
xorl %eax, %eax
callq 0xadcf
xorl %ecx, %ecx
cmpq $0x3b9ac9ff, %rcx       # imm = 0x3B9AC9FF
ja 0xad21
movq $0x0, 0x10(%rsp)
movq %rcx, 0x18(%rsp)
jmp 0xad4d
movq %rcx, %rax
shrq $0x9, %rax
movabsq $0x44b82fa09b5a53, %rdx # imm = 0x44B82FA09B5A53
mulq %rdx
shrq $0xb, %rdx
movq %rdx, 0x10(%rsp)
imulq $-0x3b9aca00, %rdx, %rax # imm = 0xC4653600
addq %rcx, %rax
movq %rax, 0x18(%rsp)
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
callq 0x61e0
cmpl $0x0, 0x1b3b4(%rip)     # 0x26114
je 0xabb9
jmp 0xab5c
callq 0x6060
movl (%rax), %ebp
leaq 0x13d88(%rip), %rdi     # 0x1eb01
callq 0x6530
xorl %eax, %eax
cmpl $0x3, %ebp
sete %al
leal 0x1(,%rax,2), %ebp
jmp 0xac00
callq 0x6060
movl (%rax), %ebp
leaq 0x13d61(%rip), %rdi     # 0x1eb01
callq 0x6530
xorl %eax, %eax
cmpl $0x3, %ebp
sete %al
leal 0x1(,%rax,2), %ebp
jmp 0xac36
leaq 0x13d48(%rip), %rdi     # 0x1eb08
callq 0x6530
movl $0x1, %ebp
jmp 0xac00