// File: 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
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x30(%rbp)
movl $0x0, 0x74(%rax)
movl $0xffffffff, 0x1c067(%rip) # imm = 0xFFFFFFFF
movq -0x18(%rbp), %rdi
leaq -0x21(%rbp), %rsi
movq (%rdi), %rax
movl $0x4, %edx
callq *0x50(%rax)
movb $0x0, -0x1d(%rbp)
movq -0x18(%rbp), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
movl %eax, -0x28(%rbp)
leaq -0x21(%rbp), %rdi
leaq 0xd63d(%rip), %rsi      # 0x33f38
callq 0x4510
cmpl $0x0, %eax
jne 0x2690b
cmpl $0x5a, -0x28(%rbp)
jle 0x26922
leaq 0xd635(%rip), %rdi      # 0x33f47
movb $0x0, %al
callq 0x1bac0
movb $0x0, -0x1(%rbp)
jmp 0x26cc9
movq -0x18(%rbp), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x18(%rbp), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x20(%rax)
movq -0x18(%rbp), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x30(%rax)
cmpl $0x0, 0x10(%rax)
jle 0x26996
movq -0x30(%rbp), %rax
movslq 0x10(%rax), %rdi
callq 0x44f0
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rdi
movq 0x8(%rax), %rsi
movslq 0x10(%rax), %rdx
movq (%rdi), %rax
callq *0x50(%rax)
jmp 0x269a2
movq -0x30(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x30(%rbp), %rax
cmpl $0x0, 0x20(%rax)
jle 0x269df
movq -0x30(%rbp), %rax
movslq 0x20(%rax), %rdi
shlq $0x2, %rdi
callq 0x44f0
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x18(%rbp), %rdi
movq 0x18(%rax), %rsi
movslq 0x20(%rax), %rdx
movq (%rdi), %rax
callq *0xb0(%rax)
jmp 0x269eb
movq -0x30(%rbp), %rax
movq $0x0, 0x18(%rax)
movq -0x30(%rbp), %rax
cmpl $0x0, 0x30(%rax)
jle 0x26a21
movq -0x30(%rbp), %rax
movslq 0x30(%rax), %rdi
callq 0x44f0
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x18(%rbp), %rdi
movq 0x28(%rax), %rsi
movslq 0x30(%rax), %rdx
movq (%rdi), %rax
callq *0x50(%rax)
jmp 0x26a2d
movq -0x30(%rbp), %rax
movq $0x0, 0x28(%rax)
movq -0x18(%rbp), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x48(%rax)
cmpl $0x0, 0x48(%rax)
jle 0x26aa6
movq -0x30(%rbp), %rax
movslq 0x48(%rax), %rdi
callq 0x44f0
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x38(%rax)
movslq 0x48(%rax), %rdi
shlq $0x2, %rdi
callq 0x44f0
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x18(%rbp), %rdi
movq 0x38(%rax), %rsi
movslq 0x48(%rax), %rdx
movq (%rdi), %rax
callq *0x50(%rax)
movq -0x30(%rbp), %rax
movq -0x18(%rbp), %rdi
movq 0x40(%rax), %rsi
movslq 0x48(%rax), %rdx
movq (%rdi), %rax
callq *0xb0(%rax)
jmp 0x26aba
movq -0x30(%rbp), %rax
movq $0x0, 0x40(%rax)
movq $0x0, 0x38(%rax)
movq -0x18(%rbp), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x58(%rax)
movslq 0x58(%rax), %rdi
shlq $0x3, %rdi
callq 0x44f0
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x50(%rax)
movl $0x0, -0x1c(%rbp)
movq -0x30(%rbp), %rcx
movl -0x1c(%rbp), %eax
cmpl 0x58(%rcx), %eax
jge 0x26b22
movq -0x30(%rbp), %rax
movq 0x50(%rax), %rdi
movslq -0x1c(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rdi
movq -0x18(%rbp), %rsi
callq 0x26760
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x26aef
movq -0x18(%rbp), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x70(%rax)
movslq 0x70(%rax), %rdi
shlq $0x3, %rdi
callq 0x44f0
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x60(%rax)
movslq 0x70(%rax), %rdi
shlq $0x2, %rdi
callq 0x44f0
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x68(%rax)
movl $0x0, -0x1c(%rbp)
movq -0x30(%rbp), %rcx
movl -0x1c(%rbp), %eax
cmpl 0x70(%rcx), %eax
jge 0x26bc0
movq -0x30(%rbp), %rax
movq 0x60(%rax), %rdi
movslq -0x1c(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rdi
movq -0x18(%rbp), %rsi
callq 0x26760
movq -0x18(%rbp), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
movl %eax, %edx
movq -0x30(%rbp), %rax
movq 0x68(%rax), %rax
movslq -0x1c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x26b6f
cmpl $0x53, -0x28(%rbp)
jl 0x26c7c
movq -0x18(%rbp), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x88(%rax)
movslq 0x88(%rax), %rdi
shlq $0x3, %rdi
callq 0x44f0
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x78(%rax)
movslq 0x88(%rax), %rdi
shlq $0x2, %rdi
callq 0x44f0
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x80(%rax)
movl $0x0, -0x1c(%rbp)
movq -0x30(%rbp), %rcx
movl -0x1c(%rbp), %eax
cmpl 0x88(%rcx), %eax
jge 0x26c7a
movq -0x30(%rbp), %rax
movq 0x78(%rax), %rdi
movslq -0x1c(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rdi
movq -0x18(%rbp), %rsi
callq 0x26760
movq -0x18(%rbp), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
movl %eax, %edx
movq -0x30(%rbp), %rax
movq 0x80(%rax), %rax
movslq -0x1c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x26c23
jmp 0x26c9d
movq -0x30(%rbp), %rax
movl $0x0, 0x88(%rax)
movq $0x0, 0x78(%rax)
movq $0x0, 0x80(%rax)
movq -0x18(%rbp), %rdi
movq (%rdi), %rax
callq *0x80(%rax)
cmpl $0xbeefcafe, %eax       # imm = 0xBEEFCAFE
je 0x26cc5
leaq 0xd2d5(%rip), %rdi      # 0x33f8d
movb $0x0, %al
callq 0x1bac0
movb $0x0, -0x1(%rbp)
jmp 0x26cc9
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)