// File: /workspace/github_c_makefile_star2_O2/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), %rsi
movq %rdi, (%rsp)
movq 0x10(%rdi), %r14
subq %rsi, %r14
cmpq %rcx, %r14
movq %rcx, %rdx
cmovbq %r14, %rdx
testq %rdx, %rdx
je 0xd373
movq %r12, %rdi
callq 0x1170
movq %r12, 0x8(%rsp)
movq %r15, %rdi
callq 0xd313
incq %r15
testb %al, %al
jne 0xd378
leaq -0x3(%r15), %r13
decq %r15
pushq $-0x1
popq %r12
incq %r12
cmpb $0x0, 0x2(%r13)
leaq 0x1(%r13), %r13
jne 0xd392
jmp 0xd3b1
movq %r13, %rdi
callq 0xd313
decq %r13
testb %al, %al
je 0xd3ba
movq %r12, %rbp
subq $0x1, %r12
jae 0xd3a2
movq (%rsp), %rax
movq 0x18(%rax), %r12
movq %r12, %rdi
callq 0x10a0
leaq (%r14,%rbp), %r13
leaq (%rax,%r13), %rcx
xorl %edx, %edx
movq %rbx, %rsi
subq %r13, %rsi
cmovaeq %rsi, %rdx
movq %rcx, (%rsp)
cmpq %rbx, %rcx
movq %rax, 0x10(%rsp)
cmovbq %rax, %rdx
testq %rdx, %rdx
je 0xd407
movq 0x8(%rsp), %rax
leaq (%rax,%r14), %rdi
addq %rbp, %rdi
movq %r12, %rsi
callq 0x1170
xorl %edx, %edx
movq %rbx, %rax
subq %r14, %rax
cmovaeq %rax, %rdx
cmpq %rbx, %r13
cmovbq %rbp, %rdx
testq %rdx, %rdx
movq 0x8(%rsp), %r12
je 0xd430
leaq (%r12,%r14), %rdi
movq %r15, %rsi
callq 0x1170
testq %rbx, %rbx
movq (%rsp), %rax
je 0xd453
addq %r12, %r14
addq %rbp, %r14
addq 0x10(%rsp), %r14
cmpq %rbx, %rax
leaq -0x1(%r12,%rbx), %rcx
cmovbq %r14, %rcx
movb $0x0, (%rcx)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq