// File: /workspace/github_cpp_makefile_star2_O3/rofl0r[P]ascc/cs_parser.cpp
// Function: parseArrayIndexOffsets(ccCompiledScript*, VariableSymlist*, bool, bool*)
int parseArrayIndexOffsets(ccCompiledScript *scrip, VariableSymlist *thisClause, bool writingOperation, bool *isArrayOffset) {

  if ((thisClause->len > 1) &&
      (sym.get_type(thisClause->syml[1]) == SYM_OPENBRACKET)) {
    // An array
    // find where the brackets end
    int arrIndexEnd = findClosingBracketOffs(1, thisClause->syml, thisClause->len);
    if (arrIndexEnd != thisClause->len - 1) {
      cc_error("Error parsing path; unexpected token after array index");
      return -1;
    }

    bool propertyIndexer = false;
    bool checkBounds = true, multiplySize = true;

    if ((sym.entries[thisClause->syml[0]].flags & SFLG_PROPERTY) ||
        (sym.entries[thisClause->syml[0]].flags & SFLG_POINTER)) {
      // an array property, or array of pointers; in this case,
      // don't touch CX but just calculate the index value into DX
      propertyIndexer = true;
      multiplySize = false;
      // don't check bounds, the property getter will do that
      if (sym.entries[thisClause->syml[0]].flags & SFLG_PROPERTY)
        checkBounds = false;
    }
    // a char pointer has no known boundaries
    else if(isRawPointer(thisClause->syml[0]))
      checkBounds = false;

    // the value to write is in AX; preserve it
    if (writingOperation)
      scrip->push_reg(SREG_AX);

    // save the current offset in CX if there is one,
    // because parse_sub_expr might destroy it
    if (*isArrayOffset)
      scrip->push_reg(SREG_CX);

    // get the byte offset of the array index into AX
    if (get_array_index_into_ax(scrip, thisClause->syml, 1, arrIndexEnd, checkBounds, multiplySize))
      return -1;

    // if there is a current offset saved in CX, restore it
    // then add the result to CX (which is counting the overall offset)
    if (*isArrayOffset) {
      scrip->pop_reg(SREG_CX);

      if (propertyIndexer)
        scrip->write_cmd2(SCMD_REGTOREG, SREG_AX, SREG_DX);
      else
        scrip->write_cmd2(SCMD_ADDREG, SREG_CX, SREG_AX);
    }
    else if (propertyIndexer)
      scrip->write_cmd2(SCMD_REGTOREG, SREG_AX, SREG_DX);
    else
      scrip->write_cmd2(SCMD_REGTOREG, SREG_AX, SREG_CX);

    if (!propertyIndexer)
      *isArrayOffset = true;

    if (writingOperation)
      scrip->pop_reg(SREG_AX);

    // the array offset has now been added to CX (normal array)
    // or put into DX (property)
  }

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
xorl %r12d, %r12d
cmpl $0x2, (%rsi)
jl 0x96e5
movq %rcx, %r14
movl %edx, %ebp
movq %rsi, %r15
movq %rdi, %rbx
movq 0x8(%rsi), %rax
movl 0x8(%rax), %esi
leaq 0x1c169(%rip), %r13     # 0x256e0
movq %r13, %rdi
callq 0x1106e
cmpl $0x7, %eax
jne 0x96e5
movq 0x8(%r15), %rsi
movl (%r15), %edx
movl $0x1, %edi
callq 0x7f04
movl %eax, %r12d
movl (%r15), %eax
decl %eax
cmpl %eax, %r12d
jne 0x95e3
movq 0x8(%r15), %rax
movq (%rax), %rdi
movq 0x28(%r13), %rax
imulq $0xb0, %rdi, %rcx
movq 0x28(%rax,%rcx), %rax
btl $0x9, %eax
jb 0x95fc
btl $0x8, %eax
jb 0x960e
callq 0x96f7
testl %eax, %eax
sete %al
movl %eax, 0x4(%rsp)
movb $0x1, %r13b
movl $0x0, (%rsp)
jmp 0x961c
leaq 0x11c3c(%rip), %rdi     # 0x1b226
xorl %eax, %eax
callq 0x102f4
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
jmp 0x96e5
movb $0x1, %al
movl %eax, (%rsp)
movl $0x0, 0x4(%rsp)
xorl %r13d, %r13d
jmp 0x961c
movb $0x1, %al
movl %eax, (%rsp)
xorl %r13d, %r13d
movb $0x1, %al
movl %eax, 0x4(%rsp)
testb %bpl, %bpl
je 0x962e
movq %rbx, %rdi
movl $0x3, %esi
callq 0x134f6
cmpb $0x1, (%r14)
jne 0x9641
movq %rbx, %rdi
movl $0x5, %esi
callq 0x134f6
movq 0x8(%r15), %rsi
movzbl 0x4(%rsp), %r8d
movzbl %r13b, %r9d
movq %rbx, %rdi
movl $0x1, %edx
movl %r12d, %ecx
callq 0x8149
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x96e5
cmpb $0x1, (%r14)
jne 0x9696
movq %rbx, %rdi
movl $0x5, %esi
callq 0x13524
cmpb $0x0, (%rsp)
jne 0x969c
movq %rbx, %rdi
movl $0xb, %esi
movl $0x5, %edx
movl $0x3, %ecx
jmp 0x96c7
cmpb $0x0, (%rsp)
je 0x96b5
movq %rbx, %rdi
movl $0x3, %esi
movl $0x3, %edx
movl $0x7, %ecx
callq 0x1348e
jmp 0x96d0
movq %rbx, %rdi
movl $0x3, %esi
movl $0x3, %edx
movl $0x5, %ecx
callq 0x1348e
movb $0x1, (%r14)
xorl %r12d, %r12d
testb %bpl, %bpl
je 0x96e5
movq %rbx, %rdi
movl $0x3, %esi
callq 0x13524
movl %r12d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq