// File: /workspace/github_cpp_makefile_star2_O1/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 0xa40d(%rip), %rsi      # 0xf113
callq 0x10f0
testq %rax, %rax
je 0x4d46
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 0x4d57
leaq 0xa460(%rip), %rdi      # 0xf1a1
jmp 0x4dcd
leaq 0xa7b0(%rip), %rdi      # 0xf4fd
callq 0x1150
jmp 0x4dda
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 0x4d9c
leaq 0xa3fb(%rip), %rdi      # 0xf195
jmp 0x4dcd
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 0x4def
leaq 0xa3bd(%rip), %rdi      # 0xf18a
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
movq %rsp, %r13
movl $0x1, %esi
movl $0x4, %edx
movq %r13, %rdi
movq %rbx, %rcx
callq 0x1130
cmpb $0x10, (%r13)
leaq 0xa32e(%rip), %rdi      # 0xf141
jne 0x4dcd
cmpb $0x0, 0x1(%rsp)
jne 0x4dcd
cmpb $0x0, 0x2(%rsp)
jne 0x4dcd
cmpb $0x0, 0x3(%rsp)
jne 0x4dcd
movq %rsp, %r13
movl $0x1, %esi
movl $0x2, %edx
movq %r13, %rdi
movq %rbx, %rcx
callq 0x1130
cmpb $0x1, (%r13)
leaq 0xa302(%rip), %rdi      # 0xf150
jne 0x4dcd
cmpb $0x0, 0x1(%rsp)
jne 0x4dcd
movq %rsp, %r13
movl $0x1, %esi
movl $0x2, %edx
movq %r13, %rdi
movq %rbx, %rcx
callq 0x1130
cmpb $0x1, (%r13)
leaq 0xa2de(%rip), %rdi      # 0xf161
jne 0x4dcd
cmpb $0x0, 0x1(%rsp)
jne 0x4dcd
xorl %ebp, %ebp
movl %ebp, (%rsp)
movb $0x0, 0x4(%rsp)
leaq 0x8(%rsp), %rdi
movl $0x4, %r13d
movl $0x1, %esi
movl $0x4, %edx
movq %rbx, %rcx
callq 0x1130
movl %ebp, (%r12)
movl %ebp, %eax
shll $0x8, %eax
movzbl 0x7(%rsp,%r13), %ebp
orl %eax, %ebp
decq %r13
jne 0x4ebf
movl %ebp, (%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 0x4f87
leaq 0x1(%rsp), %r12
movl $0x61746164, %ebp       # imm = 0x61746164
movq %rsp, %r13
cmpb $0x64, (%rsp)
jne 0x4f6d
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 0x4f87
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 0x4f30
movl $0x61746164, %eax       # imm = 0x61746164
xorl (%rsp), %eax
movzbl 0x4(%rsp), %ecx
xorl %ebp, %ebp
orl %eax, %ecx
setne %bpl
je 0x4fac
leaq 0xa208(%rip), %rdi      # 0xf1ad
callq 0x1150
jmp 0x4ffb
leaq 0x8(%rsp), %rdi
movl $0x4, %r12d
movl $0x1, %esi
movl $0x4, %edx
movq %rbx, %rcx
callq 0x1130
movl $0x0, (%r14)
xorl %eax, %eax
movl %eax, %ecx
shll $0x8, %ecx
movzbl 0x7(%rsp,%r12), %eax
orl %ecx, %eax
decq %r12
jne 0x4fd2
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)
testl %ebp, %ebp
jne 0x4dd2
movslq (%r14), %rax
leaq (,%rax,8), %rcx
testq %rax, %rax
movq $-0x1, %rdi
cmovnsq %rcx, %rdi
callq 0x1030
movq %rax, %r12
movl (%r15), %eax
movq %rax, 0x10(%rsp)
leal -0x1(%rax), %edi
movsd 0x9fd9(%rip), %xmm0     # 0xf010
callq 0x1140
movsd %xmm0, 0x30(%rsp)
cmpl $0x0, (%r14)
jle 0x511c
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 0x50cc
movl (%r15), %edi
decl %edi
movsd 0x9f5a(%rip), %xmm0     # 0xf010
callq 0x1140
xorpd %xmm3, %xmm3
andb $0x7f, %bpl
movq 0x18(%rsp), %rax
movb %bpl, (%rsp,%rax)
cmpl $0x8, 0x10(%rsp)
movsd 0xa02f(%rip), %xmm2     # 0xf108
jl 0x50fd
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 0x50e4
subsd %xmm0, %xmm3
divsd 0x30(%rsp), %xmm3
movsd %xmm3, (%r12,%r13,8)
incq %r13
movslq (%r14), %rax
cmpq %rax, %r13
jl 0x507e
movq %rbx, %rdi
callq 0x10d0
jmp 0x4ddd
nopl (%rax)