// File: /workspace/github_cpp_makefile_star2_O1/rofl0r[P]ascc/cs_parser.cpp
// Function: call_property_func(ccCompiledScript*, int, int)
int call_property_func(ccCompiledScript *scrip, int propSym, int isWrite) {
  // a Property Get
  int numargs = 0;

  // AX contains the struct address

  // Always a struct member -- set OP = AX
  if ((sym.entries[propSym].flags & SFLG_STATIC) == 0) {
    scrip->push_reg(SREG_OP);
    scrip->write_cmd1(SCMD_CALLOBJ, SREG_AX);
  }

  if (isWrite) {
    // BX contains the new value
    if (sym.entries[propSym].flags & SFLG_IMPORTED)
      scrip->write_cmd1(SCMD_PUSHREAL, SREG_BX);
    else {
      cc_error("internal error: prop is not import");
      return -1;
    }

    numargs++;
  }

  if (sym.entries[propSym].flags & SFLG_ARRAY) {
    // array indexer is in DX
    if (sym.entries[propSym].flags & SFLG_IMPORTED)
      scrip->write_cmd1(SCMD_PUSHREAL, SREG_DX);
    else {
      cc_error("internal error: prop is not import");
      return -1;
    }

    numargs++;
  }

  if (sym.entries[propSym].flags & SFLG_IMPORTED) {
    // tell it how many args for this call (nested imported functions
    // causes stack problems otherwise)
    scrip->write_cmd1(SCMD_NUMFUNCARGS, numargs);
  }

  int propFunc;
  if (isWrite)
    propFunc = sym.entries[propSym].get_propset();
  else
    propFunc = sym.entries[propSym].get_propget();

  if (propFunc < 0) {
    cc_error("Internal error: property in use but not set");
    return -1;
  }

  // AX = Func Address
  scrip->write_cmd2(SCMD_LITTOREG, SREG_AX, propFunc);

  if (sym.entries[propSym].flags & SFLG_IMPORTED) {
    scrip->fixup_previous(FIXUP_IMPORT);
    // do the call
    scrip->write_cmd1(SCMD_CALLEXT, SREG_AX);
    if (numargs > 0)
      scrip->write_cmd1(SCMD_SUBREALSTACK,numargs);
  }
  else {
    scrip->fixup_previous(FIXUP_FUNCTION);
    scrip->write_cmd1(SCMD_CALL, SREG_AX);

    // restore the stack
    if (numargs > 0) {
      scrip->cur_sp -= numargs*4;
      scrip->write_cmd2(SCMD_SUB,SREG_SP,numargs*4);
    }
  }

  if (!isWrite) {
    // function return type
    scrip->ax_val_type = sym.entries[propSym].vartype;
    scrip->ax_val_scope = SYM_LOCALVAR;
    if (sym.entries[propSym].flags & SFLG_DYNAMICARRAY)
      scrip->ax_val_type |= STYPE_DYNARRAY;
    if (sym.entries[propSym].flags & SFLG_POINTER)
      scrip->ax_val_type |= STYPE_POINTER;
    if (sym.entries[propSym].flags & SFLG_CONST)
      scrip->ax_val_type |= STYPE_CONST;
  }

  if ((sym.entries[propSym].flags & SFLG_STATIC) == 0) {
    scrip->pop_reg(SREG_OP);
  }

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movl %edx, %ebp
movq %rdi, %rbx
movslq %esi, %rax
leaq 0x1be2f(%rip), %r15     # 0x256e0
movq 0x28(%r15), %rcx
imulq $0xb0, %rax, %r12
testb $0x20, 0x29(%rcx,%r12)
jne 0x98e3
movq %rbx, %rdi
movl $0x6, %esi
callq 0x13684
movq %rbx, %rdi
movl $0x2d, %esi
movl $0x3, %edx
callq 0x135fc
testl %ebp, %ebp
je 0x990d
movq 0x28(%r15), %rax
testb $0x4, 0x28(%rax,%r12)
je 0x9921
movq %rbx, %rdi
movl $0x22, %esi
movl $0x4, %edx
callq 0x135fc
movl $0x1, %r14d
jmp 0x9910
xorl %r14d, %r14d
movq 0x28(%r15), %rax
movq 0x28(%rax,%r12), %rax
testb $0x2, %al
je 0x9942
testb $0x4, %al
jne 0x992d
leaq 0x11935(%rip), %rdi     # 0x1b25d
jmp 0x99e6
movq %rbx, %rdi
movl $0x22, %esi
movl $0x7, %edx
callq 0x135fc
incl %r14d
movq 0x28(%r15), %rax
testb $0x4, 0x28(%rax,%r12)
je 0x995e
movq %rbx, %rdi
movl $0x27, %esi
movl %r14d, %edx
callq 0x135fc
movq 0x28(%r15), %rdi
addq %r12, %rdi
testl %ebp, %ebp
je 0x9970
callq 0x11222
jmp 0x9975
callq 0x1120c
testl %eax, %eax
js 0x99df
movq %rbx, %rdi
movl $0x6, %esi
movl $0x3, %edx
movl %eax, %ecx
callq 0x1361c
movq 0x28(%r15), %rax
testb $0x4, 0x28(%rax,%r12)
jne 0x99fd
movq %rbx, %rdi
movl $0x2, %esi
callq 0x13950
movq %rbx, %rdi
movl $0x17, %esi
movl $0x3, %edx
callq 0x135fc
testl %r14d, %r14d
jle 0x9a31
shll $0x2, %r14d
subq %r14, 0x8d40(%rbx)
movq %rbx, %rdi
movl $0x2, %esi
movl $0x1, %edx
movl %r14d, %ecx
callq 0x1361c
jmp 0x9a31
leaq 0x1189a(%rip), %rdi     # 0x1b280
xorl %eax, %eax
callq 0x10418
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
movl $0x4, %esi
callq 0x13950
movq %rbx, %rdi
movl $0x21, %esi
movl $0x3, %edx
callq 0x135fc
testl %r14d, %r14d
jle 0x9a31
movq %rbx, %rdi
movl $0x23, %esi
movl %r14d, %edx
callq 0x135fc
testl %ebp, %ebp
jne 0x9a80
movq 0x28(%r15), %rax
movswl 0x30(%rax,%r12), %ecx
movl %ecx, 0x8d4c(%rbx)
movl $0x2, 0x8d50(%rbx)
movq 0x28(%rax,%r12), %rax
btl $0x12, %eax
jae 0x9a66
orl $0x10000000, %ecx       # imm = 0x10000000
movl %ecx, 0x8d4c(%rbx)
btl $0x8, %eax
jae 0x9a73
orb $0x40, 0x8d4f(%rbx)
btl $0x10, %eax
jae 0x9a80
orb $0x20, 0x8d4f(%rbx)
movq 0x28(%r15), %rax
xorl %ebp, %ebp
testb $0x20, 0x29(%rax,%r12)
jne 0x99f2
movq %rbx, %rdi
movl $0x6, %esi
callq 0x136b2
jmp 0x99f2