// File: /workspace/github_c_makefile_star2_O2/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, %r13
movl %edx, (%rdi)
movq %rdi, (%rsp)
andw $0x0, 0x4(%rdi)
cmpl $0x1, %edx
je 0xda4d
testl %edx, %edx
jne 0xdc47
cmpb $0x2f, (%r13)
je 0xda60
jmp 0xda6d
movzbl (%r13), %eax
cmpl $0x5c, %eax
je 0xda60
cmpl $0x2f, %eax
jne 0xdbe8
movq (%rsp), %rax
movw $0x1, 0x4(%rax)
incq %r13
leaq 0x1(%r13), %rcx
xorl %eax, %eax
movl %r13d, %edx
movq %r13, %rsi
movzbl -0x1(%rcx), %edi
testl %edi, %edi
je 0xda8b
cmpl $0x5c, %edi
je 0xda8b
cmpl $0x2f, %edi
jne 0xdaf8
movl %edx, %r8d
subl %esi, %r8d
testl %r8d, %r8d
jle 0xdaf0
cmpl $0x1, %r8d
je 0xdaad
cmpl $0x2, %r8d
jne 0xdab9
cmpw $0x2e2e, (%rsi)         # imm = 0x2E2E
sete %r8b
jmp 0xdabc
cmpb $0x2e, (%rsi)
sete %r9b
xorl %r8d, %r8d
jmp 0xdabf
xorl %r8d, %r8d
xorl %r9d, %r9d
cmpl $0x40, %eax
je 0xdc25
movl %edx, %r10d
subl %esi, %r10d
subl %r13d, %esi
movslq %eax, %r11
imulq $0x6, %r11, %r11
movw %si, 0x10(%rsp,%r11)
movw %r10w, 0x12(%rsp,%r11)
movb %r8b, 0x14(%rsp,%r11)
movb %r9b, 0x15(%rsp,%r11)
incl %eax
testb %dil, %dil
je 0xdb02
movq %rcx, %rsi
incq %rcx
incl %edx
jmp 0xda79
movl %eax, %ecx
leaq 0x15(%rsp), %rdx
xorl %r8d, %r8d
movl %r8d, %esi
leal 0x1(%rcx), %r8d
imulq $0x6, %rcx, %r9
addq %rdx, %r9
decl %r8d
testl %r8d, %r8d
jle 0xdb57
leaq -0x6(%r9), %rdi
decq %rcx
cmpb $0x0, -0x6(%r9)
movq %rdi, %r9
jne 0xdb1a
movw $0x1, %r9w
cmpb $0x0, -0x1(%rdi)
jne 0xdb4b
movw $0xffff, %r9w           # imm = 0xFFFF
xorl %r8d, %r8d
testw %si, %si
je 0xdb0c
movswl %r9w, %r8d
addl %esi, %r8d
movb $0x1, (%rdi)
jmp 0xdb0c
movq (%rsp), %rcx
movw %si, 0x8(%rcx)
xorl %r12d, %r12d
testl %eax, %eax
cmovlel %r12d, %eax
addq $0x8a, %rcx
movq %rcx, 0x8(%rsp)
imulq $0x6, %rax, %rbp
xorl %r14d, %r14d
xorl %ebx, %ebx
cmpq %r12, %rbp
je 0xdbcd
cmpb $0x0, 0x15(%rsp,%r12)
jne 0xdbc7
cmpw $0x200, %bx             # imm = 0x200
jae 0xdc36
movzwl %bx, %edi
addq 0x8(%rsp), %rdi
movzwl 0x10(%rsp,%r12), %esi
addq %r13, %rsi
movzwl 0x12(%rsp,%r12), %r15d
movq %r15, %rdx
callq 0x3290
addl %r15d, %ebx
movzwl %r14w, %eax
movq (%rsp), %rcx
movw %bx, 0xa(%rcx,%rax,2)
incl %r14d
addq $0x6, %r12
jmp 0xdb7d
movq (%rsp), %rax
movw %r14w, 0x6(%rax)
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 0xda6d
cmpb $0x3a, 0x1(%r13)
jne 0xda6d
movzbl 0x2(%r13), %eax
cmpl $0x5c, %eax
je 0xdc16
cmpl $0x2f, %eax
jne 0xda6d
movq (%rsp), %rax
movw $0x3, 0x4(%rax)
jmp 0xda6d
leaq 0x71db(%rip), %rdi      # 0x14e07
pushq $0x40
popq %rsi
xorl %eax, %eax
callq 0x56cd
leaq 0x71aa(%rip), %rdi      # 0x14de7
movq %r13, %rsi
xorl %eax, %eax
callq 0x56cd
leaq 0x718b(%rip), %rdi      # 0x14dd9
xorl %eax, %eax
callq 0x56cd