// File: /workspace/github_c_makefile_star2_O2/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, 0x15a76(%rip)     # 0x1b768
movabsq $0x408f400000000000, %rax # imm = 0x408F400000000000
movq %rax, 0x15a85(%rip)     # 0x1b788
callq 0x2397
cmpb $0x0, 0x949a(%rip)      # 0xf1a9
movsd 0x15a81(%rip), %xmm1    # 0x1b798
movsd 0x15a59(%rip), %xmm0    # 0x1b778
je 0x5d41
movsd 0x93bf(%rip), %xmm2     # 0xf0e8
mulsd %xmm2, %xmm0
movsd %xmm0, 0x15a43(%rip)    # 0x1b778
mulsd %xmm2, %xmm1
movsd %xmm1, 0x15a57(%rip)    # 0x1b798
movapd %xmm1, %xmm2
subsd %xmm0, %xmm2
movsd 0x457f(%rip), %xmm3     # 0xa2d0
mulsd %xmm3, %xmm2
cvttsd2si %xmm2, %eax
movapd %xmm1, %xmm2
addsd %xmm0, %xmm2
movapd %xmm2, %xmm5
movsd 0x159cb(%rip), %xmm4    # 0x1b738
subsd %xmm1, %xmm4
mulsd %xmm3, %xmm4
addsd %xmm1, %xmm4
addsd 0x4537(%rip), %xmm4     # 0xa2b8
mulsd %xmm3, %xmm5
movsd %xmm5, (%rsp)
cvttsd2si %xmm4, %ecx
movl $0xff, %edx
movl %ecx, %esi
andl %edx, %esi
cvttsd2si %xmm1, %edi
andl %edx, %edi
cvttsd2si %xmm0, %r8d
andl %edx, %r8d
addl %edi, %r8d
andl %edx, %eax
cvtsi2sd %eax, %xmm0
cmpl %esi, %r8d
movsd %xmm0, 0x8(%rsp)
subsd %xmm0, %xmm2
cvttsd2si %xmm2, %ebp
cmovael %ecx, %ebp
xorl %ebx, %ebx
leaq 0x9352(%rip), %r14      # 0xf11d
cmpq $0x2, %rbx
je 0x5e42
movb $0x1, %r15b
movb $0x8, %r12b
subb $0x1, %r12b
jb 0x5e30
callq 0x258c
movl %eax, %eax
cvtsi2sd %rax, %xmm0
movsd (%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x5e08
movsd 0x1597c(%rip), %xmm1    # 0x1b778
subsd 0x8(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jae 0x5e2b
movsd 0x15988(%rip), %xmm1    # 0x1b798
addsd 0x8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jb 0x5dd7
ucomisd (%rsp), %xmm0
jb 0x5dd7
addb %r15b, %r15b
incb %r15b
jmp 0x5dd7
addb %r15b, %r15b
jmp 0x5dd7
leaq 0x1(%rbx), %rax
cmpb (%rbx,%r14), %r15b
movq %rax, %rbx
je 0x5dcb
jmp 0x5f59
movsd 0x4486(%rip), %xmm0     # 0xa2d0
addsd (%rsp), %xmm0
cvttsd2si %xmm0, %ebx
movzbl %bpl, %ebp
movb $0x5, %r15b
xorl %r14d, %r14d
subb $0x1, %r15b
jb 0x5f33
callq 0x258c
movl %eax, %ecx
subl %ebx, %ecx
cmpl $0x2, %ecx
jb 0x5e79
cmpl %ebp, %eax
jbe 0x5ed7
movq 0x15848(%rip), %rcx     # 0x1b6c8
movq 0x15839(%rip), %rdx     # 0x1b6c0
leaq (%rdx,%rcx), %rsi
shrq %rsi
movl %eax, %eax
addq %rsi, %rax
movq %rax, %xmm0
punpckldq 0x4570(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x4578(%rip), %xmm0     # 0xa420
movsd 0x4408(%rip), %xmm1     # 0xa2b8
cmpq %rcx, %rdx
jb 0x5eb9
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
cvtsi2sd %rax, %xmm0
movsd (%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x5efd
movsd 0x15887(%rip), %xmm1    # 0x1b778
subsd 0x8(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jae 0x5f2b
movsd 0x15893(%rip), %xmm1    # 0x1b798
addsd 0x8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jb 0x5e5d
ucomisd (%rsp), %xmm0
jb 0x5e5d
addb %r14b, %r14b
incb %r14b
jmp 0x5e5d
addb %r14b, %r14b
jmp 0x5e5d
cmpb 0x91e5(%rip), %r14b     # 0xf11f
jne 0x5f59
movb $0x1, 0x92fb(%rip)      # 0xf23e
pushq $0x5
popq %rdi
pushq $0x3
popq %rsi
callq 0x5f8b
movb $0x0, 0x92e9(%rip)      # 0xf23e
movb $0x1, %bl
jmp 0x5f7c
callq 0x16b7
movzbl 0x930b(%rip), %edi      # 0xf270
movq 0x926c(%rip), %rsi      # 0xf1d8
xorl %ebx, %ebx
xorl %edx, %edx
callq 0x10d0
andl $0x0, 0x93f4(%rip)      # 0xf370
movl %ebx, %eax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq