// File: /workspace/github_cpp_makefile_star2_O1/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 $0x38, %rsp
movl %r8d, %ebp
movl %ecx, 0x4(%rsp)
movl %esi, %r10d
movq %rdi, %r15
movslq %esi, %rcx
leaq 0x1bc17(%rip), %r13     # 0x256e0
movq 0x28(%r13), %rax
imulq $0xb0, %rcx, %r14
movl 0x38(%rax,%r14), %edx
cmpl $0x2, %edx
movl $0x1b, %ecx
movl $0x8, %esi
cmovel %ecx, %esi
movl $0x19, %ecx
movl $0x7, %edi
cmovel %ecx, %edi
cmpl $0x1, %edx
movl $0x1a, %r8d
cmovnel %esi, %r8d
movl 0xa0(%rsp), %r11d
movl $0x18, %r12d
cmovnel %edi, %r12d
testl %ebp, %ebp
cmovnel %r8d, %r12d
testl %edx, %edx
je 0x9b25
movl %edx, 0x1bb8b(%rip)     # 0x256b0
movb 0x70(%rsp), %bl
cmpl $0x2e, %r11d
jne 0x9b74
movl 0x4(%rsp), %r12d
testb %r12b, %r12b
je 0x9b95
movl %r10d, 0x8(%rsp)
movq %r15, %rdi
movl $0x6, %esi
movl $0x3, %edx
xorl %ecx, %ecx
callq 0x1361c
movq 0x28(%r13), %rax
movswl 0x30(%rax,%r14), %ecx
movl 0x28(%rax,%r14), %r13d
shll $0xd, %r13d
andl $0x20000000, %r13d      # imm = 0x20000000
orl %ecx, %r13d
jmp 0xa0f3
leal -0x13(%r11), %edx
cmpl $0x1, %edx
ja 0x9b9e
orl %ebp, %r9d
je 0x9c6d
cmpl $0x13, %r11d
je 0x9bad
leaq 0x11764(%rip), %rdi     # 0x1b2f7
jmp 0x9bb4
leaq 0x11710(%rip), %rdi     # 0x1b2ac
jmp 0x9bb4
cmpl $0x16, %r11d
jne 0x9bc5
orl %ebp, %r9d
je 0x9d59
leaq 0x11723(%rip), %rdi     # 0x1b2d7
xorl %eax, %eax
callq 0x10418
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0xa170
movl 0x98(%rsp), %edx
movl 0x80(%rsp), %ecx
leal -0x1(%r11), %esi
cmpl $0x1, %esi
ja 0x9d22
movl %r10d, 0x8(%rsp)
movb 0x90(%rsp), %dil
leaq (%rax,%r14), %rsi
movswl 0x30(%rsi), %r8d
movl %r8d, 0xc(%rsp)
movl 0x28(%rsi), %r13d
shll $0xd, %r13d
andl $0x20000000, %r13d      # imm = 0x20000000
cmpb $0x0, 0x88(%rsp)
je 0x9dd3
testb %dil, %dil
jne 0x9c4e
testl %ecx, %ecx
setne %al
movl %ebx, %edx
xorb $0x1, %dl
orb 0x78(%rsp), %dl
orb %al, %dl
cmpb $0x1, %dl
jne 0x9c4e
movq %r15, %rdi
movl $0x34, %esi
callq 0x1359c
movl 0x80(%rsp), %ecx
movb 0x90(%rsp), %dil
testl %ecx, %ecx
je 0x9e72
movq %r15, %rdi
movl $0x1, %esi
movl $0x2, %edx
callq 0x1361c
jmp 0x9e6a
movb 0xb0(%rsp), %r12b
movzbl %r12b, %edx
leaq 0x11690(%rip), %rcx     # 0x1b310
leaq 0x18(%rsp), %rsi
movl %r10d, 0x8(%rsp)
movl %r10d, %edi
callq 0x60a8
testl %eax, %eax
js 0x9dbe
movl %eax, 0xc(%rsp)
movl 0x18(%rsp), %r13d
testl %r13d, %r13d
sets %al
andb %al, %r12b
cmpb $0x1, %r12b
jne 0x9d94
movl $0x400, %edi            # imm = 0x400
callq 0x140a4
testl %eax, %eax
je 0x9d94
negl %r13d
movq %r15, %rdi
movl $0x6, %esi
movl $0x3, %edx
movl %r13d, %ecx
callq 0x1361c
movq %r15, %rdi
movl $0x6, %esi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x1361c
movq %r15, %rdi
movl $0xc, %esi
movl $0x4, %edx
movl $0x3, %ecx
callq 0x1361c
movq %r15, %rdi
movl $0x3, %esi
movl $0x4, %edx
movl $0x3, %ecx
jmp 0x9da4
cmpl $0xa, %r11d
je 0x9e05
cmpl $0x15, %r11d
je 0x9dec
cmpl $0x12, %r11d
movl 0x4(%rsp), %r12d
jne 0x9f3a
testl %ebp, %ebp
je 0xa0a3
leaq 0x115d8(%rip), %rdi     # 0x1b32c
jmp 0x9bb4
movq %r13, %rdi
movl %r10d, 0x8(%rsp)
movl %r10d, %esi
callq 0x1263e
movq %rax, %rdi
callq 0x3410
cvtsd2ss %xmm0, %xmm0
movd %xmm0, %ecx
movq %r15, %rdi
movl $0x6, %esi
movl $0x3, %edx
callq 0x1361c
movl 0x14(%r13), %r13d
jmp 0xa0ee
movq %r15, %rdi
movl $0x6, %esi
movl $0x3, %edx
movl %r13d, %ecx
callq 0x1361c
movl 0x4(%rsp), %r12d
leaq 0x1b92b(%rip), %rax     # 0x256e0
movl (%rax), %r13d
movl 0xc(%rsp), %eax
jmp 0x9dc6
xorl %r13d, %r13d
movl 0x4(%rsp), %r12d
testl %eax, %eax
jns 0xa0f3
jmp 0x9bbb
cmpl $0x2, %r11d
jne 0x9e2f
movq %r15, %rdi
movl 0x8d40(%r15), %esi
subl %ecx, %esi
callq 0x5291
jmp 0x9e6a
testl %ebp, %ebp
movl 0x4(%rsp), %r12d
je 0xa07b
leaq 0x1157a(%rip), %rdi     # 0x1b37a
jmp 0x9bb4
leaq 0x1b8d4(%rip), %rsi     # 0x256e0
leaq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x11fa0
movq (%rbx), %rsi
leaq 0x1152a(%rip), %rdi     # 0x1b34d
xorl %eax, %eax
callq 0x10418
jmp 0x9f62
movslq %edx, %rdx
imulq $0xb0, %rdx, %rdx
testb $0x4, 0x28(%rax,%rdx)
movq %r15, 0x10(%rsp)
jne 0x9fef
movq %r15, %rdi
movl $0x6, %esi
movl $0x2, %edx
callq 0x1361c
movq 0x10(%rsp), %rdi
movl $0x1, %esi
callq 0x13950
movb 0x90(%rsp), %dil
movl 0xb8(%rsp), %ecx
movb 0xa8(%rsp), %dl
orl 0xc(%rsp), %r13d
cmpb $0x0, 0x78(%rsp)
je 0x9eb3
movl 0x8(%rsp), %edi
callq 0x985c
movb 0xa8(%rsp), %dl
movl 0xb8(%rsp), %ecx
movb 0x90(%rsp), %dil
testl %eax, %eax
je 0x9f86
movl %r13d, %eax
orl $0x10000000, %eax       # imm = 0x10000000
orb %dl, %dil
testb %dl, %dl
cmovnel %eax, %r13d
testl %ecx, %ecx
je 0x9ee4
movb %dil, 0xc(%rsp)
movq %r15, %rdi
movl $0x1, %esi
movl $0x2, %edx
callq 0x1361c
movb 0xc(%rsp), %dil
testl %ebp, %ebp
sete %al
xorb $0x1, %dil
orb %dil, %al
jne 0x9eff
movq %r15, %rdi
movl $0x2f, %esi
jmp 0xa0e4
testl %ebp, %ebp
setne %al
orb %dil, %al
je 0x9f2d
testb %bl, %bl
je 0xa032
movq %r15, %rdi
movl $0x3, %esi
movl $0x2, %edx
movl $0x3, %ecx
callq 0x1361c
jmp 0xa0ee
movq %r15, %rdi
movl $0x30, %esi
jmp 0xa0e4
leaq 0x1b79f(%rip), %rsi     # 0x256e0
leaq 0x18(%rsp), %r14
movq %r14, %rdi
movl %r10d, %edx
callq 0x11fa0
movq (%r14), %rsi
leaq 0x11433(%rip), %rdi     # 0x1b38e
xorl %eax, %eax
callq 0x10418
leaq 0x28(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x9bbb
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x3320
jmp 0x9bbb
testb %dl, %dl
je 0x9fc4
movq %r15, %rdi
movq %r15, 0x10(%rsp)
movl $0x30, %esi
movl $0x2, %edx
callq 0x135fc
movq 0x10(%rsp), %rdi
movl $0x34, %esi
callq 0x1359c
movq 0x10(%rsp), %rdi
movl $0x47, %esi
movl $0x5, %edx
callq 0x135fc
movq %r15, %rdi
movl $0xb, %esi
movl $0x2, %edx
movl $0x5, %ecx
callq 0x1361c
movb 0x90(%rsp), %dil
movl 0xb8(%rsp), %ecx
jmp 0x9ec4
addq %rdx, %rax
movl 0x34(%rax), %ecx
movq %r15, %rdi
movl $0x6, %esi
movl $0x2, %edx
callq 0x1361c
movq 0x10(%rsp), %rdi
movl $0x4, %esi
callq 0x13950
movl 0x80(%rsp), %ecx
testl %ecx, %ecx
movb 0x90(%rsp), %dil
jne 0x9c56
jmp 0x9e72
movl 0x8(%rsp), %edi
callq 0x985c
testl %eax, %eax
je 0xa073
cmpb $0x0, 0x78(%rsp)
je 0xa0d6
movq %r15, %rdi
movl $0x7, %esi
movl $0x2, %edx
callq 0x135fc
movq %r15, %rdi
movl $0xb, %esi
movl $0x2, %edx
movl $0x5, %ecx
callq 0x1361c
movq %r15, %rdi
movl %r12d, %esi
jmp 0xa0e4
movl %r10d, 0x8(%rsp)
movq %r15, %rdi
movl $0x6, %esi
movl $0x3, %edx
xorl %ecx, %ecx
callq 0x1361c
movq %r13, %rax
movl $0x40000000, %r13d      # imm = 0x40000000
orl 0x1c(%rax), %r13d
jmp 0xa0f3
movl %r10d, 0x8(%rsp)
movq %r15, %rdi
movl $0x6, %esi
movl $0x3, %edx
callq 0x1361c
movq %r15, %rdi
movl $0x3, %esi
callq 0x13950
movq %r13, %rax
movl $0x20000000, %r13d      # imm = 0x20000000
orl 0x10(%rax), %r13d
jmp 0xa0f3
cmpl $0x1, %ebp
movl $0x7, %esi
sbbl $-0x1, %esi
movq %r15, %rdi
movl $0x3, %edx
callq 0x135fc
movl 0x4(%rsp), %r12d
xorb $0x1, %bl
orb %r12b, %bl
je 0xa11d
testb %r12b, %r12b
je 0xa124
leaq 0x1b5d9(%rip), %rax     # 0x256e0
movq 0x28(%rax), %rax
movl 0x28(%rax,%r14), %eax
shll $0x16, %eax
andl $0x40000000, %eax       # imm = 0x40000000
orl %eax, %r13d
jmp 0xa124
orl $0x40000000, %r13d      # imm = 0x40000000
testl %ebp, %ebp
je 0xa149
movl 0x8d4c(%r15), %edi
movl %r13d, %esi
movl $0x1, %edx
callq 0x76b5
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testl %ecx, %ecx
jne 0xa170
jmp 0xa150
movl %r13d, 0x8d4c(%r15)
testb %r12b, %r12b
je 0xa16e
movq %r15, %rdi
movl 0x8(%rsp), %esi
movl %ebp, %edx
callq 0x989a
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testl %ecx, %ecx
jne 0xa170
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xa181
movq %rax, %rbx
leaq 0x28(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xa19f
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x3320
movq %rbx, %rdi
callq 0x3560