// File: /workspace/github_c_makefile_star2_O2/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 %al
testl %esi, %esi
setne %cl
pushq $0x1
popq %r13
testb %al, %cl
je 0x181fc
movl %r13d, %eax
jmp 0x187e7
cmpl $0x9, %esi
ja 0x187e5
movq %rdi, %r14
movl %esi, %eax
leaq 0x6107(%rip), %rcx      # 0x1e318
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
andq $0x0, 0x8(%rsp)
leaq 0xd121(%rip), %rax      # 0x25348
movq (%rax), %rbx
cmpb $0x2d, (%rbx)
jne 0x18239
cmpb $0x0, 0x1(%rbx)
je 0x187f6
leaq 0x61a7(%rip), %rsi      # 0x1e3e7
movq %rbx, %rdi
callq 0x6610
testq %rax, %rax
je 0x18278
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 0x18846
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
callq 0x6050
testl %ebp, %ebp
js 0x18809
pushq $0x1
popq %rdi
pushq $0x30
popq %rsi
callq 0x62f0
movq %rax, %rbx
movl %ebp, (%rax)
leaq 0xd0ae(%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 0x187d9
pushq $0x6d
popq %rsi
callq 0x6190
xorl %ecx, %ecx
testq %rax, %rax
setne %cl
jmp 0x187db
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 0x5613(%rip), %rax      # 0x1d948
leaq 0x601a(%rip), %r13      # 0x1e356
cmoveq %rax, %r13
movl 0x328(%r14), %ebp
movl 0x338(%r14), %r15d
movl 0x340(%r14), %r11d
addq $0x228, %r14            # imm = 0x228
leaq 0x5fdd(%rip), %r8       # 0x1e340
movq %rsp, %rdx
pushq $0x1
popq %rcx
movq %rdi, 0x18(%rsp)
movq %rbx, %rsi
xorl %eax, %eax
pushq %r11
pushq %r14
pushq %r15
pushq 0x38(%rsp)
pushq %rbp
pushq %r13
pushq %r12
pushq %r10
callq 0x1886b
addq $0x40, %rsp
testl %eax, %eax
jne 0x187e7
leaq 0xcfa8(%rip), %rax      # 0x25344
movsbl (%rax), %r9d
leaq 0x5fb2(%rip), %r8       # 0x1e359
movq %rsp, %rdx
movq 0x18(%rsp), %rdi
jmp 0x187ad
leaq 0x350(%r14), %r9
leaq 0x10(%rbx), %r15
addq $0x20, %rbx
leaq 0x5fb9(%rip), %r8       # 0x1e383
movq %rsp, %rdx
pushq $0x1
popq %rcx
movq %r15, %rdi
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1886b
testl %eax, %eax
jne 0x187e7
leaq 0xcf58(%rip), %r12      # 0x25344
movsbl (%r12), %r9d
leaq 0x5f61(%rip), %r8       # 0x1e359
movq %rsp, %rdx
movq %r15, %rdi
movq %rbx, %rsi
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x1886b
testl %eax, %eax
jne 0x187e7
leaq 0x450(%r14), %r9
leaq 0x5f6e(%rip), %r8       # 0x1e38e
movq %rsp, %rdx
pushq $0x1
popq %rcx
movq %r15, %rdi
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1886b
testl %eax, %eax
jne 0x187e7
movsbl (%r12), %r9d
leaq 0x5f12(%rip), %r8       # 0x1e359
movq %rsp, %rdx
movq %r15, %rdi
movq %rbx, %rsi
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x1886b
testl %eax, %eax
jne 0x187e7
leaq 0x550(%r14), %r9
leaq 0x5f2b(%rip), %r8       # 0x1e39a
movq %rsp, %rdx
pushq $0x1
popq %rcx
movq %r15, %rdi
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1886b
testl %eax, %eax
jne 0x187e7
movsbl (%r12), %r9d
leaq 0x5ec3(%rip), %r8       # 0x1e359
movq %rsp, %rdx
movq %r15, %rdi
movq %rbx, %rsi
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x1886b
testl %eax, %eax
jne 0x187e7
leaq 0x650(%r14), %r9
leaq 0x5eeb(%rip), %r8       # 0x1e3a9
movq %rsp, %rdx
pushq $0x1
popq %rcx
movq %r15, %rdi
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1886b
testl %eax, %eax
jne 0x187e7
movsbl (%r12), %r9d
leaq 0x5e74(%rip), %r8       # 0x1e359
movq %rsp, %rdx
movq %r15, %rdi
movq %rbx, %rsi
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x1886b
testl %eax, %eax
jne 0x187e7
movsd 0x750(%r14), %xmm0
leaq 0x5ea8(%rip), %r8       # 0x1e3b7
movq %rsp, %rdx
pushq $0x1
popq %rcx
movq %r15, %rdi
movq %rbx, %rsi
movb $0x1, %al
callq 0x1886b
testl %eax, %eax
jne 0x187e7
movsbl (%r12), %r9d
jmp 0x187a0
movq 0x8(%rbx), %rax
movq %rax, 0x10(%rbx)
movb $0x0, (%rax)
movq 0x18(%rbx), %rax
movq %rax, 0x20(%rbx)
jmp 0x187e5
leaq 0x10(%rbx), %r15
addq $0x20, %rbx
movq 0x758(%r14), %r9
subq $0x8, %rsp
leaq 0x5e5b(%rip), %r8       # 0x1e3c1
leaq 0x8(%rsp), %rdx
pushq $0x1
popq %rcx
movq %r15, %rdi
movq %rbx, %rsi
xorl %eax, %eax
pushq 0x760(%r14)
jmp 0x18788
movq 0x8(%rbx), %rsi
cmpq %rsi, 0x10(%rbx)
je 0x187e5
leaq 0xdb59(%rip), %rax      # 0x260f0
movq (%rax), %rdi
testq %rdi, %rdi
je 0x185d1
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x6400
movl %eax, %ecx
leaq 0xdb44(%rip), %rax      # 0x260f8
movl (%rax), %edx
pushq $0x8
popq %rax
testl %edx, %edx
jne 0x185c5
testl %ecx, %ecx
jne 0x187e7
testl %edx, %edx
je 0x185d1
testl %ecx, %ecx
je 0x187e7
leaq 0x10(%rbx), %r15
leaq 0xdb24(%rip), %rax      # 0x26100
cmpl $0x0, (%rax)
je 0x1864e
leaq 0x8(%rsp), %r12
movq %r12, %rdi
xorl %esi, %esi
callq 0x6210
cvtsi2sdq (%r12), %xmm1
cvtsi2sdq 0x8(%r12), %xmm0
leaq 0x20(%rbx), %r12
divsd 0x5d07(%rip), %xmm0     # 0x1e310
addsd %xmm1, %xmm0
leaq 0x5dbb(%rip), %r8       # 0x1e3cf
movq %rsp, %rbp
pushq $0x1
popq %rcx
movq %r15, %rdi
movq %r12, %rsi
movq %rbp, %rdx
movb $0x1, %al
callq 0x1886b
leaq 0xcd13(%rip), %rax      # 0x25344
movsbl (%rax), %r9d
leaq 0x5d1d(%rip), %r8       # 0x1e359
movq %r15, %rdi
movq %r12, %rsi
movq %rbp, %rdx
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x1886b
leaq 0xdaa7(%rip), %rax      # 0x260fc
cmpl $0x0, (%rax)
je 0x186a2
leaq 0x20(%rbx), %r12
movl (%r14), %r9d
leaq 0x5d77(%rip), %r8       # 0x1e3df
movq %rsp, %r14
pushq $0x1
popq %rcx
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x1886b
leaq 0xccbf(%rip), %rax      # 0x25344
movsbl (%rax), %r9d
leaq 0x5cc9(%rip), %r8       # 0x1e359
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x1886b
leaq 0x20(%rbx), %rsi
leaq 0xcc98(%rip), %rax      # 0x25345
movsbl (%rax), %r9d
leaq 0x5ca1(%rip), %r8       # 0x1e359
movq %rsp, %rdx
movq %r15, %rdi
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x1886b
movq 0x8(%rbx), %rsi
movq 0x10(%rbx), %r14
subq %rsi, %r14
testq %r14, %r14
jle 0x187e5
cmpl $0x0, 0x28(%rbx)
je 0x186f1
leaq 0xdf98(%rip), %rdi      # 0x26680
callq 0x6620
movq 0x8(%rbx), %rsi
movl (%rbx), %edi
movq %r14, %rdx
callq 0x6120
movq %rax, %r15
cmpq %r14, %rax
je 0x1882a
callq 0x6060
movl (%rax), %edi
testl %edi, %edi
je 0x18815
callq 0x65d0
movq %rax, %rsi
jmp 0x1881c
leaq 0x10(%rbx), %r15
addq $0x20, %rbx
movq 0x770(%r14), %r9
leaq 0x5c23(%rip), %r8       # 0x1e35c
movq %rsp, %rdx
pushq $0x1
popq %rcx
movq %r15, %rdi
movq %rbx, %rsi
xorl %eax, %eax
pushq 0x778(%r14)
pushq 0x768(%r14)
jmp 0x18788
leaq 0x10(%rbx), %r15
addq $0x20, %rbx
movq 0x780(%r14), %r9
subq $0x8, %rsp
leaq 0x5c00(%rip), %r8       # 0x1e371
leaq 0x8(%rsp), %rdx
pushq $0x1
popq %rcx
movq %r15, %rdi
movq %rbx, %rsi
xorl %eax, %eax
pushq 0x788(%r14)
callq 0x1886b
addq $0x10, %rsp
testl %eax, %eax
jne 0x187e7
leaq 0xcba8(%rip), %rax      # 0x25344
movsbl (%rax), %r9d
leaq 0x5bb2(%rip), %r8       # 0x1e359
movq %rsp, %rdx
movq %r15, %rdi
movq %rbx, %rsi
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x1886b
testl %eax, %eax
jne 0x187e7
jmp 0x187e5
movl (%rbx), %edi
callq 0x6260
movq 0x8(%rbx), %rdi
callq 0x6050
movq %rbx, %rdi
callq 0x6050
jmp 0x187e5
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
pushq $0x1
popq %rdi
callq 0x6200
testl %eax, %eax
js 0x1885a
movl %eax, %ebp
jmp 0x182ab
movl %r15d, (%rbx)
leaq 0x5c8a(%rip), %rdi      # 0x1e49d
jmp 0x18861
leaq 0x5bf5(%rip), %rsi      # 0x1e411
leaq 0x5bc7(%rip), %rdi      # 0x1e3ea
xorl %eax, %eax
callq 0xa442
cmpl $0x0, 0x28(%rbx)
je 0x1883c
leaq 0xde49(%rip), %rdi      # 0x26680
callq 0x63d0
movl %r13d, %eax
cmpq %r14, %r15
je 0x187e5
jmp 0x187e7
callq 0x6060
movl $0xc, (%rax)
leaq 0x5c23(%rip), %rdi      # 0x1e47b
jmp 0x18861
leaq 0x5bfd(%rip), %rdi      # 0x1e45e
callq 0x6530
jmp 0x181f4