// File: /workspace/github_c_makefile_star2_O3/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 0x1b0d0(%rip), %rax     # 0x25368
movq %rax, 0x798(%r14)
movq 0x1be62(%rip), %rax     # 0x26108
movq %rax, 0x7a0(%r14)
leaq 0x170(%rsp), %rdi
movl $0x208, %edx            # imm = 0x208
xorl %esi, %esi
callq 0x6230
movq 0x1bde8(%rip), %rax     # 0x260b0
testq %rax, %rax
je 0xa519
movq %rax, 0x580(%rsp)
movq 0x1bdd8(%rip), %rax     # 0x260b8
testq %rax, %rax
je 0xa540
movq %rax, 0x588(%rsp)
cmpl $0x0, 0x1bddc(%rip)     # 0x260d4
movl 0x578(%rsp), %esi
je 0xa32c
leaq 0x590(%rsp), %rcx
leaq 0x1481e(%rip), %rdx     # 0x1eb2e
leaq 0x170(%rsp), %rdi
callq 0x18ec3
testl %eax, %eax
jne 0xa574
movl 0x578(%rsp), %esi
movl $0x0, 0x1b04a(%rip)     # 0x25380
leaq 0x598(%rsp), %rcx
leaq 0x147f7(%rip), %rdx     # 0x1eb3c
leaq 0x170(%rsp), %rdi
callq 0x18ec3
testl %eax, %eax
je 0xa362
movq $0x0, 0x598(%rsp)
movl $0x1, 0x1b014(%rip)     # 0x25380
leaq 0x578(%rsp), %rdi
xorl %esi, %esi
callq *0x798(%rdi)
movl %eax, %ebp
testl %eax, %eax
jne 0xa576
movq 0x1afd3(%rip), %r14     # 0x25360
leaq 0x12620(%rip), %rsi     # 0x1c9b4
movq %r14, %rdi
callq 0x6310
testl %eax, %eax
jne 0xa6c9
movl 0x578(%rsp), %r14d
movq 0x598(%rsp), %rdx
movb $0x0, (%rsp)
testq %rdx, %rdx
je 0xa58a
leaq 0x14774(%rip), %rsi     # 0x1eb3c
leaq 0x10(%rsp), %rcx
movl $0x60, %r8d
movl %r14d, %edi
callq 0x1412d
testl %eax, %eax
jne 0xa3fc
movq 0x68(%rsp), %rdx
leaq 0x147b2(%rip), %rsi     # 0x1eb9d
movq %rsp, %rcx
movl $0x3, %r8d
movl %r14d, %edi
callq 0x1412d
cmpb $0x0, (%rsp)
je 0xa58a
movzwl (%rsp), %eax
xorl $0x2e37, %eax           # imm = 0x2E37
movzbl 0x2(%rsp), %ecx
xorl $0x30, %ecx
orw %ax, %cx
je 0xa5f7
movzwl (%rsp), %eax
xorl $0x2e37, %eax           # imm = 0x2E37
movzbl 0x2(%rsp), %ecx
xorl $0x31, %ecx
orw %ax, %cx
je 0xa647
movzwl (%rsp), %eax
xorl $0x2e37, %eax           # imm = 0x2E37
movzbl 0x2(%rsp), %ecx
xorl $0x32, %ecx
orw %ax, %cx
je 0xa650
movzwl (%rsp), %eax
xorl $0x2e37, %eax           # imm = 0x2E37
movzbl 0x2(%rsp), %ecx
xorl $0x33, %ecx
orw %ax, %cx
je 0xa674
movzwl (%rsp), %eax
xorl $0x2e37, %eax           # imm = 0x2E37
movzbl 0x2(%rsp), %ecx
xorl $0x34, %ecx
orw %ax, %cx
je 0xa67d
movzwl (%rsp), %eax
xorl $0x2e38, %eax           # imm = 0x2E38
movzbl 0x2(%rsp), %ecx
xorl $0x30, %ecx
orw %ax, %cx
je 0xa686
movzwl (%rsp), %eax
xorl $0x2e38, %eax           # imm = 0x2E38
movzbl 0x2(%rsp), %ecx
xorl $0x31, %ecx
orw %ax, %cx
je 0xa68f
movzwl (%rsp), %eax
xorl $0x2e38, %eax           # imm = 0x2E38
movzbl 0x2(%rsp), %ecx
xorl $0x32, %ecx
orw %ax, %cx
je 0xa6a9
movzwl (%rsp), %eax
xorl $0x2e38, %eax           # imm = 0x2E38
movzbl 0x2(%rsp), %ecx
xorl $0x33, %ecx
orw %ax, %cx
je 0xa6b2
movzwl (%rsp), %eax
xorl $0x2e38, %eax           # imm = 0x2E38
movzbl 0x2(%rsp), %ecx
xorl $0x34, %ecx
orw %ax, %cx
je 0xa6bb
leaq 0x1486b(%rip), %rdi     # 0x1ed7c
movq %rsp, %rsi
jmp 0xa811
leaq 0x580(%rsp), %rcx
leaq 0x145e8(%rip), %rdx     # 0x1eb10
leaq 0x170(%rsp), %rdi
movl %ebx, %esi
callq 0x18ec3
testl %eax, %eax
jne 0xa574
jmp 0xa2d9
cmpl $0x0, 0x1bb79(%rip)     # 0x260c0
je 0xa2f1
leaq 0x588(%rsp), %rcx
movl -0x10(%rcx), %esi
leaq 0x145c2(%rip), %rdx     # 0x1eb21
leaq 0x170(%rsp), %rdi
callq 0x18ec3
testl %eax, %eax
je 0xa2f1
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 0x1ade7(%rip), %rdi     # 0x25378
leaq 0x14624(%rip), %rcx     # 0x1ebbc
leaq 0x70(%rsp), %rsi
movl $0x100, %edx            # imm = 0x100
callq 0x18e34
testl %eax, %eax
jne 0xa818
leaq 0x1461b(%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 0xa603
leaq 0x14703(%rip), %rdi     # 0x1ecee
xorl %eax, %eax
callq 0xab99
jmp 0xa818
leaq 0x1381d(%rip), %r14     # 0x1de1b
jmp 0xa6c2
leaq 0x1424e(%rip), %rsi     # 0x1e858
leaq 0x170(%rsp), %rdi
callq 0x6510
testq %rax, %rax
je 0xa698
movq %rax, %r14
movq %rsp, %rdi
movl $0x1, %esi
movl $0x3, %edx
movq %rax, %rcx
callq 0x60e0
cmpq $0x3, %rax
jne 0xa659
movq %r14, %rdi
callq 0x61c0
jmp 0xa406
leaq 0x137d0(%rip), %r14     # 0x1de1e
jmp 0xa6c2
leaq 0x137ca(%rip), %r14     # 0x1de21
jmp 0xa6c2
leaq 0x146c9(%rip), %rdi     # 0x1ed29
xorl %eax, %eax
callq 0xab99
movq %r14, %rdi
callq 0x61c0
jmp 0xa818
leaq 0x137a9(%rip), %r14     # 0x1de24
jmp 0xa6c2
leaq 0x137a3(%rip), %r14     # 0x1de27
jmp 0xa6c2
leaq 0x1379d(%rip), %r14     # 0x1de2a
jmp 0xa6c2
leaq 0x13797(%rip), %r14     # 0x1de2d
jmp 0xa6c2
leaq 0x14673(%rip), %rdi     # 0x1ed12
callq 0x6530
jmp 0xa818
leaq 0x13780(%rip), %r14     # 0x1de30
jmp 0xa6c2
leaq 0x1377a(%rip), %r14     # 0x1de33
jmp 0xa6c2
leaq 0x13774(%rip), %r14     # 0x1de36
movq %r14, 0x1ac97(%rip)     # 0x25360
movzbl (%r14), %eax
leal -0x37(%rax), %ecx
negl %ecx
movl %ecx, %edx
cmpl $0x37, %eax
jne 0xa70a
movzbl 0x1(%r14), %edx
subl $0x30, %edx
jne 0xa6e8
movzbl 0x2(%r14), %edx
negl %edx
testl %edx, %edx
je 0xa82b
movl %ecx, %edx
cmpl $0x37, %eax
jne 0xa70a
movzbl 0x1(%r14), %edx
subl $0x31, %edx
jne 0xa708
movzbl 0x2(%r14), %edx
negl %edx
testl %edx, %edx
je 0xa822
movl %ecx, %edx
cmpl $0x37, %eax
jne 0xa74a
movzbl 0x1(%r14), %edx
subl $0x32, %edx
jne 0xa728
movzbl 0x2(%r14), %edx
negl %edx
testl %edx, %edx
je 0xa846
movl %ecx, %edx
cmpl $0x37, %eax
jne 0xa74a
movzbl 0x1(%r14), %edx
subl $0x33, %edx
jne 0xa748
movzbl 0x2(%r14), %edx
negl %edx
testl %edx, %edx
je 0xa834
cmpl $0x37, %eax
jne 0xa768
movzbl 0x1(%r14), %ecx
subl $0x34, %ecx
jne 0xa766
movzbl 0x2(%r14), %ecx
negl %ecx
testl %ecx, %ecx
je 0xa83d
leal -0x38(%rax), %ecx
negl %ecx
movl %ecx, %edx
cmpl $0x38, %eax
jne 0xa7ac
movzbl 0x1(%r14), %edx
subl $0x30, %edx
jne 0xa78b
movzbl 0x2(%r14), %edx
negl %edx
testl %edx, %edx
je 0xa858
movl %ecx, %edx
cmpb $0x38, %al
jne 0xa7ac
movzbl 0x1(%r14), %edx
subl $0x31, %edx
jne 0xa7aa
movzbl 0x2(%r14), %edx
negl %edx
testl %edx, %edx
je 0xa84f
movl %ecx, %edx
cmpb $0x38, %al
jne 0xa7ea
movzbl 0x1(%r14), %edx
subl $0x32, %edx
jne 0xa7c9
movzbl 0x2(%r14), %edx
negl %edx
testl %edx, %edx
je 0xa873
movl %ecx, %edx
cmpb $0x38, %al
jne 0xa7ea
movzbl 0x1(%r14), %edx
subl $0x33, %edx
jne 0xa7e8
movzbl 0x2(%r14), %edx
negl %edx
testl %edx, %edx
je 0xa861
cmpb $0x38, %al
jne 0xa803
movzbl 0x1(%r14), %ecx
subl $0x34, %ecx
jne 0xa801
movzbl 0x2(%r14), %ecx
negl %ecx
testl %ecx, %ecx
je 0xa86a
leaq 0x1362b(%rip), %rdi     # 0x1de39
movq %r14, %rsi
xorl %eax, %eax
callq 0xab99
movl $0x1, %ebp
jmp 0xa576
leaq 0x1910(%rip), %rax      # 0xc139
jmp 0xa87a
leaq 0xc9b(%rip), %rax       # 0xb4cd
jmp 0xa87a
leaq 0x31be(%rip), %rax      # 0xd9f9
jmp 0xa87a
leaq 0x3e15(%rip), %rax      # 0xe659
jmp 0xa87a
leaq 0x254c(%rip), %rax      # 0xcd99
jmp 0xa87a
leaq 0x56c3(%rip), %rax      # 0xff19
jmp 0xa87a
leaq 0x4a5a(%rip), %rax      # 0xf2b9
jmp 0xa87a
leaq 0x6f71(%rip), %rax      # 0x117d9
jmp 0xa87a
leaq 0x7bc8(%rip), %rax      # 0x12439
jmp 0xa87a
leaq 0x62ff(%rip), %rax      # 0x10b79
movq %rax, 0x1b897(%rip)     # 0x26118
cmpl $0x0, 0x1b8a8(%rip)     # 0x26130
sete %al
movq 0x1b84e(%rip), %rcx     # 0x260e0
testq %rcx, %rcx
setg %dl
andb %al, %dl
cmpb $0x1, %dl
jne 0xa91d
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 0x13099
addq (%r14), %r15
movq %r15, (%r14)
addq 0x8(%r14), %r12
movq %r12, 0x8(%r14)
cmpq $0x3b9aca00, %r12       # imm = 0x3B9ACA00
jl 0xa920
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 0xa920
xorl %r14d, %r14d
cmpl $0x0, 0x1b7ed(%rip)     # 0x26114
je 0xa976
leaq 0x578(%rsp), %rdi
movl $0x9, %esi
callq *0x798(%rdi)
xorl %ebp, %ebp
cmpl $0x0, 0x1b7eb(%rip)     # 0x26130
je 0xa576
movq 0x1b786(%rip), %rax     # 0x260d8
testq %rax, %rax
je 0xa576
cmpq %rax, 0x1b7d6(%rip)     # 0x26138
jb 0xa576
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x17b52
jmp 0xa576
leaq 0x170(%rsp), %r15
leaq 0x578(%rsp), %r12
movq %r15, %rdi
callq 0x13099
cmpl $0x0, 0x1b753(%rip)     # 0x260e8
movl $0x0, %ebp
je 0xa9cd
movl $0x10, %edi
movl %ebx, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x6420
cmpq $-0x1, %rax
je 0xab35
xorl %ebp, %ebp
movl %ebx, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x64b0
testl %eax, %eax
js 0xab83
movq %r12, %rdi
callq *0x1b742(%rip)          # 0x26118
movl %eax, %r13d
orl %ebp, %r13d
cmpl $0x0, 0x1b705(%rip)     # 0x260e8
je 0xaa06
xorl %ebp, %ebp
movl $0x11, %edi
movl %ebx, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x6420
cmpq $-0x1, %rax
je 0xab5c
orl %ebp, %r13d
testb $0x2, %r13b
jne 0xa929
movq 0x1b6c1(%rip), %rax     # 0x260d8
testq %rax, %rax
sete %cl
testl %r13d, %r13d
setne %dl
orb %cl, %dl
jne 0xaa62
cmpl $0x0, 0x1b702(%rip)     # 0x26130
je 0xaa48
lock
incq 0x1b700(%rip)           # 0x26138
movq 0x1b6f9(%rip), %rcx     # 0x26138
movq 0x1b692(%rip), %rax     # 0x260d8
jmp 0xaa59
movq 0x1b6e9(%rip), %rcx     # 0x26138
incq %rcx
movq %rcx, 0x1b6df(%rip)     # 0x26138
cmpq %rax, %rcx
jae 0xa929
testq %r14, %r14
je 0xaa86
movq 0x170(%rsp), %rax
cmpq %rax, (%r14)
jne 0xaa80
movq 0x8(%r14), %rax
cmpq 0x178(%rsp), %rax
jl 0xa929
leaq 0x70(%rsp), %rdi
callq 0x13099
movq 0x78(%rsp), %rax
movq 0x170(%rsp), %rdx
movq 0x178(%rsp), %rcx
subq 0x70(%rsp), %rdx
jne 0xaab1
subq %rax, %rcx
jmp 0xaabe
imulq $0x3b9aca00, %rdx, %rdx # imm = 0x3B9ACA00
subq %rax, %rcx
addq %rdx, %rcx
addq 0x1a873(%rip), %rcx     # 0x25338
js 0xaaf9
cmpq $0x3b9ac9ff, %rcx       # imm = 0x3B9AC9FF
jbe 0xab09
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 %rax, %rcx
jmp 0xab12
leaq 0x14061(%rip), %rdi     # 0x1eb61
xorl %eax, %eax
callq 0xab99
xorl %ecx, %ecx
movq $0x0, 0x10(%rsp)
movq %rcx, 0x18(%rsp)
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
callq 0x61e0
cmpl $0x0, 0x1b5ea(%rip)     # 0x26114
je 0xa986
jmp 0xa929
callq 0x6060
movl (%rax), %ebp
leaq 0x13fbe(%rip), %rdi     # 0x1eb01
callq 0x6530
xorl %eax, %eax
cmpl $0x3, %ebp
sete %al
leal 0x1(,%rax,2), %ebp
jmp 0xa9cd
callq 0x6060
movl (%rax), %ebp
leaq 0x13f97(%rip), %rdi     # 0x1eb01
callq 0x6530
xorl %eax, %eax
cmpl $0x3, %ebp
sete %al
leal 0x1(,%rax,2), %ebp
jmp 0xaa03
leaq 0x13f7e(%rip), %rdi     # 0x1eb08
callq 0x6530
movl $0x1, %ebp
jmp 0xa9cd