// File: /workspace/github_c_makefile_star2_O1/mincebert[P]maketzx/maketzx.c
// Function: BeepSearch
word BeepSearch(word minlen)
{
  byte tolerance,noise,nobeep;
  word count,pulsecount;
  dword pulse=0,
	maxlim=samprate/(2*425),
	minlim=samprate/(2*3500);
  double beepfreq,wavelen;
  mspause=0;
  PushFilePointers();
  do {
       noise=0; nobeep=0; wavelen=0; beeplength=0; pulsecount=0;
       do {
	    tolerance=(pulse=FindEdge1())/2;
	    if (!blocktype) return 0;
//new code begins
	    if (!tonesearch && tolerance>4) tolerance=4;
//new code ends
	  }
       while ((pulse-lastpulse)>tolerance || pulse<minlim || pulse>maxlim);
       wavelen=(beeplength=pulse)*2; pulsecount=1;
//new code begins
       if (tonesearch)
	  { tolerance=wavelen-samprate/((samprate/wavelen)+400); }
//new code ends
       for (count=0; count<minlen; count++)
	   {
	     pulse=FindEdge2();
	     if (fabs(pulse-wavelen)>tolerance)
	     { noise=1; break; }
	     else { beeplength+=pulse; pulsecount+=2; }
	   }
     }
  while (noise || nobeep);
  if (!raw)
     do {
	  pulse=FindEdge1();
	  if (!blocktype) return 0;
	  beeplength+=pulse; pulsecount++;
	}
     while (fabs(pulse-wavelen/2)<=tolerance || ((pulse-5)<3 && tonesearch));
  beeplength-=pulse;
  wavelen=(float)2*beeplength/pulsecount;
  beepfreq=(samprate/wavelen);
  if (CheckingSpeed)
     {
       PopFilePointers();
       return beepfreq;
     }
  msbeep=(beeplength*1000)/samprate;
  if (msbeep<30 || msbeep>100)
     {
       if (!tonesearch)
	  {
	    PopFilePointers();
	    lseek(inpfile.handle,filepos,SEEK_SET);
	    inpfile.bufpoint=0;
	    mspause+=(FindPause()/samprate)*1000;
	    WriteTZXPause(mspause);
	    endbeep=1;
	  }
       else RemoveFilePointers();
     }
  else {
	 byte count;
	 word beeptstates;
	 if (beepfreq) beeptstates=(word)((float)(3500000/(2*beepfreq))+.5);
	 if (!tonesearch)
	    {
	      WriteTZXTone(beeptstates,beeplength/(wavelen/2));
	      pulses[1]=6;
	      for (count=0; count<6; count++)
		  { pulseseq[count]=bump[count]; }
	      WriteTZXPulseSequence(sizeof(bump));
	    }
	 RemoveFilePointers();
       }
  mspause+=(pulse/samprate)*1000;
  return beepfreq;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movss 0xbd7d(%rip), %xmm0     # 0xf1f0
movaps %xmm0, %xmm1
divss 0x7f6e(%rip), %xmm1     # 0xb3ec
movl %edi, %ebx
movss 0x7f68(%rip), %xmm2     # 0xb3f0
cvttss2si %xmm1, %rax
subss %xmm2, %xmm1
cvttss2si %xmm1, %rcx
movq %rax, %r12
sarq $0x3f, %r12
andq %rcx, %r12
divss 0x7f4c(%rip), %xmm0     # 0xb3f4
orq %rax, %r12
cvttss2si %xmm0, %rax
subss %xmm2, %xmm0
cvttss2si %xmm0, %rcx
movq %rax, %r13
sarq $0x3f, %r13
andq %rcx, %r13
orq %rax, %r13
movq $0x0, 0xbd4f(%rip) # 0xf220
callq 0x165a
testl %ebx, %ebx
setne 0xf(%rsp)
movq %rbx, 0x10(%rsp)
leal 0x1(,%rbx,2), %eax
movl %eax, 0x1c(%rsp)
movq $0x0, 0x18a38(%rip) # 0x1bf30
movl $0x4, %ebp
callq 0x24b6
xorl %r14d, %r14d
cmpb $0x0, 0x181b5(%rip)     # 0x1b6c1
je 0x3965
movq %rax, %rbx
movl %ebx, %ecx
shrl %ecx
movb 0xbce1(%rip), %al       # 0xf200
movl %ebx, %edx
andl $0x1fe, %edx            # imm = 0x1FE
cmpl $0x9, %edx
movl %ecx, %r15d
cmovael %ebp, %r15d
testb %al, %al
cmovnel %ecx, %r15d
movq %rbx, %rcx
subq 0x1818f(%rip), %rcx     # 0x1b6d0
movzbl %r15b, %edx
cmpq %rdx, %rcx
ja 0x34fd
cmpq %r13, %rbx
jb 0x34fd
cmpq %r12, %rbx
ja 0x34fd
movq %rbx, %rcx
addq %rbx, %rcx
movq %rbx, 0x189cf(%rip)     # 0x1bf30
movq %rcx, %xmm0
punpckldq 0x7ea2(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x7eaa(%rip), %xmm0     # 0xb420
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
addsd %xmm0, %xmm2
testb %al, %al
je 0x35b1
xorps %xmm0, %xmm0
cvtss2sd 0xbc5f(%rip), %xmm0     # 0xf1f0
movaps %xmm0, %xmm1
divsd %xmm2, %xmm1
addsd 0x7d78(%rip), %xmm1     # 0xb318
divsd %xmm1, %xmm0
movapd %xmm2, %xmm1
subsd %xmm0, %xmm1
cvttsd2si %xmm1, %r15d
movl $0x1, %ebp
cmpl $0x0, 0x10(%rsp)
movapd %xmm2, 0x20(%rsp)
je 0x3613
movzbl %r15b, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x38(%rsp)
callq 0x2648
movq %rax, %rbx
movq %rax, %xmm0
punpckldq 0x7e27(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x7e2f(%rip), %xmm0     # 0xb420
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
subsd 0x20(%rsp), %xmm1
andpd 0x7df5(%rip), %xmm1     # 0xb400
ucomisd 0x38(%rsp), %xmm1
jbe 0x3619
movb 0xf(%rsp), %al
jmp 0x3692
movq %r12, 0x40(%rsp)
movl $0x1, %eax
movl $0x1, %ebp
movl %eax, %r12d
addq %rbx, 0x188fe(%rip)     # 0x1bf30
cmpl %eax, 0x10(%rsp)
je 0x3681
addl $0x2, %ebp
callq 0x2648
movq %rax, %rbx
movq %rax, %xmm0
punpckldq 0x7dc0(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x7dc8(%rip), %xmm0     # 0xb420
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
subsd 0x20(%rsp), %xmm1
andpd 0x7d8e(%rip), %xmm1     # 0xb400
leal 0x1(%r12), %eax
ucomisd 0x38(%rsp), %xmm1
jbe 0x3628
jmp 0x3685
movl 0x1c(%rsp), %ebp
cmpl 0x10(%rsp), %r12d
setb %al
movq 0x40(%rsp), %r12
testb %al, %al
jne 0x34ed
cmpb $0x0, 0xbb15(%rip)      # 0xf1b6
je 0x36c7
movq 0x18886(%rip), %rax     # 0x1bf30
subq %rbx, %rax
movq %rax, 0x1887c(%rip)     # 0x1bf30
js 0x377c
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
jmp 0x3796
callq 0x24b6
cmpb $0x0, 0x17fee(%rip)     # 0x1b6c1
je 0x3965
movq %rax, %rbx
movapd 0x20(%rsp), %xmm2
mulsd 0x7be6(%rip), %xmm2     # 0xb2d0
movzbl %r15b, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x10(%rsp)
incl %ebp
movapd %xmm2, 0x20(%rsp)
addq %rbx, 0x18826(%rip)     # 0x1bf30
movq %rbx, %xmm0
punpckldq 0x7cf9(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x7d01(%rip), %xmm0     # 0xb420
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
subsd %xmm2, %xmm1
andpd 0x7cc9(%rip), %xmm1     # 0xb400
movsd 0x10(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jae 0x375e
leaq -0x5(%rbx), %rax
cmpq $0x2, %rax
ja 0x36a3
cmpb $0x0, 0xbaa8(%rip)      # 0xf200
je 0x36a3
callq 0x24b6
movapd 0x20(%rsp), %xmm2
movq %rax, %rbx
incl %ebp
cmpb $0x0, 0x17f4c(%rip)     # 0x1b6c1
jne 0x3703
jmp 0x3965
movq %rax, %rcx
shrq %rcx
movl %eax, %edx
andl $0x1, %edx
orq %rcx, %rdx
xorps %xmm0, %xmm0
cvtsi2ss %rdx, %xmm0
addss %xmm0, %xmm0
movl %ebp, %ecx
xorps %xmm1, %xmm1
cvtsi2ss %rcx, %xmm1
addss %xmm0, %xmm0
divss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
movss 0xba3c(%rip), %xmm1     # 0xf1f0
cvtss2sd %xmm1, %xmm3
divsd %xmm0, %xmm3
cmpb $0x0, 0xb9f9(%rip)      # 0xf1bc
movsd %xmm3, 0x10(%rsp)
je 0x37dc
callq 0x16fb
cvttsd2si 0x10(%rsp), %r14
jmp 0x3965
imulq $0x3e8, %rax, %rcx      # imm = 0x3E8
testq %rcx, %rcx
js 0x37f2
xorps %xmm2, %xmm2
cvtsi2ss %rcx, %xmm2
jmp 0x3801
shrq %rcx
xorps %xmm2, %xmm2
cvtsi2ss %rcx, %xmm2
addss %xmm2, %xmm2
divss %xmm1, %xmm2
cvttss2si %xmm2, %rcx
movl %ecx, 0x18728(%rip)     # 0x1bf38
addl $-0x65, %ecx
cmpl $-0x48, %ecx
ja 0x3864
cmpb $0x0, 0xb9e1(%rip)      # 0xf200
jne 0x390a
callq 0x16fb
movzbl 0xba4f(%rip), %edi      # 0xf280
movq 0xb9b0(%rip), %rsi      # 0xf1e8
xorl %edx, %edx
callq 0x10d0
movl $0x0, 0xbb37(%rip)      # 0xf380
callq 0x4488
testq %rax, %rax
js 0x3977
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
jmp 0x398f
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm3
jne 0x386f
jnp 0x389d
movapd %xmm3, %xmm1
addsd %xmm3, %xmm1
movsd 0x7a01(%rip), %xmm2     # 0xb280
divsd %xmm1, %xmm2
xorps %xmm1, %xmm1
cvtsd2ss %xmm2, %xmm1
cvtss2sd %xmm1, %xmm1
addsd 0x7a3a(%rip), %xmm1     # 0xb2d0
cvttsd2si %xmm1, %rdi
jmp 0x389d
cmpb $0x0, 0xb95c(%rip)      # 0xf200
jne 0x390a
movq %rax, %xmm1
punpckldq 0x7b5d(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x7b65(%rip), %xmm1     # 0xb420
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
mulsd 0x7a05(%rip), %xmm0     # 0xb2d0
addsd %xmm1, %xmm2
divsd %xmm0, %xmm2
cvttsd2si %xmm2, %rsi
callq 0x3321
movb $0x6, 0xb895(%rip)      # 0xf179
movaps 0xb845(%rip), %xmm0     # 0xf130
movaps %xmm0, 0x17ece(%rip)    # 0x1b7c0
movq 0xb847(%rip), %rax      # 0xf140
movq %rax, 0x17ed0(%rip)     # 0x1b7d0
movl $0x18, %edi
callq 0x3374
callq 0x178c
testq %rbx, %rbx
movsd 0x10(%rsp), %xmm1
js 0x3924
xorps %xmm0, %xmm0
cvtsi2ss %rbx, %xmm0
jmp 0x393c
movq %rbx, %rax
shrq %rax
andl $0x1, %ebx
orq %rax, %rbx
xorps %xmm0, %xmm0
cvtsi2ss %rbx, %xmm0
addss %xmm0, %xmm0
divss 0xb8ac(%rip), %xmm0     # 0xf1f0
mulss 0x7a98(%rip), %xmm0     # 0xb3e4
cvtss2sd %xmm0, %xmm0
addsd 0xb8c8(%rip), %xmm0     # 0xf220
movsd %xmm0, 0xb8c0(%rip)     # 0xf220
cvttsd2si %xmm1, %r14
movl %r14d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
divss 0xb859(%rip), %xmm0     # 0xf1f0
mulss 0x7a45(%rip), %xmm0     # 0xb3e4
cvtss2sd %xmm0, %xmm0
addsd 0xb875(%rip), %xmm0     # 0xf220
movsd %xmm0, 0xb86d(%rip)     # 0xf220
cvttsd2si %xmm0, %rdi
callq 0x58fe
movb $0x1, 0x18578(%rip)     # 0x1bf3c
jmp 0x390f