// File: /workspace/github_c_makefile_star2_O2/mincebert[P]maketzx/maketzx.c
// Function: SyncSearch
dword SyncSearch(void)
{
  byte minsmp=(byte)(samprate/((pilot.freq+30)*4)),
       middle=(byte)((float)(samprate/((pilot.freq+50)*2)+.5)),
       maxsmp=(byte)(samprate/(pilot.freq-230)),
       pulseget=0;
  word hwaves=0;
  long pulse;
  float tolerance=(middle-syncx.samples)*.75;
  if (spdchk) syncx.samples*=ecf;
  SyncLoadOK=0;
     {
       do {
	    do {
		 if (!pulseget) pulse=FindEdge1();
		 else { pulse-=lastpulse; pulseget=0; }
		 if (!pulse || pulse<(minsmp-tolerance)/2 || pulse>maxsmp/2)
		    { mspause+=(pulse/samprate)*1000; return hwaves; }
		 if ((pulse-lastpulse)>diff*5 && (pulse-lastpulse) && abs(diff)>tolerance) pulse+=(diff*(abs(diff)<5));
		 if (pulse<0)
		 pulse-=diff;
		 hwaves++;
		 pilotlength+=pulse;
	       }
	    while (pulse>(byte)((double)(middle+tolerance)/2+.5) || (pulse/2+lastpulse/2<middle/2 && diff>minsmp/2));
	    if ((pulse-lastpulse)>middle/2 && (lastpulse-middle)>tolerance && pulse)
	       {
		 double sum=pulse+lastpulse;
//               if (!pulse) sum-=diff;
		 pulse=sum*.3; lastpulse=sum-pulse;
	       }
	    if (pulse+lastpulse>middle || ((lastpulse-(byte)pilot.samples/2)<=tolerance && pulse>=syncx.samples/2) || syncx.samples<2)// || abs(lastpulse-(byte)syncx.samples)>1) //lastpulse>syncx.samples)
	       {
		 pilotlength-=pulse;
		 if (!pulseget) { pulse+=FindEdge1(); pulseget=1; }
		 else { pulseget=0; pulse+=lastpulse; }
	       }
	    else { pulse+=lastpulse; pulseget=0; }
//          if (ClickSearch && !(hwaves%2) && abs(pulse-middle)<tolerance) pulse=middle;
	  }
       while (pulse<minsmp || pulse>middle);
       SyncLoadOK=1;
       return hwaves;
     }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
cvtss2sd 0xc41d(%rip), %xmm0     # 0xf1e0
movsd 0x1895d(%rip), %xmm1    # 0x1b728
movsd 0x7505(%rip), %xmm2     # 0xa2d8
addsd %xmm1, %xmm2
mulsd 0x7501(%rip), %xmm2     # 0xa2e0
movaps %xmm0, %xmm3
divsd %xmm2, %xmm3
cvttsd2si %xmm3, %ecx
movsd 0x74f6(%rip), %xmm2     # 0xa2e8
addsd %xmm1, %xmm2
addsd %xmm2, %xmm2
movaps %xmm0, %xmm3
divsd %xmm2, %xmm3
addsd 0x74c7(%rip), %xmm3     # 0xa2d0
cvtsd2ss %xmm3, %xmm2
cvttss2si %xmm2, %edx
addsd 0x74d7(%rip), %xmm1     # 0xa2f0
divsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movzbl %dl, %edx
cvtsi2sd %edx, %xmm1
movsd 0x18928(%rip), %xmm0    # 0x1b758
subsd %xmm0, %xmm1
mulsd 0x74bc(%rip), %xmm1     # 0xa2f8
cvtsd2ss %xmm1, %xmm1
movss %xmm1, (%rsp)
cmpb $0x0, 0xc35d(%rip)      # 0xf1a9
je 0x2e5e
mulsd 0xc292(%rip), %xmm0     # 0xf0e8
movsd %xmm0, 0x188fa(%rip)    # 0x1b758
movzbl %cl, %esi
cvtsi2ss %esi, %xmm2
movss (%rsp), %xmm1
subss %xmm1, %xmm2
mulss 0x7572(%rip), %xmm2     # 0xa3e8
movb $0x0, 0x1892d(%rip)     # 0x1b7aa
cvtsi2ss %edx, %xmm0
shrb %al
addss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x743d(%rip), %xmm1     # 0xa2d0
mulsd %xmm1, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %ecx
movzbl %al, %r12d
movzbl %cl, %r13d
movq %rdx, 0x8(%rsp)
movl %edx, %eax
shrb %al
movzbl %al, %ebp
movq %rsi, 0x10(%rsp)
movl %esi, %eax
shrb %al
movzbl %al, %r15d
xorl %edx, %edx
xorl %ecx, %ecx
movss %xmm2, 0x4(%rsp)
testb %cl, %cl
sete %cl
movl %edx, %r14d
movq %rax, %rbx
testb $0x1, %cl
je 0x2eea
callq 0x2429
movss 0x4(%rsp), %xmm2
movq %rax, %rbx
jmp 0x2ef1
subq 0x187cf(%rip), %rbx     # 0x1b6c0
cvtsi2ss %rbx, %xmm0
testq %rbx, %rbx
je 0x310b
ucomiss %xmm0, %xmm2
ja 0x310b
cmpq %r12, %rbx
jg 0x310b
movq 0x187a8(%rip), %rsi     # 0x1b6c0
movq %rbx, %rax
subq %rsi, %rax
movslq 0xc2bf(%rip), %rdi      # 0xf1e4
leaq (%rdi,%rdi,4), %rcx
cmpq %rcx, %rax
jbe 0x2f4f
movl %edi, %eax
negl %eax
cmovsl %edi, %eax
cvtsi2ss %eax, %xmm0
ucomiss (%rsp), %xmm0
jbe 0x2f4f
cmpl $0x5, %eax
movl $0x0, %eax
cmovbl %edi, %eax
cltq
addq %rax, %rbx
movq %rbx, %rax
sarq $0x3f, %rax
andq %rdi, %rax
subq %rax, %rbx
movq 0x18c6d(%rip), %rcx     # 0x1bbd0
addq %rbx, %rcx
movq %rcx, 0x18c63(%rip)     # 0x1bbd0
cmpq %r13, %rbx
jg 0x2f91
movq %rbx, %rax
cqto
pushq $0x2
popq %r8
idivq %r8
movq %rsi, %rdx
shrq %rdx
addq %rax, %rdx
cmpq %rbp, %rdx
jae 0x2f9b
cmpl %r15d, %edi
jle 0x2f9b
incl %r14d
movb $0x1, %cl
jmp 0x2ed5
movq %rbx, %rax
subq %rsi, %rax
cmpq %rbp, %rax
jbe 0x303f
movq %rsi, %rax
subq 0x8(%rsp), %rax
js 0x2fbb
cvtsi2ss %rax, %xmm0
jmp 0x2fd0
movq %rax, %rdx
shrq %rdx
andl $0x1, %eax
orq %rdx, %rax
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
ucomiss (%rsp), %xmm0
jbe 0x303f
testq %rbx, %rbx
je 0x303f
addq %rsi, %rbx
movq %rbx, %xmm0
punpckldq 0x7425(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x742d(%rip), %xmm0     # 0xa420
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, %xmm0
mulsd 0x72f5(%rip), %xmm0     # 0xa300
cvttsd2si %xmm0, %rbx
cvtsi2sd %rbx, %xmm0
subsd %xmm0, %xmm1
cvttsd2si %xmm1, %rax
movq %rax, %rdx
subsd 0x72df(%rip), %xmm1     # 0xa308
cvttsd2si %xmm1, %rsi
sarq $0x3f, %rdx
andq %rdx, %rsi
orq %rax, %rsi
movq %rsi, 0x18681(%rip)     # 0x1b6c0
leaq (%rsi,%rbx), %rax
cmpq 0x8(%rsp), %rax
jbe 0x3066
subq %rbx, %rcx
movq %rcx, 0x18b7c(%rip)     # 0x1bbd0
callq 0x2429
movss 0x4(%rsp), %xmm2
addq %rbx, %rax
movb $0x1, %cl
jmp 0x30e5
cvttsd2si 0x186ca(%rip), %edx # 0x1b738
shrb %dl
movzbl %dl, %edx
subq %rdx, %rsi
js 0x307f
cvtsi2ss %rsi, %xmm0
jmp 0x3094
movq %rsi, %rdx
shrq %rdx
andl $0x1, %esi
orq %rdx, %rsi
cvtsi2ss %rsi, %xmm0
addss %xmm0, %xmm0
movss (%rsp), %xmm1
ucomiss %xmm0, %xmm1
jae 0x30b2
movsd 0x726a(%rip), %xmm0     # 0xa310
ucomisd 0x186aa(%rip), %xmm0    # 0x1b758
ja 0x304a
jmp 0x30e3
movsd 0x1869e(%rip), %xmm0    # 0x1b758
movsd 0x724e(%rip), %xmm1     # 0xa310
ucomisd %xmm0, %xmm1
mulsd 0x7202(%rip), %xmm0     # 0xa2d0
ja 0x304a
cvtsi2sd %rbx, %xmm1
ucomisd %xmm0, %xmm1
jae 0x304a
xorl %ecx, %ecx
leal 0x1(%r14), %edx
cmpq 0x10(%rsp), %rax
jl 0x2eca
cmpq 0x8(%rsp), %rax
jg 0x2eca
movb $0x1, 0x186a4(%rip)     # 0x1b7aa
incl %r14d
jmp 0x312f
divss 0xc0cd(%rip), %xmm0     # 0xf1e0
mulss 0x72c9(%rip), %xmm0     # 0xa3e4
cvtss2sd %xmm0, %xmm0
addsd 0xc0e9(%rip), %xmm0     # 0xf210
movsd %xmm0, 0xc0e1(%rip)     # 0xf210
movl %r14d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq