// File: /workspace/github_c_makefile_star2_O1/justy989[P]ce/ce_app.c
// Function: ce_app_handle_clangd_response
void ce_app_handle_clangd_response(CeApp_t* app){
     if(app->clangd.buffer == NULL){
          return;
     }
     while(ce_clangd_outstanding_responses(&app->clangd)){
          CeClangDResponse_t response = ce_clangd_pop_response(&app->clangd);
          if(response.method != NULL){
               if(strcmp(response.method, "textDocument/typeDefinition") == 0 ||
                  strcmp(response.method, "textDocument/definition") == 0 ||
                  strcmp(response.method, "textDocument/declaration") == 0 ||
                  strcmp(response.method, "textDocument/implementation") == 0){
                    CeLayout_t* tab_layout = app->tab_list_layout->tab_list.current;
                    CeView_t* view = &tab_layout->tab.current->view;
                    CeDestination_t dest = _extract_destination_from_range_response(response.obj);
                    if(load_destination_into_view(&app->buffer_node_head,
                                                  view,
                                                  &app->config_options,
                                                  &app->vim,
                                                  true,
                                                  NULL,
                                                  &dest)){
                         ce_clangd_file_open(&app->clangd, app->buffer_node_head->buffer);
                    }
               }else if(strcmp(response.method, "textDocument/completion") == 0){
                    if(app->clangd_completion.complete){
                         ce_complete_free(app->clangd_completion.complete);
                         free(app->clangd_completion.complete);
                    }
                    app->clangd_completion.complete = _extract_completion_from_response(response.obj,
                                                                                        &app->clangd_completion.start);
                    CeLayout_t* tab_layout = app->tab_list_layout->tab_list.current;
                    if(app->clangd_completion.complete &&
                       tab_layout->tab.current->type == CE_LAYOUT_TYPE_VIEW){
                         CeView_t* completed_view = &tab_layout->tab.current->view;
                         int64_t match_len = completed_view->cursor.x - app->clangd_completion.start.x;
                         if(match_len > 0){
                              char* match = ce_buffer_dupe_string(completed_view->buffer, app->clangd_completion.start, match_len);
                              ce_complete_match(app->clangd_completion.complete, match);
                              free(match);
                         }
                         build_complete_list(app->clangd_completion.buffer,
                                             app->clangd_completion.complete);
                         build_clangd_completion_view(&app->clangd_completion.view,
                                                      app->clangd_completion.start,
                                                      completed_view,
                                                      app->clangd_completion.buffer,
                                                      &app->config_options,
                                                      &app->terminal_rect);
                    }
               }else if(strcmp(response.method, "textDocument/references") == 0){
                   _extract_reference_locations_to_buffer(response.obj, app->clangd_references_buffer,
                                                          app->buffer_node_head);
                   if(app->clangd_references_buffer->line_count > 0){
                       ce_app_open_popup_view(app, app->clangd_references_buffer);
                   }
               }
          }else{
               CeJsonFindResult_t method_find = ce_json_obj_find(response.obj, "method");
               if(method_find.type == CE_JSON_TYPE_STRING){
                    const char* method = ce_json_obj_get_string(response.obj, &method_find);
                    if(method != NULL){
                        if(strcmp(method, "textDocument/publishDiagnostics") == 0){
                            CeClangDDiagnostics_t diagnostics = _extract_diagnostics_from_response(response.obj);
                            if(diagnostics.filepath != NULL){
                                // DEBUG
                                // printf("diags: %s %lld\n", diagnostics.filepath, diagnostics.count);
                                // for(int64_t i = 0; i < diagnostics.count; i++){
                                //     CeClangDDiagnostic_t* diag = diagnostics.elements + i;
                                //     printf("  %lld, %lld -> %lld, %lld %s\n",
                                //            diag->start.x, diag->start.y, diag->end.x, diag->end.y,
                                //            diag->message);
                                // }
                                CeBuffer_t* buffer = find_already_loaded_file(app->buffer_node_head, diagnostics.filepath);
                                if(buffer){
                                    CeAppBufferData_t* app_data = (CeAppBufferData_t*)(buffer->app_data);
                                    if(app_data->clangd_diagnostics.count > 0){
                                        ce_clangd_diag_free(&app_data->clangd_diagnostics);
                                    }
                                    memcpy(&app_data->clangd_diagnostics, &diagnostics, sizeof(diagnostics));
                                }else{
                                    ce_clangd_diag_free(&diagnostics);
                                }
                            }
                        }
                    }
               }
          }
          ce_clangd_response_free(&response);
     }
}

// Assembly:
cmpq $0x0, 0x2cf8(%rdi)
je 0x13bb8
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4b8, %rsp            # imm = 0x4B8
movq %rdi, %rbx
leaq 0x2cd0(%rdi), %r14
movq %r14, %rdi
callq 0x161a1
testb %al, %al
je 0x13ba7
leaq 0x2d50(%rbx), %rax
movq %rax, 0x8(%rsp)
leaq 0x2d80(%rbx), %rax
movq %rax, 0x38(%rsp)
leaq 0x1a50(%rbx), %rbp
leaq 0x2828(%rbx), %rax
movq %rax, 0x50(%rsp)
leaq 0x20(%rbx), %rax
movq %rax, 0x48(%rsp)
leaq 0x20(%rsp), %r15
movq %rbp, 0x40(%rsp)
movq %r15, %rdi
movq %r14, %rsi
callq 0x161aa
movq 0x30(%rsp), %r12
testq %r12, %r12
je 0x139ea
movq %r12, %rdi
leaq 0x1c51e(%rip), %rsi     # 0x2fc25
callq 0xd390
testl %eax, %eax
je 0x137b6
movq %r12, %rdi
leaq 0x1c523(%rip), %rsi     # 0x2fc41
callq 0xd390
testl %eax, %eax
je 0x137b6
movq %r12, %rdi
leaq 0x1c524(%rip), %rsi     # 0x2fc59
callq 0xd390
testl %eax, %eax
je 0x137b6
movq %r12, %rdi
leaq 0x1c52a(%rip), %rsi     # 0x2fc72
callq 0xd390
testl %eax, %eax
je 0x137b6
movq %r12, %rdi
leaq 0x1c533(%rip), %rsi     # 0x2fc8e
callq 0xd390
testl %eax, %eax
je 0x13abd
movq %r12, %rdi
leaq 0x1c534(%rip), %rsi     # 0x2fca6
callq 0xd390
testl %eax, %eax
jne 0x13a30
movq 0x28(%rsp), %rdi
movq 0x2828(%rbx), %rdx
movq 0x2898(%rbx), %rsi
callq 0x13378
movq 0x2898(%rbx), %rsi
cmpq $0x0, 0x8(%rsi)
jle 0x13a30
movq %rbx, %rdi
callq 0x13c89
jmp 0x13a30
movq 0x2818(%rbx), %rax
movq 0x10(%rax), %rax
movq 0x10(%rax), %r13
movq 0x28(%rsp), %r12
movl $0x410, %edx            # imm = 0x410
leaq 0xa8(%rsp), %rdi
xorl %esi, %esi
callq 0xd2a0
movq %r12, %rdi
leaq 0x1c3cf(%rip), %rsi     # 0x2fbb7
callq 0x1c543
movl %eax, 0x10(%rsp)
movq %rdx, 0x18(%rsp)
cmpl $0x2, %eax
jne 0x139a3
movq %r12, %rdi
leaq 0x10(%rsp), %rsi
callq 0x1c5b1
testq %rax, %rax
je 0x139a3
movq %rax, %r12
cmpq $0x0, 0x8(%rax)
je 0x139a3
movq (%r12), %rax
cmpl $0x1, (%rax)
jne 0x139a3
movq (%r12), %rdi
addq $0x8, %rdi
leaq 0x1c3ab(%rip), %rsi     # 0x2fbea
callq 0x1c543
movl %eax, 0x58(%rsp)
movq %rdx, 0x60(%rsp)
cmpl $0x1, %eax
jne 0x1399e
movq (%r12), %rdi
addq $0x8, %rdi
leaq 0x58(%rsp), %rsi
callq 0x1c596
testq %rax, %rax
je 0x1399e
movq %rax, %rbp
movq %rax, %rdi
leaq 0x1ae4b(%rip), %rsi     # 0x2e6c9
callq 0x1c543
movl %eax, 0x98(%rsp)
movq %rdx, 0xa0(%rsp)
cmpl $0x1, 0x58(%rsp)
jne 0x1399e
movq %rbp, %rdi
leaq 0x98(%rsp), %rsi
callq 0x1c596
testq %rax, %rax
je 0x1399e
movq %rax, %rbp
movq %rax, %rdi
leaq 0x1c32d(%rip), %rsi     # 0x2fbf0
callq 0x1c543
movl %eax, 0x88(%rsp)
movq %rdx, 0x90(%rsp)
cmpl $0x4, %eax
jne 0x1399e
movq %rbp, %rdi
leaq 0x88(%rsp), %rsi
callq 0x1c5e5
testq %rax, %rax
je 0x1399e
cvttsd2si (%rax), %rax
movq %rax, 0xa8(%rsp)
movq %rbp, %rdi
leaq 0x1b3ab(%rip), %rsi     # 0x2ecbb
callq 0x1c543
movl %eax, 0x78(%rsp)
movq %rdx, 0x80(%rsp)
cmpl $0x4, %eax
jne 0x1399e
movq %rbp, %rdi
leaq 0x78(%rsp), %rsi
callq 0x1c5e5
testq %rax, %rax
je 0x1399e
cvttsd2si (%rax), %rax
movq %rax, 0xb0(%rsp)
movq (%r12), %rdi
addq $0x8, %rdi
leaq 0x1c2ad(%rip), %rsi     # 0x2fc01
callq 0x1c543
movl %eax, 0x68(%rsp)
movq %rdx, 0x70(%rsp)
cmpl $0x3, %eax
jne 0x1399e
movq (%r12), %rdi
addq $0x8, %rdi
leaq 0x68(%rsp), %rsi
callq 0x1c5cc
testq %rax, %rax
je 0x1399e
movl $0x3ff, %edx            # imm = 0x3FF
leaq 0xb8(%rsp), %r12
movq %r12, %rdi
movq %rax, %rsi
callq 0xd0b0
movq %r12, %rdi
callq 0x132c6
movq 0x40(%rsp), %rbp
addq $0x8, %r13
leaq 0xa8(%rsp), %rax
movq %rax, (%rsp)
movq 0x50(%rsp), %r12
movq %r12, %rdi
movq %r13, %rsi
movq %rbp, %rdx
movq 0x48(%rsp), %rcx
movl $0x1, %r8d
xorl %r9d, %r9d
callq 0x13bb9
testq %rax, %rax
je 0x13a30
movq (%r12), %rax
movq (%rax), %rsi
movq %r14, %rdi
callq 0x159ff
jmp 0x13a30
movq 0x28(%rsp), %rdi
leaq 0x1c2c8(%rip), %rsi     # 0x2fcbe
callq 0x1c543
movl %eax, 0x10(%rsp)
movq %rdx, 0x18(%rsp)
cmpl $0x3, %eax
jne 0x13a30
movq 0x28(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x1c5cc
testq %rax, %rax
je 0x13a30
movq %rax, %rdi
leaq 0x1c29e(%rip), %rsi     # 0x2fcc5
callq 0xd390
testl %eax, %eax
je 0x13a4d
movq %r15, %rdi
callq 0x1635d
movq %r14, %rdi
callq 0x161a1
testb %al, %al
jne 0x136e4
jmp 0x13ba7
movq 0x28(%rsp), %rsi
leaq 0xa8(%rsp), %rdi
callq 0x12f9c
movq 0xb8(%rsp), %rsi
testq %rsi, %rsi
je 0x13a30
movq 0x2828(%rbx), %rdi
callq 0x11827
testq %rax, %rax
je 0x13b95
movq 0x58(%rax), %rax
leaq 0x610(%rax), %r12
cmpq $0x0, 0x618(%rax)
jle 0x13a9e
movq %r12, %rdi
callq 0x163f3
movq 0xb8(%rsp), %rax
movq %rax, 0x10(%r12)
movups 0xa8(%rsp), %xmm0
movups %xmm0, (%r12)
jmp 0x13a30
movq 0x2d70(%rbx), %rdi
testq %rdi, %rdi
je 0x13ada
callq 0x1a0bc
movq 0x2d70(%rbx), %rdi
callq 0xd090
movq 0x28(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x12a0e
movq %rax, 0x2d70(%rbx)
testq %rax, %rax
je 0x13a30
movq 0x2818(%rbx), %rax
movq 0x10(%rax), %rax
movq 0x10(%rax), %r12
cmpl $0x0, (%r12)
jne 0x13a30
movq 0x38(%r12), %rcx
movq 0x8(%rsp), %rax
movq (%rax), %rsi
subq %rsi, %rcx
jle 0x13b50
movq 0x48(%r12), %rdi
movq 0x2d58(%rbx), %rdx
callq 0xf79a
movq %rax, %r13
movq 0x2d70(%rbx), %rdi
movq %rax, %rsi
callq 0x1a14a
movq %r13, %rdi
callq 0xd090
addq $0x8, %r12
movq 0x2d70(%rbx), %rsi
movq 0x2d78(%rbx), %rdi
callq 0x11e3a
movq 0x2d78(%rbx), %r8
movq 0x2d50(%rbx), %rsi
movq 0x2d58(%rbx), %rdx
movq %rbx, (%rsp)
movq 0x38(%rsp), %rdi
movq %r12, %rcx
movq %rbp, %r9
callq 0x12e5d
jmp 0x13a30
leaq 0xa8(%rsp), %rdi
callq 0x163f3
jmp 0x13a30
addq $0x4b8, %rsp            # imm = 0x4B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq