// File: /workspace/github_c_makefile_star2_O2/mincebert[P]maketzx/maketzx.c
// Function: PilotSearch
dword PilotSearch(word pulses)
{
  byte tolerance=pilot.samples-samprate/(pilot.freq+200-(pilot.freq/100*correction));
  word count;
  dword skipped1=0,skipped2=0,hpulse,pulse;
  wmaxcount=wmincount=wmax=wmin=0;
  PilotLinkingOK=0;
  if (spdchk && pilot.freq==808)
     do {
	  word pfreq;
	  CheckingSpeed=1;
	  PushFilePointers();
	  pfreq=BeepSearch(16);
	  if (!pfreq && !blocktype)
	     {
	       PopFilePointers();
	       lseek(inpfile.handle,filepos,SEEK_SET);
	       inpfile.bufpoint=0;
	       ecf=1; break;
	     }
	  PopFilePointers();
	  lseek(inpfile.handle,filepos,SEEK_SET);
	  inpfile.bufpoint=0;
	  ecf=(double)808/pfreq;
	  if (fabs(ecf)<.10) ecf=1;
	  CheckingSpeed=0;
	}
     while (fabs(ecf-1)>.20);
  if (!FindingPause && !ClickSearch) printf("Searching pilot...     \x0d");
  if (spdchk) pilot.samples*=ecf;
  do {
       do {
	    hpulse=FindEdge1();
	    if (!blocktype) return skipped1+skipped2+hpulse;
	    skipped1+=hpulse;
	  }
       while (hpulse<(pilot.samples/2)-tolerance || hpulse>(pilot.samples/2)+tolerance);
       skipped2=0;
       PushFilePointers();
       for (count=0; count<pulses; count++)
	   {
	     skipped2+=(pulse=FindEdge2());
	     if ((pulse<pilot.samples-(2*tolerance) || pulse>pilot.samples+(2*tolerance)) && !tonesearch)
		{ RemoveFilePointers(); break; }
/*           if (tapespeedcheck)
		{
		  if (pulse==wmax-(wmax-wmin)/2 && wmax!=0) wmaxcount++;
		  else if (pulse==wmin+(wmax-wmin)/2 && wmin!=0) wmincount++;
		  if (pulse<wmin) wmincount=1;
		  else if (pulse>wmax) wmaxcount=1;
		  if (wmax==0) wmax=pulse;
		  else wmax=(pulse*(pulse>wmax))+(wmax*(pulse<=wmax));
		  if (wmin==0) wmin=pulse;
		  else wmin=(pulse*(pulse<wmin))+(wmin*(pulse>=wmin));
		} */
	   }
       if (count==pulses) PilotLinkingOK=1;
       else { skipped1+=skipped2; skipped2=0; }
     }
  while (count!=pulses);
  if (!FindingPause) pilotlength+=skipped2+hpulse;
  if (!FindingPause && !ClickSearch) printf("Loading pilot...       \x0d");
  if (FindingPause)
     {
       PopFilePointers();
       lseek(inpfile.handle,filepos,SEEK_SET);
       inpfile.bufpoint=0;
       RemoveFilePointers();
       PushFilePointers();
     }
  else RemoveFilePointers();
  return skipped1-hpulse;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %edi, %r13d
movsd 0x18ca3(%rip), %xmm4    # 0x1b738
cvtss2sd 0xc743(%rip), %xmm1     # 0xf1e0
movsd 0x18c83(%rip), %xmm0    # 0x1b728
movapd %xmm0, %xmm2
divsd 0x77ef(%rip), %xmm2     # 0xa2a0
mulsd 0xc72f(%rip), %xmm2     # 0xf1e8
movsd 0x77d7(%rip), %xmm3     # 0xa298
andq $0x0, 0x190e7(%rip)     # 0x1bbb0
addsd %xmm0, %xmm3
andq $0x0, 0x190e3(%rip)     # 0x1bbb8
addsd %xmm3, %xmm2
andl $0x0, 0x190e0(%rip)     # 0x1bbc0
divsd %xmm2, %xmm1
andl $0x0, 0x190d9(%rip)     # 0x1bbc4
subsd %xmm1, %xmm4
movsd %xmm4, (%rsp)
movb $0x0, 0x190cd(%rip)     # 0x1bbc8
cmpb $0x0, 0xc6a7(%rip)      # 0xf1a9
je 0x2bde
ucomisd 0x7798(%rip), %xmm0     # 0xa2a8
jne 0x2bde
jp 0x2bde
pushq $0x10
popq %rbp
movb $0x1, 0xc686(%rip)      # 0xf1ac
callq 0x1618
movl %ebp, %edi
callq 0x3287
movl %eax, %r14d
movb 0x18b76(%rip), %bl      # 0x1b6b1
callq 0x16b7
movzbl 0xc729(%rip), %edi      # 0xf270
movq 0xc68a(%rip), %rsi      # 0xf1d8
xorl %edx, %edx
callq 0x10d0
andl $0x0, 0xc814(%rip)      # 0xf370
testl %r14d, %r14d
jne 0x2b65
testb %bl, %bl
je 0x2bcd
movl %r14d, %eax
cvtsi2sd %rax, %xmm0
movsd 0x7733(%rip), %xmm1     # 0xa2a8
divsd %xmm0, %xmm1
movapd %xmm1, %xmm0
cmpltsd 0x772a(%rip), %xmm0     # 0xa2b0
movapd %xmm0, %xmm2
andnpd %xmm1, %xmm2
movsd 0x7722(%rip), %xmm1     # 0xa2b8
andpd %xmm1, %xmm0
orpd %xmm2, %xmm0
movlpd %xmm0, 0xc542(%rip)     # 0xf0e8
movb $0x0, 0xc5ff(%rip)      # 0xf1ac
addsd 0x770b(%rip), %xmm0     # 0xa2c0
andpd 0x7843(%rip), %xmm0     # 0xa400
ucomisd 0x7703(%rip), %xmm0     # 0xa2c8
ja 0x2b1f
jmp 0x2bde
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0xc50a(%rip)      # 0xf0e8
cvttsd2si (%rsp), %ebx
movb 0xc5c2(%rip), %al       # 0xf1ab
orb 0xc5bb(%rip), %al       # 0xf1aa
jne 0x2bff
leaq 0x79fc(%rip), %rdi      # 0xa5f4
xorl %eax, %eax
callq 0x10b0
cmpb $0x0, 0xc5a3(%rip)      # 0xf1a9
je 0x2c20
movsd 0xc4d8(%rip), %xmm0     # 0xf0e8
mulsd 0x18b20(%rip), %xmm0    # 0x1b738
movsd %xmm0, 0x18b18(%rip)    # 0x1b738
movzbl %bl, %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x10(%rsp)
addl %eax, %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, (%rsp)
xorl %r12d, %r12d
movl %r13d, 0xc(%rsp)
movq %r12, %r14
callq 0x2429
addq %rax, %r12
cmpb $0x0, 0x18a5f(%rip)     # 0x1b6b1
je 0x2d98
movq %rax, %r15
movq %rax, %xmm1
punpckldq 0x77a8(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x77b0(%rip), %xmm1     # 0xa420
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
movsd 0x18ab4(%rip), %xmm1    # 0x1b738
mulsd 0x7644(%rip), %xmm1     # 0xa2d0
movapd %xmm1, %xmm2
subsd 0x10(%rsp), %xmm2
ucomisd %xmm0, %xmm2
ja 0x2c40
addsd 0x10(%rsp), %xmm1
ucomisd %xmm1, %xmm0
ja 0x2c40
callq 0x1618
movl %r13d, %ebp
xorl %r13d, %r13d
movl %ebp, %ebx
subl $0x1, %ebp
jb 0x2d27
callq 0x258c
addq %rax, %r13
movq %rax, %xmm1
punpckldq 0x7741(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x7749(%rip), %xmm1     # 0xa420
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
movsd 0x18a4d(%rip), %xmm1    # 0x1b738
movapd %xmm1, %xmm2
movsd (%rsp), %xmm3
subsd %xmm3, %xmm2
ucomisd %xmm0, %xmm2
ja 0x2d08
addsd %xmm3, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x2cb3
cmpb $0x0, 0xc4e1(%rip)      # 0xf1f0
jne 0x2cb3
callq 0x1748
testl %ebx, %ebx
je 0x2d27
addq %r13, %r12
movl 0xc(%rsp), %r13d
jmp 0x2c40
movb $0x1, 0x18e9a(%rip)     # 0x1bbc8
movb 0xc476(%rip), %al       # 0xf1aa
testb %al, %al
jne 0x2d42
addq %r15, %r13
addq %r13, 0x18e8e(%rip)     # 0x1bbd0
movb 0xc463(%rip), %cl       # 0xf1ab
orb %al, %cl
jne 0x2d60
leaq 0x78ba(%rip), %rdi      # 0xa60d
xorl %eax, %eax
callq 0x10b0
movb 0xc44a(%rip), %al       # 0xf1aa
testb %al, %al
je 0x2d91
callq 0x16b7
movzbl 0xc500(%rip), %edi      # 0xf270
movq 0xc461(%rip), %rsi      # 0xf1d8
xorl %edx, %edx
callq 0x10d0
andl $0x0, 0xc5eb(%rip)      # 0xf370
callq 0x1748
callq 0x1618
jmp 0x2d9b
callq 0x1748
jmp 0x2d9b
movq %r12, %r14
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq