// File: /workspace/github_c_makefile_star2_O1/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, %r15
movq %rdi, %r12
movl %edx, (%rdi)
movw $0x0, 0x4(%rdi)
cmpl $0x1, %edx
je 0xf220
testl %edx, %edx
jne 0xf41e
cmpb $0x2f, (%r15)
je 0xf232
jmp 0xf23d
movzbl (%r15), %eax
cmpl $0x5c, %eax
je 0xf232
cmpl $0x2f, %eax
jne 0xf3bf
movw $0x1, 0x4(%r12)
incq %r15
xorl %eax, %eax
movq %r15, %rdx
movq %r15, %rcx
movzbl (%rcx), %esi
testl %esi, %esi
je 0xf256
cmpl $0x5c, %esi
je 0xf256
cmpl $0x2f, %esi
jne 0xf2c1
movq %rcx, %rdi
subq %rdx, %rdi
testl %edi, %edi
jle 0xf2af
cmpl $0x2, %edi
jne 0xf270
cmpw $0x2e2e, (%rdx)         # imm = 0x2E2E
sete %r8b
jmp 0xf273
xorl %r8d, %r8d
cmpl $0x1, %edi
jne 0xf281
cmpb $0x2e, (%rdx)
sete %r9b
jmp 0xf284
xorl %r9d, %r9d
cmpl $0x40, %eax
je 0xf3fa
subl %r15d, %edx
movslq %eax, %r10
leaq (%r10,%r10,2), %r10
movw %dx, 0x10(%rsp,%r10,2)
movw %di, 0x12(%rsp,%r10,2)
movb %r8b, 0x14(%rsp,%r10,2)
movb %r9b, 0x15(%rsp,%r10,2)
incl %eax
xorl %edi, %edi
testb %sil, %sil
sete %r8b
leaq 0x1(%rcx), %rdx
testb %sil, %sil
je 0xf2c8
incq %rcx
xorl %edi, %edi
jmp 0xf2cd
movb %r8b, %dil
addl %edi, %edi
testl %edi, %edi
je 0xf245
movl %eax, %ecx
testl %eax, %eax
jle 0xf323
leaq (%rcx,%rcx,2), %rdx
leaq 0x1(%rcx), %rsi
leaq (%rsp,%rdx,2), %rdi
addq $0xf, %rdi
xorl %edx, %edx
cmpb $0x0, (%rdi)
jne 0xf314
movw $0x1, %r8w
cmpb $0x0, -0x1(%rdi)
jne 0xf307
movw $0xffff, %r8w           # imm = 0xFFFF
testw %dx, %dx
je 0xf314
movswl %r8w, %r8d
addl %edx, %r8d
movb $0x1, (%rdi)
movl %r8d, %edx
decq %rsi
addq $-0x6, %rdi
cmpq $0x1, %rsi
jg 0xf2ed
jmp 0xf325
xorl %edx, %edx
movw %dx, 0x8(%r12)
testl %eax, %eax
jle 0xf3a4
leaq 0x8a(%r12), %rax
movq %rax, 0x8(%rsp)
addq %rcx, %rcx
leaq (%rcx,%rcx,2), %rbp
xorl %ebx, %ebx
xorl %eax, %eax
xorl %r13d, %r13d
movq %r15, (%rsp)
cmpb $0x0, 0x15(%rsp,%rbx)
jne 0xf399
movzwl %ax, %r14d
cmpl $0x200, %r14d           # imm = 0x200
jae 0xf40d
movq %r14, %rdi
addq 0x8(%rsp), %rdi
movzwl 0x10(%rsp,%rbx), %esi
addq %r15, %rsi
movzwl 0x12(%rsp,%rbx), %r15d
movq %r15, %rdx
callq 0x3290
leal (%r15,%r14), %eax
movq (%rsp), %r15
movzwl %r13w, %ecx
movw %ax, 0xa(%r12,%rcx,2)
incl %r13d
addq $0x6, %rbx
cmpq %rbx, %rbp
jne 0xf34e
jmp 0xf3a7
xorl %r13d, %r13d
movw %r13w, 0x6(%r12)
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 0xf23d
cmpb $0x3a, 0x1(%r15)
jne 0xf23d
movzbl 0x2(%r15), %eax
cmpl $0x5c, %eax
je 0xf3ed
cmpl $0x2f, %eax
jne 0xf23d
movw $0x3, 0x4(%r12)
jmp 0xf23d
leaq 0x7a0c(%rip), %rdi      # 0x16e0d
movl $0x40, %esi
xorl %eax, %eax
callq 0x5bdd
leaq 0x79d9(%rip), %rdi      # 0x16ded
movq %r15, %rsi
xorl %eax, %eax
callq 0x5bdd
leaq 0x79ba(%rip), %rdi      # 0x16ddf
xorl %eax, %eax
callq 0x5bdd