// File: /workspace/github_c_makefile_star2_O2/justy989[P]ce/ce_clangd.c
// Function: ce_clangd_init
bool ce_clangd_init(const char* executable_path,
                    CeClangD_t* clangd){
     char command[MAX_COMMAND_SIZE];
     // DEBUG
     // snprintf(command, MAX_COMMAND_SIZE, "%s --log=verbose", executable_path);
     snprintf(command, MAX_COMMAND_SIZE, "%s", executable_path);
     bool use_shell = false;
     if(!ce_subprocess_open(&clangd->proc, command, CE_PROC_COMM_STDIN | CE_PROC_COMM_STDOUT, use_shell)){
          return false;
     }

     HandleOutputData_t* thread_data = malloc(sizeof(*thread_data));
     thread_data->buffer = clangd->buffer;
     thread_data->proc = &clangd->proc;
     thread_data->response_queue = &clangd->response_queue;

#if defined(PLATFORM_WINDOWS)
     clangd->response_queue.mutex = CreateMutex(NULL, false, NULL);
     if(clangd->response_queue.mutex == NULL){
          ce_log("Failed to create clangd mutex\n");
          return false;
     }

     clangd->thread_handle = CreateThread(NULL,
                                          0,
                                          handle_output_fn,
                                          thread_data,
                                          0,
                                          &clangd->thread_id);
     if(clangd->thread_handle == INVALID_HANDLE_VALUE){
          ce_log("failed to create thread to run command\n");
          return false;
     }
#else
     int rc = pthread_create(&clangd->thread, NULL, handle_output_fn, thread_data);
     if(rc != 0){
          ce_log("pthread_create() failed: '%s'\n", strerror(errno));
          return false;
     }

     rc = pthread_mutex_init(&clangd->response_queue.mutex, NULL);
     if(rc != 0){
          ce_log("pthread_mutex_init() failed: %s\n", strerror(rc));
          return false;
     }
#endif

     // Build our initialization structure.
     CeJsonObj_t initialize_obj = {};
     ce_json_obj_set_string(&initialize_obj, "jsonrpc", "2.0");
     ce_json_obj_set_number(&initialize_obj, "id", 0);
     ce_json_obj_set_string(&initialize_obj, "method", "initialize");

     char cwd[MAX_PATH_LEN + 1];
     char cwd_uri[MAX_PATH_LEN + 16];

     CeJsonObj_t params_obj = {};

#if defined(PLATFORM_WINDOWS)
     DWORD pid = GetCurrentProcessId();
     _getcwd(cwd, MAX_PATH_LEN);
#else
     pid_t pid = getpid();
     getcwd(cwd, MAX_PATH_LEN);
#endif
     ce_json_obj_set_number(&params_obj, "processId", (double)(pid));

#if defined(PLATFORM_WINDOWS)
     strncpy(cwd_uri, cwd, MAX_PATH_LEN);
     _convert_windows_path_to_uri(cwd_uri, MAX_PATH_LEN);
     char* json_cwd = _convert_string_to_json_string(cwd);
#else
     snprintf(cwd_uri, MAX_PATH_LEN + 15, "file://%s", cwd);
     char* json_cwd = _convert_string_to_json_string(cwd);
#endif

     ce_json_obj_set_string(&params_obj, "rootPath", json_cwd);
     ce_json_obj_set_string(&params_obj, "rootUri", cwd_uri);
     ce_json_obj_set_obj(&initialize_obj, "params", &params_obj);

     CeJsonObj_t client_info_obj = {};
     ce_json_obj_set_string(&client_info_obj, "name", "ce");
     ce_json_obj_set_string(&client_info_obj, "version", "9.8.7");
     ce_json_obj_set_obj(&initialize_obj, "ClientInfo", &client_info_obj);

     bool result = _send_json_obj(&initialize_obj, &clangd->proc);
     ce_json_obj_free(&initialize_obj);
     ce_json_obj_free(&params_obj);
     ce_json_obj_free(&client_info_obj);

     free(json_cwd);
     return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc58, %rsp            # imm = 0xC58
movq %rsi, %r14
movq %rdi, %rcx
leaq 0x194f4(%rip), %rdx     # 0x2d906
xorl %ebp, %ebp
leaq 0x30(%rsp), %r15
movl $0x400, %esi            # imm = 0x400
movq %r15, %rdi
xorl %eax, %eax
callq 0xd260
leaq 0x8(%r14), %rbx
pushq $0x3
popq %rdx
movq %rbx, %rdi
movq %r15, %rsi
xorl %ecx, %ecx
callq 0x1d791
testb %al, %al
je 0x144b4
pushq $0x18
popq %rdi
callq 0xd4c0
movq 0x28(%r14), %rcx
movq %rcx, (%rax)
movq %rbx, 0x8(%rax)
leaq 0x38(%r14), %rcx
movq %rcx, 0x10(%rax)
leaq 0x1ca(%rip), %rdx       # 0x1462c
movq %r14, %rdi
xorl %esi, %esi
movq %rax, %rcx
callq 0xd570
testl %eax, %eax
je 0x14488
callq 0xd0a0
movl (%rax), %edi
callq 0xd650
leaq 0x178d6(%rip), %rdi     # 0x2bd5c
jmp 0x144a8
addq $0x48, %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0xd6c0
testl %eax, %eax
je 0x144c8
movl %eax, %edi
callq 0xd650
leaq 0x17b80(%rip), %rdi     # 0x2c028
xorl %ebp, %ebp
movq %rax, %rsi
xorl %eax, %eax
callq 0xd8e5
movl %ebp, %eax
addq $0xc58, %rsp            # imm = 0xC58
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %r14
movaps %xmm0, (%r14)
leaq 0x17b6e(%rip), %rsi     # 0x2c049
leaq 0x17b6f(%rip), %rdx     # 0x2c051
movq %r14, %rdi
callq 0x1ab25
leaq 0x18b77(%rip), %rsi     # 0x2d068
xorps %xmm0, %xmm0
movq %r14, %rdi
callq 0x1aba5
leaq 0x177bd(%rip), %rsi     # 0x2bcc0
leaq 0x17b4b(%rip), %rdx     # 0x2c055
movq %r14, %rdi
callq 0x1ab25
leaq 0x10(%rsp), %r15
xorps %xmm0, %xmm0
movaps %xmm0, (%r15)
callq 0xd1e0
movl %eax, %ebp
leaq 0x430(%rsp), %r12
movl $0x400, %esi            # imm = 0x400
movq %r12, %rdi
callq 0xd2d0
cvtsi2sd %ebp, %xmm0
leaq 0x17b1b(%rip), %rsi     # 0x2c060
movq %r15, %rdi
callq 0x1aba5
leaq 0x17b16(%rip), %rdx     # 0x2c06a
leaq 0x840(%rsp), %r13
movl $0x40f, %esi            # imm = 0x40F
movq %r13, %rdi
movq %r12, %rcx
xorl %eax, %eax
callq 0xd260
movq %r12, %rdi
callq 0x14b24
movq %rax, %r12
leaq 0x17af4(%rip), %rsi     # 0x2c074
movq %r15, %rdi
movq %rax, %rdx
callq 0x1ab25
leaq 0x17aeb(%rip), %rsi     # 0x2c07d
movq %r15, %rdi
movq %r13, %rdx
callq 0x1ab25
leaq 0x17658(%rip), %rsi     # 0x2bbfc
movq %r14, %rdi
movq %r15, %rdx
callq 0x1a949
movq %rsp, %r13
xorpd %xmm0, %xmm0
movapd %xmm0, (%r13)
leaq 0x170c4(%rip), %rsi     # 0x2b687
leaq 0x15b92(%rip), %rdx     # 0x2a15c
movq %r13, %rdi
callq 0x1ab25
leaq 0x17aac(%rip), %rsi     # 0x2c085
leaq 0x17aad(%rip), %rdx     # 0x2c08d
movq %r13, %rdi
callq 0x1ab25
leaq 0x17aa4(%rip), %rsi     # 0x2c093
movq %r14, %rdi
movq %r13, %rdx
callq 0x1a949
movq %r14, %rdi
movq %rbx, %rsi
callq 0x14c22
movl %eax, %ebp
movq %r14, %rdi
callq 0x1ad0b
movq %r15, %rdi
callq 0x1ad0b
movq %r13, %rdi
callq 0x1ad0b
movq %r12, %rdi
callq 0xd090
jmp 0x144b4