// File: /workspace/github_cpp_makefile_star2_O1/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:
cmpl $0x2, (%rsi)
jl 0x97dc
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rcx, %r14
movl %edx, %ebp
movq %rsi, %r15
movq %rdi, %rbx
movq 0x8(%rsi), %rax
movl 0x8(%rax), %esi
leaq 0x1bffa(%rip), %r13     # 0x256e0
movq %r13, %rdi
callq 0x111aa
cmpl $0x7, %eax
jne 0x984b
movq 0x8(%r15), %rsi
movl (%r15), %edx
movl $0x1, %edi
callq 0x803d
movl %eax, %r12d
movl (%r15), %eax
decl %eax
cmpl %eax, %r12d
jne 0x97df
movq 0x8(%r15), %rax
movq (%rax), %rdi
movq 0x28(%r13), %rax
imulq $0xb0, %rdi, %rcx
movq 0x28(%rax,%rcx), %rax
btl $0x9, %eax
setae %r13b
movq %rax, (%rsp)
testl $0x300, %eax            # imm = 0x300
jne 0x974e
callq 0x985c
testl %eax, %eax
sete %r13b
testb %bpl, %bpl
je 0x9760
movq %rbx, %rdi
movl $0x3, %esi
callq 0x13684
cmpb $0x1, (%r14)
jne 0x9773
movq %rbx, %rdi
movl $0x5, %esi
callq 0x13684
xorl %r9d, %r9d
testl $0x300, (%rsp)          # imm = 0x300
sete %r9b
movq 0x8(%r15), %rsi
movzbl %r13b, %r8d
movq %rbx, %rdi
movl $0x1, %edx
movl %r12d, %ecx
callq 0x82e9
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testl %ecx, %ecx
jne 0x984d
cmpb $0x1, (%r14)
jne 0x97f4
movq %rbx, %rdi
movl $0x5, %esi
callq 0x136b2
movq (%rsp), %r15
testl $0x300, %r15d           # imm = 0x300
jne 0x9801
movq %rbx, %rdi
movl $0xb, %esi
movl $0x5, %edx
movl $0x3, %ecx
jmp 0x9827
xorl %eax, %eax
retq
leaq 0x11a40(%rip), %rdi     # 0x1b226
xorl %eax, %eax
callq 0x10418
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x984d
movq (%rsp), %r15
testl $0x300, %r15d           # imm = 0x300
je 0x9815
movq %rbx, %rdi
movl $0x3, %esi
movl $0x3, %edx
movl $0x7, %ecx
jmp 0x9827
movq %rbx, %rdi
movl $0x3, %esi
movl $0x3, %edx
movl $0x5, %ecx
callq 0x1361c
testl $0x300, %r15d           # imm = 0x300
jne 0x9839
movb $0x1, (%r14)
testb %bpl, %bpl
je 0x984b
movq %rbx, %rdi
movl $0x3, %esi
callq 0x136b2
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq