// File: 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
movq %rsp, %rbp
subq $0x90, %rsp
movl %r8d, -0x74(%rbp)
movl %ecx, %eax
movl -0x74(%rbp), %ecx
movl %eax, -0x70(%rbp)
movl %edx, %eax
movl -0x70(%rbp), %edx
movl %eax, -0x6c(%rbp)
movq %rsi, %rax
movl -0x6c(%rbp), %esi
movq %rax, -0x68(%rbp)
movq %rdi, %r8
movq -0x68(%rbp), %rdi
movb %r9b, %al
movq %r8, -0x10(%rbp)
movq %rdi, -0x18(%rbp)
movl %esi, -0x1c(%rbp)
movl %edx, -0x20(%rbp)
andb $0x1, %cl
movb %cl, -0x21(%rbp)
andb $0x1, %al
movb %al, -0x22(%rbp)
movq -0x10(%rbp), %rax
movl 0x8d4c(%rax), %eax
movl %eax, -0x28(%rbp)
movl 0x36e01(%rip), %eax     # 0x42840
movl %eax, -0x2c(%rbp)
movq -0x18(%rbp), %rdi
movl -0x1c(%rbp), %eax
addl $0x1, %eax
cltq
shlq $0x3, %rax
addq %rax, %rdi
movl -0x20(%rbp), %esi
movl -0x1c(%rbp), %eax
addl $0x1, %eax
subl %eax, %esi
movq -0x10(%rbp), %rdx
callq 0xbc50
cmpl $0x0, %eax
je 0xba7a
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xbc38
movl -0x2c(%rbp), %eax
movl %eax, 0x36dbd(%rip)     # 0x42840
movq -0x10(%rbp), %rax
movl 0x8d4c(%rax), %edi
leaq 0x36ddc(%rip), %rax     # 0x42870
movl (%rax), %esi
movl $0x1, %edx
callq 0xa7c0
cmpl $0x0, %eax
je 0xbab1
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xbc38
movl -0x28(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x8d4c(%rax)
movq -0x18(%rbp), %rax
movl -0x1c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movl %eax, -0x30(%rbp)
movslq -0x30(%rbp), %rsi
leaq 0x36d93(%rip), %rdi     # 0x42870
addq $0x28, %rdi
callq 0x17e60
movq 0x28(%rax), %rax
andq $0x2, %rax
cmpq $0x0, %rax
jne 0xbb56
movl -0x30(%rbp), %edx
leaq 0x36d72(%rip), %rsi     # 0x42870
leaq -0x50(%rbp), %rdi
movq %rdi, -0x80(%rbp)
callq 0x1f080
movq -0x80(%rbp), %rdi
callq 0x41b0
movq %rax, %rsi
leaq 0x26679(%rip), %rdi     # 0x32197
xorl %eax, %eax
callq 0x1bac0
jmp 0xbb27
leaq -0x50(%rbp), %rdi
callq 0x4270
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xbc38
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x4270
jmp 0xbc44
testb $0x1, -0x21(%rbp)
je 0xbbd5
movl $0x100, %edi            # imm = 0x100
callq 0x265a0
cmpl $0x0, %eax
je 0xbbd5
cmpl $0x2e, 0x3687e(%rip)    # 0x423f0
jl 0xbbd5
movslq -0x30(%rbp), %rsi
leaq 0x36cf1(%rip), %rdi     # 0x42870
addq $0x28, %rdi
callq 0x17e60
movq 0x28(%rax), %rax
andq $0x40000, %rax          # imm = 0x40000
cmpq $0x0, %rax
jne 0xbbd3
movq -0x10(%rbp), %rax
movq %rax, -0x88(%rbp)
movslq -0x30(%rbp), %rsi
leaq 0x36cc2(%rip), %rdi     # 0x42870
addq $0x28, %rdi
callq 0x17e60
movq -0x88(%rbp), %rdi
movq 0x48(%rax), %rax
movl %eax, %ecx
movl $0x2e, %esi
movl $0x3, %edx
callq 0x25240
jmp 0xbbd5
testb $0x1, -0x22(%rbp)
je 0xbc31
movslq -0x30(%rbp), %rsi
leaq 0x36c8a(%rip), %rdi     # 0x42870
addq $0x28, %rdi
callq 0x17e60
cmpq $0x1, 0x38(%rax)
je 0xbc31
movq -0x10(%rbp), %rax
movq %rax, -0x90(%rbp)
movslq -0x30(%rbp), %rsi
leaq 0x36c64(%rip), %rdi     # 0x42870
addq $0x28, %rdi
callq 0x17e60
movq -0x90(%rbp), %rdi
movq 0x38(%rax), %rax
movl %eax, %ecx
movl $0x20, %esi
movl $0x3, %edx
callq 0x25240
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
movq -0x58(%rbp), %rdi
callq 0x4670
nopl (%rax)