// File: /workspace/github_cpp_makefile_star2_O2/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
subq $0x18, %rsp
movq %rdi, %rbx
movslq 0x48(%rdi), %r12
movq 0x50(%rdi), %rax
movq %rax, 0x8(%rsp)
andl $0x0, (%rax)
xorq $0x1, %r12
xorl %edi, %edi
pushq $0x4
popq %r14
xorl %ebp, %ebp
xorl %r15d, %r15d
xorl %r9d, %r9d
movl 0xc(%rbx), %ecx
cmpl %ecx, %ebp
jge 0x8c02
movq %r14, 0x10(%rsp)
movl %r15d, %r14d
movl %r9d, %r15d
movslq 0x8(%rbx), %rsi
movslq 0x48(%rbx), %rax
movl %esi, %edx
imull %ebp, %edx
movslq %edx, %rdx
addq $0x4, %rdx
xorl %r9d, %r9d
movq %r9, %r8
cmpq %rsi, %r9
jge 0x8b37
movq 0x38(%rbx,%rax,8), %r9
movl (%r9,%rdx,4), %r10d
movq 0x38(%rbx,%r12,8), %r11
leaq 0x3(%r8), %r9
leaq 0x3(%rdx), %r13
cmpl (%r11,%rdx,4), %r10d
movq %r13, %rdx
je 0x8ae0
movl %ecx, %eax
cltd
pushq $0x6
popq %r9
idivl %r9d
cmpl %eax, %r15d
jge 0x8c59
movl %r15d, %r9d
incl %r9d
movzbl %dil, %edi
testb $0x1, %dil
pushq $0x1
popq %rax
cmovel %eax, %edi
movl %r14d, %r15d
cmovel %ebp, %r15d
jmp 0x8b3d
movl %r15d, %r9d
movl %r14d, %r15d
testb $0x1, %dil
movq 0x10(%rsp), %r10
je 0x8bf4
cmpq %rsi, %r8
setl %dl
leal -0x2(%rcx), %eax
cmpl %eax, %ebp
setne %r8b
testb %r8b, %dl
jne 0x8bf4
movq 0x8(%rsp), %rdx
incw 0x2(%rdx)
cmpl %eax, %ebp
movq 0x50(%rbx), %rdi
cmovnel %ebp, %ecx
movslq %r10d, %rax
andw $0x0, (%rdi,%rax)
subl %r15d, %ecx
movl %r15d, %edx
rolw $0x8, %dx
movw %dx, 0x2(%rdi,%rax)
movl %esi, %edx
rolw $0x8, %dx
movw %dx, 0x4(%rdi,%rax)
movl %ecx, %edx
rolw $0x8, %dx
movw %dx, 0x6(%rdi,%rax)
andl $0x0, 0x8(%rdi,%rax)
imull %esi, %ecx
leal (%r10,%rcx,4), %r13d
addl $0xc, %r13d
cmpl 0x58(%rbx), %r13d
jg 0x8c59
addl $0xc, %r10d
shll $0x2, %ecx
movslq %r10d, %rax
addq %rax, %rdi
movslq 0x48(%rbx), %rax
movq 0x38(%rbx,%rax,8), %rax
imull %r15d, %esi
movslq %esi, %rdx
leaq (%rax,%rdx,4), %rsi
addq $0x10, %rsi
movslq %ecx, %rdx
movl %r9d, %r14d
callq 0x4170
movl %r14d, %r9d
xorl %edi, %edi
jmp 0x8bf7
movl %r10d, %r13d
addl $0x2, %ebp
movl %r13d, %r14d
jmp 0x8ab3
xorl %ebp, %ebp
testl %r9d, %r9d
je 0x8c48
movq 0x8(%rsp), %rax
rolw $0x8, 0x2(%rax)
movq 0x50(%rbx), %rsi
addq $0x20, %rbx
movq %rbx, %rdi
movl %r14d, %edx
callq 0x6d62
cmpl %r14d, %eax
je 0x8c48
leaq 0x2864(%rip), %rdi      # 0xb496
leaq 0x2877(%rip), %rsi      # 0xb4b0
movl $0x1d8, %edx            # imm = 0x1D8
xorl %eax, %eax
callq 0x4050
pushq $-0x1
popq %rbp
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x9170
nop