// File: /workspace/github_c_makefile_star2_O3/mincebert[P]maketzx/maketzx.c
// Function: MidSyncSearch
byte MidSyncSearch(void)
{
  byte count1,count2,value,tolerance,maxpulse;
//  byte good=1;
  word pulse;
  double middle;
  bit0.freq=2000; bit1.freq=1000;
  SetTimings();
  if (spdchk) { bit0.samples*=ecf; bit1.samples*=ecf; }
  tolerance=(bit1.samples-bit0.samples)/2;
  middle=(double)(bit1.samples+bit0.samples)/2;
  maxpulse=bit1.samples+(pilot.samples-bit1.samples)/2+1;
  if (maxpulse>(byte)bit1.samples+(byte)bit0.samples) maxpulse=bit1.samples+bit0.samples-tolerance;
  for (count1=0; count1<2; count1++)
      {
	value=1;
	for (count2=0; count2<8; count2++)
	    {
	      pulse=0;

/*                   do {
			  pulse+=FindEdge1();
			  if (currpulse<2) good=0;
			  else good=1;
			}
		     while (!good && blocktype);
		     do {
			  pulse+=FindEdge1();
			  if (currpulse<2) good=0;
			  else good=1;
			}
		     while (!good && blocktype); */


	      pulse=FindEdge2();


//            if ((abs(pulse-(int)(middle+.5))<2 /* && abs(pulse-2*currpulse)>2 */) || pulse>maxpulse)
//            pulse=((currpulse+lastpulse)/2+pulse-middle+(lastpulse<currpulse))*2;
	      if ((pulse<middle) && (pulse>=bit0.samples-tolerance)) value<<=1;
	      else if ((pulse<=bit1.samples+tolerance) && (pulse>=middle)) { value<<=1; value|=0x1; }
	    }
	if (value!=midsync[count1])
	   {
	     PopFilePointers();
	     lseek(inpfile.handle,filepos,SEEK_SET);
	     inpfile.bufpoint=0;
	     return 0;
	   }
      }
  value=0;
  for (count2=0; count2<5; count2++)
      {
	pulse=0;

/*                   do {
			  pulse+=FindEdge1();
			  if (currpulse<2) good=0;
			  else good=1;
			}
		     while (!good && blocktype);
		     do {
			  pulse+=FindEdge1();
			  if (currpulse<2) good=0;
			  else good=1;
			}
		     while (!good && blocktype); */


	pulse=FindEdge2();
	if (((pulse-(int)(middle+.5))<2 /* && abs(pulse-2*currpulse)>2 */) || pulse>maxpulse) pulse=((currpulse+lastpulse)/2+pulse-middle+(lastpulse<currpulse))*2;
	if ((pulse<middle) && (pulse>=bit0.samples-tolerance)) value<<=1;
	else if ((pulse<=bit1.samples+tolerance) && (pulse>=middle)) { value<<=1; value|=0x1; }
      }
  if (value!=midsync[2])
     {
       PopFilePointers();
       lseek(inpfile.handle,filepos,SEEK_SET);
       inpfile.bufpoint=0;
       return 0;
     }
  savemidsync=1;
  WriteTZXPureData(5,3);
  savemidsync=0;
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movabsq $0x409f400000000000, %rax # imm = 0x409F400000000000
movq %rax, 0x15693(%rip)     # 0x1b778
movabsq $0x408f400000000000, %rax # imm = 0x408F400000000000
movq %rax, 0x156a2(%rip)     # 0x1b798
callq 0x240d
cmpb $0x0, 0x90b7(%rip)      # 0xf1b9
movsd 0x1569e(%rip), %xmm1    # 0x1b7a8
movsd 0x15676(%rip), %xmm0    # 0x1b788
je 0x6134
movsd 0x8fdc(%rip), %xmm2     # 0xf0f8
mulsd %xmm2, %xmm0
movsd %xmm0, 0x15660(%rip)    # 0x1b788
mulsd %xmm2, %xmm1
movsd %xmm1, 0x15674(%rip)    # 0x1b7a8
movapd %xmm1, %xmm3
subsd %xmm0, %xmm3
movsd 0x518c(%rip), %xmm2     # 0xb2d0
mulsd %xmm2, %xmm3
cvttsd2si %xmm3, %eax
movapd %xmm1, %xmm3
addsd %xmm0, %xmm3
movsd 0x155ec(%rip), %xmm4    # 0x1b748
subsd %xmm1, %xmm4
mulsd %xmm2, %xmm4
addsd %xmm1, %xmm4
addsd 0x5148(%rip), %xmm4     # 0xb2b8
cvttsd2si %xmm4, %ecx
movapd %xmm3, %xmm4
movl $0xff, %edx
movl %ecx, %esi
andl %edx, %esi
cvttsd2si %xmm1, %edi
cvttsd2si %xmm0, %r8d
andl %edx, %edi
andl %edx, %r8d
addl %edi, %r8d
andl %edx, %eax
cmpl %esi, %r8d
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x10(%rsp)
subsd %xmm0, %xmm3
cvttsd2si %xmm3, %ebp
cmovael %ecx, %ebp
mulsd %xmm2, %xmm4
movsd %xmm4, 0x8(%rsp)
movb $0x1, %bl
xorl %r15d, %r15d
leaq 0x8f68(%rip), %r14      # 0xf12d
movb $0x8, %r12b
movb $0x1, %r13b
callq 0x260f
movl %eax, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movsd 0x8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x61fa
movsd 0x1559a(%rip), %xmm1    # 0x1b788
subsd 0x10(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jae 0x621e
movsd 0x155a6(%rip), %xmm1    # 0x1b7a8
addsd 0x10(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jb 0x6221
ucomisd 0x8(%rsp), %xmm0
jb 0x6221
addb %r13b, %r13b
incb %r13b
jmp 0x6221
addb %r13b, %r13b
decb %r12b
jne 0x61cb
cmpb (%r15,%r14), %r13b
jne 0x6351
movl $0x1, %r15d
testb $0x1, %bl
movl $0x0, %ebx
jne 0x61c5
movsd 0x5088(%rip), %xmm0     # 0xb2d0
addsd 0x8(%rsp), %xmm0
cvttsd2si %xmm0, %ebx
movzbl %bpl, %ebp
movb $0x5, %r15b
xorl %r14d, %r14d
callq 0x260f
movl %eax, %ecx
subl %ebx, %ecx
cmpl $0x2, %ecx
jb 0x626e
cmpl %ebp, %eax
jbe 0x62cd
movq 0x15463(%rip), %rcx     # 0x1b6d8
movq 0x15454(%rip), %rdx     # 0x1b6d0
leaq (%rdx,%rcx), %rsi
shrq %rsi
movl %eax, %eax
addq %rsi, %rax
movq %rax, %xmm0
punpckldq 0x517b(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x5183(%rip), %xmm0     # 0xb420
movsd 0x5013(%rip), %xmm1     # 0xb2b8
cmpq %rcx, %rdx
jb 0x62ae
xorpd %xmm1, %xmm1
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
addsd %xmm0, %xmm2
subsd 0x8(%rsp), %xmm2
addsd %xmm1, %xmm2
addsd %xmm2, %xmm2
cvttsd2si %xmm2, %rax
movl %eax, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movsd 0x8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x62f7
movsd 0x1549d(%rip), %xmm1    # 0x1b788
subsd 0x10(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jae 0x631b
movsd 0x154a9(%rip), %xmm1    # 0x1b7a8
addsd 0x10(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jb 0x631e
ucomisd 0x8(%rsp), %xmm0
jb 0x631e
addb %r14b, %r14b
incb %r14b
jmp 0x631e
addb %r14b, %r14b
decb %r15b
jne 0x625c
cmpb 0x8e01(%rip), %r14b     # 0xf12f
jne 0x6351
movb $0x1, 0x8f17(%rip)      # 0xf24e
movl $0x3, %esi
movl $0x5, %edi
callq 0x6388
movb $0x0, 0x8f01(%rip)      # 0xf24e
movb $0x1, %bl
jmp 0x6377
callq 0x16fa
movzbl 0x8f23(%rip), %edi      # 0xf280
movq 0x8e84(%rip), %rsi      # 0xf1e8
xorl %ebx, %ebx
xorl %edx, %edx
callq 0x10d0
movl $0x0, 0x9009(%rip)      # 0xf380
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq