// File: /workspace/github_cpp_makefile_star2_O2/rofl0r[P]ascc/cs_parser.cpp
// Function: get_array_index_into_ax(ccCompiledScript*, long*, int, int, bool, bool)
int get_array_index_into_ax(ccCompiledScript *scrip, long *symlist, int openBracketOffs, int closeBracketOffs, bool checkBounds, bool multiplySize) {

  // "push" the ax val type (because this is just an array index,
  // we're actually interested in the type of the variable being read)
  int axValTypeWas = scrip->ax_val_type;

  // save the size of the array element, so it doesn't get
  // overwritten by the size of the array index variable
  int saveOldReadcmd = readcmd_lastcalledwith;
  // parse expression inside brackets to return the array index in AX
  if (parse_sub_expr(&symlist[openBracketOffs + 1], closeBracketOffs - (openBracketOffs + 1), scrip))
    return -1;
  readcmd_lastcalledwith = saveOldReadcmd;

  // array index must be an int
  if (check_type_mismatch(scrip->ax_val_type, sym.normalIntSym, 1))
    return -1;

  // "pop" the ax val type
  scrip->ax_val_type = axValTypeWas;

  int arrSym = symlist[openBracketOffs - 1];

  if ((sym.entries[arrSym].flags & SFLG_ARRAY) == 0) {
    cc_error("Internal error: not an array: '%s'", sym.get_friendly_name(arrSym).c_str());
    return -1;
  }

  if (checkBounds && ccGetOption(SCOPT_CHECKBOUNDS) && compiler_max_command >= SCMD_CHECKBOUNDS) {
    // check the array bounds that have been calculated in AX,
    // before they are added to the overall offset
    if ((sym.entries[arrSym].flags & SFLG_DYNAMICARRAY) == 0)
    {
      scrip->write_cmd2(SCMD_CHECKBOUNDS, SREG_AX, sym.entries[arrSym].arrsize);
    }
  }

  if (multiplySize && sym.entries[arrSym].ssize != 1) {
    // multiply up array index (in AX) by size of array element
    // to get memory offset
    scrip->write_cmd2(SCMD_MUL, SREG_AX, sym.entries[arrSym].ssize);
  }

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %r9d, 0x4(%rsp)
movl %r8d, %r13d
movq %rsi, %r12
movq %rdi, %rbx
movl 0x8d4c(%rdi), %ebp
movl 0x1dc8c(%rip), %r15d    # 0x256c0
movslq %edx, %r14
leaq (%rsi,%r14,8), %rdi
addq $0x8, %rdi
movl %r14d, %esi
notl %esi
addl %ecx, %esi
movq %rbx, %rdx
callq 0x7b57
pushq $-0x1
popq %rdx
testl %eax, %eax
jne 0x7acb
movl %r15d, 0x1dc64(%rip)    # 0x256c0
movl 0x8d4c(%rbx), %edi
leaq 0x1dc87(%rip), %r15     # 0x256f0
movl (%r15), %esi
pushq $0x1
popq %rdx
callq 0x6faa
testl %eax, %eax
jne 0x7ac8
movl %ebp, 0x8d4c(%rbx)
movq -0x8(%r12,%r14,8), %rdx
movslq %edx, %rax
movq 0x28(%r15), %rcx
imulq $0xb0, %rax, %r12
testb $0x2, 0x28(%rcx,%r12)
jne 0x7adc
leaq 0x1dc50(%rip), %rsi     # 0x256f0
leaq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x10864
movq (%rbx), %rsi
leaq 0x1172c(%rip), %rdi     # 0x191e3
xorl %eax, %eax
callq 0xf258
leaq 0x8(%rsp), %rdi
callq 0x31a0
pushq $-0x1
popq %rdx
movl %edx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testb %r13b, %r13b
je 0x7b18
movl $0x100, %edi            # imm = 0x100
callq 0x127df
testl %eax, %eax
je 0x7b18
cmpl $0x2e, 0x1d80a(%rip)    # 0x25300
jl 0x7b18
movq 0x28(%r15), %rax
testb $0x4, 0x2a(%rax,%r12)
jne 0x7b18
addq %r12, %rax
movl 0x48(%rax), %ecx
pushq $0x2e
popq %rsi
pushq $0x3
popq %rdx
movq %rbx, %rdi
callq 0x11e6a
xorl %edx, %edx
cmpb $0x0, 0x4(%rsp)
je 0x7acb
movq 0x28(%r15), %rax
movq 0x38(%rax,%r12), %rcx
cmpq $0x1, %rcx
je 0x7acb
pushq $0x20
popq %rsi
pushq $0x3
popq %rdx
movq %rbx, %rdi
callq 0x11e6a
xorl %edx, %edx
jmp 0x7acb
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x31a0
movq %rbx, %rdi
callq 0x3560