// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rcx, %r14
movq %rdx, %r15
movq %rsi, %r12
leaq 0x9ebc(%rip), %rsi      # 0xf1ab
callq 0x10f0
testq %rax, %rax
je 0x532f
movq %rax, %rbx
movq %rsp, %r13
movl $0x1, %esi
movl $0x4, %edx
movq %r13, %rdi
movq %rax, %rcx
callq 0x1130
movb $0x0, 0x4(%r13)
cmpl $0x46464952, (%r13)     # imm = 0x46464952
je 0x5340
leaq 0x9f0f(%rip), %rdi      # 0xf239
jmp 0x555f
leaq 0xa2a7(%rip), %rdi      # 0xf5dd
callq 0x1150
jmp 0x556c
movl $0x4, %esi
movq %rbx, %rdi
movl $0x1, %edx
callq 0x10e0
movq %rsp, %r13
movl $0x1, %esi
movl $0x4, %edx
movq %r13, %rdi
movq %rbx, %rcx
callq 0x1130
movl $0x45564157, %eax       # imm = 0x45564157
xorl (%r13), %eax
movzbl 0x4(%r13), %ecx
orl %eax, %ecx
je 0x5388
leaq 0x9eaa(%rip), %rdi      # 0xf22d
jmp 0x555f
movq %rsp, %r13
movl $0x1, %esi
movl $0x4, %edx
movq %r13, %rdi
movq %rbx, %rcx
callq 0x1130
movl $0x20746d66, %eax       # imm = 0x20746D66
xorl (%r13), %eax
movzbl 0x4(%r13), %ecx
orl %eax, %ecx
je 0x53be
leaq 0x9e69(%rip), %rdi      # 0xf222
jmp 0x555f
movq %rsp, %r13
movl $0x1, %esi
movl $0x4, %edx
movq %r13, %rdi
movq %rbx, %rcx
callq 0x1130
cmpl $0x10, (%r13)
jne 0x5558
movq %rsp, %r13
movl $0x1, %esi
movl $0x2, %edx
movq %r13, %rdi
movq %rbx, %rcx
callq 0x1130
cmpb $0x1, (%r13)
leaq 0x9de3(%rip), %rdi      # 0xf1e8
jne 0x555f
cmpb $0x0, 0x1(%rsp)
jne 0x555f
movq %rsp, %r13
movl $0x1, %esi
movl $0x2, %edx
movq %r13, %rdi
movq %rbx, %rcx
callq 0x1130
cmpb $0x1, (%r13)
leaq 0x9dbf(%rip), %rdi      # 0xf1f9
jne 0x555f
cmpb $0x0, 0x1(%rsp)
jne 0x555f
movb $0x0, 0x4(%rsp)
movl $0x0, (%rsp)
leaq 0x8(%rsp), %rdi
movl $0x4, %r13d
movl $0x1, %esi
movl $0x4, %edx
movq %rbx, %rcx
callq 0x1130
xorl %eax, %eax
movl %eax, %ecx
shll $0x8, %ecx
movzbl 0x7(%rsp,%r13), %eax
orl %ecx, %eax
decq %r13
jne 0x5476
movl %eax, (%r12)
movl $0x6, %esi
movq %rbx, %rdi
movl $0x1, %edx
callq 0x10e0
leaq 0x8(%rsp), %r12
movl $0x1, %esi
movl $0x2, %edx
movq %r12, %rdi
movq %rbx, %rcx
callq 0x1130
movzbl (%r12), %eax
movl %eax, (%r15)
movq %rsp, %rdi
movl $0x1, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x1130
testq %rax, %rax
je 0x553e
leaq 0x1(%rsp), %r12
movl $0x61746164, %ebp       # imm = 0x61746164
movq %rsp, %r13
cmpb $0x64, (%rsp)
jne 0x5524
movl $0x1, %esi
movl $0x3, %edx
movq %r12, %rdi
movq %rbx, %rcx
callq 0x1130
movl (%rsp), %eax
xorl %ebp, %eax
movzbl 0x4(%rsp), %ecx
orl %eax, %ecx
je 0x553e
movq %rbx, %rdi
movq $-0x3, %rsi
movl $0x1, %edx
callq 0x10e0
movl $0x1, %esi
movl $0x1, %edx
movq %r13, %rdi
movq %rbx, %rcx
callq 0x1130
testq %rax, %rax
jne 0x54e7
movl $0x61746164, %eax       # imm = 0x61746164
xorl (%rsp), %eax
movzbl 0x4(%rsp), %ecx
orl %eax, %ecx
je 0x5581
leaq 0x9cef(%rip), %rdi      # 0xf245
jmp 0x555f
leaq 0x9c7a(%rip), %rdi      # 0xf1d9
callq 0x1150
movq %rbx, %rdi
callq 0x10d0
xorl %r12d, %r12d
movq %r12, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8(%rsp), %rdi
movl $0x4, %r12d
movl $0x1, %esi
movl $0x4, %edx
movq %rbx, %rcx
callq 0x1130
xorl %eax, %eax
movl %eax, %ecx
shll $0x8, %ecx
movzbl 0x7(%rsp,%r12), %eax
orl %ecx, %eax
decq %r12
jne 0x55a0
movl %eax, (%r14)
movl (%r15), %ecx
leal 0x7(%rcx), %esi
testl %ecx, %ecx
cmovnsl %ecx, %esi
sarl $0x3, %esi
cltd
idivl %esi
movl %eax, (%r14)
cltq
leaq (,%rax,8), %rcx
testl %eax, %eax
movq $-0x1, %rdi
cmovnsq %rcx, %rdi
callq 0x1030
movq %rax, %r12
movl (%r15), %eax
movq %rax, 0x10(%rsp)
leal -0x1(%rax), %edi
movsd 0x9a15(%rip), %xmm0     # 0xf010
callq 0x1140
movsd %xmm0, 0x30(%rsp)
cmpl $0x0, (%r14)
jle 0x56e0
movq 0x10(%rsp), %rax
leal 0x7(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
sarl $0x3, %ecx
movl %ecx, 0xc(%rsp)
movslq %ecx, %rax
movq %rax, 0x28(%rsp)
leaq -0x1(%rax), %rcx
movq %rcx, 0x18(%rsp)
movl %ecx, %eax
addq %rsp, %rax
movq %rax, 0x20(%rsp)
xorl %r13d, %r13d
movl $0x1, %esi
movq %rsp, %rdi
movq 0x28(%rsp), %rdx
movq %rbx, %rcx
callq 0x1130
movq 0x18(%rsp), %rax
movb (%rsp,%rax), %bpl
xorpd %xmm3, %xmm3
xorpd %xmm0, %xmm0
testb %bpl, %bpl
jns 0x5690
movl (%r15), %edi
decl %edi
movsd 0x9996(%rip), %xmm0     # 0xf010
callq 0x1140
xorpd %xmm3, %xmm3
andb $0x7f, %bpl
movq 0x18(%rsp), %rax
movb %bpl, (%rsp,%rax)
cmpl $0x8, 0x10(%rsp)
movsd 0x9b03(%rip), %xmm2     # 0xf1a0
jl 0x56c1
movq 0x20(%rsp), %rax
movl 0xc(%rsp), %ecx
movzbl (%rax), %edx
xorps %xmm1, %xmm1
cvtsi2sd %edx, %xmm1
mulsd %xmm2, %xmm3
addsd %xmm1, %xmm3
decq %rax
decl %ecx
jg 0x56a8
subsd %xmm0, %xmm3
divsd 0x30(%rsp), %xmm3
movsd %xmm3, (%r12,%r13,8)
incq %r13
movslq (%r14), %rax
cmpq %rax, %r13
jl 0x5642
movq %rbx, %rdi
callq 0x10d0
jmp 0x556f
nopl (%rax)