// File: /workspace/github_c_makefile_star2_O2/justy989[P]ce/ce_app.c
// Function: extract_reference_locations_to_buffer
void _extract_reference_locations_to_buffer(CeJsonObj_t* obj, CeBuffer_t* buffer,
                                            CeBufferNode_t* buffer_node_head){
     buffer->status = CE_BUFFER_STATUS_NONE;
     ce_buffer_empty(buffer);

     CeJsonFindResult_t result_find = ce_json_obj_find(obj, "result");
     if(result_find.type != CE_JSON_TYPE_ARRAY){
         return;
     }
     CeJsonArray_t* result_array = ce_json_obj_get_array(obj, &result_find);
     if(result_array == NULL){
         return;
     }

     char* filepath = NULL;
     for(int64_t i = 0; i < result_array->count; i++){
          if(filepath){
               free(filepath);
               filepath = NULL;
          }

          CeJsonValue_t* value = result_array->values + i;
          if(value->type != CE_JSON_TYPE_OBJECT){
                continue;
          }

          CeJsonFindResult_t uri_find = ce_json_obj_find(&value->obj, "uri");
          if(uri_find.type != CE_JSON_TYPE_STRING){
               continue;
          }

          const char* file_uri = ce_json_obj_get_string(&value->obj, &uri_find);
          if(file_uri == NULL){
               continue;
          }

          filepath = strdup(file_uri);
#if defined(PLATFORM_WINDOWS)
          _convert_uri_to_windows_path(filepath);
#else
          _convert_uri_to_linux_path(filepath);
#endif

          CeJsonFindResult_t range_find = ce_json_obj_find(&value->obj, "range");
          if(range_find.type != CE_JSON_TYPE_OBJECT){
              continue;
          }

          CeJsonObj_t* range_obj = ce_json_obj_get_obj(&value->obj, &range_find);
          if(range_obj == NULL){
              continue;
          }

          // This logic can be compressed.
          CeJsonFindResult_t start_find = ce_json_obj_find(range_obj, "start");
          if(start_find.type != CE_JSON_TYPE_OBJECT){
               continue;
          }
          CeJsonObj_t* start_obj = ce_json_obj_get_obj(range_obj, &start_find);
          if(start_obj == NULL){
               continue;
          }
          CeJsonFindResult_t line_find = ce_json_obj_find(start_obj, "line");
          if(line_find.type != CE_JSON_TYPE_NUMBER){
               continue;
          }
          CeJsonFindResult_t character_find = ce_json_obj_find(start_obj, "character");
          if(character_find.type != CE_JSON_TYPE_NUMBER){
               continue;
          }
          int64_t start_x = (int64_t)(*ce_json_obj_get_number(start_obj, &character_find));
          int64_t start_y = (int64_t)(*ce_json_obj_get_number(start_obj, &line_find));

          char line[MAX_PATH_LEN];
          snprintf(line, MAX_PATH_LEN, "%s:%" PRId64 ":%" PRId64 " ", filepath, start_y + 1, start_x + 1);
          CePoint_t end = ce_buffer_end_point(buffer);
          ce_buffer_insert_string(buffer, line, end);

          // Find the line to insert text from that buffer.
          CeBufferNode_t* buffer_itr = buffer_node_head;
          while(buffer_itr){
#if defined(PLATFORM_WINDOWS)
              char* buffer_full_path = _fullpath(NULL, buffer_itr->buffer->name, MAX_PATH_LEN);
#else
              char* buffer_full_path = realpath(buffer_itr->buffer->name, NULL);
#endif
              bool matches = (strcmp(filepath, buffer_full_path) == 0);
              free(buffer_full_path);
              if(matches){
                  if(start_y < buffer_itr->buffer->line_count){
                      char* buffer_line = buffer_itr->buffer->lines[start_y];
                      end = ce_buffer_end_point(buffer);
                      ce_buffer_insert_string(buffer, buffer_line, end);
                  }
                  break;
              }
              buffer_itr = buffer_itr->next;
          }

          end = ce_buffer_end_point(buffer);
          ce_buffer_insert_string(buffer, "\n", end);
     }
     if(filepath){
          free(filepath);
          filepath = NULL;
     }
     buffer->status = CE_BUFFER_STATUS_READONLY;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x488, %rsp            # imm = 0x488
movq %rdx, %r12
movq %rsi, %rbx
movq %rdi, %r14
andl $0x0, 0x18(%rsi)
movq %rsi, %rdi
callq 0xe2e2
leaq 0x19225(%rip), %rsi     # 0x2bbb9
movq %r14, %rdi
callq 0x1af29
movl %eax, 0x70(%rsp)
movq %rdx, 0x78(%rsp)
cmpl $0x2, %eax
jne 0x12c25
leaq 0x70(%rsp), %rsi
movq %r14, %rdi
callq 0x1afa7
testq %rax, %rax
je 0x12c25
movq %rax, %r15
movq %r12, 0x18(%rsp)
movq %rbx, (%rsp)
leaq 0x1922c(%rip), %r13     # 0x2bc03
xorl %r12d, %r12d
xorl %ebp, %ebp
movq 0x8(%r15), %rbx
movq %rbp, %rdi
callq 0xd090
cmpq %rbx, %r12
jge 0x12c1a
movq (%r15), %r14
imulq $0x18, %r12, %rax
cmpl $0x1, (%r14,%rax)
jne 0x12bb6
addq %rax, %r14
addq $0x8, %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x1af29
movl %eax, 0x60(%rsp)
movq %rdx, 0x68(%rsp)
cmpl $0x3, %eax
jne 0x12bb6
movq %r14, %rdi
leaq 0x60(%rsp), %rsi
callq 0x1afc2
testq %rax, %rax
je 0x12bb6
movq %rax, %rdi
callq 0xd640
movq %rax, %rbp
movq %rax, %rdi
callq 0x128d5
movq %r14, %rdi
leaq 0x19192(%rip), %rsi     # 0x2bbec
callq 0x1af29
movl %eax, 0x50(%rsp)
movq %rdx, 0x58(%rsp)
cmpl $0x1, %eax
jne 0x12bb8
movq %r14, %rdi
leaq 0x50(%rsp), %rsi
callq 0x1af8c
testq %rax, %rax
je 0x12bb8
movq %rax, %r14
movq %rax, %rdi
leaq 0x17c35(%rip), %rsi     # 0x2a6c9
callq 0x1af29
movl %eax, 0x40(%rsp)
movq %rdx, 0x48(%rsp)
cmpl $0x1, %eax
jne 0x12bb8
movq %r14, %rdi
leaq 0x40(%rsp), %rsi
callq 0x1af8c
testq %rax, %rax
je 0x12bb8
movq %rax, %r14
movq %rax, %rdi
leaq 0x181ef(%rip), %rsi     # 0x2acbd
callq 0x1af29
movl %eax, 0x30(%rsp)
movq %rdx, 0x38(%rsp)
cmpl $0x4, %eax
jne 0x12bb8
movq %r14, %rdi
leaq 0x19103(%rip), %rsi     # 0x2bbf2
callq 0x1af29
movl %eax, 0x20(%rsp)
movq %rdx, 0x28(%rsp)
cmpl $0x4, %eax
jne 0x12bb8
movq %r14, %rdi
leaq 0x20(%rsp), %rsi
callq 0x1afdb
cvttsd2si (%rax), %rbx
movq %r14, %rdi
leaq 0x30(%rsp), %rsi
callq 0x1afdb
cvttsd2si (%rax), %rcx
movq %rcx, 0x10(%rsp)
leaq 0x1(%rcx), %r8
incq %rbx
movl $0x400, %esi            # imm = 0x400
leaq 0x80(%rsp), %r14
movq %r14, %rdi
leaq 0x190ce(%rip), %rdx     # 0x2bc1b
movq %rbp, %rcx
movq %rbx, %r9
xorl %eax, %eax
callq 0xd260
movq (%rsp), %rbx
movq %rbx, %rdi
callq 0xd9cc
movq %rdx, %rcx
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0xd9ef
movq 0x18(%rsp), %rbx
testq %rbx, %rbx
je 0x12bf7
movq (%rbx), %rax
movq 0x10(%rax), %rdi
xorl %esi, %esi
callq 0xd410
movq %rax, %r14
movq %rbp, %rdi
movq %rax, %rsi
callq 0xd390
movl %eax, 0xc(%rsp)
movq %r14, %rdi
callq 0xd090
cmpl $0x0, 0xc(%rsp)
je 0x12bc0
movq 0x8(%rbx), %rbx
jmp 0x12b7c
xorl %ebp, %ebp
incq %r12
jmp 0x129dc
movq (%rbx), %rax
movq 0x10(%rsp), %rcx
cmpq %rcx, 0x8(%rax)
jle 0x12bf7
movq (%rax), %rax
movq 0x10(%rsp), %rcx
movq (%rax,%rcx,8), %r14
movq (%rsp), %rbx
movq %rbx, %rdi
callq 0xd9cc
movq %rdx, %rcx
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0xd9ef
movq (%rsp), %rbx
movq %rbx, %rdi
callq 0xd9cc
movq %rdx, %rcx
movq %rbx, %rdi
leaq 0x192e1(%rip), %rsi     # 0x2bef1
movq %rax, %rdx
callq 0xd9ef
jmp 0x12bb8
movq (%rsp), %rax
movl $0x2, 0x18(%rax)
addq $0x488, %rsp            # imm = 0x488
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq