// File: /workspace/github_c_makefile_star2_O1/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), %r14
testq %r14, %r14
sete %al
testl %esi, %esi
setne %cl
movl $0x1, %ebp
testb %al, %cl
jne 0x1a012
movq $0x0, (%rsp)
cmpl $0x9, %esi
ja 0x1a010
movq %rdi, %rbx
movl %esi, %eax
leaq 0x58f2(%rip), %rcx      # 0x1f320
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq $0x0, 0x8(%rsp)
leaq 0xb901(%rip), %rax      # 0x25348
movq (%rax), %r14
leaq 0x2f45(%rip), %rsi      # 0x1c996
movq %r14, %rdi
callq 0x6310
testl %eax, %eax
je 0x19f8c
leaq 0x5987(%rip), %rsi      # 0x1f3ef
movq %r14, %rdi
callq 0x6610
testq %rax, %rax
je 0x19aa0
movl $0xba, %edi
xorl %eax, %eax
callq 0x6330
leaq 0x8(%rsp), %rdi
movq %r14, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x6460
testl %eax, %eax
js 0x1a023
movq 0x8(%rsp), %r14
xorl %ebp, %ebp
movq %r14, %rdi
movl $0x241, %esi            # imm = 0x241
movl $0x1a4, %edx            # imm = 0x1A4
xorl %eax, %eax
callq 0x64f0
movl %eax, %r14d
callq 0x6060
movq %rax, %r15
movl (%rax), %r12d
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x19ad3
callq 0x6050
testl %r14d, %r14d
jns 0x19fa3
movl %r12d, (%r15)
leaq 0x59bf(%rip), %rdi      # 0x1f4a5
jmp 0x1a03e
leaq 0x28(%rbx), %rax
movq %rax, 0x20(%rsp)
leaq 0x10(%r14), %rdi
addq $0x20, %r14
movl 0x348(%rbx), %r9d
movq 0x330(%rbx), %r10
leaq 0x128(%rbx), %r12
testq %r10, %r10
leaq 0x4e2d(%rip), %rax      # 0x1e948
leaq 0x583c(%rip), %r13      # 0x1f35e
cmoveq %rax, %r13
movl 0x328(%rbx), %ebp
movl 0x338(%rbx), %r15d
movl 0x340(%rbx), %r11d
addq $0x228, %rbx            # imm = 0x228
leaq 0x5800(%rip), %r8       # 0x1f348
movq %rsp, %rdx
movq %rdi, 0x18(%rsp)
movq %r14, %rsi
movl $0x1, %ecx
xorl %eax, %eax
pushq %r11
pushq %rbx
pushq %r15
pushq 0x38(%rsp)
pushq %rbp
pushq %r13
pushq %r12
pushq %r10
callq 0x1a04f
addq $0x40, %rsp
testl %eax, %eax
jne 0x19f67
leaq 0xb7c2(%rip), %rax      # 0x25344
movsbl (%rax), %r9d
leaq 0x57d4(%rip), %r8       # 0x1f361
movq %rsp, %rdx
movq 0x18(%rsp), %rdi
jmp 0x19f53
leaq 0x350(%rbx), %r9
leaq 0x10(%r14), %r15
addq $0x20, %r14
leaq 0x57db(%rip), %r8       # 0x1f38b
movq %rsp, %rdx
movq %r15, %rdi
movq %r14, %rsi
movl $0x1, %ecx
xorl %eax, %eax
callq 0x1a04f
testl %eax, %eax
jne 0x19f67
leaq 0xb770(%rip), %r12      # 0x25344
movsbl (%r12), %r9d
leaq 0x5781(%rip), %r8       # 0x1f361
movq %rsp, %rdx
movq %r15, %rdi
movq %r14, %rsi
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x1a04f
testl %eax, %eax
jne 0x19f67
leaq 0x450(%rbx), %r9
leaq 0x578e(%rip), %r8       # 0x1f396
movq %rsp, %rdx
movq %r15, %rdi
movq %r14, %rsi
movl $0x1, %ecx
xorl %eax, %eax
callq 0x1a04f
testl %eax, %eax
jne 0x19f67
movsbl (%r12), %r9d
leaq 0x5730(%rip), %r8       # 0x1f361
movq %rsp, %rdx
movq %r15, %rdi
movq %r14, %rsi
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x1a04f
testl %eax, %eax
jne 0x19f67
leaq 0x550(%rbx), %r9
leaq 0x5749(%rip), %r8       # 0x1f3a2
movq %rsp, %rdx
movq %r15, %rdi
movq %r14, %rsi
movl $0x1, %ecx
xorl %eax, %eax
callq 0x1a04f
testl %eax, %eax
jne 0x19f67
movsbl (%r12), %r9d
leaq 0x56df(%rip), %r8       # 0x1f361
movq %rsp, %rdx
movq %r15, %rdi
movq %r14, %rsi
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x1a04f
testl %eax, %eax
jne 0x19f67
leaq 0x650(%rbx), %r9
leaq 0x5707(%rip), %r8       # 0x1f3b1
movq %rsp, %rdx
movq %r15, %rdi
movq %r14, %rsi
movl $0x1, %ecx
xorl %eax, %eax
callq 0x1a04f
testl %eax, %eax
jne 0x19f67
movsbl (%r12), %r9d
leaq 0x568e(%rip), %r8       # 0x1f361
movq %rsp, %rdx
movq %r15, %rdi
movq %r14, %rsi
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x1a04f
testl %eax, %eax
jne 0x19f67
movsd 0x750(%rbx), %xmm0
leaq 0x56c3(%rip), %r8       # 0x1f3bf
movq %rsp, %rdx
movq %r15, %rdi
movq %r14, %rsi
movl $0x1, %ecx
movb $0x1, %al
callq 0x1a04f
testl %eax, %eax
jne 0x19f67
movsbl (%r12), %r9d
jmp 0x19f46
movq 0x8(%r14), %rax
movq %rax, 0x10(%r14)
movb $0x0, (%rax)
movq 0x18(%r14), %rax
movq %rax, 0x20(%r14)
jmp 0x1a010
leaq 0x10(%r14), %r15
addq $0x20, %r14
movq 0x758(%rbx), %r9
subq $0x8, %rsp
leaq 0x5674(%rip), %r8       # 0x1f3c9
leaq 0x8(%rsp), %rdx
movq %r15, %rdi
movq %r14, %rsi
movl $0x1, %ecx
xorl %eax, %eax
pushq 0x760(%rbx)
jmp 0x19f2e
movq 0x8(%r14), %rsi
cmpq %rsi, 0x10(%r14)
je 0x1a010
leaq 0xc369(%rip), %rax      # 0x260f0
movq (%rax), %rdi
testq %rdi, %rdi
je 0x19dc1
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x6400
leaq 0xc356(%rip), %rcx      # 0x260f8
movl (%rcx), %ecx
movl $0x8, %ebp
testl %ecx, %ecx
jne 0x19db5
testl %eax, %eax
jne 0x1a012
testl %ecx, %ecx
je 0x19dc1
testl %eax, %eax
je 0x1a012
leaq 0x10(%r14), %r15
leaq 0xc334(%rip), %rax      # 0x26100
cmpl $0x0, (%rax)
je 0x19e40
leaq 0x8(%rsp), %r12
movq %r12, %rdi
xorl %esi, %esi
callq 0x6210
cvtsi2sdq (%r12), %xmm1
cvtsi2sdq 0x8(%r12), %xmm0
leaq 0x20(%r14), %r12
divsd 0x551f(%rip), %xmm0     # 0x1f318
addsd %xmm1, %xmm0
leaq 0x55d3(%rip), %r8       # 0x1f3d7
movq %rsp, %r13
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
movl $0x1, %ecx
movb $0x1, %al
callq 0x1a04f
leaq 0xb521(%rip), %rax      # 0x25344
movsbl (%rax), %r9d
leaq 0x5533(%rip), %r8       # 0x1f361
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x1a04f
leaq 0xc2b5(%rip), %rax      # 0x260fc
cmpl $0x0, (%rax)
je 0x19e96
leaq 0x20(%r14), %r12
movl (%rbx), %r9d
leaq 0x558d(%rip), %r8       # 0x1f3e7
movq %rsp, %rbx
movq %r15, %rdi
movq %r12, %rsi
movq %rbx, %rdx
movl $0x1, %ecx
xorl %eax, %eax
callq 0x1a04f
leaq 0xb4cb(%rip), %rax      # 0x25344
movsbl (%rax), %r9d
leaq 0x54dd(%rip), %r8       # 0x1f361
movq %r15, %rdi
movq %r12, %rsi
movq %rbx, %rdx
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x1a04f
leaq 0x20(%r14), %rsi
leaq 0xb4a4(%rip), %rax      # 0x25345
movsbl (%rax), %r9d
leaq 0x54b5(%rip), %r8       # 0x1f361
movq %rsp, %rdx
movq %r15, %rdi
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x1a04f
movq %r14, %rdi
callq 0x1a14d
jmp 0x19f5f
leaq 0x10(%r14), %r15
addq $0x20, %r14
movq 0x770(%rbx), %r9
leaq 0x5486(%rip), %r8       # 0x1f364
movq %rsp, %rdx
movq %r15, %rdi
movq %r14, %rsi
movl $0x1, %ecx
xorl %eax, %eax
pushq 0x778(%rbx)
pushq 0x768(%rbx)
jmp 0x19f2e
leaq 0x10(%r14), %r15
addq $0x20, %r14
movq 0x780(%rbx), %r9
subq $0x8, %rsp
leaq 0x5463(%rip), %r8       # 0x1f379
leaq 0x8(%rsp), %rdx
movq %r15, %rdi
movq %r14, %rsi
movl $0x1, %ecx
xorl %eax, %eax
pushq 0x788(%rbx)
callq 0x1a04f
addq $0x10, %rsp
testl %eax, %eax
jne 0x19f67
leaq 0xb402(%rip), %rax      # 0x25344
movsbl (%rax), %r9d
leaq 0x5414(%rip), %r8       # 0x1f361
movq %rsp, %rdx
movq %r15, %rdi
movq %r14, %rsi
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x1a04f
testl %eax, %eax
je 0x1a010
movl %eax, %ebp
jmp 0x1a012
movl (%r14), %edi
callq 0x6260
movq 0x8(%r14), %rdi
callq 0x6050
movq %r14, %rdi
callq 0x6050
jmp 0x1a010
movl $0x1, %edi
callq 0x6200
testl %eax, %eax
js 0x1a037
movl %eax, %r14d
xorl %ebp, %ebp
movb $0x1, %al
testb %al, %al
je 0x1a012
movl $0x1, %edi
movl $0x30, %esi
callq 0x62f0
movq %rax, %r15
movl %r14d, (%rax)
leaq 0xb3ab(%rip), %rax      # 0x25370
movslq (%rax), %r14
incq %r14
movq %r14, 0x18(%r15)
movq %r14, %rdi
callq 0x63e0
movq %rax, 0x8(%r15)
movq %rax, 0x10(%r15)
movq %r14, 0x20(%r15)
movq 0x7a0(%rbx), %rdi
testq %rdi, %rdi
je 0x1a003
movl $0x6d, %esi
callq 0x6190
xorl %ecx, %ecx
testq %rax, %rax
setne %cl
jmp 0x1a005
xorl %ecx, %ecx
movl %ecx, 0x28(%r15)
movq %r15, 0x790(%rbx)
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x6060
movl $0xc, (%rax)
leaq 0x544e(%rip), %rdi      # 0x1f483
jmp 0x1a03e
leaq 0x5428(%rip), %rdi      # 0x1f466
callq 0x6530
movl $0x1, %ebp
xorl %eax, %eax
jmp 0x19fa5