// File: /workspace/github_cpp_makefile_star2_O3/rofl0r[P]ascc/cc_script.cpp
// Function: ccScript::Read(AGS::Common::Stream*)
bool ccScript::Read(Stream *in)
{
  instances = 0;
  int n;
  char gotsig[5];
  currentline = -1;
  // MACPORT FIX: swap 'size' and 'nmemb'
  in->Read(gotsig, 4);
  gotsig[4] = 0;

  int fileVer = in->ReadInt32();

  if ((strcmp(gotsig, scfilesig) != 0) || (fileVer > SCOM_VERSION)) {
    cc_error("file was not written by ccScript::Write or seek position is incorrect");
    return false;
  }

  globaldatasize = in->ReadInt32();
  codesize = in->ReadInt32();
  stringssize = in->ReadInt32();

  if (globaldatasize > 0) {
    globaldata = (char *)malloc(globaldatasize);
    // MACPORT FIX: swap
    in->Read(globaldata, globaldatasize);
  }
  else
    globaldata = nullptr;

  if (codesize > 0) {
    code = (int32_t *)malloc(codesize * sizeof(int32_t));
    // MACPORT FIX: swap

    // 64 bit: Read code into 8 byte array, necessary for being able to perform
    // relocations on the references.
    in->ReadArrayOfInt32(code, codesize);
  }
  else
    code = nullptr;

  if (stringssize > 0) {
    strings = (char *)malloc(stringssize);
    // MACPORT FIX: swap
    in->Read(strings, stringssize);
  } 
  else
    strings = nullptr;

  numfixups = in->ReadInt32();
  if (numfixups > 0) {
    fixuptypes = (char *)malloc(numfixups);
    fixups = (int32_t *)malloc(numfixups * sizeof(int32_t));
    // MACPORT FIX: swap 'size' and 'nmemb'
    in->Read(fixuptypes, numfixups);
    in->ReadArrayOfInt32(fixups, numfixups);
  }
  else {
    fixups = nullptr;
    fixuptypes = nullptr;
  }

  numimports = in->ReadInt32();

  imports = (char**)malloc(sizeof(char*) * numimports);
  for (n = 0; n < numimports; n++)
    freadstring(&imports[n], in);

  numexports = in->ReadInt32();
  exports = (char**)malloc(sizeof(char*) * numexports);
  export_addr = (int32_t*)malloc(sizeof(int32_t) * numexports);
  for (n = 0; n < numexports; n++) {
    freadstring(&exports[n], in);
    export_addr[n] = in->ReadInt32();
  }

  if (fileVer >= 83) {
    // read in the Sections
    numSections = in->ReadInt32();
    sectionNames = (char**)malloc(numSections * sizeof(char*));
    sectionOffsets = (int32_t*)malloc(numSections * sizeof(int32_t));
    for (n = 0; n < numSections; n++) {
      freadstring(&sectionNames[n], in);
      sectionOffsets[n] = in->ReadInt32();
    }
  }
  else
  {
    numSections = 0;
    sectionNames = nullptr;
    sectionOffsets = nullptr;
  }

  if (in->ReadInt32() != ENDFILESIG) {
    cc_error("internal error rebuilding script");
    return false;
  }
  return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x10, %rsp
movq %rsi, %rbx
movq %rdi, %r14
movl $0x0, 0x74(%rdi)
movl $0xffffffff, 0x1169b(%rip) # imm = 0xFFFFFFFF
movq (%rsi), %rax
leaq 0xb(%rsp), %r15
movl $0x4, %edx
movq %rsi, %rdi
movq %r15, %rsi
callq *0x50(%rax)
movb $0x0, 0x4(%r15)
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x80(%rax)
leaq 0x8e4f(%rip), %rdi      # 0x1cf82
cmpl $0x5a, %eax
jg 0x143f4
movl %eax, %ebp
movl $0x4d4f4353, %eax       # imm = 0x4D4F4353
xorl 0xb(%rsp), %eax
movzbl 0xf(%rsp), %ecx
orl %eax, %ecx
jne 0x143f4
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x80(%rax)
movl %eax, 0x10(%r14)
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x80(%rax)
movl %eax, 0x20(%r14)
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x80(%rax)
movl %eax, 0x30(%r14)
movslq 0x10(%r14), %r15
testq %r15, %r15
jle 0x141aa
movq %r15, %rdi
callq 0x3440
movq %rax, 0x8(%r14)
movq (%rbx), %rcx
movq %rbx, %rdi
movq %rax, %rsi
movq %r15, %rdx
callq *0x50(%rcx)
jmp 0x141b2
movq $0x0, 0x8(%r14)
movslq 0x20(%r14), %r15
testq %r15, %r15
jle 0x141e0
leaq (,%r15,4), %rdi
callq 0x3440
movq %rax, 0x18(%r14)
movq (%rbx), %rcx
movq %rbx, %rdi
movq %rax, %rsi
movq %r15, %rdx
callq *0xb0(%rcx)
jmp 0x141e8
movq $0x0, 0x18(%r14)
movslq 0x30(%r14), %r15
testq %r15, %r15
jle 0x1420e
movq %r15, %rdi
callq 0x3440
movq %rax, 0x28(%r14)
movq (%rbx), %rcx
movq %rbx, %rdi
movq %rax, %rsi
movq %r15, %rdx
callq *0x50(%rcx)
jmp 0x14216
movq $0x0, 0x28(%r14)
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x80(%rax)
movl %eax, 0x48(%r14)
testl %eax, %eax
jle 0x14272
movl %eax, %r15d
movq %r15, %rdi
callq 0x3440
movq %rax, %r12
movq %rax, 0x38(%r14)
leaq (,%r15,4), %rdi
callq 0x3440
movq %rax, 0x40(%r14)
movq (%rbx), %rax
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq *0x50(%rax)
movq 0x40(%r14), %rsi
movslq 0x48(%r14), %rdx
movq (%rbx), %rax
movq %rbx, %rdi
callq *0xb0(%rax)
jmp 0x1427a
xorps %xmm0, %xmm0
movups %xmm0, 0x38(%r14)
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x80(%rax)
movl %eax, 0x58(%r14)
movslq %eax, %r15
leaq (,%r15,8), %rdi
callq 0x3440
movq %rax, 0x50(%r14)
testl %r15d, %r15d
jle 0x142c8
xorl %r15d, %r15d
xorl %r12d, %r12d
movq 0x50(%r14), %rdi
addq %r15, %rdi
movq %rbx, %rsi
callq 0x14004
incq %r12
movslq 0x58(%r14), %rax
addq $0x8, %r15
cmpq %rax, %r12
jl 0x142a9
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x80(%rax)
movl %eax, 0x70(%r14)
movslq %eax, %r15
leaq (,%r15,8), %rdi
callq 0x3440
movq %rax, 0x60(%r14)
leaq (,%r15,4), %rdi
callq 0x3440
movq %rax, 0x68(%r14)
testl %r15d, %r15d
jle 0x1433b
xorl %r15d, %r15d
xorl %r12d, %r12d
movq 0x60(%r14), %rdi
addq %r15, %rdi
movq %rbx, %rsi
callq 0x14004
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x80(%rax)
movq 0x68(%r14), %rcx
movl %eax, (%rcx,%r12,4)
incq %r12
movslq 0x70(%r14), %rax
addq $0x8, %r15
cmpq %rax, %r12
jl 0x14308
cmpl $0x53, %ebp
jl 0x143c5
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x80(%rax)
movl %eax, 0x88(%r14)
movslq %eax, %r15
leaq (,%r15,8), %rdi
callq 0x3440
movq %rax, 0x78(%r14)
leaq (,%r15,4), %rdi
callq 0x3440
movq %rax, 0x80(%r14)
testl %r15d, %r15d
jle 0x143d8
xorl %r15d, %r15d
xorl %r12d, %r12d
movq 0x78(%r14), %rdi
addq %r15, %rdi
movq %rbx, %rsi
callq 0x14004
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x80(%rax)
movq 0x80(%r14), %rcx
movl %eax, (%rcx,%r12,4)
incq %r12
movslq 0x88(%r14), %rax
addq $0x8, %r15
cmpq %rax, %r12
jl 0x1438a
jmp 0x143d8
xorps %xmm0, %xmm0
movups %xmm0, 0x78(%r14)
movl $0x0, 0x88(%r14)
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x80(%rax)
movb $0x1, %bl
cmpl $0xbeefcafe, %eax       # imm = 0xBEEFCAFE
je 0x143fd
leaq 0x8bd4(%rip), %rdi      # 0x1cfc8
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x102f4
movl %ebx, %eax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq