// File: /workspace/github_cpp_makefile_star2_O2/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 0x8d05
movq %rcx, %r14
movl %edx, %ebp
movq %rsi, %r15
movq %rdi, %rbx
movq 0x8(%rsi), %rax
movl 0x8(%rax), %esi
leaq 0x1caca(%rip), %r13     # 0x256f0
movq %r13, %rdi
callq 0xfe60
cmpl $0x7, %eax
jne 0x8d05
movq 0x8(%r15), %rsi
movl (%r15), %edx
pushq $0x1
popq %rdi
callq 0x77e1
movl %eax, %r12d
movl (%r15), %eax
decl %eax
cmpl %eax, %r12d
jne 0x8c90
movq 0x8(%r15), %rax
movq (%rax), %rdi
movq 0x28(%r13), %rax
imulq $0xb0, %rdi, %rcx
movq 0x28(%rax,%rcx), %rax
btl $0x9, %eax
jb 0x8ca0
btl $0x8, %eax
jb 0x8cb2
callq 0x8d7d
testl %eax, %eax
sete %al
movl %eax, 0x4(%rsp)
movb $0x1, %r13b
movl $0x0, (%rsp)
jmp 0x8cc0
leaq 0x1056f(%rip), %rdi     # 0x19206
xorl %eax, %eax
callq 0xf258
jmp 0x8d01
movb $0x1, %al
movl %eax, (%rsp)
movl $0x0, 0x4(%rsp)
xorl %r13d, %r13d
jmp 0x8cc0
movb $0x1, %al
movl %eax, (%rsp)
xorl %r13d, %r13d
movb $0x1, %al
movl %eax, 0x4(%rsp)
testb %bpl, %bpl
je 0x8cd0
pushq $0x3
popq %rsi
movq %rbx, %rdi
callq 0x11ed2
cmpb $0x1, (%r14)
jne 0x8ce1
pushq $0x5
popq %rsi
movq %rbx, %rdi
callq 0x11ed2
movq 0x8(%r15), %rsi
movzbl 0x4(%rsp), %r8d
movzbl %r13b, %r9d
pushq $0x1
popq %rdx
movq %rbx, %rdi
movl %r12d, %ecx
callq 0x7a0b
testl %eax, %eax
je 0x8d17
pushq $-0x1
popq %r12
movl %r12d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpb $0x1, (%r14)
jne 0x8d3c
pushq $0x5
popq %rsi
movq %rbx, %rdi
callq 0x11eea
cmpb $0x0, (%rsp)
jne 0x8d42
pushq $0xb
popq %rsi
pushq $0x5
popq %rdx
pushq $0x3
popq %rcx
movq %rbx, %rdi
jmp 0x8d5f
cmpb $0x0, (%rsp)
je 0x8d54
pushq $0x3
popq %rdx
pushq $0x7
popq %rcx
movq %rbx, %rdi
movl %edx, %esi
callq 0x11e6a
jmp 0x8d68
pushq $0x3
popq %rdx
pushq $0x5
popq %rcx
movq %rbx, %rdi
movl %edx, %esi
callq 0x11e6a
movb $0x1, (%r14)
xorl %r12d, %r12d
testb %bpl, %bpl
je 0x8d05
pushq $0x3
popq %rsi
movq %rbx, %rdi
callq 0x11eea
jmp 0x8d05