// File: Sleepwalking[P]world-octave/matlabfunctions.cpp
// Function: wavread(char*, int*, int*, int*)
double * wavread(char* filename, int *fs, int *nbit, int *wav_length) {
  FILE *fp = fopen(filename, "rb");
  if (NULL == fp) {
    printf("File not found.\n");
    return NULL;
  }

  if (CheckHeader(fp) == false) {
    fclose(fp);
    return NULL;
  }

  if (GetParameters(fp, fs, nbit, wav_length) == false) {
    fclose(fp);
    return NULL;
  }

  double *waveform = new double[*wav_length];
  if (waveform == NULL) return NULL;

  int quantization_byte = *nbit / 8;
  double zero_line = pow(2.0, *nbit - 1);
  double tmp, sign_bias;
  unsigned char for_int_number[4];
  for (int i = 0; i < *wav_length; ++i) {
    sign_bias = tmp = 0.0;
    fread(for_int_number, 1, quantization_byte, fp);  // "data"
    if (for_int_number[quantization_byte-1] >= 128) {
      sign_bias = pow(2.0, *nbit - 1);
      for_int_number[quantization_byte - 1] =
        for_int_number[quantization_byte - 1] & 0x7F;
    }
    for (int j = quantization_byte - 1; j >= 0; --j)
      tmp = tmp * 256.0 + for_int_number[j];
    waveform[i] = (tmp - sign_bias) / zero_line;
  }
  fclose(fp);
  return waveform;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x14484(%rip), %rsi     # 0x1c317
callq 0x10f0
movq %rax, -0x30(%rbp)
xorl %eax, %eax
cmpq -0x30(%rbp), %rax
jne 0x7ebf
leaq 0x145a3(%rip), %rdi     # 0x1c44e
movb $0x0, %al
callq 0x1040
movq $0x0, -0x8(%rbp)
jmp 0x808e
movq -0x30(%rbp), %rdi
callq 0x80a0
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x7ee8
movq -0x30(%rbp), %rdi
callq 0x10d0
movq $0x0, -0x8(%rbp)
jmp 0x808e
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x28(%rbp), %rcx
callq 0x8290
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x7f1d
movq -0x30(%rbp), %rdi
callq 0x10d0
movq $0x0, -0x8(%rbp)
jmp 0x808e
movq -0x28(%rbp), %rax
movslq (%rax), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x1030
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x7f5d
movq $0x0, -0x8(%rbp)
jmp 0x808e
movq -0x20(%rbp), %rax
movl (%rax), %eax
movl $0x8, %ecx
cltd
idivl %ecx
movl %eax, -0x3c(%rbp)
movq -0x20(%rbp), %rax
movl (%rax), %edi
subl $0x1, %edi
movsd 0x14089(%rip), %xmm0    # 0x1c008
callq 0x8470
movsd %xmm0, -0x48(%rbp)
movl $0x0, -0x60(%rbp)
movl -0x60(%rbp), %eax
movq -0x28(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x807d
xorps %xmm0, %xmm0
movsd %xmm0, -0x50(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x58(%rbp)
leaq -0x5c(%rbp), %rdi
movslq -0x3c(%rbp), %rdx
movq -0x30(%rbp), %rcx
movl $0x1, %esi
callq 0x1140
movl -0x3c(%rbp), %eax
subl $0x1, %eax
cltq
movzbl -0x5c(%rbp,%rax), %eax
cmpl $0x80, %eax
jl 0x8012
movq -0x20(%rbp), %rax
movl (%rax), %edi
subl $0x1, %edi
movsd 0x1401e(%rip), %xmm0    # 0x1c008
callq 0x8470
movsd %xmm0, -0x58(%rbp)
movl -0x3c(%rbp), %eax
subl $0x1, %eax
cltq
movzbl -0x5c(%rbp,%rax), %eax
andl $0x7f, %eax
movb %al, %cl
movl -0x3c(%rbp), %eax
subl $0x1, %eax
cltq
movb %cl, -0x5c(%rbp,%rax)
movl -0x3c(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x64(%rbp)
cmpl $0x0, -0x64(%rbp)
jl 0x8053
movsd -0x50(%rbp), %xmm0
movslq -0x64(%rbp), %rax
movzbl -0x5c(%rbp,%rax), %eax
cvtsi2sd %eax, %xmm1
movsd 0x1428d(%rip), %xmm2    # 0x1c2c8
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x50(%rbp)
movl -0x64(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x801b
movsd -0x50(%rbp), %xmm0
subsd -0x58(%rbp), %xmm0
divsd -0x48(%rbp), %xmm0
movq -0x38(%rbp), %rax
movslq -0x60(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x7f90
movq -0x30(%rbp), %rdi
callq 0x10d0
movq -0x38(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax,%rax)