// File: tomfunkhouser[P]gaps/pkgs/jpeg/jdmarker.c
// Function: jpeg_resync_to_restart
GLOBAL(jboolean)
jpeg_resync_to_restart (j_decompress_ptr cinfo, int desired)
{
  int marker = cinfo->unread_marker;
  int action = 1;
  
  /* Always put up a warning. */
  WARNMS2(cinfo, JWRN_MUST_RESYNC, marker, desired);
  
  /* Outer loop handles repeated decision after scanning forward. */
  for (;;) {
    if (marker < (int) M_SOF0)
      action = 2;		/* invalid marker */
    else if (marker < (int) M_RST0 || marker > (int) M_RST7)
      action = 3;		/* valid non-restart marker */
    else {
      if (marker == ((int) M_RST0 + ((desired+1) & 7)) ||
	  marker == ((int) M_RST0 + ((desired+2) & 7)))
	action = 3;		/* one of the next two expected restarts */
      else if (marker == ((int) M_RST0 + ((desired-1) & 7)) ||
	       marker == ((int) M_RST0 + ((desired-2) & 7)))
	action = 2;		/* a prior restart, so advance */
      else
	action = 1;		/* desired restart or too far away */
    }
    TRACEMS2(cinfo, 4, JTRC_RECOVERY_ACTION, marker, action);
    switch (action) {
    case 1:
      /* Discard marker and let entropy decoder resume processing. */
      cinfo->unread_marker = 0;
      return TRUE;
    case 2:
      /* Scan to the next marker, and repeat the decision loop. */
      if (! next_marker(cinfo))
	return FALSE;
      marker = cinfo->unread_marker;
      break;
    case 3:
      /* Return without advancing past this marker. */
      /* Entropy decoder will be forced to process an empty segment. */
      return TRUE;
    }
  } /* end loop */
}

// Assembly:
subq $0x28, %rsp
movq %rdi, 0x18(%rsp)
movl %esi, 0x14(%rsp)
movq 0x18(%rsp), %rax
movl 0x21c(%rax), %eax
movl %eax, 0x10(%rsp)
movl $0x1, 0xc(%rsp)
movq 0x18(%rsp), %rax
movq (%rax), %rax
movl $0x79, 0x28(%rax)
movl 0x10(%rsp), %ecx
movq 0x18(%rsp), %rax
movq (%rax), %rax
movl %ecx, 0x2c(%rax)
movl 0x14(%rsp), %ecx
movq 0x18(%rsp), %rax
movq (%rax), %rax
movl %ecx, 0x30(%rax)
movq 0x18(%rsp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movq 0x18(%rsp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq *%rax
cmpl $0xc0, 0x10(%rsp)
jge 0xe5390
movl $0x2, 0xc(%rsp)
jmp 0xe5433
cmpl $0xd0, 0x10(%rsp)
jl 0xe53a4
cmpl $0xd7, 0x10(%rsp)
jle 0xe53b1
movl $0x3, 0xc(%rsp)
jmp 0xe5431
movl 0x10(%rsp), %eax
movl 0x14(%rsp), %ecx
addl $0x1, %ecx
andl $0x7, %ecx
addl $0xd0, %ecx
cmpl %ecx, %eax
je 0xe53e1
movl 0x10(%rsp), %eax
movl 0x14(%rsp), %ecx
addl $0x2, %ecx
andl $0x7, %ecx
addl $0xd0, %ecx
cmpl %ecx, %eax
jne 0xe53eb
movl $0x3, 0xc(%rsp)
jmp 0xe542f
movl 0x10(%rsp), %eax
movl 0x14(%rsp), %ecx
subl $0x1, %ecx
andl $0x7, %ecx
addl $0xd0, %ecx
cmpl %ecx, %eax
je 0xe541b
movl 0x10(%rsp), %eax
movl 0x14(%rsp), %ecx
subl $0x2, %ecx
andl $0x7, %ecx
addl $0xd0, %ecx
cmpl %ecx, %eax
jne 0xe5425
movl $0x2, 0xc(%rsp)
jmp 0xe542d
movl $0x1, 0xc(%rsp)
jmp 0xe542f
jmp 0xe5431
jmp 0xe5433
movq 0x18(%rsp), %rax
movq (%rax), %rax
movl $0x61, 0x28(%rax)
movl 0x10(%rsp), %ecx
movq 0x18(%rsp), %rax
movq (%rax), %rax
movl %ecx, 0x2c(%rax)
movl 0xc(%rsp), %ecx
movq 0x18(%rsp), %rax
movq (%rax), %rax
movl %ecx, 0x30(%rax)
movq 0x18(%rsp), %rdi
movq (%rdi), %rax
movq 0x8(%rax), %rax
movl $0x4, %esi
callq *%rax
movl 0xc(%rsp), %eax
movl %eax, 0x8(%rsp)
subl $0x1, %eax
je 0xe5498
jmp 0xe5482
movl 0x8(%rsp), %eax
subl $0x2, %eax
je 0xe54b1
jmp 0xe548d
movl 0x8(%rsp), %eax
subl $0x3, %eax
je 0xe54db
jmp 0xe54e5
movq 0x18(%rsp), %rax
movl $0x0, 0x21c(%rax)
movl $0x1, 0x24(%rsp)
jmp 0xe54ea
movq 0x18(%rsp), %rdi
callq 0xe5500
cmpl $0x0, %eax
jne 0xe54ca
movl $0x0, 0x24(%rsp)
jmp 0xe54ea
movq 0x18(%rsp), %rax
movl 0x21c(%rax), %eax
movl %eax, 0x10(%rsp)
jmp 0xe54e5
movl $0x1, 0x24(%rsp)
jmp 0xe54ea
jmp 0xe5379
movl 0x24(%rsp), %eax
addq $0x28, %rsp
retq
nopw %cs:(%rax,%rax)