// File: /workspace/github_cpp_makefile_star2_O3/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, %r15d
movq %rdi, %rbx
leaq 0xa105(%rip), %rsi      # 0xf1a8
movq %r8, %rdi
callq 0x10f0
testq %rax, %rax
je 0x52b7
movq %rax, %r14
leaq 0x10(%rsp), %r13
movl $0x46464952, (%r13)     # imm = 0x46464952
leal (%r15,%r15), %eax
movl %eax, 0xc(%rsp)
leal 0x24(%r15,%r15), %eax
leaq 0x24(%rsp), %r12
movl %eax, (%r12)
movl $0x1, %esi
movl $0x4, %edx
movq %r13, %rdi
movq %r14, %rcx
callq 0x11c0
movl $0x4, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %r14, %rcx
callq 0x11c0
movl $0x45564157, (%r13)     # imm = 0x45564157
movl $0x1, %esi
movl $0x4, %edx
movq %r13, %rdi
movq %r14, %rcx
callq 0x11c0
movl $0x20746d66, (%r13)     # imm = 0x20746D66
movl $0x1, %esi
movl $0x4, %edx
movq %r13, %rdi
movq %r14, %rcx
callq 0x11c0
movl $0x10, (%r12)
movl $0x4, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %r14, %rcx
callq 0x11c0
movw $0x1, %r13w
movq %r15, 0x18(%rsp)
leaq 0x16(%rsp), %r15
movw %r13w, (%r15)
movl $0x2, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x11c0
movw %r13w, (%r15)
movl $0x2, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x11c0
movl %ebp, (%r12)
movl $0x4, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %r14, %rcx
callq 0x11c0
addl %ebp, %ebp
movl %ebp, (%r12)
movl $0x4, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %r14, %rcx
callq 0x11c0
movw $0x2, (%r15)
movl $0x2, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x11c0
movw $0x10, (%r15)
movl $0x2, %esi
movl $0x1, %edx
movq %r15, %rdi
movq 0x18(%rsp), %r15
movq %r14, %rcx
callq 0x11c0
leaq 0x10(%rsp), %rdi
movl $0x61746164, (%rdi)     # imm = 0x61746164
movl $0x1, %esi
movl $0x4, %edx
movq %r14, %rcx
callq 0x11c0
movl 0xc(%rsp), %eax
movl %eax, (%r12)
movl $0x4, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %r14, %rcx
callq 0x11c0
testl %r15d, %r15d
jle 0x52a1
movl %r15d, %r12d
xorl %r13d, %r13d
leaq 0xa(%rsp), %r15
movsd (%rbx,%r13,8), %xmm0
movsd 0x9f2d(%rip), %xmm2     # 0xf190
mulsd %xmm2, %xmm0
movsd 0x9f29(%rip), %xmm1     # 0xf198
maxsd %xmm0, %xmm1
movapd %xmm2, %xmm0
minsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movw %ax, 0xa(%rsp)
movl $0x2, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x11c0
incq %r13
cmpq %r13, %r12
jne 0x5255
movq %r14, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x10d0
leaq 0x9f04(%rip), %rdi      # 0xf1c2
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1150