// File: /workspace/github_c_makefile_star2_O3/tditlu[P]shadetabler/src/cwalk/cwalk.c
// Function: cwk_path_change_segment
size_t cwk_path_change_segment(struct cwk_segment *segment, const char *value,
  char *buffer, size_t buffer_size)
{
  size_t pos, value_size, tail_size;

  // First we have to output the head, which is the whole string up to the
  // beginning of the segment. This part of the path will just stay the same.
  pos = cwk_path_output_sized(buffer, buffer_size, 0, segment->path,
    (size_t)(segment->begin - segment->path));

  // In order to trip the submitted value, we will skip any separator at the
  // beginning of it and behave as if it was never there.
  while (cwk_path_is_separator(value)) {
    ++value;
  }

  // Now we determine the length of the value. In order to do that we first
  // locate the '\0'.
  value_size = 0;
  while (value[value_size]) {
    ++value_size;
  }

  // Since we trim separators at the beginning and in the end of the value we
  // have to subtract from the size until there are either no more characters
  // left or the last character is no separator.
  while (value_size > 0 && cwk_path_is_separator(&value[value_size - 1])) {
    --value_size;
  }

  // We also have to determine the tail size, which is the part of the string
  // following the current segment. This part will not change.
  tail_size = strlen(segment->end);

  // Now we output the tail. We have to do that, because if the buffer and the
  // source are overlapping we would override the tail if the value is
  // increasing in length.
  cwk_path_output_sized(buffer, buffer_size, pos + value_size, segment->end,
    tail_size);

  // Finally we can output the value in the middle of the head and the tail,
  // where we have enough space to fit the whole trimmed value.
  pos += cwk_path_output_sized(buffer, buffer_size, pos, value, value_size);

  // Now we add the tail size to the current position and terminate the output -
  // basically, ensure that there is a '\0' at the end of the buffer.
  pos += tail_size;
  cwk_path_terminate_output(buffer, buffer_size, pos);

  // And now tell the caller how long the whole path would be.
  return pos;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rcx, %rbx
movq %rdx, %r12
movq %rsi, %r15
movq %rdi, %r13
movq (%rdi), %rsi
movq 0x10(%rdi), %rcx
subq %rsi, %rcx
cmpq %rbx, %rcx
movq %rbx, %rdx
movq %rcx, 0x8(%rsp)
cmovbq %rcx, %rdx
testq %rdx, %rdx
je 0xf517
movq %r12, %rdi
callq 0x1180
movl 0x9c03(%rip), %eax      # 0x19120
leaq 0x971c(%rip), %rcx      # 0x18c40
movq (%rcx,%rax,8), %rax
movb (%rax), %cl
testb %cl, %cl
je 0xf552
leaq 0x1(%rax), %rdx
movb (%r15), %sil
movq %rdx, %rdi
movl %ecx, %r8d
cmpb %sil, %r8b
je 0xf54d
movb (%rdi), %r8b
incq %rdi
testb %r8b, %r8b
jne 0xf53b
jmp 0xf552
incq %r15
jmp 0xf532
movq $-0x1, %r14
cmpb $0x0, 0x1(%r15,%r14)
leaq 0x1(%r14), %r14
jne 0xf559
testq %r14, %r14
movq %rbx, 0x10(%rsp)
movq %r12, (%rsp)
je 0xf5a5
incq %rax
movq %r14, %rdx
testb %cl, %cl
je 0xf5ad
leaq -0x1(%rdx), %rsi
movb (%r15,%rsi), %dil
movq %rax, %r8
movl %ecx, %r9d
cmpb %dil, %r9b
je 0xf59d
movb (%r8), %r9b
incq %r8
testb %r9b, %r9b
jne 0xf58b
jmp 0xf5aa
movq %rsi, %rdx
testq %rsi, %rsi
jne 0xf579
xorl %r14d, %r14d
jmp 0xf5ad
movq %rdx, %r14
movq 0x18(%r13), %rbp
movq %rbp, %rdi
callq 0x10a0
movq 0x8(%rsp), %rcx
leaq (%r14,%rcx), %rbx
leaq (%rax,%rbx), %r13
xorl %edx, %edx
movq 0x10(%rsp), %rsi
movq %rsi, %rcx
subq %rbx, %rcx
cmovaeq %rcx, %rdx
movq %rsi, %r12
cmpq %rsi, %r13
cmovbq %rax, %rdx
testq %rdx, %rdx
je 0xf5f6
movq (%rsp), %rax
leaq (%rax,%rbx), %rdi
movq %rbp, %rsi
callq 0x1180
xorl %edx, %edx
movq %r12, %rax
movq 0x8(%rsp), %rdi
subq %rdi, %rax
cmovaeq %rax, %rdx
cmpq %r12, %rbx
cmovbq %r14, %rdx
testq %rdx, %rdx
movq (%rsp), %rbx
je 0xf622
addq %rbx, %rdi
movq %r15, %rsi
callq 0x1180
testq %r12, %r12
je 0xf63a
leaq (%rbx,%r13), %rax
cmpq %r12, %r13
leaq -0x1(%rbx,%r12), %rcx
cmovbq %rax, %rcx
movb $0x0, (%rcx)
movq %r13, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq