// File: /workspace/github_c_makefile_star2_O2/tditlu[P]shadetabler/src/cwalk/cwalk.c
// Function: cwk_path_change_basename
size_t cwk_path_change_basename(const char *path, const char *new_basename,
  char *buffer, size_t buffer_size)
{
  struct cwk_segment segment;
  size_t pos, root_size, new_basename_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.
    cwk_path_get_root(path, &root_size);
    pos = cwk_path_output_sized(buffer, buffer_size, 0, path, root_size);

    // We have to trim the separators from the beginning of the new basename.
    // This is quite easy to do.
    while (cwk_path_is_separator(new_basename)) {
      ++new_basename;
    }

    // Now we measure the length of the new basename, this is a two step
    // process. First we find the '\0' character at the end of the string.
    new_basename_size = 0;
    while (new_basename[new_basename_size]) {
      ++new_basename_size;
    }

    // And then we trim the separators at the end of the basename until we reach
    // the first valid character.
    while (new_basename_size > 0 &&
           cwk_path_is_separator(&new_basename[new_basename_size - 1])) {
      --new_basename_size;
    }

    // Now we will output the new basename after the root.
    pos += cwk_path_output_sized(buffer, buffer_size, pos, new_basename,
      new_basename_size);

    // And finally terminate the output and return the total size of the path.
    cwk_path_terminate_output(buffer, buffer_size, pos);
    return pos;
  }

  // If there is a last segment we can just forward this call, which is fairly
  // easy.
  return cwk_path_change_segment(&segment, new_basename, buffer, buffer_size);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rcx, %rbx
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %r12
leaq 0x10(%rsp), %rsi
callq 0xd1dc
testb %al, %al
je 0xd243
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
movq %r14, %rdx
movq %rbx, %rcx
callq 0xd337
movq %rax, %r12
jmp 0xd301
leaq 0x8(%rsp), %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0xce34
movq (%r13), %r13
cmpq %rbx, %r13
movq %rbx, %rdx
cmovbq %r13, %rdx
testq %rdx, %rdx
je 0xd271
movq %r14, %rdi
movq %r12, %rsi
callq 0x1170
movq %r15, %rdi
callq 0xd313
incq %r15
testb %al, %al
jne 0xd271
leaq -0x3(%r15), %r12
decq %r15
pushq $0x1
popq %rcx
decq %rcx
cmpb $0x0, 0x2(%r12)
leaq 0x1(%r12), %r12
jne 0xd28a
jmp 0xd2af
movq %r12, %rdi
callq 0xd313
leaq 0x1(%rbp), %rcx
decq %r12
testb %al, %al
je 0xd2b7
movq %rcx, %rbp
testq %rcx, %rcx
jne 0xd29c
movq %r13, %r12
subq %rbp, %r12
xorl %eax, %eax
movq %rbx, %rcx
subq %r13, %rcx
cmovaeq %rcx, %rax
movq %rbp, %rdx
negq %rdx
cmpq %rbx, %r12
cmovaeq %rax, %rdx
testq %rdx, %rdx
je 0xd2e7
leaq (%r14,%r13), %rdi
movq %r15, %rsi
callq 0x1170
testq %rbx, %rbx
je 0xd301
addq %r14, %r13
subq %rbp, %r13
cmpq %rbx, %r12
leaq -0x1(%r14,%rbx), %rax
cmovbq %r13, %rax
movb $0x0, (%rax)
movq %r12, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq