// File: /workspace/github_c_makefile_star2_O1/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 %r12
pushq %rbx
subq $0x10, %rsp
movabsq $0x409f400000000000, %rax # imm = 0x409F400000000000
movq %rax, 0x155d1(%rip)     # 0x1b778
movabsq $0x408f400000000000, %rax # imm = 0x408F400000000000
movq %rax, 0x155e0(%rip)     # 0x1b798
callq 0x2424
cmpb $0x0, 0x8ff5(%rip)      # 0xf1b9
je 0x61f2
movsd 0x8f2a(%rip), %xmm0     # 0xf0f8
movsd 0x155b2(%rip), %xmm1    # 0x1b788
mulsd %xmm0, %xmm1
movsd %xmm1, 0x155a6(%rip)    # 0x1b788
mulsd 0x155be(%rip), %xmm0    # 0x1b7a8
movsd %xmm0, 0x155b6(%rip)    # 0x1b7a8
movsd 0x155ae(%rip), %xmm2    # 0x1b7a8
movsd 0x15586(%rip), %xmm1    # 0x1b788
movsd 0x50c6(%rip), %xmm3     # 0xb2d0
movapd %xmm2, %xmm0
movsd 0x15532(%rip), %xmm4    # 0x1b748
subsd %xmm2, %xmm4
mulsd %xmm3, %xmm4
cvttsd2si %xmm2, %eax
addsd %xmm2, %xmm4
subsd %xmm1, %xmm2
mulsd %xmm3, %xmm2
cvttsd2si %xmm2, %ecx
addsd %xmm1, %xmm0
movapd %xmm0, %xmm2
addsd 0x5076(%rip), %xmm4     # 0xb2b8
cvttsd2si %xmm4, %edx
mulsd %xmm3, %xmm2
movsd %xmm2, (%rsp)
movl $0xff, %esi
movl %edx, %edi
andl %esi, %edi
andl %esi, %eax
cvttsd2si %xmm1, %r8d
andl %esi, %r8d
addl %eax, %r8d
andl %esi, %ecx
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
cmpl %edi, %r8d
movsd %xmm1, 0x8(%rsp)
subsd %xmm1, %xmm0
cvttsd2si %xmm0, %ebp
cmovael %edx, %ebp
xorl %ebx, %ebx
leaq 0x8ea2(%rip), %r14      # 0xf12d
movb $0x1, %r15b
movb $0x8, %r12b
callq 0x2648
movl %eax, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movsd (%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x62bf
movsd 0x154d5(%rip), %xmm1    # 0x1b788
subsd 0x8(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jae 0x62e2
movsd 0x154e1(%rip), %xmm1    # 0x1b7a8
addsd 0x8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jb 0x62e5
ucomisd (%rsp), %xmm0
jb 0x62e5
addb %r15b, %r15b
incb %r15b
jmp 0x62e5
addb %r15b, %r15b
decb %r12b
jne 0x6291
cmpb (%rbx,%r14), %r15b
jne 0x640d
leaq 0x1(%rbx), %rax
testq %rbx, %rbx
movq %rax, %rbx
je 0x628b
movsd 0x4fc8(%rip), %xmm0     # 0xb2d0
addsd (%rsp), %xmm0
cvttsd2si %xmm0, %ebx
movzbl %bpl, %ebp
movb $0x5, %r15b
xorl %r14d, %r14d
callq 0x2648
movl %eax, %ecx
subl %ebx, %ecx
cmpl $0x2, %ecx
jb 0x632d
cmpl %ebp, %eax
jbe 0x638b
movq 0x153a4(%rip), %rcx     # 0x1b6d8
movq 0x15395(%rip), %rdx     # 0x1b6d0
leaq (%rdx,%rcx), %rsi
shrq %rsi
movl %eax, %eax
addq %rsi, %rax
movq %rax, %xmm0
punpckldq 0x50bc(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x50c4(%rip), %xmm0     # 0xb420
movsd 0x4f54(%rip), %xmm1     # 0xb2b8
cmpq %rcx, %rdx
jb 0x636d
xorpd %xmm1, %xmm1
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
addsd %xmm0, %xmm2
subsd (%rsp), %xmm2
addsd %xmm1, %xmm2
addsd %xmm2, %xmm2
cvttsd2si %xmm2, %rax
movl %eax, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movsd (%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x63b4
movsd 0x153e0(%rip), %xmm1    # 0x1b788
subsd 0x8(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jae 0x63d7
movsd 0x153ec(%rip), %xmm1    # 0x1b7a8
addsd 0x8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jb 0x63da
ucomisd (%rsp), %xmm0
jb 0x63da
addb %r14b, %r14b
incb %r14b
jmp 0x63da
addb %r14b, %r14b
decb %r15b
jne 0x631b
cmpb 0x8d45(%rip), %r14b     # 0xf12f
jne 0x640d
movb $0x1, 0x8e5b(%rip)      # 0xf24e
movl $0x3, %esi
movl $0x5, %edi
callq 0x6442
movb $0x0, 0x8e45(%rip)      # 0xf24e
movb $0x1, %bl
jmp 0x6433
callq 0x16fb
movzbl 0x8e67(%rip), %edi      # 0xf280
movq 0x8dc8(%rip), %rsi      # 0xf1e8
xorl %ebx, %ebx
xorl %edx, %edx
callq 0x10d0
movl $0x0, 0x8f4d(%rip)      # 0xf380
movl %ebx, %eax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq