// File: /workspace/github_c_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rcx, %r14
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r13
leaq 0x10(%rsp), %rsi
callq 0xf617
testb %al, %al
je 0xfb79
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rbp
movq %rbp, %r12
cmpq %rbp, %rax
jae 0xfa8a
movq %rbp, %r12
cmpb $0x2e, (%rax)
cmoveq %rax, %r12
incq %rax
cmpq %rbp, %rax
jne 0xfa7b
movq 0x10(%rsp), %rsi
subq %rsi, %r12
cmpq %r14, %r12
movq %r14, %rdx
cmovbq %r12, %rdx
testq %rdx, %rdx
je 0xfaa9
movq %rbx, %rdi
callq 0x1180
movq %rbx, (%rsp)
xorl %r13d, %r13d
cmpb $0x2e, (%r15)
sete %r13b
addq %r15, %r13
movq %r13, %rdi
callq 0x10a0
leaq (%r12,%rax), %r15
leaq (%r12,%rax), %rbx
incq %rbx
movq %rbp, %rdi
callq 0x10a0
leaq (%rax,%r15), %rsi
incq %rsi
movq %rax, %r15
xorl %edx, %edx
movq %r14, %rcx
subq %rbx, %rcx
cmovaeq %rcx, %rdx
cmpq %r14, %rsi
cmovbq %rax, %rdx
testq %rdx, %rdx
je 0xfb07
addq (%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x1180
leaq 0x1(%r12), %rbp
xorl %eax, %eax
movq %r14, %rcx
subq %r12, %rcx
cmovaeq %rcx, %rax
cmpq %r14, %rbp
movl $0x1, %edx
cmovaeq %rax, %rdx
testq %rdx, %rdx
movq (%rsp), %rbx
je 0xfb3f
addq %rbx, %r12
leaq 0x45bd(%rip), %rsi      # 0x140f4
movq %r12, %rdi
callq 0x1130
movq %r13, %rdi
callq 0x10a0
leaq (%rax,%rbp), %r12
xorl %edx, %edx
movq %r14, %rcx
subq %rbp, %rcx
cmovaeq %rcx, %rdx
cmpq %r14, %r12
cmovbq %rax, %rdx
testq %rdx, %rdx
je 0xfb71
addq %rbx, %rbp
movq %rbp, %rdi
movq %r13, %rsi
callq 0x1180
addq %r15, %r12
jmp 0xfc1d
movq %rbx, %rbp
leaq 0x8(%rsp), %rbx
movq %r13, %rdi
movq %rbx, %rsi
callq 0xf131
movq (%rbx), %r12
movq %rbp, %rbx
cmpq %r14, %r12
movq %r14, %rdx
cmovbq %r12, %rdx
testq %rdx, %rdx
je 0xfbac
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1180
cmpb $0x2e, (%r15)
jne 0xfbb7
movq %r12, %r13
jmp 0xfbeb
leaq 0x1(%r12), %r13
xorl %eax, %eax
movq %r14, %rcx
subq %r12, %rcx
cmovaeq %rcx, %rax
cmpq %r14, %r13
movl $0x1, %edx
cmovaeq %rax, %rdx
testq %rdx, %rdx
je 0xfbeb
addq %rbx, %r12
leaq 0x4511(%rip), %rsi      # 0x140f4
movq %r12, %rdi
callq 0x1130
movq %r15, %rdi
callq 0x10a0
leaq (%rax,%r13), %r12
xorl %edx, %edx
movq %r14, %rcx
subq %r13, %rcx
cmovaeq %rcx, %rdx
cmpq %r14, %r12
cmovbq %rax, %rdx
testq %rdx, %rdx
je 0xfc1d
addq %rbx, %r13
movq %r13, %rdi
movq %r15, %rsi
callq 0x1180
testq %r14, %r14
je 0xfc35
cmpq %r14, %r12
jae 0xfc2c
addq %r12, %rbx
jmp 0xfc32
addq %r14, %rbx
decq %rbx
movb $0x0, (%rbx)
movq %r12, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq