// File: jp-myk[P]WAVUtil/src/SimpleWAV/WAVUtil.cpp
// Function: SimpleWAV::combine_wav(WAV&, WAV&, WAV&)
int combine_wav(WAV& wav1, WAV& wav2, WAV& outwav){
  
  if(wav1.getChannels() != wav2.getChannels()){
    printLOG("wav1 and wav2 are not same channel");
    return -1;
  }

  if(wav1.getSampleRate() != wav2.getSampleRate()){
    printLOG("wav1 and wav2 are not same samplerate");
    return -1;
  }

  if(wav1.getBit() != wav2.getBit()){
    printLOG("wav1 and wav2 are not same bit");
    return -1;
  }
  
  outwav.createHeader(wav1.getChannels(), wav1.getSampleRate(), wav1.getBit());
  
  unsigned int datasize=0;
  void* combinedData;
  datasize+=wav1.getDataSize();
  datasize+=wav2.getDataSize();
  combinedData=(void*)malloc(datasize);
  
  memcpy(combinedData, wav1.getData(), wav1.getDataSize());
  memcpy((char*)combinedData+wav1.getDataSize(), wav2.getData(), wav2.getDataSize());    
    
  if(outwav.setData(combinedData, datasize)!=0){
    free(combinedData);
    printLOG("error combine wav1 and wav2");
    return -1;
  }
  
  free(combinedData);
  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rdi
callq 0x38a0
movzwl %ax, %eax
movl %eax, -0x34(%rbp)
movq -0x18(%rbp), %rdi
callq 0x38a0
movw %ax, %cx
movl -0x34(%rbp), %eax
movzwl %cx, %ecx
cmpl %ecx, %eax
je 0x3df0
leaq 0x2241(%rip), %rdi      # 0x6004
leaq 0x2439(%rip), %rsi      # 0x6203
leaq 0x244c(%rip), %rdx      # 0x621d
movl $0x41, %ecx
leaq 0x244c(%rip), %r8       # 0x6229
movb $0x0, %al
callq 0x2030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3fe3
movq -0x10(%rbp), %rdi
callq 0x38c0
movl %eax, -0x38(%rbp)
movq -0x18(%rbp), %rdi
callq 0x38c0
movl %eax, %ecx
movl -0x38(%rbp), %eax
cmpl %ecx, %eax
je 0x3e42
leaq 0x21ef(%rip), %rdi      # 0x6004
leaq 0x23e7(%rip), %rsi      # 0x6203
leaq 0x23fa(%rip), %rdx      # 0x621d
movl $0x46, %ecx
leaq 0x241d(%rip), %r8       # 0x624c
movb $0x0, %al
callq 0x2030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3fe3
movq -0x10(%rbp), %rdi
callq 0x38e0
movzwl %ax, %eax
movl %eax, -0x3c(%rbp)
movq -0x18(%rbp), %rdi
callq 0x38e0
movw %ax, %cx
movl -0x3c(%rbp), %eax
movzwl %cx, %ecx
cmpl %ecx, %eax
je 0x3e9b
leaq 0x2196(%rip), %rdi      # 0x6004
leaq 0x238e(%rip), %rsi      # 0x6203
leaq 0x23a1(%rip), %rdx      # 0x621d
movl $0x4b, %ecx
leaq 0x23ea(%rip), %r8       # 0x6272
movb $0x0, %al
callq 0x2030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3fe3
movq -0x20(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x10(%rbp), %rdi
callq 0x38a0
movw %ax, -0x6e(%rbp)
movq -0x10(%rbp), %rdi
callq 0x38c0
movl %eax, -0x6c(%rbp)
movq -0x10(%rbp), %rdi
callq 0x38e0
movq -0x78(%rbp), %rdi
movw -0x6e(%rbp), %cx
movl -0x6c(%rbp), %edx
movzwl %cx, %esi
movzwl %ax, %ecx
callq 0x24c0
movl $0x0, -0x24(%rbp)
movq -0x10(%rbp), %rdi
callq 0x3900
addl -0x24(%rbp), %eax
movl %eax, -0x24(%rbp)
movq -0x18(%rbp), %rdi
callq 0x3900
addl -0x24(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
movl %eax, %edi
callq 0x2180
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x10(%rbp), %rdi
callq 0x2720
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rdi
callq 0x3900
movq -0x68(%rbp), %rdi
movq -0x60(%rbp), %rsi
movl %eax, %eax
movl %eax, %edx
callq 0x20e0
movq -0x30(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rdi
callq 0x3900
movl %eax, %ecx
movq -0x58(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x18(%rbp), %rdi
callq 0x2720
movq %rax, -0x48(%rbp)
movq -0x18(%rbp), %rdi
callq 0x3900
movq -0x50(%rbp), %rdi
movq -0x48(%rbp), %rsi
movl %eax, %eax
movl %eax, %edx
callq 0x20e0
movq -0x20(%rbp), %rdi
movq -0x30(%rbp), %rsi
movl -0x24(%rbp), %edx
callq 0x2570
cmpl $0x0, %eax
je 0x3fd3
movq -0x30(%rbp), %rdi
callq 0x2140
leaq 0x205b(%rip), %rdi      # 0x6004
leaq 0x2253(%rip), %rsi      # 0x6203
leaq 0x2266(%rip), %rdx      # 0x621d
movl $0x5c, %ecx
leaq 0x22ce(%rip), %r8       # 0x6291
movb $0x0, %al
callq 0x2030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3fe3
movq -0x30(%rbp), %rdi
callq 0x2140
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nop