// File: /workspace/github_c_makefile_star2_O0/tditlu[P]shadetabler/src/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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x17f80
testb $0x1, %al
jne 0x1847a
movq -0x10(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x17620
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x10(%rbp), %rcx
movq -0x70(%rbp), %r8
xorl %eax, %eax
movl %eax, %edx
callq 0x17af0
movq %rax, -0x68(%rbp)
movq -0x18(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2e, %eax
je 0x1843f
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x68(%rbp), %rdx
callq 0x18590
addq -0x68(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x68(%rbp), %rdx
movq -0x18(%rbp), %rcx
callq 0x185d0
addq -0x68(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x68(%rbp), %rdx
callq 0x17bb0
movq -0x68(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x1857d
movq -0x38(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x38(%rbp), %rax
jae 0x184b8
movq -0x58(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2e, %eax
jne 0x184a8
movq -0x58(%rbp), %rax
movq %rax, -0x60(%rbp)
jmp 0x184aa
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x1848a
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x50(%rbp), %rcx
movq -0x60(%rbp), %r8
movq -0x50(%rbp), %rax
subq %rax, %r8
xorl %eax, %eax
movl %eax, %edx
callq 0x17af0
movq %rax, -0x68(%rbp)
movq -0x18(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2e, %eax
jne 0x184f4
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
callq 0x1080
addq $0x1, %rax
movq %rax, -0x80(%rbp)
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x68(%rbp), %rdx
addq -0x80(%rbp), %rdx
movq -0x38(%rbp), %rcx
callq 0x185d0
movq %rax, -0x78(%rbp)
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x68(%rbp), %rdx
callq 0x18590
addq -0x68(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x68(%rbp), %rdx
movq -0x18(%rbp), %rcx
callq 0x185d0
addq -0x68(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x78(%rbp), %rax
addq -0x68(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x68(%rbp), %rdx
callq 0x17bb0
movq -0x68(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x80, %rsp
popq %rbp
retq
nopw (%rax,%rax)