// File: /workspace/github_c_makefile_star2_O3/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 %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x30, %rsp
movq %rcx, %rbx
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %r13
leaq 0x8(%rsp), %rsi
callq 0xf30f
testb %al, %al
je 0xf397
leaq 0x8(%rsp), %rdi
movq %r15, %rsi
movq %r14, %rdx
movq %rbx, %rcx
callq 0xf4d7
movq %rax, %r13
jmp 0xf491
movq %rsp, %r12
movq %r13, %rdi
movq %r12, %rsi
callq 0xeee6
movq (%r12), %r12
cmpq %rbx, %r12
movq %rbx, %rdx
cmovbq %r12, %rdx
testq %rdx, %rdx
je 0xf3c3
movq %r14, %rdi
movq %r13, %rsi
callq 0x1180
movl 0x9d57(%rip), %eax      # 0x19120
leaq 0x9870(%rip), %rcx      # 0x18c40
movq (%rcx,%rax,8), %rax
movb (%rax), %cl
testb %cl, %cl
je 0xf3fe
leaq 0x1(%rax), %rdx
movb (%r15), %sil
movq %rdx, %rdi
movl %ecx, %r8d
cmpb %sil, %r8b
je 0xf3f9
movb (%rdi), %r8b
incq %rdi
testb %r8b, %r8b
jne 0xf3e7
jmp 0xf3fe
incq %r15
jmp 0xf3de
movq $-0x1, %rsi
cmpb $0x0, 0x1(%r15,%rsi)
leaq 0x1(%rsi), %rsi
jne 0xf405
testq %rsi, %rsi
je 0xf448
incq %rax
movq %rsi, %rdx
testb %cl, %cl
je 0xf44f
leaq -0x1(%rdx), %rdi
movb (%r15,%rdi), %r8b
movq %rax, %r9
movl %ecx, %r10d
cmpb %r8b, %r10b
je 0xf440
movb (%r9), %r10b
incq %r9
testb %r10b, %r10b
jne 0xf42e
jmp 0xf44c
movq %rdi, %rdx
testq %rdi, %rdi
jne 0xf41c
xorl %esi, %esi
jmp 0xf44f
movq %rdx, %rsi
leaq (%rsi,%r12), %r13
xorl %edx, %edx
movq %rbx, %rax
subq %r12, %rax
cmovaeq %rax, %rdx
cmpq %rbx, %r13
cmovbq %rsi, %rdx
testq %rdx, %rdx
je 0xf479
addq %r14, %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x1180
testq %rbx, %rbx
je 0xf491
leaq (%r14,%r13), %rax
cmpq %rbx, %r13
leaq -0x1(%r14,%rbx), %rcx
cmovbq %rax, %rcx
movb $0x0, (%rcx)
movq %r13, %rax
addq $0x30, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq