// File: /workspace/github_cpp_makefile_star2_O2/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 %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbx
movq %rdi, %r14
andl $0x0, 0x74(%rdi)
orl $-0x1, 0x12e36(%rip)    # 0x257b0
movq (%rsi), %rax
leaq 0x3(%rsp), %r15
pushq $0x4
popq %rdx
movq %rsi, %rdi
movq %r15, %rsi
callq *0x50(%rax)
movb $0x0, 0x4(%r15)
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x80(%rax)
movl %eax, %ebp
leaq 0x85dc(%rip), %rsi      # 0x1af84
pushq $0x5
popq %rdx
movq %r15, %rdi
callq 0x3310
leaq 0x85d9(%rip), %rdi      # 0x1af93
cmpl $0x5a, %ebp
jg 0x12c5f
testl %eax, %eax
jne 0x12c5f
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 0x12a21
movq %r15, %rdi
callq 0x3430
movq %rax, 0x8(%r14)
movq (%rbx), %rcx
movq %rbx, %rdi
movq %rax, %rsi
movq %r15, %rdx
callq *0x50(%rcx)
jmp 0x12a26
andq $0x0, 0x8(%r14)
movslq 0x20(%r14), %r15
testq %r15, %r15
jle 0x12a54
leaq (,%r15,4), %rdi
callq 0x3430
movq %rax, 0x18(%r14)
movq (%rbx), %rcx
movq %rbx, %rdi
movq %rax, %rsi
movq %r15, %rdx
callq *0xb0(%rcx)
jmp 0x12a59
andq $0x0, 0x18(%r14)
movslq 0x30(%r14), %r15
testq %r15, %r15
jle 0x12a7f
movq %r15, %rdi
callq 0x3430
movq %rax, 0x28(%r14)
movq (%rbx), %rcx
movq %rbx, %rdi
movq %rax, %rsi
movq %r15, %rdx
callq *0x50(%rcx)
jmp 0x12a84
andq $0x0, 0x28(%r14)
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x80(%rax)
movl %eax, 0x48(%r14)
testl %eax, %eax
jle 0x12ae0
movl %eax, %r15d
movq %r15, %rdi
callq 0x3430
movq %rax, %r12
movq %rax, 0x38(%r14)
leaq (,%r15,4), %rdi
callq 0x3430
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 0x12ae8
xorps %xmm0, %xmm0
movups %xmm0, 0x38(%r14)
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x80(%rax)
movl %eax, %r15d
movl %eax, 0x58(%r14)
movslq %eax, %rdi
shlq $0x3, %rdi
callq 0x3430
movq %rax, 0x50(%r14)
xorl %r12d, %r12d
xorl %r13d, %r13d
movslq %r15d, %rax
cmpq %rax, %r13
jge 0x12b35
movq 0x50(%r14), %rdi
addq %r12, %rdi
movq %rbx, %rsi
callq 0x128b8
incq %r13
movl 0x58(%r14), %r15d
addq $0x8, %r12
jmp 0x12b11
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x80(%rax)
movl %eax, %r15d
movl %eax, 0x70(%r14)
movslq %eax, %r12
leaq (,%r12,8), %rdi
callq 0x3430
movq %rax, 0x60(%r14)
shlq $0x2, %r12
movq %r12, %rdi
callq 0x3430
movq %rax, 0x68(%r14)
xorl %r12d, %r12d
xorl %r13d, %r13d
movslq %r15d, %rax
cmpq %rax, %r13
jge 0x12baa
movq 0x60(%r14), %rdi
addq %r12, %rdi
movq %rbx, %rsi
callq 0x128b8
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x80(%rax)
movq 0x68(%r14), %rcx
movl %eax, (%rcx,%r13,4)
incq %r13
movl 0x70(%r14), %r15d
addq $0x8, %r12
jmp 0x12b72
cmpl $0x53, %ebp
jl 0x12c33
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x80(%rax)
movl %eax, %ebp
movl %eax, 0x88(%r14)
movslq %eax, %r15
leaq (,%r15,8), %rdi
callq 0x3430
movq %rax, 0x78(%r14)
shlq $0x2, %r15
movq %r15, %rdi
callq 0x3430
movq %rax, 0x80(%r14)
xorl %r15d, %r15d
xorl %r12d, %r12d
movslq %ebp, %rax
cmpq %rax, %r12
jge 0x12c43
movq 0x78(%r14), %rdi
addq %r15, %rdi
movq %rbx, %rsi
callq 0x128b8
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x80(%rax)
movq 0x80(%r14), %rcx
movl %eax, (%rcx,%r12,4)
incq %r12
movl 0x88(%r14), %ebp
addq $0x8, %r15
jmp 0x12bf5
xorps %xmm0, %xmm0
movups %xmm0, 0x78(%r14)
andl $0x0, 0x88(%r14)
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x80(%rax)
movb $0x1, %bl
cmpl $0xbeefcafe, %eax       # imm = 0xBEEFCAFE
je 0x12c68
leaq 0x837a(%rip), %rdi      # 0x1afd9
xorl %ebx, %ebx
xorl %eax, %eax
callq 0xf258
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop