// File: /workspace/github_cpp_makefile_star2_O3/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, %r14
movl 0x8d4c(%rdi), %ebx
movl 0x1d53e(%rip), %r15d    # 0x256b0
movslq %edx, %rbp
leaq (%rsi,%rbp,8), %rdi
addq $0x8, %rdi
movl %ebp, %esi
notl %esi
addl %ecx, %esi
movq %r14, %rdx
callq 0x82d3
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x81bf
movl %r15d, 0x1d515(%rip)    # 0x256b0
movl 0x8d4c(%r14), %edi
leaq 0x1d537(%rip), %r15     # 0x256e0
movl (%r15), %esi
movl $0x1, %edx
callq 0x75b3
testl %eax, %eax
je 0x81d0
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl %edx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %ebx, 0x8d4c(%r14)
movq -0x8(%r12,%rbp,8), %rdx
movslq %edx, %rax
movq 0x28(%r15), %rcx
imulq $0xb0, %rax, %r12
testb $0x2, 0x28(%rcx,%r12)
jne 0x8234
leaq 0x1d4e7(%rip), %rsi     # 0x256e0
leaq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x11e2c
movq (%r14), %rsi
leaq 0x12ff3(%rip), %rdi     # 0x1b203
xorl %eax, %eax
callq 0x102f4
leaq 0x18(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x81ba
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3320
jmp 0x81ba
testb %r13b, %r13b
je 0x8274
movl $0x100, %edi            # imm = 0x100
callq 0x13f2c
testl %eax, %eax
je 0x8274
cmpl $0x2e, 0x1d0b2(%rip)    # 0x25300
jl 0x8274
movq 0x28(%r15), %rax
testb $0x4, 0x2a(%rax,%r12)
jne 0x8274
addq %r12, %rax
movl 0x48(%rax), %ecx
movq %r14, %rdi
movl $0x2e, %esi
movl $0x3, %edx
callq 0x1348e
xorl %edx, %edx
cmpb $0x0, 0x4(%rsp)
je 0x81bf
movq 0x28(%r15), %rax
movq 0x38(%rax,%r12), %rcx
cmpq $0x1, %rcx
je 0x81bf
movq %r14, %rdi
movl $0x20, %esi
movl $0x3, %edx
callq 0x1348e
xorl %edx, %edx
jmp 0x81bf
movq %rax, %rbx
leaq 0x18(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x82cb
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3320
movq %rbx, %rdi
callq 0x3560