// File: /workspace/github_c_makefile_star2_O3/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 0x1a15e(%rip), %xmm0    # 0x2f124
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 0x1f7d8
testq %rax, %rax
jle 0x1517e
movq %rax, %r13
movb $0x0, 0x130(%rsp,%rax)
xorl %r12d, %r12d
xorl %r14d, %r14d
xorl %r15d, %r15d
xorl %ebx, %ebx
movq 0x120(%rsp), %rbp
testq %rbp, %rbp
jle 0x15063
testq %r15, %r15
jne 0x15030
movq 0x128(%rsp), %rdi
movq %rbp, %rsi
callq 0xd2c0
movq %rax, %r15
cmpq %rbp, %r15
jge 0x151cc
movb 0x130(%rsp,%r14), %al
movq 0x128(%rsp), %rcx
movb %al, (%rcx,%r15)
incq %r15
movq %rbx, %rbp
incq %r14
movq %rbp, %rbx
cmpq %r14, %r13
jne 0x1500b
jmp 0x151cc
testq %r12, %r12
jne 0x15075
leaq 0x20(%rsp), %rdi
callq 0xd210
movq %rax, %r12
testq %rbx, %rbx
jne 0x1508a
leaq 0xa0(%rsp), %rdi
callq 0xd210
movq %rax, %rbx
cmpq %r12, %rbx
jge 0x150ad
movb 0x130(%rsp,%r14), %al
cmpb 0x20(%rsp,%rbx), %al
jne 0x1514d
movb %al, 0xa0(%rsp,%rbx)
incq %rbx
jmp 0x15050
cmpq $0x80, %rbx
jge 0x151a2
movb 0x130(%rsp,%r14), %al
movb %al, 0xa0(%rsp,%rbx)
leaq 0x1(%rbx), %rbp
cmpb $0xd, 0x9d(%rsp,%rbx)
jne 0x15053
cmpb $0xa, 0x9e(%rsp,%rbx)
jne 0x15053
movb 0x9f(%rsp,%rbx), %cl
xorb $0xd, %cl
xorb $0xa, %al
orb %cl, %al
jne 0x15053
movq $0x0, 0x10(%rsp)
leaq 0xa0(%rsp), %rbx
leaq (%rbx,%r12), %rdi
leaq 0x10(%rsp), %rsi
movl $0xa, %edx
callq 0xd400
movq %rax, 0x120(%rsp)
cmpq 0x10(%rsp), %rbx
je 0x151a2
incq %rax
movl $0x1, %edi
movq %rax, %rsi
callq 0xd380
movq %rax, 0x128(%rsp)
jmp 0x15053
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 0x15053
callq 0xd0a0
movl (%rax), %eax
cmpl $0xb, %eax
je 0x15193
cmpl $0x4, %eax
jne 0x15400
movl $0x3e8, %edi            # imm = 0x3E8
callq 0xd740
jmp 0x14fdb
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), %rbx
testq %rbx, %rbx
movq 0x8(%rsp), %rbp
je 0x15357
movq 0x120(%rsp), %r14
movq %rbx, %rdi
callq 0xd210
cmpq %r14, %rax
jne 0x15357
movl $0x1, %edi
movl $0x10, %esi
callq 0xd380
movq %rax, %r15
movq %rbx, %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x1ca1b
testb %al, %al
je 0x15297
movq 0x10(%rbp), %r14
movq %r15, %rdi
leaq 0x1aea4(%rip), %rsi     # 0x300d0
callq 0x1c943
movl %eax, 0x10(%rsp)
movq %rdx, 0x18(%rsp)
movq $-0x1, %r12
cmpl $0x4, %eax
jne 0x1525d
movq %r15, %rdi
leaq 0x10(%rsp), %rsi
callq 0x1c9e5
testq %rax, %rax
je 0x1525d
cvttsd2si (%rax), %r12
leaq 0x10(%r14), %rbx
movq %rbx, %rdi
callq 0xd700
testl %eax, %eax
je 0x152a5
movl %eax, %edi
callq 0xd650
leaq 0x19eba(%rip), %rdi     # 0x2f135
movq %rax, %rsi
xorl %eax, %eax
callq 0xd954
movq %r15, %rdi
callq 0x1c6aa
movq %r15, %rdi
callq 0xd090
jmp 0x15309
leaq 0x19f1b(%rip), %rdi     # 0x2f1b9
callq 0xd120
jmp 0x15309
movq (%r14), %rbp
movq 0x8(%r14), %rdi
leaq 0x8(,%rbp,8), %rax
incq %rbp
leaq (%rax,%rax,2), %rsi
callq 0xd520
movq %rax, 0x8(%r14)
movq (%r14), %rcx
leaq (%rcx,%rcx,2), %rcx
movq $0x0, 0x10(%rax,%rcx,8)
movq %r12, (%rax,%rcx,8)
movq %r15, 0x8(%rax,%rcx,8)
movq %rbp, (%r14)
movq %rbx, %rdi
callq 0xd490
testl %eax, %eax
je 0x15304
movl %eax, %edi
callq 0xd650
leaq 0x19e6c(%rip), %rdi     # 0x2f166
movq %rax, %rsi
xorl %eax, %eax
callq 0xd954
movq 0x8(%rsp), %rbp
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 0x15357
xorl %ebx, %ebx
movq %rbx, 0x120(%rsp)
callq 0xd090
movq %rbx, 0x128(%rsp)
xorl %eax, %eax
leaq 0x277a4(%rip), %r14     # 0x3cb04
leaq 0x19b8c(%rip), %r15     # 0x2eef3
movb 0x130(%rsp,%rax), %cl
cmpb $0x20, %cl
setge %dl
cmpb $0xa, %cl
sete %cl
orb %dl, %cl
jne 0x15386
movb $0x3f, 0x130(%rsp,%rax)
incq %rax
cmpq %rax, %r13
jne 0x15367
movq (%rbp), %rbx
movq %rbx, %rdi
callq 0xda40
movl $0x1, %ecx
movq %rbx, %rdi
movq %rax, %rsi
callq 0xee7a
movq %rdx, %rcx
movq (%rbp), %rdi
movl $0x0, 0x18(%rdi)
leaq 0x130(%rsp), %rbx
movq %rbx, %rsi
movq %rax, %rdx
callq 0xda6e
movq (%rbp), %rax
movl $0x2, 0x18(%rax)
movl 0x4(%r14), %edi
movl $0x2, %edx
movq %r15, %rsi
callq 0xd1d0
cmpq $-0x1, %rax
jne 0x14fdb
callq 0xd0a0
cmpl $0x4, (%rax)
je 0x153d6
jmp 0x14fdb
movq 0x8(%rbp), %rdi
callq 0x1f70f
movl %eax, %r8d
andl $0x7f, %r8d
je 0x15442
movl %r8d, %ecx
shll $0x18, %ecx
addl $0x1000000, %ecx        # imm = 0x1000000
cmpl $0x2000000, %ecx        # imm = 0x2000000
jl 0x15454
movq 0x8(%rbp), %rax
movl (%rax), %ecx
leaq 0x19b5d(%rip), %rdx     # 0x2ef90
leaq 0x130(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
jmp 0x15481
movq 0x8(%rbp), %rcx
movl (%rcx), %ecx
movzbl %ah, %eax
leaq 0x19b22(%rip), %rdx     # 0x2ef74
jmp 0x15471
movq 0x8(%rbp), %rcx
movl (%rcx), %ecx
cmpb $0x7f, %al
jne 0x1546a
movzbl %ah, %eax
leaq 0x19b44(%rip), %rdx     # 0x2efac
jmp 0x15471
leaq 0x19b58(%rip), %rdx     # 0x2efc9
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