// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x1180, %rsp           # imm = 0x1180
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x10(%rbp)
leaq -0x120(%rbp), %rdi
xorl %esi, %esi
movl $0x110, %edx            # imm = 0x110
callq 0xd290
leaq -0x120(%rbp), %rdi
leaq 0x2a0fe(%rip), %rsi     # 0x440a6
callq 0xd0e0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq -0x1120(%rbp), %rsi
movl $0xfff, %edx            # imm = 0xFFF
callq 0x2b000
movq %rax, -0x1128(%rbp)
cmpq $0x0, -0x1128(%rbp)
jg 0x1a006
callq 0xd0a0
cmpl $0xb, (%rax)
je 0x19ff5
callq 0xd0a0
cmpl $0xb, (%rax)
je 0x19ff5
callq 0xd0a0
cmpl $0x4, (%rax)
jne 0x1a001
movl $0x3e8, %edi            # imm = 0x3E8
callq 0xd720
jmp 0x19fad
jmp 0x1a221
movq -0x1128(%rbp), %rax
movb $0x0, -0x1120(%rbp,%rax)
leaq -0x1120(%rbp), %rsi
movq -0x1128(%rbp), %rdx
leaq -0x120(%rbp), %rdi
callq 0x1b740
leaq -0x120(%rbp), %rdi
callq 0x1ba20
testb $0x1, %al
jne 0x1a044
jmp 0x1a0cc
movl $0x10, %edi
callq 0xd4a0
movq %rax, -0x1130(%rbp)
movq -0x1130(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0xd290
movq -0x18(%rbp), %rdi
movq -0x1130(%rbp), %rsi
xorl %edx, %edx
callq 0x260a0
testb $0x1, %al
jne 0x1a080
jmp 0x1a0b2
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x1130(%rbp), %rsi
callq 0x1ba70
testb $0x1, %al
jne 0x1a0b0
movq -0x1130(%rbp), %rdi
callq 0x25b50
movq -0x1130(%rbp), %rdi
callq 0xd090
jmp 0x1a0c0
leaq 0x29ffe(%rip), %rdi     # 0x440b7
movb $0x0, %al
callq 0xd230
leaq -0x120(%rbp), %rdi
callq 0x1bbd0
movl $0x0, -0x1134(%rbp)
movslq -0x1134(%rbp), %rax
cmpq -0x1128(%rbp), %rax
jge 0x1a130
movslq -0x1134(%rbp), %rax
movsbl -0x1120(%rbp,%rax), %eax
cmpl $0x20, %eax
jge 0x1a11d
movslq -0x1134(%rbp), %rax
movsbl -0x1120(%rbp,%rax), %eax
cmpl $0xa, %eax
je 0x1a11d
movslq -0x1134(%rbp), %rax
movb $0x3f, -0x1120(%rbp,%rax)
jmp 0x1a11f
movl -0x1134(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1134(%rbp)
jmp 0x1a0d6
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x1170(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0xda10
movq -0x1170(%rbp), %rdi
movq %rax, -0x1158(%rbp)
movq %rdx, -0x1150(%rbp)
movq -0x1158(%rbp), %rsi
movq -0x1150(%rbp), %rdx
movl $0x1, %ecx
callq 0xfaf0
movq %rax, -0x1148(%rbp)
movq %rdx, -0x1140(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl $0x0, 0x18(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq -0x1120(%rbp), %rsi
movq -0x1148(%rbp), %rdx
movq -0x1140(%rbp), %rcx
callq 0xda70
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl $0x2, 0x18(%rax)
movq $0x0, -0x1160(%rbp)
leaq 0x369e0(%rip), %rax     # 0x50bb4
movl 0x4(%rax), %edi
leaq 0x29ccc(%rip), %rsi     # 0x43eaa
movl $0x2, %edx
callq 0xd1c0
movq %rax, -0x1160(%rbp)
xorl %eax, %eax
cmpq $-0x1, -0x1160(%rbp)
movb %al, -0x1171(%rbp)
jne 0x1a212
callq 0xd0a0
cmpl $0x4, (%rax)
sete %al
movb %al, -0x1171(%rbp)
movb -0x1171(%rbp), %al
testb $0x1, %al
jne 0x1a1cd
jmp 0x19fad
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x2af10
movl %eax, -0x1164(%rbp)
movl -0x1164(%rbp), %eax
andl $0x7f, %eax
cmpl $0x0, %eax
jne 0x1a27d
leaq -0x1120(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movl (%rax), %ecx
movl -0x1164(%rbp), %r8d
andl $0xff00, %r8d           # imm = 0xFF00
sarl $0x8, %r8d
movl $0x1000, %esi           # imm = 0x1000
leaq 0x29cba(%rip), %rdx     # 0x43f2b
movb $0x0, %al
callq 0xd250
jmp 0x1a33b
movl -0x1164(%rbp), %eax
andl $0x7f, %eax
addl $0x1, %eax
movsbl %al, %eax
sarl %eax
cmpl $0x0, %eax
jle 0x1a2c4
leaq -0x1120(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movl (%rax), %ecx
movl -0x1164(%rbp), %r8d
andl $0x7f, %r8d
movl $0x1000, %esi           # imm = 0x1000
leaq 0x29c8c(%rip), %rdx     # 0x43f47
movb $0x0, %al
callq 0xd250
jmp 0x1a339
movl -0x1164(%rbp), %eax
andl $0xff, %eax
cmpl $0x7f, %eax
jne 0x1a30c
leaq -0x1120(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movl (%rax), %ecx
movl -0x1164(%rbp), %r8d
andl $0xff00, %r8d           # imm = 0xFF00
sarl $0x8, %r8d
movl $0x1000, %esi           # imm = 0x1000
leaq 0x29c60(%rip), %rdx     # 0x43f63
movb $0x0, %al
callq 0xd250
jmp 0x1a337
leaq -0x1120(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movl (%rax), %ecx
movl -0x1164(%rbp), %r8d
movl $0x1000, %esi           # imm = 0x1000
leaq 0x29c50(%rip), %rdx     # 0x43f80
movb $0x0, %al
callq 0xd250
jmp 0x1a339
jmp 0x1a33b
movq -0x10(%rbp), %rdi
callq 0xd090
xorl %eax, %eax
addq $0x1180, %rsp           # imm = 0x1180
popq %rbp
retq
nop