// File: /workspace/github_c_makefile_star2_O3/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 $0x28, %rsp
movss 0xbe01(%rip), %xmm0     # 0xf1f0
movaps %xmm0, %xmm1
divss 0x7ff2(%rip), %xmm1     # 0xb3ec
movl %edi, %ebx
movss 0x7fec(%rip), %xmm2     # 0xb3f0
cvttss2si %xmm1, %rax
subss %xmm2, %xmm1
cvttss2si %xmm1, %rcx
movq %rax, %r12
sarq $0x3f, %r12
andq %rcx, %r12
divss 0x7fd0(%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, 0xbdd3(%rip) # 0xf220
callq 0x1659
movq %rbx, 0x20(%rsp)
leal 0x1(,%rbx,2), %eax
movl %eax, 0x1c(%rsp)
movl $0x4, %r15d
movq $0x0, 0x18abd(%rip) # 0x1bf30
callq 0x249f
xorl %r14d, %r14d
cmpb $0x0, 0x1823f(%rip)     # 0x1b6c1
je 0x387a
movl %eax, %esi
shrl %esi
movb 0xbd6e(%rip), %dl       # 0xf200
movl %eax, %ecx
andl $0x1fe, %ecx            # imm = 0x1FE
cmpl $0x9, %ecx
movl %esi, %ecx
cmovael %r15d, %ecx
testb %dl, %dl
cmovnel %esi, %ecx
movq %rax, %rsi
subq 0x1821e(%rip), %rsi     # 0x1b6d0
movzbl %cl, %edi
cmpq %rdi, %rsi
ja 0x3473
cmpq %r13, %rax
jb 0x3473
cmpq %r12, %rax
ja 0x3473
movq %rax, %rsi
addq %rax, %rsi
movq %rax, 0x18a5f(%rip)     # 0x1bf30
movq %rsi, %xmm0
punpckldq 0x7f32(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x7f3a(%rip), %xmm0     # 0xb420
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
addsd %xmm0, %xmm2
testb %dl, %dl
je 0x3520
xorps %xmm0, %xmm0
cvtss2sd 0xbcef(%rip), %xmm0     # 0xf1f0
movaps %xmm0, %xmm1
divsd %xmm2, %xmm1
addsd 0x7e08(%rip), %xmm1     # 0xb318
divsd %xmm1, %xmm0
movapd %xmm2, %xmm1
subsd %xmm0, %xmm1
cvttsd2si %xmm1, %ecx
movzbl %cl, %edx
movq 0x20(%rsp), %rcx
testl %ecx, %ecx
movapd %xmm2, (%rsp)
movl %edx, 0x18(%rsp)
je 0x37a6
xorps %xmm0, %xmm0
cvtsi2sd %edx, %xmm0
movsd %xmm0, 0x10(%rsp)
movl %ecx, %ebp
callq 0x260f
movapd (%rsp), %xmm2
movq %rax, %xmm0
punpckldq 0x7eb1(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x7eb9(%rip), %xmm0     # 0xb420
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
subsd %xmm2, %xmm1
andpd 0x7e81(%rip), %xmm1     # 0xb400
ucomisd 0x10(%rsp), %xmm1
ja 0x3468
movq %rax, %rbx
movq 0x1899b(%rip), %rax     # 0x1bf30
addq %rbx, %rax
movq %rax, 0x18991(%rip)     # 0x1bf30
decl %ebp
jne 0x3548
movl 0x1c(%rsp), %ebp
cmpb $0x0, 0xbc08(%rip)      # 0xf1b6
je 0x35cd
subq %rbx, %rax
movq %rax, 0x18976(%rip)     # 0x1bf30
js 0x3687
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
jmp 0x36a1
callq 0x249f
cmpb $0x0, 0x180e8(%rip)     # 0x1b6c1
je 0x387a
movq %rax, %rbx
movapd (%rsp), %xmm2
mulsd 0x7ce1(%rip), %xmm2     # 0xb2d0
xorps %xmm0, %xmm0
cvtsi2sdl 0x18(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
incl %ebp
movapd %xmm2, (%rsp)
movq 0x18924(%rip), %rax     # 0x1bf30
addq %rbx, %rax
movq %rax, 0x1891a(%rip)     # 0x1bf30
movq %rbx, %xmm0
punpckldq 0x7ded(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x7df5(%rip), %xmm0     # 0xb420
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
subsd %xmm2, %xmm1
andpd 0x7dbd(%rip), %xmm1     # 0xb400
movsd 0x10(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jae 0x366a
leaq -0x5(%rbx), %rcx
cmpq $0x2, %rcx
ja 0x35b0
cmpb $0x0, 0xbb9c(%rip)      # 0xf200
je 0x35b0
callq 0x249f
movapd (%rsp), %xmm2
movq %rax, %rbx
incl %ebp
cmpb $0x0, 0x18041(%rip)     # 0x1b6c1
jne 0x3605
jmp 0x387a
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 0xbb31(%rip), %xmm1     # 0xf1f0
cvtss2sd %xmm1, %xmm3
divsd %xmm0, %xmm3
cmpb $0x0, 0xbaee(%rip)      # 0xf1bc
movsd %xmm3, (%rsp)
je 0x36e5
callq 0x16fa
cvttsd2si (%rsp), %r14
jmp 0x387a
imulq $0x3e8, %rax, %rcx      # imm = 0x3E8
testq %rcx, %rcx
js 0x36fb
xorps %xmm2, %xmm2
cvtsi2ss %rcx, %xmm2
jmp 0x370a
shrq %rcx
xorps %xmm2, %xmm2
cvtsi2ss %rcx, %xmm2
addss %xmm2, %xmm2
divss %xmm1, %xmm2
cvttss2si %xmm2, %rcx
movl %ecx, 0x1881f(%rip)     # 0x1bf38
addl $-0x65, %ecx
cmpl $-0x48, %ecx
ja 0x376d
cmpb $0x0, 0xbad8(%rip)      # 0xf200
jne 0x3820
callq 0x16fa
movzbl 0xbb46(%rip), %edi      # 0xf280
movq 0xbaa7(%rip), %rsi      # 0xf1e8
xorl %edx, %edx
callq 0x10d0
movl $0x0, 0xbc2e(%rip)      # 0xf380
callq 0x43aa
testq %rax, %rax
js 0x388c
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
jmp 0x38a4
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm3
jne 0x3778
jnp 0x37b3
movapd %xmm3, %xmm1
addsd %xmm3, %xmm1
movsd 0x7af8(%rip), %xmm2     # 0xb280
divsd %xmm1, %xmm2
xorps %xmm1, %xmm1
cvtsd2ss %xmm2, %xmm1
cvtss2sd %xmm1, %xmm1
addsd 0x7b31(%rip), %xmm1     # 0xb2d0
cvttsd2si %xmm1, %rdi
jmp 0x37b3
movl $0x1, %ebp
movq %rax, %rbx
jmp 0x35a7
cmpb $0x0, 0xba46(%rip)      # 0xf200
jne 0x3820
movq %rax, %xmm1
punpckldq 0x7c47(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x7c4f(%rip), %xmm1     # 0xb420
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
mulsd 0x7aef(%rip), %xmm0     # 0xb2d0
addsd %xmm1, %xmm2
divsd %xmm0, %xmm2
cvttsd2si %xmm2, %rsi
callq 0x329a
movb $0x6, 0xb97f(%rip)      # 0xf179
movaps 0xb92f(%rip), %xmm0     # 0xf130
movaps %xmm0, 0x17fb8(%rip)    # 0x1b7c0
movq 0xb931(%rip), %rax      # 0xf140
movq %rax, 0x17fba(%rip)     # 0x1b7d0
movl $0x18, %edi
callq 0x32ed
callq 0x178b
testq %rbx, %rbx
movsd (%rsp), %xmm1
js 0x3839
xorps %xmm0, %xmm0
cvtsi2ss %rbx, %xmm0
jmp 0x3851
movq %rbx, %rax
shrq %rax
andl $0x1, %ebx
orq %rax, %rbx
xorps %xmm0, %xmm0
cvtsi2ss %rbx, %xmm0
addss %xmm0, %xmm0
divss 0xb997(%rip), %xmm0     # 0xf1f0
mulss 0x7b83(%rip), %xmm0     # 0xb3e4
cvtss2sd %xmm0, %xmm0
addsd 0xb9b3(%rip), %xmm0     # 0xf220
movsd %xmm0, 0xb9ab(%rip)     # 0xf220
cvttsd2si %xmm1, %r14
movl %r14d, %eax
addq $0x28, %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 0xb944(%rip), %xmm0     # 0xf1f0
mulss 0x7b30(%rip), %xmm0     # 0xb3e4
cvtss2sd %xmm0, %xmm0
addsd 0xb960(%rip), %xmm0     # 0xf220
movsd %xmm0, 0xb958(%rip)     # 0xf220
cvttsd2si %xmm0, %rdi
callq 0x5832
movb $0x1, 0x18663(%rip)     # 0x1bf3c
jmp 0x3825