// File: 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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movl %ecx, -0x14(%rbp)
movq %r8, -0x20(%rbp)
movq -0x20(%rbp), %rdi
leaq 0x146c8(%rip), %rsi     # 0x1c2fc
callq 0x10f0
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x7c57
leaq 0x146b4(%rip), %rdi     # 0x1c2ff
movb $0x0, %al
callq 0x1040
jmp 0x7e68
movl 0x14750(%rip), %eax     # 0x1c3ad
movl %eax, -0x2c(%rbp)
movl -0xc(%rbp), %eax
shll %eax
addl $0x24, %eax
movl %eax, -0x30(%rbp)
leaq -0x2c(%rbp), %rdi
movq -0x28(%rbp), %rcx
movl $0x1, %esi
movl $0x4, %edx
callq 0x11b0
movq -0x28(%rbp), %rcx
leaq -0x30(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x11b0
movb $0x57, -0x2c(%rbp)
movb $0x41, -0x2b(%rbp)
movb $0x56, -0x2a(%rbp)
movb $0x45, -0x29(%rbp)
leaq -0x2c(%rbp), %rdi
movq -0x28(%rbp), %rcx
movl $0x1, %esi
movl $0x4, %edx
callq 0x11b0
movb $0x66, -0x2c(%rbp)
movb $0x6d, -0x2b(%rbp)
movb $0x74, -0x2a(%rbp)
movb $0x20, -0x29(%rbp)
leaq -0x2c(%rbp), %rdi
movq -0x28(%rbp), %rcx
movl $0x1, %esi
movl $0x4, %edx
callq 0x11b0
movl $0x10, -0x30(%rbp)
movq -0x28(%rbp), %rcx
leaq -0x30(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x11b0
movw $0x1, -0x32(%rbp)
movq -0x28(%rbp), %rcx
leaq -0x32(%rbp), %rdi
movl $0x2, %esi
movl $0x1, %edx
callq 0x11b0
movw $0x1, -0x32(%rbp)
movq -0x28(%rbp), %rcx
leaq -0x32(%rbp), %rdi
movl $0x2, %esi
movl $0x1, %edx
callq 0x11b0
movl -0x10(%rbp), %eax
movl %eax, -0x30(%rbp)
movq -0x28(%rbp), %rcx
leaq -0x30(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x11b0
movl -0x10(%rbp), %eax
shll %eax
movl %eax, -0x30(%rbp)
movq -0x28(%rbp), %rcx
leaq -0x30(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x11b0
movw $0x2, -0x32(%rbp)
movq -0x28(%rbp), %rcx
leaq -0x32(%rbp), %rdi
movl $0x2, %esi
movl $0x1, %edx
callq 0x11b0
movw $0x10, -0x32(%rbp)
movq -0x28(%rbp), %rcx
leaq -0x32(%rbp), %rdi
movl $0x2, %esi
movl $0x1, %edx
callq 0x11b0
movb $0x64, -0x2c(%rbp)
movb $0x61, -0x2b(%rbp)
movb $0x74, -0x2a(%rbp)
movb $0x61, -0x29(%rbp)
leaq -0x2c(%rbp), %rdi
movq -0x28(%rbp), %rcx
movl $0x1, %esi
movl $0x4, %edx
callq 0x11b0
movl -0xc(%rbp), %eax
shll %eax
movl %eax, -0x30(%rbp)
movq -0x28(%rbp), %rcx
leaq -0x30(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x11b0
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x7e5f
movq -0x8(%rbp), %rax
movslq -0x38(%rbp), %rcx
movsd 0x144a6(%rip), %xmm0    # 0x1c2c0
mulsd (%rax,%rcx,8), %xmm0
cvttsd2si %xmm0, %esi
movl $0x7fff, %edi           # imm = 0x7FFF
callq 0x4fb0
movl %eax, %esi
movl $0xffff8000, %edi       # imm = 0xFFFF8000
callq 0x4f80
movw %ax, -0x34(%rbp)
movq -0x28(%rbp), %rcx
leaq -0x34(%rbp), %rdi
movl $0x2, %esi
movl $0x1, %edx
callq 0x11b0
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x7e02
movq -0x28(%rbp), %rdi
callq 0x10d0
addq $0x40, %rsp
popq %rbp
retq
nop