// File: /workspace/github_cpp_makefile_star2_O1/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, 0x11523(%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 0x8cd7(%rip), %rdi      # 0x1cf82
cmpl $0x5a, %eax
jg 0x14571
movl %eax, %ebp
movl $0x4d4f4353, %eax       # imm = 0x4D4F4353
xorl 0xb(%rsp), %eax
movzbl 0xf(%rsp), %ecx
orl %eax, %ecx
jne 0x14571
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 0x14322
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 0x1432a
movq $0x0, 0x8(%r14)
movslq 0x20(%r14), %r15
testq %r15, %r15
jle 0x14358
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 0x14360
movq $0x0, 0x18(%r14)
movslq 0x30(%r14), %r15
testq %r15, %r15
jle 0x14386
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 0x1438e
movq $0x0, 0x28(%r14)
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x80(%rax)
movl %eax, 0x48(%r14)
testl %eax, %eax
jle 0x143ea
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 0x143f2
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 0x14440
xorl %r15d, %r15d
xorl %r12d, %r12d
movq 0x50(%r14), %rdi
addq %r15, %rdi
movq %rbx, %rsi
callq 0x1417c
incq %r12
movslq 0x58(%r14), %rax
addq $0x8, %r15
cmpq %rax, %r12
jl 0x14421
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)
shlq $0x2, %r15
movq %r15, %rdi
callq 0x3440
movq %rax, 0x68(%r14)
cmpl $0x0, 0x70(%r14)
jle 0x144b4
xorl %r15d, %r15d
xorl %r12d, %r12d
movq 0x60(%r14), %rdi
addq %r15, %rdi
movq %rbx, %rsi
callq 0x1417c
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 0x14481
cmpl $0x53, %ebp
jl 0x14542
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)
shlq $0x2, %r15
movq %r15, %rdi
callq 0x3440
movq %rax, 0x80(%r14)
cmpl $0x0, 0x88(%r14)
jle 0x14555
xorl %r15d, %r15d
xorl %r12d, %r12d
movq 0x78(%r14), %rdi
addq %r15, %rdi
movq %rbx, %rsi
callq 0x1417c
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 0x14507
jmp 0x14555
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 0x1457a
leaq 0x8a57(%rip), %rdi      # 0x1cfc8
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x10418
movl %ebx, %eax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
nop