// File: /workspace/github_cpp_makefile_star2_O2/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 $0x28, %rsp
movq %rcx, %r14
movq %rdx, %r15
movq %rsi, %r12
leaq 0x88fd(%rip), %rsi      # 0xd143
callq 0x10f0
testq %rax, %rax
je 0x4890
movq %rax, %rbx
leaq 0x4(%rsp), %r13
pushq $0x1
popq %rsi
pushq $0x4
popq %rdx
movq %r13, %rdi
movq %rax, %rcx
callq 0x1130
movb $0x0, 0x4(%r13)
leaq 0x88d1(%rip), %rsi      # 0xd146
pushq $0x5
popq %rdx
movq %r13, %rdi
callq 0x10d0
testl %eax, %eax
je 0x48a1
leaq 0x8946(%rip), %rdi      # 0xd1d1
jmp 0x49d7
leaq 0x8ce6(%rip), %rdi      # 0xd57d
callq 0x1150
jmp 0x49e4
pushq $0x4
popq %r13
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x10e0
leaq 0x4(%rsp), %rbp
pushq $0x1
popq %rsi
movq %rbp, %rdi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x1130
leaq 0x887b(%rip), %rsi      # 0xd14b
pushq $0x5
popq %rdx
movq %rbp, %rdi
callq 0x10d0
testl %eax, %eax
je 0x48eb
leaq 0x88df(%rip), %rdi      # 0xd1c5
jmp 0x49d7
leaq 0x4(%rsp), %r13
pushq $0x1
popq %rsi
pushq $0x4
popq %rdx
movq %r13, %rdi
movq %rbx, %rcx
callq 0x1130
leaq 0x8848(%rip), %rsi      # 0xd150
pushq $0x5
popq %rdx
movq %r13, %rdi
callq 0x10d0
testl %eax, %eax
je 0x4923
leaq 0x889c(%rip), %rdi      # 0xd1ba
jmp 0x49d7
leaq 0x4(%rsp), %r13
pushq $0x1
popq %rsi
pushq $0x4
popq %rdx
movq %r13, %rdi
movq %rbx, %rcx
callq 0x1130
cmpl $0x10, (%r13)
jne 0x49d0
leaq 0x4(%rsp), %r13
pushq $0x1
popq %rsi
pushq $0x2
popq %rdx
movq %r13, %rdi
movq %rbx, %rcx
callq 0x1130
cmpb $0x1, (%r13)
leaq 0x881a(%rip), %rdi      # 0xd180
jne 0x49d7
cmpb $0x0, 0x5(%rsp)
jne 0x49d7
leaq 0x4(%rsp), %r13
pushq $0x1
popq %rsi
pushq $0x2
popq %rdx
movq %r13, %rdi
movq %rbx, %rcx
callq 0x1130
cmpb $0x1, (%r13)
leaq 0x8800(%rip), %rdi      # 0xd191
jne 0x49d7
cmpb $0x0, 0x5(%rsp)
jne 0x49d7
movb $0x0, 0x8(%rsp)
andl $0x0, 0x4(%rsp)
leaq 0xc(%rsp), %rdi
pushq $0x1
popq %rsi
pushq $0x4
popq %rdx
movq %rbx, %rcx
callq 0x1130
pushq $0x3
popq %rcx
xorl %eax, %eax
testl %ecx, %ecx
js 0x49f9
shll $0x8, %eax
movl %ecx, %edx
movzbl 0xc(%rsp,%rdx), %edx
orl %edx, %eax
decl %ecx
jmp 0x49bc
leaq 0x879a(%rip), %rdi      # 0xd171
callq 0x1150
movq %rbx, %rdi
callq 0x10c0
xorl %r12d, %r12d
movq %r12, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, (%r12)
pushq $0x6
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rdi
callq 0x10e0
leaq 0xc(%rsp), %r13
pushq $0x1
popq %r12
pushq $0x2
popq %rdx
movq %r13, %rdi
movq %r12, %rsi
movq %rbx, %rcx
callq 0x1130
movzbl (%r13), %eax
movl %eax, (%r15)
leaq 0x4(%rsp), %rbp
pushq $0x5
popq %r13
movq %rbp, %rdi
movq %r12, %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x1130
testq %rax, %rax
je 0x4a8c
cmpb $0x64, 0x4(%rsp)
jne 0x4a36
leaq 0x5(%rsp), %rdi
movq %r12, %rsi
pushq $0x3
popq %rdx
movq %rbx, %rcx
callq 0x1130
movq %rbp, %rdi
leaq 0x86e5(%rip), %rsi      # 0xd155
movq %r13, %rdx
callq 0x10d0
testl %eax, %eax
je 0x4a8c
movq %rbx, %rdi
pushq $-0x3
popq %rsi
pushq $0x1
popq %rdx
callq 0x10e0
jmp 0x4a36
leaq 0x86c2(%rip), %rsi      # 0xd155
leaq 0x4(%rsp), %rdi
pushq $0x5
popq %rdx
callq 0x10d0
testl %eax, %eax
je 0x4ab0
leaq 0x8732(%rip), %rdi      # 0xd1dd
jmp 0x49d7
leaq 0xc(%rsp), %rdi
pushq $0x1
popq %rsi
pushq $0x4
popq %rdx
movq %rbx, %rcx
callq 0x1130
pushq $0x3
popq %rax
xorl %ecx, %ecx
testl %eax, %eax
js 0x4adc
shll $0x8, %ecx
movl %eax, %edx
movzbl 0xc(%rsp,%rdx), %edx
orl %edx, %ecx
decl %eax
jmp 0x4ac8
movl %ecx, (%r14)
movl (%r15), %eax
pushq $0x8
popq %rbp
cltd
idivl %ebp
movl %eax, %esi
movl %ecx, %eax
cltd
idivl %esi
movl %eax, (%r14)
cltq
leaq (,%rax,8), %rcx
testl %eax, %eax
pushq $-0x1
popq %rdi
cmovnsq %rcx, %rdi
callq 0x1030
movq %rax, %r12
movl (%r15), %edi
movl %edi, %eax
cltd
idivl %ebp
movl %eax, %ebp
decl %edi
movsd 0x84ef(%rip), %xmm0     # 0xd010
callq 0x1140
movsd %xmm0, 0x18(%rsp)
movslq %ebp, %rax
movq %rax, 0x10(%rsp)
leaq -0x1(%rax), %rdx
xorl %ecx, %ecx
movslq (%r14), %rax
cmpq %rax, %rcx
jge 0x4bdc
movq %rcx, 0x20(%rsp)
leaq 0x4(%rsp), %rdi
pushq $0x1
popq %rsi
movq %rdx, %r13
movq 0x10(%rsp), %rdx
movq %rbx, %rcx
callq 0x1130
movq %r13, %rdx
movb 0x4(%rsp,%r13), %bpl
xorpd %xmm3, %xmm3
xorpd %xmm0, %xmm0
testb %bpl, %bpl
jns 0x4b9a
movl (%r15), %edi
decl %edi
movsd 0x848b(%rip), %xmm0     # 0xd010
callq 0x1140
xorpd %xmm3, %xmm3
movq %r13, %rdx
andb $0x7f, %bpl
movb %bpl, 0x4(%rsp,%r13)
movl %edx, %eax
movsd 0x8594(%rip), %xmm2     # 0xd138
testl %eax, %eax
js 0x4bbf
movl %eax, %ecx
movzbl 0x4(%rsp,%rcx), %ecx
cvtsi2sd %ecx, %xmm1
mulsd %xmm2, %xmm3
addsd %xmm1, %xmm3
decl %eax
jmp 0x4ba4
subsd %xmm0, %xmm3
divsd 0x18(%rsp), %xmm3
movq 0x20(%rsp), %rcx
movsd %xmm3, (%r12,%rcx,8)
incq %rcx
jmp 0x4b3a
movq %rbx, %rdi
callq 0x10c0
jmp 0x49e7
nopl (%rax)