// File: /workspace/github_c_makefile_star2_O3/deplinenoise[P]tundra/src/PathUtil.cpp
// Function: t2::PathInit(t2::PathBuffer*, char const*, t2::PathType::Enum)
void PathInit(PathBuffer* buffer, const char* path, PathType::Enum type)
{
  // Initialize
  buffer->m_Type  = type;
  buffer->m_Flags = 0;

  // Check to see if the path is absolute
  switch (type)
  {
    case PathType::kUnix:
      if ('/' == path[0])
      {
        buffer->m_Flags |= PathBuffer::kFlagAbsolute;
        path++;
      }
      break;

    case PathType::kWindows:
      // Check for absolute path w/o device name
      if ('\\' == path[0] || '/' == path[0])
      {
        buffer->m_Flags |= PathBuffer::kFlagAbsolute;
        path++;
      }
      // Check for X:\ style path
      else if (isalpha(path[0]) && ':' == path[1] && ('\\' == path[2] || '/' == path[2]))
      {
        buffer->m_Flags |= PathBuffer::kFlagAbsolute | PathBuffer::kFlagWindowsDevicePath;
      }
      // FIXME: network paths
      break;

    default:
      Croak("bad path type");
      break;
  }

  // Initialize segment data
  PathSeg segments[kMaxPathSegments];

  int raw_seg_count = PathGetSegments(path, segments);
  uint16_t dotdot_drops = 0;

  // Drop segments based on .. following them
  for (int i = raw_seg_count - 1; i >= 0; --i)
  {
    if (segments[i].drop)
      continue;

    if (segments[i].dotdot)
    {
      ++dotdot_drops;
      segments[i].drop = 1;
    }
    else if (dotdot_drops > 0)
    {
      --dotdot_drops;
      segments[i].drop = 1;
    }
  }

  buffer->m_LeadingDotDots = dotdot_drops;

  // Copy retained segments to output array
  uint16_t output_seg_count = 0;
  uint16_t output_pos = 0;
  for (int i = 0; i < raw_seg_count; ++i)
  {
    if (segments[i].drop)
      continue;

    if (output_pos >= kMaxPathLength)
      Croak("Path too long: %s", path);

    memcpy(buffer->m_Data + output_pos, path + segments[i].offset, segments[i].len);
    output_pos += segments[i].len;
    buffer->m_SegEnds[output_seg_count] = output_pos;
    ++output_seg_count;
  }

  buffer->m_SegCount = output_seg_count;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x198, %rsp            # imm = 0x198
movq %rsi, %rbx
movq %rdi, %r14
movl %edx, (%rdi)
movw $0x0, 0x4(%rdi)
cmpl $0x1, %edx
je 0xeb3f
testl %edx, %edx
jne 0xed3b
cmpb $0x2f, (%rbx)
je 0xeb50
jmp 0xeb5a
movzbl (%rbx), %eax
cmpl $0x5c, %eax
je 0xeb50
cmpl $0x2f, %eax
jne 0xecde
movw $0x1, 0x4(%r14)
incq %rbx
leaq 0x1(%rbx), %rcx
xorl %eax, %eax
movl %ebx, %edx
movq %rbx, %rsi
movzbl -0x1(%rcx), %edi
testl %edi, %edi
je 0xeb77
cmpl $0x5c, %edi
je 0xeb77
cmpl $0x2f, %edi
jne 0xebe3
movl %edx, %r8d
subl %esi, %r8d
testl %r8d, %r8d
jle 0xebdb
cmpl $0x1, %r8d
je 0xeb99
cmpl $0x2, %r8d
jne 0xeba5
cmpw $0x2e2e, (%rsi)         # imm = 0x2E2E
sete %r8b
jmp 0xeba8
cmpb $0x2e, (%rsi)
sete %r9b
xorl %r8d, %r8d
jmp 0xebab
xorl %r8d, %r8d
xorl %r9d, %r9d
cmpl $0x40, %eax
je 0xed16
movl %edx, %r10d
subl %esi, %r10d
subl %ebx, %esi
movslq %eax, %r11
leaq (%r11,%r11,2), %r11
movw %si, 0x10(%rsp,%r11,2)
movw %r10w, 0x12(%rsp,%r11,2)
movb %r8b, 0x14(%rsp,%r11,2)
movb %r9b, 0x15(%rsp,%r11,2)
incl %eax
testb %dil, %dil
je 0xebed
movq %rcx, %rsi
incq %rcx
incl %edx
jmp 0xeb65
testl %eax, %eax
jle 0xecbd
movl %eax, %ecx
leaq (%rcx,%rcx,2), %rsi
leaq 0x1(%rcx), %rdx
leaq (%rsp,%rsi,2), %rsi
addq $0xf, %rsi
xorl %edi, %edi
cmpb $0x0, (%rsi)
jne 0xec30
movw $0x1, %r8w
cmpb $0x0, -0x1(%rsi)
jne 0xec23
movw $0xffff, %r8w           # imm = 0xFFFF
testw %di, %di
je 0xec3f
movswl %r8w, %r8d
addl %edi, %r8d
movb $0x1, (%rsi)
movl %r8d, %edi
decq %rdx
addq $-0x6, %rsi
cmpq $0x1, %rdx
ja 0xec09
jmp 0xec43
xorl %edi, %edi
jmp 0xec30
movw %di, 0x8(%r14)
testl %eax, %eax
jle 0xecc4
movq %rbx, (%rsp)
leaq 0x8a(%r14), %rax
movq %rax, 0x8(%rsp)
addq %rcx, %rcx
leaq (%rcx,%rcx,2), %rbp
xorl %ebx, %ebx
xorl %eax, %eax
xorl %r13d, %r13d
cmpb $0x0, 0x15(%rsp,%rbx)
jne 0xecb2
movzwl %ax, %r12d
cmpl $0x200, %r12d           # imm = 0x200
jae 0xed29
movq %r12, %rdi
addq 0x8(%rsp), %rdi
movzwl 0x10(%rsp,%rbx), %esi
addq (%rsp), %rsi
movzwl 0x12(%rsp,%rbx), %r15d
movq %r15, %rdx
callq 0x3290
leal (%r15,%r12), %eax
movzwl %r13w, %ecx
movw %ax, 0xa(%r14,%rcx,2)
incl %r13d
addq $0x6, %rbx
cmpq %rbx, %rbp
jne 0xec6a
jmp 0xecc7
movw $0x0, 0x8(%r14)
xorl %r13d, %r13d
movw %r13w, 0x6(%r14)
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movsbl %al, %edi
callq 0x3570
testl %eax, %eax
je 0xeb5a
cmpb $0x3a, 0x1(%rbx)
jne 0xeb5a
movzbl 0x2(%rbx), %eax
cmpl $0x5c, %eax
je 0xed0a
cmpl $0x2f, %eax
jne 0xeb5a
movw $0x3, 0x4(%r14)
jmp 0xeb5a
leaq 0x80f0(%rip), %rdi      # 0x16e0d
movl $0x40, %esi
xorl %eax, %eax
callq 0x5a61
leaq 0x80bd(%rip), %rdi      # 0x16ded
movq (%rsp), %rsi
xorl %eax, %eax
callq 0x5a61
leaq 0x809d(%rip), %rdi      # 0x16ddf
xorl %eax, %eax
callq 0x5a61