// File: /workspace/github_c_makefile_star2_O1/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, %r12
movq %rdi, %r13
leaq 0x10(%rsp), %rsi
callq 0xf617
testb %al, %al
je 0xf6a0
leaq 0x10(%rsp), %rdi
movq %r12, %rsi
movq %r14, %rdx
movq %rbx, %rcx
callq 0xf805
movq %rax, %r12
jmp 0xf7b6
leaq 0x8(%rsp), %r15
movq %r13, %rdi
movq %r15, %rsi
callq 0xf131
movq (%r15), %r15
cmpq %rbx, %r15
movq %rbx, %rdx
cmovbq %r15, %rdx
testq %rdx, %rdx
je 0xf6cd
movq %r14, %rdi
movq %r13, %rsi
callq 0x1180
movl 0x9a4d(%rip), %eax      # 0x19120
leaq 0x9566(%rip), %rcx      # 0x18c40
movq (%rcx,%rax,8), %rax
movb (%rax), %cl
testb %cl, %cl
setne %dl
leaq 0x1(%rax), %rdi
movq %r12, %rsi
movl %edx, %r8d
testb %cl, %cl
je 0xf718
movb (%rsi), %r9b
movb $0x1, %r8b
cmpb %r9b, %cl
je 0xf718
movq %rdi, %r10
movb (%r10), %r11b
testb %r11b, %r11b
setne %r8b
cmpb %r9b, %r11b
je 0xf718
incq %r10
testb %r11b, %r11b
jne 0xf701
leaq 0x1(%rsi), %r12
testb %r8b, %r8b
jne 0xf6e9
movq $-0x1, %rdi
cmpb $0x0, (%r12,%rdi)
leaq 0x1(%rdi), %rdi
jne 0xf728
testq %rdi, %rdi
je 0xf776
incq %rax
leaq -0x1(%rdi), %r8
movl %edx, %r10d
testb %cl, %cl
je 0xf769
movb (%rsi,%r8), %r9b
cmpb %r9b, %cl
je 0xf76e
movq %rax, %r11
movb (%r11), %bpl
testb %bpl, %bpl
setne %r10b
cmpb %r9b, %bpl
je 0xf769
incq %r11
testb %bpl, %bpl
jne 0xf752
testb %r10b, %r10b
je 0xf778
movq %r8, %rdi
testq %r8, %r8
jne 0xf73b
xorl %edi, %edi
leaq (%rdi,%r15), %r12
xorl %edx, %edx
movq %rbx, %rax
subq %r15, %rax
cmovaeq %rax, %rdx
cmpq %rbx, %r12
cmovbq %rdi, %rdx
testq %rdx, %rdx
je 0xf79f
addq %r14, %r15
movq %r15, %rdi
callq 0x1180
testq %rbx, %rbx
je 0xf7b6
cmpq %rbx, %r12
jae 0xf7b0
movb $0x0, (%r14,%r12)
jmp 0xf7b6
movb $0x0, -0x1(%r14,%rbx)
movq %r12, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq