// File: /workspace/github_c_makefile_star2_O3/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 %r14
pushq %rbx
movq 0xcd5e(%rip), %rbx      # 0xf208
movq $0x0, 0xcd53(%rip) # 0xf208
movb 0x19204(%rip), %bpl     # 0x1b6c0
movq 0x19215(%rip), %rax     # 0x1b6d8
movl 0x19207(%rip), %ecx     # 0x1b6d0
movq %rax, 0x19200(%rip)     # 0x1b6d0
subl %ecx, %eax
movl %eax, 0xcd1c(%rip)      # 0xf1f4
callq 0x20c2
cmpb $0x0, 0x191dd(%rip)     # 0x1b6c1
je 0x2607
movl $0x4, %r14d
movl %ebp, %edi
movl %eax, %ebp
movzbl %al, %esi
movzbl %dil, %edx
subl %edx, %esi
movl %esi, %ecx
negl %ecx
cmovsl %esi, %ecx
testb %cl, %cl
js 0x25e8
cmpb %dil, %bpl
jne 0x2519
incq %rbx
jmp 0x25d4
testl %esi, %esi
setg %r8b
movb 0xcbcc(%rip), %al       # 0xf0f1
testb %al, %al
sete %r9b
testb %r9b, %r8b
je 0x2539
movb %dil, 0xcbbb(%rip)      # 0xf0f2
jmp 0x2540
movzbl 0xcbb2(%rip), %edx      # 0xf0f2
testl %esi, %esi
setg %dil
movl $0x80, %r8d
subl %edx, %r8d
movl %r8d, %esi
negl %esi
cmovsl %r8d, %esi
cmpb %dil, %al
jne 0x25a8
movq 0xcca4(%rip), %rdx      # 0xf208
testq %rdx, %rdx
je 0x2574
movq $0x0, 0xcc94(%rip) # 0xf208
movzbl %cl, %ecx
movzbl %sil, %esi
imull $0xab, %esi, %esi
shrl $0xa, %esi
addq %rdx, %rbx
incq %rbx
movzbl %sil, %edx
cmpb $0x4, %dl
cmovael %r14d, %edx
shlb $0x2, %dl
movb $-0x80, %sil
subb %dl, %sil
movzbl %sil, %edx
cmpl %edx, %ecx
jb 0x25d4
jmp 0x25f1
addl $-0x80, %edx
movl %edx, %ecx
negl %ecx
cmovsl %edx, %ecx
movq 0xcc4f(%rip), %rdx      # 0xf208
incq %rdx
cmpl $0x7, %ecx
movq %rdx, 0xcc42(%rip)      # 0xf208
movl $0x2, %ecx
sbbq $0x0, %rcx
cmpq %rcx, %rdx
ja 0x25f1
callq 0x20c2
cmpb $0x0, 0x190e1(%rip)     # 0x1b6c1
jne 0x24f0
jmp 0x2607
incq %rbx
movb 0xcb00(%rip), %al       # 0xf0f1
xorb $0x1, %al
movb %al, 0xcaf8(%rip)       # 0xf0f1
movb %bpl, 0x190c0(%rip)     # 0x1b6c0
movq %rbx, 0x190d1(%rip)     # 0x1b6d8
movq %rbx, %rax
popq %rbx
popq %r14
popq %rbp
retq