// File: /workspace/github_cpp_makefile_star2_O3/mikeakohn[P]cloudtari/build/../src/TelevisionVNC.cxx
// Function: TelevisionVNC::send_image_diff()
int TelevisionVNC::send_image_diff()
{
  int line_mismatch_count = 0;
  int old_page = image_page ^ 1;
  int mismatch_start = 0;
  bool in_mismatch = false;
  int diff_ptr = sizeof(FramebufferUpdate);
  FramebufferUpdate *frame_buffer_update = (FramebufferUpdate *)diff_buffer;

  memset(frame_buffer_update, 0, sizeof(FramebufferUpdate));

  for (int y = 0; y < height; y += 2)
  {
    int line = y * width;
    bool is_mismatch = false;

    for (int x = 0; x < width; x += 3)
    {
      is_mismatch =
        image_packet[image_page]->data[line + x] !=
        image_packet[old_page]->data[line + x];

      if (is_mismatch)
      {
        line_mismatch_count++;

        if (line_mismatch_count > height / 6) { return send_image_full(); }
        if (!in_mismatch) { mismatch_start = y; in_mismatch = true; }

        break;
      }
    }

    // If mismatch flag is set and this is the last line in the image or
    // no longer in a mismatch.
    if (in_mismatch)
    {
      if (y == height - 2 || is_mismatch == false)
      {
        frame_buffer_update->number_of_rectangles++;

        UpdateRectangle *update_rectangle =
          (UpdateRectangle *)(diff_buffer + diff_ptr);

        int copy_height = y - mismatch_start;

        if (y == height - 2)
        {
          copy_height = height - mismatch_start;
        }

        update_rectangle->x = htons(0);
        update_rectangle->y = htons(mismatch_start);
        update_rectangle->width = htons(width);
        update_rectangle->height = htons(copy_height);
        update_rectangle->encoding_type = htons(0);

        diff_ptr += sizeof(UpdateRectangle);

        int copy_size = width * copy_height * 4;

        if (diff_ptr + copy_size > diff_buffer_length)
        {
          return send_image_full();
        }

        memcpy(
          diff_buffer + diff_ptr,
          &image_packet[image_page]->data[mismatch_start * width],
          copy_size);

        diff_ptr += copy_size;

        in_mismatch = false;
      }
    }
  }

  if (line_mismatch_count == 0) { return 0; }

  frame_buffer_update->number_of_rectangles =
    htons(frame_buffer_update->number_of_rectangles);

  if (net_send(diff_buffer, diff_ptr) != diff_ptr)
  {
    printf("Error: Send packet %s:%d\n", __FILE__, __LINE__);
    return -1;
  }

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movslq 0x48(%rdi), %r11
movq 0x50(%rdi), %rax
movq %rax, (%rsp)
movl $0x0, (%rax)
movl 0xc(%rdi), %eax
testl %eax, %eax
jle 0x9820
movq %rdi, %rbx
xorq $0x1, %r11
movl $0x4, %r14d
xorl %r13d, %r13d
xorl %ebp, %ebp
xorl %r15d, %r15d
xorl %edx, %edx
movl 0x8(%rbx), %ecx
movb $0x1, %sil
testl %ecx, %ecx
jle 0x9715
movslq 0x48(%rbx), %rdi
movq 0x38(%rbx,%rdi,8), %r8
movq 0x38(%rbx,%r11,8), %rdi
movl %ecx, %r9d
imull %r15d, %r9d
leaq (%rdi,%r9,4), %rdi
addq $0x10, %rdi
leaq (%r8,%r9,4), %r8
addq $0x10, %r8
xorl %r9d, %r9d
movl (%r8,%r9,4), %r10d
cmpl (%rdi,%r9,4), %r10d
jne 0x96de
addq $0x3, %r9
cmpl %r9d, %ecx
jg 0x96c9
jmp 0x9715
movslq %eax, %rsi
imulq $0x2aaaaaab, %rsi, %rsi # imm = 0x2AAAAAAB
movq %rsi, %rdi
shrq $0x3f, %rdi
shrq $0x20, %rsi
addl %edi, %esi
cmpl %esi, %r13d
jge 0x9833
incl %r13d
movzbl %dl, %edx
testb $0x1, %dl
movl $0x1, %esi
cmovel %esi, %edx
cmovel %r15d, %ebp
xorl %esi, %esi
testb $0x1, %dl
je 0x97ca
leal -0x2(%rax), %edi
cmpl %edi, %r15d
setne %r8b
notb %sil
testb %r8b, %sil
jne 0x97ca
movq (%rsp), %rdx
incw 0x2(%rdx)
cmpl %edi, %r15d
movq 0x50(%rbx), %rdi
cmovnel %r15d, %eax
movslq %r14d, %rdx
subl %ebp, %eax
movw $0x0, (%rdi,%rdx)
movl %ebp, %esi
rolw $0x8, %si
movw %si, 0x2(%rdi,%rdx)
movl %ecx, %esi
rolw $0x8, %si
movw %si, 0x4(%rdi,%rdx)
movl %eax, %esi
rolw $0x8, %si
movw %si, 0x6(%rdi,%rdx)
movl $0x0, 0x8(%rdi,%rdx)
imull %ecx, %eax
leal (%r14,%rax,4), %r12d
addl $0xc, %r12d
cmpl 0x58(%rbx), %r12d
jg 0x9833
addl $0xc, %r14d
shll $0x2, %eax
movslq %r14d, %rdx
addq %rdx, %rdi
movslq 0x48(%rbx), %rdx
movq 0x38(%rbx,%rdx,8), %rdx
imull %ebp, %ecx
movslq %ecx, %rcx
leaq (%rdx,%rcx,4), %rsi
addq $0x10, %rsi
movslq %eax, %rdx
movq %r11, %r14
callq 0x4150
movq %r14, %r11
movl 0xc(%rbx), %eax
xorl %edx, %edx
movl %r12d, %r14d
addl $0x2, %r15d
cmpl %eax, %r15d
jl 0x9697
xorl %ebp, %ebp
testl %r13d, %r13d
je 0x9822
movq (%rsp), %rax
rolw $0x8, 0x2(%rax)
movq 0x50(%rbx), %rsi
addq $0x20, %rbx
movq %rbx, %rdi
movl %r14d, %edx
callq 0x7242
cmpl %r14d, %eax
je 0x9822
leaq 0x2e00(%rip), %rdi      # 0xc606
leaq 0x2e13(%rip), %rsi      # 0xc620
movl $0x1d8, %edx            # imm = 0x1D8
xorl %eax, %eax
callq 0x4050
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x9822
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x9dfc
nop