// File: /workspace/github_c_makefile_star2_O2/justy989[P]ce/ce_clangd.c
// Function: handle_output_fn
static void* handle_output_fn(void* user_data)
#endif
{
     HandleOutputData_t* data = (HandleOutputData_t*)(user_data);
     // bool sent_definition_request = false;
     ParseResponse_t parse = {};
     strcpy(parse.expected_header_prefix, "Content-Length: ");

     char block[READ_BLOCK_SIZE];
     while(true){
          int64_t bytes_read = ce_subprocess_read_stdout(data->proc, block, (READ_BLOCK_SIZE - 1));
          if(bytes_read <= 0){
#if !defined(PLATFORM_WINDOWS)
               if(errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR){
                    usleep(1000);
                    continue;
               }
#endif
               break;
          }

          block[bytes_read] = 0;

          // Attempt to parse the messages before we sanitize and print them.
          _parse_response_block(&parse, block, bytes_read);
          if(_parse_response_complete(&parse)){
               CeJsonObj_t* obj = malloc(sizeof(*obj));
               memset(obj, 0, sizeof(*obj));
               if(ce_json_parse(parse.message_body, obj, false)){
                    // DEBUG
                    // char* buffer = malloc(MAX_PRINT_SIZE + 1);
                    // ce_json_obj_to_string(obj, buffer, MAX_PRINT_SIZE, 1);
                    // printf("%s\n", buffer);
                    // free(buffer);
                    if(!_push_response(data->response_queue, obj)){
                         ce_json_obj_free(obj);
                         free(obj);
                    }
               }else{
                    printf("Failed to parse json obj\n");
               }
               _parse_response_free(&parse);
          }

          // sanitize block for non-printable characters
          for(int i = 0; i < bytes_read; i++){
              if(block[i] < 32 && block[i] != '\n') block[i] = '?';
          }
          // Insert into the clangd buffer.
          CePoint_t end = ce_buffer_advance_point(data->buffer,
                                                  ce_buffer_end_point(data->buffer), 1);
          data->buffer->status = CE_BUFFER_STATUS_NONE;
          ce_buffer_insert_string(data->buffer, block, end);
          data->buffer->status = CE_BUFFER_STATUS_READONLY;

#if defined(DISPLAY_TERMINAL)
          int64_t bell_bytes_read = 0;
          do{
               bell_bytes_read = write(g_shell_command_ready_fds[1], "1", 2);
          }while(bell_bytes_read == -1 && errno == EINTR);
#endif
     }

     int status = ce_subprocess_close(data->proc);

#if defined(PLATFORM_WINDOWS)
    (void)(status);
#else
     // TODO: Could compress this with ce_app.c's version.
     if(WIFEXITED(status)){
          snprintf(block, READ_BLOCK_SIZE, "\npid %d exited with code %d", data->proc->pid, WEXITSTATUS(status));
     }else if(WIFSIGNALED(status)){
          snprintf(block, READ_BLOCK_SIZE, "\npid %d killed by signal %d", data->proc->pid, WTERMSIG(status));
     }else if(WIFSTOPPED(status)){
          snprintf(block, READ_BLOCK_SIZE, "\npid %d stopped by signal %d", data->proc->pid, WSTOPSIG(status));
     }else{
          snprintf(block, READ_BLOCK_SIZE, "\npid %d stopped with unexpected status %d", data->proc->pid, status);
     }
#endif

     free(data);
     return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1138, %rsp           # imm = 0x1138
movq %rdi, %rbp
leaq 0x31(%rsp), %rbx
movl $0xff, %edx
movq %rbx, %rdi
xorl %esi, %esi
callq 0xd2a0
movups 0x17ac9(%rip), %xmm0    # 0x2c124
movaps %xmm0, -0x11(%rbx)
movb $0x0, -0x1(%rbx)
leaq 0x130(%rsp), %rbx
movq %rbp, 0x8(%rsp)
movq 0x8(%rbp), %rdi
movl $0xfff, %edx            # imm = 0xFFF
movq %rbx, %rsi
callq 0x1d9db
testq %rax, %rax
jle 0x1480f
movq %rax, %r12
movb $0x0, 0x130(%rsp,%rax)
xorl %r13d, %r13d
xorl %r15d, %r15d
xorl %r14d, %r14d
xorl %ebx, %ebx
cmpq %r14, %r12
je 0x1485d
movq 0x120(%rsp), %rbp
testq %rbp, %rbp
jle 0x146f9
testq %r15, %r15
jne 0x146ce
movq 0x128(%rsp), %rdi
movq %rbp, %rsi
callq 0xd2c0
movq %rax, %r15
cmpq %rbp, %r15
jge 0x1485d
movb 0x130(%rsp,%r14), %al
movq 0x128(%rsp), %rcx
movb %al, (%rcx,%r15)
incq %r15
movq %r13, %rbp
incq %r14
movq %rbp, %r13
jmp 0x146a0
testq %rbx, %rbx
jne 0x1470b
leaq 0x20(%rsp), %rdi
callq 0xd210
movq %rax, %rbx
testq %r13, %r13
jne 0x14720
leaq 0xa0(%rsp), %rdi
callq 0xd210
movq %rax, %r13
cmpq %rbx, %r13
jge 0x14745
movb 0x130(%rsp,%r14), %al
cmpb 0x20(%rsp,%r13), %al
jne 0x147de
movb %al, 0xa0(%rsp,%r13)
incq %r13
jmp 0x146ee
cmpq $0x80, %r13
jge 0x14833
movb 0x130(%rsp,%r14), %al
movb %al, 0xa0(%rsp,%r13)
leaq 0x1(%r13), %rbp
cmpb $0xd, 0x9d(%rsp,%r13)
jne 0x146f1
cmpb $0xa, 0x9e(%rsp,%r13)
jne 0x146f1
movb 0x9f(%rsp,%r13), %cl
xorb $0xd, %cl
xorb $0xa, %al
orb %cl, %al
jne 0x146f1
andq $0x0, 0x10(%rsp)
leaq 0xa0(%rsp), %r13
leaq (%rbx,%r13), %rdi
leaq 0x10(%rsp), %rsi
pushq $0xa
popq %rdx
callq 0xd400
movq %rax, 0x120(%rsp)
cmpq 0x10(%rsp), %r13
je 0x14833
incq %rax
pushq $0x1
popq %rdi
movq %rax, %rsi
callq 0xd380
movq %rax, 0x128(%rsp)
jmp 0x146f1
leaq 0xa0(%rsp), %rax
xorps %xmm0, %xmm0
movups %xmm0, 0x70(%rax)
movups %xmm0, 0x60(%rax)
movups %xmm0, 0x50(%rax)
movups %xmm0, 0x40(%rax)
movups %xmm0, 0x30(%rax)
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
xorl %ebp, %ebp
jmp 0x146f1
callq 0xd0a0
movl (%rax), %eax
cmpl $0xb, %eax
je 0x14824
cmpl $0x4, %eax
jne 0x14a80
movl $0x3e8, %edi            # imm = 0x3E8
callq 0xd740
jmp 0x14670
leaq 0xa0(%rsp), %rax
xorps %xmm0, %xmm0
movups %xmm0, 0x70(%rax)
movups %xmm0, 0x60(%rax)
movups %xmm0, 0x50(%rax)
movups %xmm0, 0x40(%rax)
movups %xmm0, 0x30(%rax)
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
movq 0x128(%rsp), %r15
testq %r15, %r15
movq 0x8(%rsp), %rbp
pushq $0x2
popq %r13
je 0x149dc
movq %r15, %rdi
callq 0xd210
cmpq 0x120(%rsp), %rax
jne 0x149dc
pushq $0x1
popq %rdi
pushq $0x10
popq %rsi
callq 0xd380
movq %rax, %rbx
movq %r15, %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x1b011
testb %al, %al
je 0x14926
movq 0x10(%rbp), %r14
movq %rbx, %rdi
leaq 0x187ae(%rip), %rsi     # 0x2d068
callq 0x1af29
movl %eax, 0x10(%rsp)
movq %rdx, 0x18(%rsp)
pushq $-0x1
popq %r13
cmpl $0x4, %eax
jne 0x148ec
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
callq 0x1afdb
pushq $-0x1
popq %r13
testq %rax, %rax
je 0x148ec
cvttsd2si (%rax), %r13
leaq 0x10(%r14), %r15
movq %r15, %rdi
callq 0xd700
testl %eax, %eax
je 0x14934
movl %eax, %edi
callq 0xd650
leaq 0x1782b(%rip), %rdi     # 0x2c135
movq %rax, %rsi
xorl %eax, %eax
callq 0xd8e5
movq %rbx, %rdi
callq 0x1ad0b
movq %rbx, %rdi
callq 0xd090
jmp 0x1498a
leaq 0x1788c(%rip), %rdi     # 0x2c1b9
callq 0xd120
jmp 0x1498e
movq (%r14), %rbp
movq 0x8(%r14), %rdi
incq %rbp
imulq $0x18, %rbp, %rsi
callq 0xd520
movq %rax, 0x8(%r14)
imulq $0x18, (%r14), %rcx
andq $0x0, 0x10(%rax,%rcx)
movq %r13, (%rax,%rcx)
movq %rbx, 0x8(%rax,%rcx)
movq %rbp, (%r14)
movq %r15, %rdi
callq 0xd490
testl %eax, %eax
je 0x14985
movl %eax, %edi
callq 0xd650
leaq 0x177eb(%rip), %rdi     # 0x2c166
movq %rax, %rsi
xorl %eax, %eax
callq 0xd8e5
movq 0x8(%rsp), %rbp
pushq $0x2
popq %r13
leaq 0xa0(%rsp), %rax
xorps %xmm0, %xmm0
movups %xmm0, 0x70(%rax)
movups %xmm0, 0x60(%rax)
movups %xmm0, 0x50(%rax)
movups %xmm0, 0x40(%rax)
movups %xmm0, 0x30(%rax)
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
movq 0x128(%rsp), %rdi
testq %rdi, %rdi
je 0x149dc
andq $0x0, 0x120(%rsp)
callq 0xd090
andq $0x0, 0x128(%rsp)
xorl %eax, %eax
leaq 0x2711f(%rip), %r14     # 0x3bb04
leaq 0x17507(%rip), %r15     # 0x2bef3
cmpq %rax, %r12
je 0x14a15
movb 0x130(%rsp,%rax), %cl
cmpb $0x20, %cl
setge %dl
cmpb $0xa, %cl
sete %cl
orb %dl, %cl
jne 0x14a10
movb $0x3f, 0x130(%rsp,%rax)
incq %rax
jmp 0x149ec
movq (%rbp), %rbx
movq %rbx, %rdi
callq 0xd9cc
movq %rbx, %rdi
movq %rax, %rsi
pushq $0x1
popq %rcx
callq 0xec3a
movq %rdx, %rcx
movq (%rbp), %rdi
andl $0x0, 0x18(%rdi)
leaq 0x130(%rsp), %rbx
movq %rbx, %rsi
movq %rax, %rdx
callq 0xd9ef
movq (%rbp), %rax
movl $0x2, 0x18(%rax)
movl 0x4(%r14), %edi
movq %r15, %rsi
movq %r13, %rdx
callq 0xd1d0
cmpq $-0x1, %rax
jne 0x14670
callq 0xd0a0
cmpl $0x4, (%rax)
je 0x14a58
jmp 0x14670
movq 0x8(%rbp), %rdi
callq 0x1d934
movl %eax, %r8d
andl $0x7f, %r8d
je 0x14ac2
movl %r8d, %ecx
shll $0x18, %ecx
addl $0x1000000, %ecx        # imm = 0x1000000
cmpl $0x2000000, %ecx        # imm = 0x2000000
jl 0x14ad4
movq 0x8(%rbp), %rax
movl (%rax), %ecx
leaq 0x174dd(%rip), %rdx     # 0x2bf90
leaq 0x130(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
jmp 0x14b01
movq 0x8(%rbp), %rcx
movl (%rcx), %ecx
movzbl %ah, %eax
leaq 0x174a2(%rip), %rdx     # 0x2bf74
jmp 0x14af1
movq 0x8(%rbp), %rcx
movl (%rcx), %ecx
cmpb $0x7f, %al
jne 0x14aea
movzbl %ah, %eax
leaq 0x174c4(%rip), %rdx     # 0x2bfac
jmp 0x14af1
leaq 0x174d8(%rip), %rdx     # 0x2bfc9
leaq 0x130(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
movl %eax, %r8d
xorl %eax, %eax
callq 0xd260
movq %rbp, %rdi
callq 0xd090
xorl %eax, %eax
addq $0x1138, %rsp           # imm = 0x1138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq