// File: /workspace/github_c_makefile_star2_O2/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 $0x38, %rsp
movss 0xbf43(%rip), %xmm0     # 0xf1e0
movaps %xmm0, %xmm1
divss 0x7144(%rip), %xmm1     # 0xa3ec
cvttss2si %xmm1, %rax
movss 0x713b(%rip), %xmm2     # 0xa3f0
subss %xmm2, %xmm1
cvttss2si %xmm1, %rcx
movl %edi, %ebx
movq %rax, %r12
sarq $0x3f, %r12
andq %rcx, %r12
divss 0x7122(%rip), %xmm0     # 0xa3f4
cvttss2si %xmm0, %rcx
subss %xmm2, %xmm0
cvttss2si %xmm0, %rdx
orq %rax, %r12
movq %rcx, %r13
sarq $0x3f, %r13
andq %rdx, %r13
orq %rcx, %r13
andq $0x0, 0xbf18(%rip)      # 0xf210
callq 0x1618
leal (%rbx,%rbx), %eax
movl %eax, 0x24(%rsp)
movq %rbx, 0x30(%rsp)
leal 0x1(,%rbx,2), %eax
movl %eax, 0xc(%rsp)
pushq $0x4
popq %r15
andq $0x0, 0x18c00(%rip)     # 0x1bf20
callq 0x2429
xorl %r14d, %r14d
cmpb $0x0, 0x18382(%rip)     # 0x1b6b1
je 0x36e9
movl %eax, %esi
shrl %esi
movb 0xbeb1(%rip), %dl       # 0xf1f0
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 0x18361(%rip), %rsi     # 0x1b6c0
movzbl %cl, %edi
cmpq %rdi, %rsi
ja 0x3320
cmpq %r13, %rax
jb 0x3320
cmpq %r12, %rax
ja 0x3320
movq %rax, %rsi
addq %rax, %rsi
movq %rax, 0x18ba2(%rip)     # 0x1bf20
movq %rsi, %xmm0
punpckldq 0x7085(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x708d(%rip), %xmm0     # 0xa420
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
addsd %xmm0, %xmm2
testb %dl, %dl
je 0x33ca
cvtss2sd 0xbe35(%rip), %xmm0     # 0xf1e0
movaps %xmm0, %xmm1
divsd %xmm2, %xmm1
addsd 0x6f5e(%rip), %xmm1     # 0xa318
divsd %xmm1, %xmm0
movapd %xmm2, %xmm1
subsd %xmm0, %xmm1
cvttsd2si %xmm1, %ecx
movzbl %cl, %ecx
cvtsi2sd %ecx, %xmm0
movsd %xmm0, 0x28(%rsp)
movq 0x30(%rsp), %rcx
movl %ecx, %ebp
movq %rax, %rbx
movapd %xmm2, 0x10(%rsp)
subl $0x1, %ebp
jb 0x3446
callq 0x258c
movq %rax, %xmm0
punpckldq 0x7012(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x701a(%rip), %xmm0     # 0xa420
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd 0x10(%rsp), %xmm2
subsd %xmm2, %xmm1
andpd 0x6fdc(%rip), %xmm1     # 0xa400
ucomisd 0x28(%rsp), %xmm1
ja 0x3318
movq %rax, %rbx
movq 0x18ae6(%rip), %rax     # 0x1bf20
addq %rbx, %rax
movq %rax, 0x18adc(%rip)     # 0x1bf20
jmp 0x33e7
cmpb $0x0, 0xbd59(%rip)      # 0xf1a6
je 0x3469
subq %rbx, %rax
movq %rax, 0x18ac7(%rip)     # 0x1bf20
js 0x350a
cvtsi2ss %rax, %xmm0
jmp 0x3521
movapd 0x10(%rsp), %xmm0
mulsd 0x6e59(%rip), %xmm0     # 0xa2d0
movapd %xmm0, 0x10(%rsp)
movl 0x24(%rsp), %ebp
addl $0x2, %ebp
callq 0x2429
cmpb $0x0, 0x18221(%rip)     # 0x1b6b1
je 0x36e9
movq %rax, %rbx
movq 0x18a80(%rip), %rax     # 0x1bf20
addq %rbx, %rax
movq %rax, 0x18a76(%rip)     # 0x1bf20
movq %rbx, %xmm0
punpckldq 0x6f59(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x6f61(%rip), %xmm0     # 0xa420
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
subsd 0x10(%rsp), %xmm1
andpd 0x6f27(%rip), %xmm1     # 0xa400
movsd 0x28(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jae 0x3503
movq %rbx, %rcx
addq $-0x5, %rcx
cmpq $0x2, %rcx
ja 0x361a
cmpb $0x0, 0xbcf3(%rip)      # 0xf1f0
je 0x361a
incl %ebp
jmp 0x3484
movq %rax, %rcx
shrq %rcx
movl %eax, %edx
andl $0x1, %edx
orq %rcx, %rdx
cvtsi2ss %rdx, %xmm0
addss %xmm0, %xmm0
movl 0xc(%rsp), %ecx
movl %ecx, %ecx
cvtsi2ss %rcx, %xmm1
addss %xmm0, %xmm0
divss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
movss 0xbca0(%rip), %xmm1     # 0xf1e0
cvtss2sd %xmm1, %xmm3
divsd %xmm0, %xmm3
cmpb $0x0, 0xbc5d(%rip)      # 0xf1ac
movsd %xmm3, 0x10(%rsp)
je 0x3568
callq 0x16b7
cvttsd2si 0x10(%rsp), %r14
jmp 0x36e9
imulq $0x3e8, %rax, %rcx      # imm = 0x3E8
testq %rcx, %rcx
js 0x357b
cvtsi2ss %rcx, %xmm2
jmp 0x3587
shrq %rcx
cvtsi2ss %rcx, %xmm2
addss %xmm2, %xmm2
divss %xmm1, %xmm2
cvttss2si %xmm2, %rcx
movl %ecx, 0x18992(%rip)     # 0x1bf28
addl $-0x65, %ecx
cmpl $-0x48, %ecx
ja 0x35e4
cmpb $0x0, 0xbc4b(%rip)      # 0xf1f0
jne 0x3694
callq 0x16b7
movzbl 0xbcb9(%rip), %edi      # 0xf270
movq 0xbc1a(%rip), %rsi      # 0xf1d8
xorl %edx, %edx
callq 0x10d0
andl $0x0, 0xbda4(%rip)      # 0xf370
callq 0x4167
testq %rax, %rax
js 0x36fb
cvtsi2ss %rax, %xmm0
jmp 0x3710
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm3
jne 0x35ef
jnp 0x3623
movapd %xmm3, %xmm1
addsd %xmm3, %xmm1
movsd 0x6c81(%rip), %xmm2     # 0xa280
divsd %xmm1, %xmm2
cvtsd2ss %xmm2, %xmm1
cvtss2sd %xmm1, %xmm1
addsd 0x6cbd(%rip), %xmm1     # 0xa2d0
cvttsd2si %xmm1, %rdi
jmp 0x3623
movl %ebp, 0xc(%rsp)
jmp 0x344f
cmpb $0x0, 0xbbc6(%rip)      # 0xf1f0
jne 0x3694
movq %rax, %xmm1
punpckldq 0x6dd7(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x6ddf(%rip), %xmm1     # 0xa420
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
mulsd 0x6c7f(%rip), %xmm0     # 0xa2d0
addsd %xmm1, %xmm2
divsd %xmm0, %xmm2
cvttsd2si %xmm2, %rsi
callq 0x3141
movb $0x6, 0xbaff(%rip)      # 0xf169
xorl %eax, %eax
leaq 0xbaad(%rip), %rcx      # 0xf120
leaq 0x18136(%rip), %rdx     # 0x1b7b0
cmpq $0x18, %rax
je 0x368c
movl (%rax,%rcx), %esi
movl %esi, (%rax,%rdx)
addq $0x4, %rax
jmp 0x367a
pushq $0x18
popq %rdi
callq 0x319d
callq 0x1748
testq %rbx, %rbx
movsd 0x10(%rsp), %xmm1
js 0x36ab
cvtsi2ss %rbx, %xmm0
jmp 0x36c0
movq %rbx, %rax
shrq %rax
andl $0x1, %ebx
orq %rax, %rbx
cvtsi2ss %rbx, %xmm0
addss %xmm0, %xmm0
divss 0xbb18(%rip), %xmm0     # 0xf1e0
mulss 0x6d14(%rip), %xmm0     # 0xa3e4
cvtss2sd %xmm0, %xmm0
addsd 0xbb34(%rip), %xmm0     # 0xf210
movsd %xmm0, 0xbb2c(%rip)     # 0xf210
cvttsd2si %xmm1, %r14
movl %r14d, %eax
addq $0x38, %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
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
divss 0xbac8(%rip), %xmm0     # 0xf1e0
mulss 0x6cc4(%rip), %xmm0     # 0xa3e4
cvtss2sd %xmm0, %xmm0
addsd 0xbae4(%rip), %xmm0     # 0xf210
movsd %xmm0, 0xbadc(%rip)     # 0xf210
cvttsd2si %xmm0, %rdi
callq 0x5486
movb $0x1, 0x187e7(%rip)     # 0x1bf2c
jmp 0x3699