// File: /workspace/github_cpp_makefile_star2_O2/Sleepwalking[P]world-octave/matlabfunctions.cpp
// Function: wavwrite(double*, int, int, int, char*)
void wavwrite(double *x, int x_length, int fs, int nbit, char *filename) {
  FILE *fp = fopen(filename, "wb");
  if (fp == NULL) {
    printf("File cannot be opened.\n");
    return;
  }

  char text[4] = {'R', 'I', 'F', 'F'};
  uint32_t long_number = 36 + x_length * 2;
  fwrite(text, 1, 4, fp);
  fwrite(&long_number, 4, 1, fp);

  text[0] = 'W';
  text[1] = 'A';
  text[2] = 'V';
  text[3] = 'E';
  fwrite(text, 1, 4, fp);
  text[0] = 'f';
  text[1] = 'm';
  text[2] = 't';
  text[3] = ' ';
  fwrite(text, 1, 4, fp);

  long_number = 16;
  fwrite(&long_number, 4, 1, fp);
  int16_t short_number = 1;
  fwrite(&short_number, 2, 1, fp);
  short_number = 1;
  fwrite(&short_number, 2, 1, fp);
  long_number = fs;
  fwrite(&long_number, 4, 1, fp);
  long_number = fs * 2;
  fwrite(&long_number, 4, 1, fp);
  short_number = 2;
  fwrite(&short_number, 2, 1, fp);
  short_number = 16;
  fwrite(&short_number, 2, 1, fp);

  text[0] = 'd';
  text[1] = 'a';
  text[2] = 't';
  text[3] = 'a';
  fwrite(text, 1, 4, fp);
  long_number = x_length * 2;
  fwrite(&long_number, 4, 1, fp);

  int16_t tmp_signal;
  for (int i = 0; i < x_length; ++i) {
    tmp_signal = static_cast<int16_t>(MyMax(-32768,
        MyMin(32767, static_cast<int>(x[i] * 32767))));
    fwrite(&tmp_signal, 2, 1, fp);
  }

  fclose(fp);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %edx, %ebp
movl %esi, %ebx
movq %rdi, 0x20(%rsp)
leaq 0x8b28(%rip), %rsi      # 0xd140
movq %r8, %rdi
callq 0x10f0
testq %rax, %rax
je 0x480e
movq %rax, %r15
leaq 0x18(%rsp), %r13
movl $0x46464952, (%r13)     # imm = 0x46464952
leal (%rbx,%rbx), %eax
movl %eax, 0x14(%rsp)
leal 0x24(%rbx,%rbx), %eax
leaq 0xc(%rsp), %rcx
movl %eax, (%rcx)
pushq $0x1
popq %r12
pushq $0x4
popq %r14
movq %r13, %rdi
movq %r12, %rsi
movq %r14, %rdx
movq %r15, %rcx
callq 0x11c0
leaq 0xc(%rsp), %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x11c0
movl $0x45564157, (%r13)     # imm = 0x45564157
movq %r13, %rdi
movq %r12, %rsi
movq %r14, %rdx
movq %r15, %rcx
callq 0x11c0
movl $0x20746d66, (%r13)     # imm = 0x20746D66
movq %r13, %rdi
movq %r12, %rsi
movq %r14, %rdx
movq %r15, %rcx
callq 0x11c0
leaq 0xc(%rsp), %rdi
movl $0x10, (%rdi)
movq %r14, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x11c0
movw $0x1, %ax
leaq 0x1e(%rsp), %r13
movw %ax, (%r13)
pushq $0x2
popq %r14
movq %r13, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x11c0
movw $0x1, %ax
movw %ax, (%r13)
movq %r13, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x11c0
leaq 0xc(%rsp), %rdi
movl %ebp, (%rdi)
pushq $0x4
popq %r14
movq %r14, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x11c0
addl %ebp, %ebp
leaq 0xc(%rsp), %rdi
movl %ebp, (%rdi)
movq %r14, %rsi
movq %r14, %rbp
movq %r12, %rdx
movq %r15, %rcx
callq 0x11c0
movw $0x2, (%r13)
movq %r13, %rdi
pushq $0x2
popq %r14
movq %r14, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x11c0
movw $0x10, (%r13)
movq %r13, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x11c0
leaq 0x18(%rsp), %rdi
movl $0x61746164, (%rdi)     # imm = 0x61746164
movq %r12, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x11c0
movl 0x14(%rsp), %eax
leaq 0xc(%rsp), %rdi
movl %eax, (%rdi)
movq %rbp, %rsi
movq %r12, %rbp
movq %r12, %rdx
movq %r15, %rcx
callq 0x11c0
xorl %r12d, %r12d
testl %ebx, %ebx
cmovlel %r12d, %ebx
leaq 0x12(%rsp), %r13
cmpq %r12, %rbx
je 0x47f8
movq 0x20(%rsp), %rax
movsd (%rax,%r12,8), %xmm0
movsd 0x8967(%rip), %xmm2     # 0xd128
mulsd %xmm2, %xmm0
movsd 0x8963(%rip), %xmm1     # 0xd130
maxsd %xmm0, %xmm1
movapd %xmm2, %xmm0
minsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movw %ax, 0x12(%rsp)
movq %r13, %rdi
movq %r14, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x11c0
incq %r12
jmp 0x47a9
movq %r15, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x10c0
leaq 0x8945(%rip), %rdi      # 0xd15a
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1150