// File: /workspace/github_c_makefile_star2_O3/adsr[P]phpspy/event_fout.c
// Function: event_handler_fout
int event_handler_fout(struct trace_context_s *context, int event_type) {
    int rv, fd;
    size_t len;
    trace_frame_t *frame;
    trace_request_t *request;
    event_handler_fout_udata_t *udata;
    struct timeval tv;

    udata = (event_handler_fout_udata_t*)context->event_udata;
    if (!udata && event_type != PHPSPY_TRACE_EVENT_INIT) {
        return PHPSPY_ERR;
    }
    len = 0;
    switch (event_type) {
        case PHPSPY_TRACE_EVENT_INIT:
            try(rv, event_handler_fout_open(&fd));
            udata = calloc(1, sizeof(event_handler_fout_udata_t));
            udata->fd = fd;
            udata->buf_size = opt_fout_buffer_size + 1; /* + 1 for null char */
            udata->buf = malloc(udata->buf_size);
            udata->cur = udata->buf;
            udata->rem = udata->buf_size;
            udata->use_mutex = context->event_handler_opts != NULL
                && strchr(context->event_handler_opts, 'm') != NULL ? 1 : 0;
            context->event_udata = udata;
            break;
        case PHPSPY_TRACE_EVENT_STACK_BEGIN:
            udata->cur = udata->buf;
            udata->cur[0] = '\0';
            udata->rem = udata->buf_size;
            break;
        case PHPSPY_TRACE_EVENT_FRAME:
            frame = &context->event.frame;
            try(rv, event_handler_fout_snprintf(
                &udata->cur,
                &udata->rem,
                &len,
                1,
                "%d %.*s%s%.*s %.*s:%d",
                frame->depth,
                (int)frame->loc.class_len, frame->loc.class,
                frame->loc.class_len > 0 ? "::" : "",
                (int)frame->loc.func_len, frame->loc.func,
                (int)frame->loc.file_len, frame->loc.file,
                frame->loc.lineno
            ));
            try(rv, event_handler_fout_snprintf(&udata->cur, &udata->rem, &len, 0, "%c", opt_frame_delim));
            break;
        case PHPSPY_TRACE_EVENT_VARPEEK:
            try(rv, event_handler_fout_snprintf(
                &udata->cur,
                &udata->rem,
                &len,
                1,
                "# varpeek %s@%s = %s",
                context->event.varpeek.var->name,
                context->event.varpeek.entry->filename_lineno,
                context->event.varpeek.zval_str
            ));
            try(rv, event_handler_fout_snprintf(&udata->cur, &udata->rem, &len, 0, "%c", opt_frame_delim));
            break;
        case PHPSPY_TRACE_EVENT_GLOPEEK:
            try(rv, event_handler_fout_snprintf(
                &udata->cur,
                &udata->rem,
                &len,
                1,
                "# glopeek %s = %s",
                context->event.glopeek.gentry->key,
                context->event.glopeek.zval_str
            ));
            try(rv, event_handler_fout_snprintf(&udata->cur, &udata->rem, &len, 0, "%c", opt_frame_delim));
            break;
        case PHPSPY_TRACE_EVENT_REQUEST:
            request = &context->event.request;
            try(rv, event_handler_fout_snprintf(&udata->cur, &udata->rem, &len, 1, "# uri = %s", request->uri));
            try(rv, event_handler_fout_snprintf(&udata->cur, &udata->rem, &len, 0, "%c", opt_frame_delim));
            try(rv, event_handler_fout_snprintf(&udata->cur, &udata->rem, &len, 1, "# path = %s", request->path));
            try(rv, event_handler_fout_snprintf(&udata->cur, &udata->rem, &len, 0, "%c", opt_frame_delim));
            try(rv, event_handler_fout_snprintf(&udata->cur, &udata->rem, &len, 1, "# qstring = %s", request->qstring));
            try(rv, event_handler_fout_snprintf(&udata->cur, &udata->rem, &len, 0, "%c", opt_frame_delim));
            try(rv, event_handler_fout_snprintf(&udata->cur, &udata->rem, &len, 1, "# cookie = %s", request->cookie));
            try(rv, event_handler_fout_snprintf(&udata->cur, &udata->rem, &len, 0, "%c", opt_frame_delim));
            try(rv, event_handler_fout_snprintf(&udata->cur, &udata->rem, &len, 1, "# ts = %f", request->ts));
            try(rv, event_handler_fout_snprintf(&udata->cur, &udata->rem, &len, 0, "%c", opt_frame_delim));
            break;
        case PHPSPY_TRACE_EVENT_MEM:
            try(rv, event_handler_fout_snprintf(
                &udata->cur,
                &udata->rem,
                &len,
                1,
                "# mem %lu %lu",
                (uint64_t)context->event.mem.size,
                (uint64_t)context->event.mem.peak
            ));
            try(rv, event_handler_fout_snprintf(&udata->cur, &udata->rem, &len, 0, "%c", opt_frame_delim));
            break;
        case PHPSPY_TRACE_EVENT_STACK_END:
            if (udata->cur == udata->buf) {
                /* buffer is empty */
                break;
            }
            if (opt_filter_re) {
                rv = regexec(opt_filter_re, udata->buf, 0, NULL, 0);
                if (opt_filter_negate == 0 && rv != 0) return PHPSPY_ERR_SKIPPED;
                if (opt_filter_negate != 0 && rv == 0) return PHPSPY_ERR_SKIPPED;
            }
            do {
                if (opt_verbose_fields_ts) {
                    gettimeofday(&tv, NULL);
                    try_break(rv, event_handler_fout_snprintf(&udata->cur, &udata->rem, &len, 1, "# trace_ts = %f", (double)(tv.tv_sec + tv.tv_usec / 1000000.0)));
                    try_break(rv, event_handler_fout_snprintf(&udata->cur, &udata->rem, &len, 0, "%c", opt_frame_delim));
                }
                if (opt_verbose_fields_pid) {
                    try_break(rv, event_handler_fout_snprintf(&udata->cur, &udata->rem, &len, 1, "# pid = %d", context->target.pid));
                    try_break(rv, event_handler_fout_snprintf(&udata->cur, &udata->rem, &len, 0, "%c", opt_frame_delim));
                }
                try_break(rv, event_handler_fout_snprintf(&udata->cur, &udata->rem, &len, 0, "%c", opt_trace_delim));
            } while (0);
            try(rv, event_handler_fout_write(udata));
            break;
        case PHPSPY_TRACE_EVENT_DEINIT:
            close(udata->fd);
            free(udata->buf);
            free(udata);
            break;
    }
    return PHPSPY_OK;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq 0x790(%rdi), %rbx
testq %rbx, %rbx
sete %cl
testl %esi, %esi
setne %dl
movl $0x1, %eax
testb %cl, %dl
jne 0x1986e
cmpl $0x9, %esi
ja 0x1986c
movq %rdi, %r14
movl %esi, %eax
leaq 0x6066(%rip), %rcx      # 0x1f318
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq $0x0, 0x8(%rsp)
leaq 0xc07d(%rip), %rax      # 0x25348
movq (%rax), %rbx
cmpb $0x2d, (%rbx)
jne 0x192dd
cmpb $0x0, 0x1(%rbx)
je 0x1987d
leaq 0x6103(%rip), %rsi      # 0x1f3e7
movq %rbx, %rdi
callq 0x6610
testq %rax, %rax
je 0x1931c
movl $0xba, %edi
xorl %eax, %eax
callq 0x6330
leaq 0x8(%rsp), %rdi
movq %rbx, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x6460
testl %eax, %eax
js 0x198a3
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
movl $0x241, %esi            # imm = 0x241
movl $0x1a4, %edx            # imm = 0x1A4
xorl %eax, %eax
callq 0x64f0
movl %eax, %ebp
callq 0x6060
movq %rax, %rbx
movl (%rax), %r15d
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x1934c
callq 0x6050
testl %ebp, %ebp
js 0x19892
movl $0x1, %edi
movl $0x30, %esi
callq 0x62f0
movq %rax, %rbx
movl %ebp, (%rax)
leaq 0xc001(%rip), %rax      # 0x25370
movslq (%rax), %r15
incq %r15
movq %r15, 0x18(%rbx)
movq %r15, %rdi
callq 0x63e0
movq %rax, 0x8(%rbx)
movq %rax, 0x10(%rbx)
movq %r15, 0x20(%rbx)
movq 0x7a0(%r14), %rdi
testq %rdi, %rdi
je 0x19860
movl $0x6d, %esi
callq 0x6190
xorl %ecx, %ecx
testq %rax, %rax
setne %cl
jmp 0x19862
leaq 0x28(%r14), %rax
movq %rax, 0x20(%rsp)
leaq 0x10(%rbx), %rdi
addq $0x20, %rbx
movl 0x348(%r14), %r9d
movq 0x330(%r14), %r10
leaq 0x128(%r14), %r12
testq %r10, %r10
leaq 0x5564(%rip), %rax      # 0x1e948
leaq 0x5f6b(%rip), %r13      # 0x1f356
cmoveq %rax, %r13
movl 0x328(%r14), %ebp
movl 0x338(%r14), %r15d
movl 0x340(%r14), %r11d
addq $0x228, %r14            # imm = 0x228
leaq 0x5f2e(%rip), %r8       # 0x1f340
movq %rsp, %rdx
movq %rdi, 0x18(%rsp)
movq %rbx, %rsi
movl $0x1, %ecx
xorl %eax, %eax
pushq %r11
pushq %r14
pushq %r15
pushq 0x38(%rsp)
pushq %rbp
pushq %r13
pushq %r12
pushq %r10
callq 0x198b8
addq $0x40, %rsp
testl %eax, %eax
jne 0x1986e
leaq 0xbef7(%rip), %rax      # 0x25344
movsbl (%rax), %r9d
leaq 0x5f01(%rip), %r8       # 0x1f359
movq %rsp, %rdx
movq 0x18(%rsp), %rdi
jmp 0x19834
leaq 0x350(%r14), %r9
leaq 0x10(%rbx), %r15
addq $0x20, %rbx
leaq 0x5f08(%rip), %r8       # 0x1f383
movq %rsp, %rdx
movq %r15, %rdi
movq %rbx, %rsi
movl $0x1, %ecx
xorl %eax, %eax
callq 0x198b8
testl %eax, %eax
jne 0x1986e
leaq 0xbea5(%rip), %r12      # 0x25344
movsbl (%r12), %r9d
leaq 0x5eae(%rip), %r8       # 0x1f359
movq %rsp, %rdx
movq %r15, %rdi
movq %rbx, %rsi
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x198b8
testl %eax, %eax
jne 0x1986e
leaq 0x450(%r14), %r9
leaq 0x5ebb(%rip), %r8       # 0x1f38e
movq %rsp, %rdx
movq %r15, %rdi
movq %rbx, %rsi
movl $0x1, %ecx
xorl %eax, %eax
callq 0x198b8
testl %eax, %eax
jne 0x1986e
movsbl (%r12), %r9d
leaq 0x5e5d(%rip), %r8       # 0x1f359
movq %rsp, %rdx
movq %r15, %rdi
movq %rbx, %rsi
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x198b8
testl %eax, %eax
jne 0x1986e
leaq 0x550(%r14), %r9
leaq 0x5e76(%rip), %r8       # 0x1f39a
movq %rsp, %rdx
movq %r15, %rdi
movq %rbx, %rsi
movl $0x1, %ecx
xorl %eax, %eax
callq 0x198b8
testl %eax, %eax
jne 0x1986e
movsbl (%r12), %r9d
leaq 0x5e0c(%rip), %r8       # 0x1f359
movq %rsp, %rdx
movq %r15, %rdi
movq %rbx, %rsi
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x198b8
testl %eax, %eax
jne 0x1986e
leaq 0x650(%r14), %r9
leaq 0x5e34(%rip), %r8       # 0x1f3a9
movq %rsp, %rdx
movq %r15, %rdi
movq %rbx, %rsi
movl $0x1, %ecx
xorl %eax, %eax
callq 0x198b8
testl %eax, %eax
jne 0x1986e
movsbl (%r12), %r9d
leaq 0x5dbb(%rip), %r8       # 0x1f359
movq %rsp, %rdx
movq %r15, %rdi
movq %rbx, %rsi
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x198b8
testl %eax, %eax
jne 0x1986e
movsd 0x750(%r14), %xmm0
leaq 0x5def(%rip), %r8       # 0x1f3b7
movq %rsp, %rdx
movq %r15, %rdi
movq %rbx, %rsi
movl $0x1, %ecx
movb $0x1, %al
callq 0x198b8
testl %eax, %eax
jne 0x1986e
movsbl (%r12), %r9d
jmp 0x19827
movq 0x8(%rbx), %rax
movq %rax, 0x10(%rbx)
movb $0x0, (%rax)
movq 0x18(%rbx), %rax
movq %rax, 0x20(%rbx)
jmp 0x1986c
leaq 0x10(%rbx), %r15
addq $0x20, %rbx
movq 0x758(%r14), %r9
subq $0x8, %rsp
leaq 0x5da0(%rip), %r8       # 0x1f3c1
leaq 0x8(%rsp), %rdx
movq %r15, %rdi
movq %rbx, %rsi
movl $0x1, %ecx
xorl %eax, %eax
pushq 0x760(%r14)
jmp 0x1980f
movq 0x8(%rbx), %rsi
cmpq %rsi, 0x10(%rbx)
je 0x1986c
leaq 0xca9c(%rip), %rax      # 0x260f0
movq (%rax), %rdi
testq %rdi, %rdi
je 0x19690
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x6400
movl %eax, %ecx
leaq 0xca87(%rip), %rax      # 0x260f8
movl (%rax), %edx
movl $0x8, %eax
testl %edx, %edx
jne 0x19684
testl %ecx, %ecx
jne 0x1986e
testl %edx, %edx
je 0x19690
testl %ecx, %ecx
je 0x1986e
leaq 0x10(%rbx), %r15
leaq 0xca65(%rip), %rax      # 0x26100
cmpl $0x0, (%rax)
je 0x1970f
leaq 0x8(%rsp), %r12
movq %r12, %rdi
xorl %esi, %esi
callq 0x6210
cvtsi2sdq (%r12), %xmm1
cvtsi2sdq 0x8(%r12), %xmm0
leaq 0x20(%rbx), %r12
divsd 0x5c48(%rip), %xmm0     # 0x1f310
addsd %xmm1, %xmm0
leaq 0x5cfc(%rip), %r8       # 0x1f3cf
movq %rsp, %r13
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
movl $0x1, %ecx
movb $0x1, %al
callq 0x198b8
leaq 0xbc52(%rip), %rax      # 0x25344
movsbl (%rax), %r9d
leaq 0x5c5c(%rip), %r8       # 0x1f359
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x198b8
leaq 0xc9e6(%rip), %rax      # 0x260fc
cmpl $0x0, (%rax)
je 0x19765
leaq 0x20(%rbx), %r12
movl (%r14), %r9d
leaq 0x5cb6(%rip), %r8       # 0x1f3df
movq %rsp, %r14
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
movl $0x1, %ecx
xorl %eax, %eax
callq 0x198b8
leaq 0xbbfc(%rip), %rax      # 0x25344
movsbl (%rax), %r9d
leaq 0x5c06(%rip), %r8       # 0x1f359
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x198b8
leaq 0x20(%rbx), %rsi
leaq 0xbbd5(%rip), %rax      # 0x25345
movsbl (%rax), %r9d
leaq 0x5bde(%rip), %r8       # 0x1f359
movq %rsp, %rdx
movq %r15, %rdi
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x198b8
movq %rbx, %rdi
callq 0x199ce
movl %eax, %ecx
movl $0x1, %eax
testl %ecx, %ecx
jne 0x1986e
jmp 0x1986c
leaq 0x10(%rbx), %r15
addq $0x20, %rbx
movq 0x770(%r14), %r9
leaq 0x5ba0(%rip), %r8       # 0x1f35c
movq %rsp, %rdx
movq %r15, %rdi
movq %rbx, %rsi
movl $0x1, %ecx
xorl %eax, %eax
pushq 0x778(%r14)
pushq 0x768(%r14)
jmp 0x1980f
leaq 0x10(%rbx), %r15
addq $0x20, %rbx
movq 0x780(%r14), %r9
subq $0x8, %rsp
leaq 0x5b7b(%rip), %r8       # 0x1f371
leaq 0x8(%rsp), %rdx
movq %r15, %rdi
movq %rbx, %rsi
movl $0x1, %ecx
xorl %eax, %eax
pushq 0x788(%r14)
callq 0x198b8
addq $0x10, %rsp
testl %eax, %eax
jne 0x1986e
leaq 0xbb21(%rip), %rax      # 0x25344
movsbl (%rax), %r9d
leaq 0x5b2b(%rip), %r8       # 0x1f359
movq %rsp, %rdx
movq %r15, %rdi
movq %rbx, %rsi
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x198b8
testl %eax, %eax
jne 0x1986e
jmp 0x1986c
movl (%rbx), %edi
callq 0x6260
movq 0x8(%rbx), %rdi
callq 0x6050
movq %rbx, %rdi
callq 0x6050
jmp 0x1986c
xorl %ecx, %ecx
movl %ecx, 0x28(%rbx)
movq %rbx, 0x790(%r14)
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, %edi
callq 0x6200
testl %eax, %eax
js 0x198af
movl %eax, %ebp
jmp 0x19354
movl %r15d, (%rbx)
leaq 0x5c01(%rip), %rdi      # 0x1f49d
callq 0x6530
jmp 0x198a8
callq 0x67b2
movl $0x1, %eax
jmp 0x1986e
leaq 0x5ba8(%rip), %rdi      # 0x1f45e
jmp 0x1989c