// File: /workspace/github_c_makefile_star2_O0/atomlang[P]atomlang/cli/thirdparty/cwalk/cwalk.c
// Function: cwk_path_change_extension
size_t cwk_path_change_extension(const char* path, const char* new_extension,
  char* buffer, size_t buffer_size)
{
  struct cwk_segment segment;
  const char* c, * old_extension;
  size_t pos, root_size, trail_size, new_extension_size;

  // First we try to get the last segment. We may only have a root without any
  // segments, in which case we will create one.
  if (!cwk_path_get_last_segment(path, &segment)) {

    // So there is no segment in this path. First we grab the root and output
    // that. We are not going to modify the root in any way. If there is no
    // root, this will end up with a root size 0, and nothing will be written.
    cwk_path_get_root(path, &root_size);
    pos = cwk_path_output_sized(buffer, buffer_size, 0, path, root_size);

    // Add a dot if the submitted value doesn't have any.
    if (*new_extension != '.') {
      pos += cwk_path_output_dot(buffer, buffer_size, pos);
    }

    // And finally terminate the output and return the total size of the path.
    pos += cwk_path_output(buffer, buffer_size, pos, new_extension);
    cwk_path_terminate_output(buffer, buffer_size, pos);
    return pos;
  }

  // Now we seek the old extension in the last segment, which we will replace
  // with the new one. If there is no old extension, it will point to the end of
  // the segment.
  old_extension = segment.end;
  for (c = segment.begin; c < segment.end; ++c) {
    if (*c == '.') {
      old_extension = c;
    }
  }

  pos = cwk_path_output_sized(buffer, buffer_size, 0, segment.path,
    (size_t)(old_extension - segment.path));

  // If the new extension starts with a dot, we will skip that dot. We always
  // output exactly one dot before the extension. If the extension contains
  // multiple dots, we will output those as part of the extension.
  if (*new_extension == '.') {
    ++new_extension;
  }

  // We calculate the size of the new extension, including the dot, in order to
  // output the trail - which is any part of the path coming after the
  // extension. We must output this first, since the buffer may overlap with the
  // submitted path - and it would be overridden by longer extensions.
  new_extension_size = strlen(new_extension) + 1;
  trail_size = cwk_path_output(buffer, buffer_size, pos + new_extension_size,
    segment.end);

  // Finally we output the dot and the new extension. The new extension itself
  // doesn't contain the dot anymore, so we must output that first.
  pos += cwk_path_output_dot(buffer, buffer_size, pos);
  pos += cwk_path_output(buffer, buffer_size, pos, new_extension);

  // Now we terminate the output with a null-terminating character, but before
  // we do that we must add the size of the trail to the position which we
  // output before.
  pos += trail_size;
  cwk_path_terminate_output(buffer, buffer_size, pos);

  // And the position is our output size now.
  return pos;
}

// Assembly:
subq $0x88, %rsp
movq %rdi, 0x78(%rsp)
movq %rsi, 0x70(%rsp)
movq %rdx, 0x68(%rsp)
movq %rcx, 0x60(%rsp)
movq 0x78(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x276b0
testb $0x1, %al
jne 0x27c27
movq 0x78(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x26c90
movq 0x68(%rsp), %rdi
movq 0x60(%rsp), %rsi
movq 0x78(%rsp), %rcx
movq 0x18(%rsp), %r8
xorl %eax, %eax
movl %eax, %edx
callq 0x27220
movq %rax, 0x20(%rsp)
movq 0x70(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x2e, %eax
je 0x27bde
movq 0x68(%rsp), %rdi
movq 0x60(%rsp), %rsi
movq 0x20(%rsp), %rdx
callq 0x27d70
addq 0x20(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x68(%rsp), %rdi
movq 0x60(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq 0x70(%rsp), %rcx
callq 0x27db0
addq 0x20(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x68(%rsp), %rdi
movq 0x60(%rsp), %rsi
movq 0x20(%rsp), %rdx
callq 0x26e00
movq 0x20(%rsp), %rax
movq %rax, 0x80(%rsp)
jmp 0x27d5c
movq 0x50(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x48(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x30(%rsp), %rax
cmpq 0x50(%rsp), %rax
jae 0x27c70
movq 0x30(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x2e, %eax
jne 0x27c5e
movq 0x30(%rsp), %rax
movq %rax, 0x28(%rsp)
jmp 0x27c60
movq 0x30(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x30(%rsp)
jmp 0x27c3b
movq 0x68(%rsp), %rdi
movq 0x60(%rsp), %rsi
movq 0x38(%rsp), %rcx
movq 0x28(%rsp), %r8
movq 0x38(%rsp), %rax
subq %rax, %r8
xorl %eax, %eax
movl %eax, %edx
callq 0x27220
movq %rax, 0x20(%rsp)
movq 0x70(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x2e, %eax
jne 0x27cb5
movq 0x70(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x70(%rsp)
movq 0x70(%rsp), %rdi
callq 0x2130
addq $0x1, %rax
movq %rax, 0x8(%rsp)
movq 0x68(%rsp), %rdi
movq 0x60(%rsp), %rsi
movq 0x20(%rsp), %rdx
addq 0x8(%rsp), %rdx
movq 0x50(%rsp), %rcx
callq 0x27db0
movq %rax, 0x10(%rsp)
movq 0x68(%rsp), %rdi
movq 0x60(%rsp), %rsi
movq 0x20(%rsp), %rdx
callq 0x27d70
addq 0x20(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x68(%rsp), %rdi
movq 0x60(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq 0x70(%rsp), %rcx
callq 0x27db0
addq 0x20(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x10(%rsp), %rax
addq 0x20(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x68(%rsp), %rdi
movq 0x60(%rsp), %rsi
movq 0x20(%rsp), %rdx
callq 0x26e00
movq 0x20(%rsp), %rax
movq %rax, 0x80(%rsp)
movq 0x80(%rsp), %rax
addq $0x88, %rsp
retq
nopl (%rax)