// File: /workspace/github_c_makefile_star2_O1/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 0xcd43(%rip), %rbx      # 0xf208
movq $0x0, 0xcd38(%rip) # 0xf208
movb 0x191e9(%rip), %r12b    # 0x1b6c0
movq 0x191fa(%rip), %rax     # 0x1b6d8
movl 0x191ec(%rip), %ecx     # 0x1b6d0
movq %rax, 0x191e5(%rip)     # 0x1b6d0
subl %ecx, %eax
movl %eax, 0xcd01(%rip)      # 0xf1f4
xorl %r15d, %r15d
movl $0x1, %ebp
callq 0x20dc
movb 0x191bb(%rip), %cl      # 0x1b6c1
testb %cl, %cl
je 0x2529
movzbl %al, %esi
movzbl %r12b, %edx
subl %edx, %esi
movl %esi, %edx
negl %edx
cmovsl %esi, %edx
testb %dl, %dl
jns 0x2531
incq %rbx
movb $0x1, %r15b
jmp 0x2613
movq %rbx, %r14
jmp 0x2613
cmpb %r12b, %al
jne 0x2541
incq %rbx
xorl %r15d, %r15d
jmp 0x2613
testl %esi, %esi
setg %r8b
movb 0xcba3(%rip), %dil      # 0xf0f1
testb %dil, %dil
sete %r9b
andb %r8b, %r9b
cmpb $0x1, %r9b
jne 0x2565
movb %r12b, 0xcb8d(%rip)     # 0xf0f2
testl %esi, %esi
setg %r9b
movzbl 0xcb80(%rip), %esi      # 0xf0f2
movl $0x80, %r10d
subl %esi, %r10d
movl %r10d, %r8d
negl %r8d
cmovsl %r10d, %r8d
cmpb %r9b, %dil
jne 0x25e1
movq 0xcc77(%rip), %rsi      # 0xf208
testq %rsi, %rsi
je 0x25a1
movq $0x0, 0xcc67(%rip) # 0xf208
imulq $0x2aaaaaab, %r8, %rdi  # imm = 0x2AAAAAAB
shrq $0x20, %rdi
cmpl $0x4, %edi
jb 0x25b6
movl $0x4, %edi
movzbl %dl, %edx
addq %rsi, %rbx
incq %rbx
leal (,%rdi,4), %esi
movl $0x80, %r8d
subl %esi, %r8d
cmpl %r8d, %edx
setae %sil
cmpl %edi, %edx
seta %r15b
andb %sil, %r15b
jmp 0x2613
addl $-0x80, %esi
movl %esi, %edx
negl %edx
cmovsl %esi, %edx
movq 0xcc16(%rip), %rsi      # 0xf208
incq %rsi
cmpl $0x7, %edx
movq %rsi, 0xcc09(%rip)      # 0xf208
movl $0x2, %edx
sbbq $0x0, %rdx
cmpq %rdx, %rsi
movzbl %r15b, %r15d
cmoval %ebp, %r15d
testb %cl, %cl
je 0x2639
movl %eax, %r12d
testb %r15b, %r15b
je 0x24fb
xorb $0x1, 0xcac7(%rip)      # 0xf0f1
movb %al, 0x19090(%rip)      # 0x1b6c0
movq %rbx, 0x190a1(%rip)     # 0x1b6d8
jmp 0x263c
movq %r14, %rbx
movq %rbx, %rax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq