// File: /workspace/github_cpp_makefile_star2_O3/rofl0r[P]ascc/cs_parser.cpp
// Function: do_variable_memory_access(ccCompiledScript*, int, int, bool, int, int, bool, bool, int, bool, bool, int, int, bool, bool, int)
int do_variable_memory_access(ccCompiledScript *scrip, int variableSym,
                              int variableSymType, bool isProperty,
                              int writing, int mustBeWritable,
                              bool addressof, bool extraoffset,
                              int soffset, bool isPointer,
                              bool wholePointerAccess,
                              int mainVariableSym, int mainVariableType,
                              bool isDynamicArray, bool negateLiteral,
                              int extra_struct_offset) {
  int gotValType = 0;
  int readcmd = get_readcmd_for_size(sym.entries[variableSym].ssize, writing);

  if (mainVariableType == SYM_VARTYPE) {
    // it's a static member property
    if (!isProperty) {
      cc_error("static non-property access: internal error");
      return -1;
    }
    // just write 0 to AX for ease of debugging if anything
    // goes wrong
    scrip->write_cmd2(SCMD_LITTOREG, SREG_AX, 0);

    gotValType = sym.entries[variableSym].vartype;
    if (sym.entries[variableSym].flags & SFLG_CONST)
      gotValType |= STYPE_CONST;
  }
  else if ((mainVariableType == SYM_LITERALVALUE) || (mainVariableType == SYM_CONSTANT)) {
    if ((writing) || (mustBeWritable)) {
      if(mainVariableType == SYM_LITERALVALUE)
        cc_error("cannot write to a literal value");
      else
        cc_error("cannot write to constant");
      return -1;
    }
    int varSymValue;
    if (accept_literal_or_constant_value(variableSym, varSymValue, negateLiteral, "Error parsing integer value") < 0) {
      return -1;
    }
    if(negateLiteral && varSymValue < 0 && ccGetOption(SCOPT_OLDNEGATE)!=0) {
      scrip->write_cmd2(SCMD_LITTOREG, SREG_AX, varSymValue*-1);
      scrip->write_cmd2(SCMD_LITTOREG, SREG_BX, 0);
      scrip->write_cmd2(SCMD_SUBREG, SREG_BX, SREG_AX);
      scrip->write_cmd2(SCMD_REGTOREG, SREG_BX, SREG_AX);
    } else {
      scrip->write_cmd2(SCMD_LITTOREG,SREG_AX, varSymValue);
    }
    gotValType = sym.normalIntSym;
  }
  else if (mainVariableType == SYM_LITERALFLOAT) {
    if ((writing) || (mustBeWritable)) {
      cc_error("cannot write to a literal value");
      return -1;
    }
    scrip->write_cmd2(SCMD_LITTOREG, SREG_AX, float_to_int_raw((float)atof(sym.get_name(variableSym))));
    gotValType = sym.normalFloatSym;
  }
  else if ((mainVariableType == SYM_LOCALVAR) ||
           (mainVariableType == SYM_GLOBALVAR)) {

    gotValType = sym.entries[variableSym].vartype;
    if (sym.entries[variableSym].flags & SFLG_CONST)
      gotValType |= STYPE_CONST;

    // a "normal" variable
    if (isPointer) {
      // the address is already in MAR by the caller
      if ((!wholePointerAccess) && ((!addressof) || (soffset) || (extraoffset)))
        scrip->write_cmd(SCMD_CHECKNULL);
      if (soffset)
        scrip->write_cmd2(SCMD_ADD, SREG_MAR, soffset);
    }
    else if (mainVariableType == SYM_LOCALVAR) {
      // a local one
      load_stack_variable(scrip, scrip->cur_sp - soffset);
    }
    else {
      // global variable
      if (sym.entries[mainVariableSym].flags & SFLG_IMPORTED) {
        // imported variable, so get the import address and then add any offset
        scrip->write_cmd2(SCMD_LITTOREG,SREG_MAR,sym.entries[mainVariableSym].soffs);
        scrip->fixup_previous(FIXUP_IMPORT);
        if (soffset != 0)
          scrip->write_cmd2(SCMD_ADD,SREG_MAR,soffset);
      }
      else {
        scrip->write_cmd2(SCMD_LITTOREG,SREG_MAR,soffset);
        scrip->fixup_previous(FIXUP_GLOBALDATA);
      }
    }

    if (extraoffset && !isRawPointer(variableSym))
    {
      if (isDynamicArray)
      {
        scrip->write_cmd1(SCMD_MEMREADPTR, SREG_MAR);
        scrip->write_cmd(SCMD_CHECKNULL);
        scrip->write_cmd1(SCMD_DYNAMICBOUNDS, SREG_CX);
      }

      scrip->write_cmd2(SCMD_ADDREG,SREG_MAR,SREG_CX);
    }
    else if (isDynamicArray)
    {
      // not accessing an element of it, must be whole thing
      wholePointerAccess = true;
      gotValType |= STYPE_DYNARRAY;
    }

    if (extra_struct_offset)
      scrip->write_cmd2(SCMD_ADD, SREG_MAR, extra_struct_offset);

    if ((wholePointerAccess) && (writing))
      scrip->write_cmd1(SCMD_MEMWRITEPTR, SREG_AX);
    else if ((wholePointerAccess) && (!writing))
      scrip->write_cmd1(SCMD_MEMREADPTR, SREG_AX);
    else if (addressof)
      scrip->write_cmd2(SCMD_REGTOREG,SREG_MAR,SREG_AX);
    else if(isRawPointer(variableSym)) {
      if (extraoffset) {
        // load pointer value into mar
        scrip->write_cmd1(SCMD_MEMREAD, SREG_MAR);
        // add offset
        scrip->write_cmd2(SCMD_ADDREG,SREG_MAR,SREG_CX);
        // read pointed-to value
        scrip->write_cmd1(readcmd,SREG_AX);
      } else {
        // put pointer value into AX
        scrip->write_cmd1(writing ? SCMD_MEMWRITE : SCMD_MEMREAD, SREG_AX);
      }
    } else
      scrip->write_cmd1(readcmd,SREG_AX);
    }
  else if (mainVariableType == SYM_STRING) {
    if (writing) {
      cc_error("cannot write to a literal string");
      return -1;
    }

    scrip->write_cmd2(SCMD_LITTOREG,SREG_AX,soffset);
    scrip->fixup_previous(FIXUP_STRING);
    gotValType = sym.normalStringSym | STYPE_CONST;
  }
  else if (mainVariableType == SYM_STRUCTMEMBER) {
    cc_error("must include parent structure of member '%s'",sym.get_friendly_name(mainVariableSym).c_str());
    return -1;
    }
  else if (mainVariableType == SYM_NULL) {
    if (writing) {
      cc_error("Invalid use of null");
      return -1;
    }
    scrip->write_cmd2(SCMD_LITTOREG, SREG_AX, 0);
    gotValType = sym.nullSym | STYPE_POINTER;
  }
  else {
    cc_error("read/write ax called with non-variable parameter ('%s')",sym.get_friendly_name(variableSym).c_str());
    return -1;
    }

  if ((addressof) && (!isProperty))
    gotValType |= STYPE_POINTER;
  else if ((isProperty) && (sym.entries[variableSym].flags & SFLG_POINTER))
    gotValType |= STYPE_POINTER;

  if (writing) {
    if (check_type_mismatch(scrip->ax_val_type, gotValType, 1))
      return -1;
  }
  else
    scrip->ax_val_type = gotValType;

  if (isProperty) {
    // process_arrays_and_members will have set addressOf to true,
    // so AX now contains the struct address, and BX
    // contains the new value if this is a Set
    if (call_property_func(scrip, variableSym, writing))
      return -1;
  }

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %r8d, %ebp
movl %esi, %ebx
movq %rdi, %r14
movl 0x90(%rsp), %eax
movslq %esi, %rdx
leaq 0x1bd69(%rip), %r13     # 0x256e0
movq 0x28(%r13), %r10
imulq $0xb0, %rdx, %r15
movl 0x38(%r10,%r15), %edx
testl %edx, %edx
je 0x9991
movl %edx, 0x1bd1f(%rip)     # 0x256b0
cmpl $0x2, %edx
movl $0x19, %esi
movl $0x7, %edi
cmovel %esi, %edi
movl $0x1b, %esi
movl $0x8, %r8d
cmovel %esi, %r8d
cmpl $0x1, %edx
movl $0x18, %edx
cmovnel %edi, %edx
movl $0x1a, %r12d
cmovnel %r8d, %r12d
testl %ebp, %ebp
cmovel %edx, %r12d
cmpl $0x2e, %eax
jne 0x9a1e
testb %cl, %cl
je 0x9a42
movl %ecx, 0x4(%rsp)
movq %r14, %rdi
movl $0x6, %esi
movl $0x3, %edx
xorl %ecx, %ecx
callq 0x1348e
movq 0x28(%r13), %rax
movswl 0x30(%rax,%r15), %ecx
movl 0x28(%rax,%r15), %r13d
shll $0xd, %r13d
andl $0x20000000, %r13d      # imm = 0x20000000
orl %ecx, %r13d
movl 0x28(%rax,%r15), %eax
shll $0x16, %eax
andl $0x40000000, %eax       # imm = 0x40000000
orl %eax, %r13d
jmp 0x9e5d
movb 0x60(%rsp), %sil
leal -0x13(%rax), %edx
cmpl $0x1, %edx
ja 0x9a4b
orl %ebp, %r9d
je 0x9b0e
cmpl $0x13, %eax
je 0x9a59
leaq 0x118b7(%rip), %rdi     # 0x1b2f7
jmp 0x9a60
leaq 0x11863(%rip), %rdi     # 0x1b2ac
jmp 0x9a60
cmpl $0x16, %eax
jne 0x9a71
orl %ebp, %r9d
je 0x9be9
leaq 0x11877(%rip), %rdi     # 0x1b2d7
xorl %eax, %eax
callq 0x102f4
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x9ea9
movl 0x88(%rsp), %edx
movl 0x70(%rsp), %r8d
leal -0x1(%rax), %esi
cmpl $0x1, %esi
ja 0x9bba
movl %ecx, 0x4(%rsp)
leaq (%r10,%r15), %rsi
movswl 0x30(%rsi), %edi
movl 0x28(%rsi), %r13d
shll $0xd, %r13d
andl $0x20000000, %r13d      # imm = 0x20000000
cmpb $0x0, 0x78(%rsp)
je 0x9c4c
cmpb $0x0, 0x80(%rsp)
jne 0x9aed
testl %r8d, %r8d
setne %al
movb 0x60(%rsp), %cl
xorb $0x1, %cl
orb 0x68(%rsp), %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x9d70
movl %edi, (%rsp)
movq %r14, %rdi
movl $0x34, %esi
callq 0x1340c
movl (%rsp), %edi
movl 0x70(%rsp), %r8d
testl %r8d, %r8d
je 0x9d70
movl %edi, (%rsp)
movq %r14, %rdi
movl $0x1, %esi
movl $0x2, %edx
movl %r8d, %ecx
jmp 0x9d68
movl %ecx, 0x4(%rsp)
movb 0xa0(%rsp), %r13b
movzbl %r13b, %edx
leaq 0x117eb(%rip), %rcx     # 0x1b310
leaq 0x8(%rsp), %rsi
movl %ebx, %edi
callq 0x601e
testl %eax, %eax
js 0x9a67
movl 0x8(%rsp), %r12d
testl %r12d, %r12d
sets %al
andb %al, %r13b
cmpb $0x1, %r13b
jne 0x9c28
movl $0x400, %edi            # imm = 0x400
callq 0x13f2c
testl %eax, %eax
je 0x9c28
negl %r12d
movq %r14, %rdi
movl $0x6, %esi
movl $0x3, %edx
movl %r12d, %ecx
callq 0x1348e
movq %r14, %rdi
movl $0x6, %esi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x1348e
movq %r14, %rdi
movl $0xc, %esi
movl $0x4, %edx
movl $0x3, %ecx
callq 0x1348e
movq %r14, %rdi
movl $0x3, %esi
movl $0x4, %edx
movl $0x3, %ecx
jmp 0x9c38
cmpl $0xa, %eax
je 0x9c7f
cmpl $0x15, %eax
je 0x9c6b
cmpl $0x12, %eax
jne 0x9cdf
testl %ebp, %ebp
je 0x9f87
leaq 0x11748(%rip), %rdi     # 0x1b32c
jmp 0x9a60
movl %ecx, 0x4(%rsp)
movq %r13, %rdi
movl %esi, %r12d
movl %ebx, %esi
callq 0x124c6
movq %rax, %rdi
callq 0x3410
cvtsd2ss %xmm0, %xmm0
movd %xmm0, %ecx
movq %r14, %rdi
movl $0x6, %esi
movl $0x3, %edx
callq 0x1348e
movl %r12d, %eax
movl 0x14(%r13), %r13d
jmp 0x9e37
movq %r14, %rdi
movl $0x6, %esi
movl $0x3, %edx
movl %r12d, %ecx
callq 0x1348e
leaq 0x1ba9c(%rip), %rax     # 0x256e0
movl (%rax), %r13d
jmp 0x9e33
cmpl $0x2, %eax
movl %edi, (%rsp)
jne 0x9ca6
movl 0x8d40(%r14), %esi
subl %r8d, %esi
movq %r14, %rdi
callq 0x5216
jmp 0x9d6d
testl %ebp, %ebp
je 0x9f5d
leaq 0x11700(%rip), %rdi     # 0x1b37a
jmp 0x9a60
leaq 0x1ba5a(%rip), %rsi     # 0x256e0
leaq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x11e2c
movq (%rbx), %rsi
leaq 0x116b0(%rip), %rdi     # 0x1b34d
xorl %eax, %eax
callq 0x102f4
jmp 0x9d06
movslq %edx, %rax
imulq $0xb0, %rax, %rax
testb $0x4, 0x28(%r10,%rax)
jne 0x9d2a
movq %r14, %rdi
movl $0x6, %esi
movl $0x2, %edx
movl %r8d, %ecx
callq 0x1348e
movq %r14, %rdi
movl $0x1, %esi
callq 0x137c6
jmp 0x9d6d
leaq 0x1b9fa(%rip), %rsi     # 0x256e0
leaq 0x8(%rsp), %r14
movq %r14, %rdi
movl %ebx, %edx
callq 0x11e2c
movq (%r14), %rsi
leaq 0x1168f(%rip), %rdi     # 0x1b38e
xorl %eax, %eax
callq 0x102f4
leaq 0x18(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x9a67
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3320
jmp 0x9a67
addq %rax, %r10
movl 0x34(%r10), %ecx
movq %r14, %rdi
movl $0x6, %esi
movl $0x2, %edx
callq 0x1348e
movq %r14, %rdi
movl $0x4, %esi
callq 0x137c6
movl 0x70(%rsp), %ecx
testl %ecx, %ecx
movl (%rsp), %edi
je 0x9d70
movq %r14, %rdi
movl $0x1, %esi
movl $0x2, %edx
callq 0x1348e
movl (%rsp), %edi
movl 0xa8(%rsp), %r8d
movb 0x98(%rsp), %dl
orl %edi, %r13d
cmpb $0x0, 0x68(%rsp)
je 0x9da7
movl %ebx, %edi
callq 0x96f7
movb 0x98(%rsp), %dl
movl 0xa8(%rsp), %r8d
testl %eax, %eax
je 0x9eb8
movl %r13d, %eax
orl $0x10000000, %eax       # imm = 0x10000000
movb 0x80(%rsp), %cl
orb %dl, %cl
testb %dl, %dl
cmovnel %eax, %r13d
testl %r8d, %r8d
je 0x9dde
movq %r14, %rdi
movl $0x1, %esi
movl $0x2, %edx
movb %cl, (%rsp)
movl %r8d, %ecx
callq 0x1348e
movb (%rsp), %cl
testl %ebp, %ebp
sete %al
xorb $0x1, %cl
orb %cl, %al
jne 0x9df4
movq %r14, %rdi
movl $0x2f, %esi
jmp 0x9e29
testl %ebp, %ebp
setne %al
orb %cl, %al
je 0x9e21
cmpb $0x0, 0x60(%rsp)
je 0x9f18
movq %r14, %rdi
movl $0x3, %esi
movl $0x2, %edx
movl $0x3, %ecx
callq 0x1348e
jmp 0x9e33
movq %r14, %rdi
movl $0x30, %esi
movl $0x3, %edx
callq 0x1346e
movb 0x60(%rsp), %al
xorb $0x1, %al
orb 0x4(%rsp), %al
je 0x9e56
cmpb $0x0, 0x4(%rsp)
je 0x9e5d
leaq 0x1b893(%rip), %rax     # 0x256e0
movq 0x28(%rax), %rax
jmp 0x9a09
orl $0x40000000, %r13d      # imm = 0x40000000
testl %ebp, %ebp
je 0x9e82
movl 0x8d4c(%r14), %edi
movl %r13d, %esi
movl $0x1, %edx
callq 0x75b3
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testl %ecx, %ecx
jne 0x9ea9
jmp 0x9e89
movl %r13d, 0x8d4c(%r14)
cmpb $0x0, 0x4(%rsp)
je 0x9ea7
movq %r14, %rdi
movl %ebx, %esi
movl %ebp, %edx
callq 0x9735
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testl %ecx, %ecx
jne 0x9ea9
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testb %dl, %dl
je 0x9eed
movq %r14, %rdi
movl $0x30, %esi
movl $0x2, %edx
callq 0x1346e
movq %r14, %rdi
movl $0x34, %esi
callq 0x1340c
movq %r14, %rdi
movl $0x47, %esi
movl $0x5, %edx
callq 0x1346e
movq %r14, %rdi
movl $0xb, %esi
movl $0x2, %edx
movl $0x5, %ecx
callq 0x1348e
movb 0x80(%rsp), %cl
movl 0xa8(%rsp), %r8d
jmp 0x9dbe
movl %ebx, %edi
callq 0x96f7
testl %eax, %eax
je 0x9fcc
cmpb $0x0, 0x68(%rsp)
je 0x9fbf
movq %r14, %rdi
movl $0x7, %esi
movl $0x2, %edx
callq 0x1346e
movq %r14, %rdi
movl $0xb, %esi
movl $0x2, %edx
movl $0x5, %ecx
callq 0x1348e
jmp 0x9fcc
movl %ecx, 0x4(%rsp)
movq %r14, %rdi
movl $0x6, %esi
movl $0x3, %edx
xorl %ecx, %ecx
callq 0x1348e
movq %r13, %rax
movl $0x40000000, %r13d      # imm = 0x40000000
orl 0x1c(%rax), %r13d
jmp 0x9e33
movl %ecx, 0x4(%rsp)
movq %r14, %rdi
movl $0x6, %esi
movl $0x3, %edx
movl %r8d, %ecx
callq 0x1348e
movq %r14, %rdi
movl $0x3, %esi
callq 0x137c6
movq %r13, %rax
movl $0x20000000, %r13d      # imm = 0x20000000
orl 0x10(%rax), %r13d
jmp 0x9e33
cmpl $0x1, %ebp
movl $0x7, %r12d
sbbl $-0x1, %r12d
movq %r14, %rdi
movl %r12d, %esi
movl $0x3, %edx
callq 0x1346e
jmp 0x9e3f
jmp 0x9fe3
movq %rax, %rbx
leaq 0x18(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xa001
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3320
movq %rbx, %rdi
callq 0x3560