// File: /workspace/github_c_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, %rbx
movq %rdx, %r13
movq %rsi, %r15
movq %rdi, %r14
leaq 0x20(%rsp), %rsi
callq 0x161ab
testb %al, %al
je 0x165cb
movq %r13, %r14
movq 0x30(%rsp), %rax
movq 0x38(%rsp), %r13
movq %r13, %r12
cmpq %r13, %rax
jae 0x1651d
cmpb $0x2e, (%rax)
cmoveq %rax, %r12
incq %rax
jmp 0x1650c
movq 0x20(%rsp), %rsi
subq %rsi, %r12
cmpq %rbx, %r12
movq %rbx, %rdx
cmovbq %r12, %rdx
testq %rdx, %rdx
je 0x1653c
movq %r14, %rdi
callq 0x2300
xorl %ebp, %ebp
cmpb $0x2e, (%r15)
sete %bpl
addq %r15, %rbp
movq %rbp, %rdi
callq 0x2140
movq %r14, %rdi
movq %r14, 0x8(%rsp)
leaq 0x1(%r12), %r14
leaq (%r12,%rax), %rdx
incq %rdx
movq %rbx, %rsi
movq %r13, %rcx
callq 0x16677
movq %rax, %r15
xorl %eax, %eax
movq %rbx, %rcx
subq %r12, %rcx
cmovaeq %rcx, %rax
movq %r14, 0x10(%rsp)
cmpq %rbx, %r14
pushq $0x1
popq %rdx
cmovaeq %rax, %rdx
testq %rdx, %rdx
movq 0x8(%rsp), %r13
je 0x165aa
addq %r13, %r12
leaq 0x351c(%rip), %rsi      # 0x19abe
movq %r12, %rdi
callq 0x2280
movq %r13, %rdi
movq %rbx, %rsi
movq 0x10(%rsp), %r14
movq %r14, %rdx
movq %rbp, %rcx
callq 0x16677
addq %r14, %r15
addq %rax, %r15
jmp 0x1664d
leaq 0x18(%rsp), %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x15e03
movq (%r12), %r12
cmpq %rbx, %r12
movq %rbx, %rdx
cmovbq %r12, %rdx
testq %rdx, %rdx
je 0x165f9
movq %r13, %rdi
movq %r14, %rsi
callq 0x2300
cmpb $0x2e, (%r15)
jne 0x16604
movq %r12, %r14
jmp 0x16636
leaq 0x1(%r12), %r14
xorl %eax, %eax
movq %rbx, %rcx
subq %r12, %rcx
cmovaeq %rcx, %rax
cmpq %rbx, %r14
pushq $0x1
popq %rdx
cmovaeq %rax, %rdx
testq %rdx, %rdx
je 0x16636
addq %r13, %r12
leaq 0x3490(%rip), %rsi      # 0x19abe
movq %r12, %rdi
callq 0x2280
movq %r13, %rdi
movq %rbx, %rsi
movq %r14, %rdx
movq %r15, %rcx
callq 0x16677
movq %rax, %r15
addq %r14, %r15
testq %rbx, %rbx
je 0x16665
leaq (%r15,%r13), %rax
cmpq %rbx, %r15
leaq -0x1(%r13,%rbx), %rcx
cmovbq %rax, %rcx
movb $0x0, (%rcx)
movq %r15, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq