// File: tomfunkhouser[P]gaps/apps/mshalign/mshalign.cpp
// Function: ReadWeights(char const*, double*&, int&)
static RNScalar *
ReadWeights(const char *filename, RNScalar *& weights, int& nweights)
{
  // Start statistics
  RNTime start_time;
  start_time.Read();

  // Open file
  FILE *fp = fopen(filename, "r");
  if (!fp) {
    RNFail("Unable to open edge weight file %s\n", filename);
    return NULL;
  }

  // Count weights
  nweights = 0;
  RNScalar dummy;
  while (fscanf(fp, "%lf", &dummy) == 1) nweights++;
  fseek(fp, 0, SEEK_SET);

  // Check number of weights
  if (nweights == 0) {
    RNFail("There are no weights in %s\n", filename);
    return NULL;
  }

  // Allocate array for weights
  weights = new RNScalar [ nweights ];
  if (!weights) {
    RNFail("Unable to allocate weights for %s\n", filename);
    return NULL;
  }

  // Read weights
  for (int i = 0; i < nweights; i++) {
    if (fscanf(fp, "%lf", &weights[i]) != 1) {
      RNFail("Unable to read weight %d from %s\n", i, filename);
      return NULL;
    }
  }

  // Close file
  fclose(fp);

  // Print message
  if (print_verbose) {
    printf("Read weights from %s ...\n", filename);
    printf("  Time = %.2f seconds\n", start_time.Elapsed());
    printf("  # Weights = %d\n", nweights);
  }

  // Return weights
  return weights;
}

// Assembly:
subq $0x48, %rsp
movq %rdi, 0x38(%rsp)
movq %rsi, 0x30(%rsp)
movq %rdx, 0x28(%rsp)
leaq 0x18(%rsp), %rdi
callq 0xa2b30
leaq 0x18(%rsp), %rdi
callq 0x15df0
movq 0x38(%rsp), %rdi
leaq 0x97765(%rip), %rsi     # 0xa83e8
callq 0xd4d0
movq %rax, 0x10(%rsp)
cmpq $0x0, 0x10(%rsp)
jne 0x10cb6
movq 0x38(%rsp), %rsi
leaq 0x965bc(%rip), %rdi     # 0xa725d
movb $0x0, %al
callq 0xa6d50
movq $0x0, 0x40(%rsp)
jmp 0x10e59
movq 0x28(%rsp), %rax
movl $0x0, (%rax)
movq 0x10(%rsp), %rdi
leaq 0x96e8f(%rip), %rsi     # 0xa7b5c
leaq 0x8(%rsp), %rdx
movb $0x0, %al
callq 0xd710
cmpl $0x1, %eax
jne 0x10cec
movq 0x28(%rsp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
jmp 0x10cc1
movq 0x10(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0xd490
movq 0x28(%rsp), %rax
cmpl $0x0, (%rax)
jne 0x10d27
movq 0x38(%rsp), %rsi
leaq 0x9656f(%rip), %rdi     # 0xa7281
movb $0x0, %al
callq 0xa6d50
movq $0x0, 0x40(%rsp)
jmp 0x10e59
movq 0x28(%rsp), %rax
movslq (%rax), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0xd030
movq %rax, %rcx
movq 0x30(%rsp), %rax
movq %rcx, (%rax)
movq 0x30(%rsp), %rax
cmpq $0x0, (%rax)
jne 0x10d87
movq 0x38(%rsp), %rsi
leaq 0x9652b(%rip), %rdi     # 0xa729d
movb $0x0, %al
callq 0xa6d50
movq $0x0, 0x40(%rsp)
jmp 0x10e59
movl $0x0, 0x4(%rsp)
movl 0x4(%rsp), %eax
movq 0x28(%rsp), %rcx
cmpl (%rcx), %eax
jge 0x10df9
movq 0x10(%rsp), %rdi
movq 0x30(%rsp), %rax
movq (%rax), %rdx
movslq 0x4(%rsp), %rax
shlq $0x3, %rax
addq %rax, %rdx
leaq 0x96da0(%rip), %rsi     # 0xa7b5c
movb $0x0, %al
callq 0xd710
cmpl $0x1, %eax
je 0x10dea
movl 0x4(%rsp), %esi
movq 0x38(%rsp), %rdx
leaq 0x964e8(%rip), %rdi     # 0xa72c0
movb $0x0, %al
callq 0xa6d50
movq $0x0, 0x40(%rsp)
jmp 0x10e59
jmp 0x10dec
movl 0x4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4(%rsp)
jmp 0x10d8f
movq 0x10(%rsp), %rdi
callq 0xd3c0
cmpl $0x0, 0xbce06(%rip)     # 0xcdc10
je 0x10e4c
movq 0x38(%rsp), %rsi
leaq 0x964ca(%rip), %rdi     # 0xa72e2
movb $0x0, %al
callq 0xd070
leaq 0x18(%rsp), %rdi
callq 0xa2bf0
leaq 0x963e3(%rip), %rdi     # 0xa7213
movb $0x1, %al
callq 0xd070
movq 0x28(%rsp), %rax
movl (%rax), %esi
leaq 0x964b7(%rip), %rdi     # 0xa72fc
movb $0x0, %al
callq 0xd070
movq 0x30(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x40(%rsp)
movq 0x40(%rsp), %rax
addq $0x48, %rsp
retq
nopw %cs:(%rax,%rax)