// File: /workspace/github_c_makefile_star2_O2/mincebert[P]maketzx/maketzx.c
// Function: FindEdge1
dword FindEdge1(void)
{
  dword samples=revsamples;
  byte curr,last,wavdir=direction,loopbreak=0;
  revsamples=0;
  curr=last=lastsample;
  diff=(currpulse-lastpulse);
  lastpulse=currpulse;
/****************************************************************************/
  do {
       byte delta,maxrev;
       last=curr;
       curr=ReadVOCData();
       if (!blocktype) return samples;
       if (abs(curr-last)>=128) { samples++; loopbreak=1; }
       else if (!(curr-last)) { samples++; loopbreak=0; }
	    else {

		   wavdir=(curr-last>0);
		   if (wavdir && !direction) lastpeak=last;
		   delta=(abs(128-lastpeak)/6); if (delta>4) delta=4;
		   maxrev=2-(abs(lastpeak-128)<7);
		   if (maxrev>2) maxrev=2; if (!maxrev) maxrev=1;
		   if (wavdir!=direction)
		      {
			revsamples++;
			if (revsamples>maxrev) { wavdir^=1; loopbreak=1; }
		      }
		   else {
			  samples+=(revsamples+1);
			  if (revsamples) revsamples=0;
			  if (abs(curr-last)<128-delta*4 && (wavdir==direction) || (abs(curr-last)<=delta && wavdir==direction) || curr==last) loopbreak=0;
			  else loopbreak=1;
			}
		 }
     }
 while (!loopbreak);
  direction^=1;
  lastsample=curr;
  currpulse=samples;
  return samples;

/****************************************************************************/
/*  if (lastsample>128) while (curr>128)
			    {
			      last=curr;
			      samples++;
			      curr=ReadVOCData();
			      if (!blocktype) return samples;
			      if (abs(last-curr)>=128 || !(last-curr)) curr=240*(curr>128)+15*(curr<=128);
			      else if (curr>127) curr=240;
				   else curr=16;
			    }
  else while (curr<=128)
	     {                           // Routine 2
	       last=curr;                // For squared and non squared files
	       samples++;
	       curr=ReadVOCData();
	       if (!blocktype) return samples;
	       if (abs(last-curr)>=128 || !(last-curr)) curr=240*(curr>128)+15*(curr<=128);
	       else if (curr>127) curr=240;
		    else curr=16;
	     }
  lastsample=curr;
  currpulse=samples;
  return samples; */
/****************************************************************************/
/*  if (lastsample>128) while (last>128)
			    {
			      samples++;
			      last=ReadVOCData();
			      if (!blocktype) return samples;
			      last=240*(last>128)+15*(last<=128);
			    }
  else while (last<=128)                    // Routine 1
	     {                              // For squared files only
	       samples++;
	       last=ReadVOCData();
	       if (!blocktype) return samples;
	       last=240*(last>128)+15*(last<=128);
	     } */
//  peak=last;
/*  lastsample=last;
  currpulse=samples;
  return samples;*/
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movq 0xcdc0(%rip), %rbx      # 0xf1f8
andq $0x0, 0xcdb8(%rip)      # 0xf1f8
movb 0x1926a(%rip), %cl      # 0x1b6b0
movq 0x1927b(%rip), %rax     # 0x1b6c8
movl 0x1926d(%rip), %edx     # 0x1b6c0
movq %rax, 0x19266(%rip)     # 0x1b6c0
subl %edx, %eax
movl %eax, 0xcd82(%rip)      # 0xf1e4
movb $0x6, %bpl
pushq $0x4
popq %r14
pushq $0x2
popq %r15
movl %ecx, %r12d
callq 0x2053
cmpb $0x0, 0x19235(%rip)     # 0x1b6b1
je 0x2580
movl %eax, %ecx
movzbl %al, %edi
movzbl %r12b, %eax
subl %eax, %edi
movl %edi, %esi
negl %esi
cmovsl %edi, %esi
testb %sil, %sil
js 0x2561
cmpb %r12b, %cl
je 0x2507
testl %edi, %edi
setg %r8b
movb 0xcc33(%rip), %dl       # 0xf0e1
testb %dl, %dl
sete %r9b
testb %r9b, %r8b
je 0x24c2
movb %r12b, 0xcc22(%rip)     # 0xf0e2
jmp 0x24c9
movzbl 0xcc19(%rip), %eax      # 0xf0e2
testl %edi, %edi
setg %dil
cmpb %dil, %dl
je 0x250f
addl $-0x80, %eax
movl %eax, %esi
negl %esi
cmovsl %eax, %esi
movq 0xcd13(%rip), %rax      # 0xf1f8
incq %rax
cmpl $0x7, %esi
movq %rax, 0xcd06(%rip)      # 0xf1f8
movq %r15, %rsi
sbbq $0x0, %rsi
movl %ecx, %r12d
cmpq %rsi, %rax
jbe 0x2470
jmp 0x256a
incq %rbx
jmp 0x246d
movl $0x80, %edi
subl %eax, %edi
movl %edi, %eax
negl %eax
cmovsl %edi, %eax
movzbl %al, %eax
divb %bpl
movq 0xccce(%rip), %rdi      # 0xf1f8
testq %rdi, %rdi
je 0x2537
andq $0x0, 0xccc1(%rip)      # 0xf1f8
movzbl %sil, %esi
addq %rdi, %rbx
incq %rbx
movzbl %al, %eax
cmpb $0x4, %al
cmovael %r14d, %eax
shlb $0x2, %al
movb $-0x80, %dil
subb %al, %dil
movzbl %dil, %eax
cmpl %eax, %esi
jb 0x246d
jmp 0x256a
incq %rbx
movb 0xcb77(%rip), %dl       # 0xf0e1
xorb $0x1, %dl
movb %dl, 0xcb6e(%rip)       # 0xf0e1
movb %cl, 0x19137(%rip)      # 0x1b6b0
movq %rbx, 0x19148(%rip)     # 0x1b6c8
movq %rbx, %rax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq