// File: /workspace/github_c_makefile_star2_O1/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 %rdx, %rbx
movq %rsi, %rbp
movq %rdi, %r13
movq (%rdi), %rsi
movq 0x10(%rdi), %r15
subq %rsi, %r15
cmpq %rcx, %r15
movq %rcx, 0x10(%rsp)
movq %rcx, %rdx
cmovbq %r15, %rdx
testq %rdx, %rdx
je 0xf842
movq %rbx, %rdi
callq 0x1180
movl 0x98d8(%rip), %eax      # 0x19120
leaq 0x93f1(%rip), %rcx      # 0x18c40
movq (%rcx,%rax,8), %rax
movb (%rax), %cl
testb %cl, %cl
setne %dl
leaq 0x1(%rax), %rsi
movq %rbp, %r12
movl %edx, %edi
testb %cl, %cl
je 0xf88d
movb (%r12), %r8b
movb $0x1, %dil
cmpb %r8b, %cl
je 0xf88d
movq %rsi, %r9
movb (%r9), %r10b
testb %r10b, %r10b
setne %dil
cmpb %r8b, %r10b
je 0xf88d
incq %r9
testb %r10b, %r10b
jne 0xf876
leaq 0x1(%r12), %rbp
testb %dil, %dil
jne 0xf85e
movq $-0x1, %r14
cmpb $0x0, (%rbp,%r14)
leaq 0x1(%r14), %r14
jne 0xf89e
testq %r14, %r14
je 0xf8ed
incq %rax
leaq -0x1(%r14), %rsi
movl %edx, %r8d
testb %cl, %cl
je 0xf8e0
movb (%r12,%rsi), %dil
cmpb %dil, %cl
je 0xf8e5
movq %rax, %r9
movb (%r9), %r10b
testb %r10b, %r10b
setne %r8b
cmpb %dil, %r10b
je 0xf8e0
incq %r9
testb %r10b, %r10b
jne 0xf8c9
testb %r8b, %r8b
je 0xf8f0
movq %rsi, %r14
testq %rsi, %rsi
jne 0xf8b2
xorl %r14d, %r14d
movq %rbx, 0x8(%rsp)
movq 0x18(%r13), %rbp
movq %rbp, %rdi
callq 0x10a0
leaq (%r14,%r15), %rbx
leaq (%rax,%rbx), %r13
xorl %edx, %edx
movq 0x10(%rsp), %rsi
movq %rsi, %rcx
subq %rbx, %rcx
cmovaeq %rcx, %rdx
cmpq %rsi, %r13
cmovbq %rax, %rdx
testq %rdx, %rdx
je 0xf937
movq 0x8(%rsp), %rax
leaq (%rax,%rbx), %rdi
movq %rbp, %rsi
callq 0x1180
xorl %edx, %edx
movq 0x10(%rsp), %rbp
movq %rbp, %rax
subq %r15, %rax
cmovaeq %rax, %rdx
cmpq %rbp, %rbx
cmovbq %r14, %rdx
testq %rdx, %rdx
movq 0x8(%rsp), %rbx
je 0xf967
addq %rbx, %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x1180
testq %rbp, %rbp
je 0xf97f
leaq (%rbx,%r13), %rax
cmpq %rbp, %r13
leaq -0x1(%rbx,%rbp), %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