// File: /workspace/github_cpp_makefile_star2_O1/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 $0x28, %rsp
movq %rdi, %rbx
movslq 0x48(%rdi), %rdx
movq 0x50(%rdi), %rax
movq %rax, 0x18(%rsp)
movl $0x0, (%rax)
movl 0xc(%rdi), %ecx
testl %ecx, %ecx
setg %r12b
jle 0x96c5
xorq $0x1, %rdx
movq %rdx, 0x20(%rsp)
movl $0x4, (%rsp)
movq $0x0, 0x8(%rsp)
xorl %r14d, %r14d
xorl %ebp, %ebp
movl $0x0, 0x4(%rsp)
movslq 0x8(%rbx), %rdx
movb $0x1, %al
testq %rdx, %rdx
setle %r15b
jle 0x95ba
movl %edx, %r8d
imull %ebp, %r8d
movslq 0x48(%rbx), %rsi
movq 0x38(%rbx,%rsi,8), %rsi
movq 0x20(%rsp), %rdi
movq 0x38(%rbx,%rdi,8), %rdi
movl 0x10(%rsi,%r8,4), %r9d
cmpl 0x10(%rdi,%r8,4), %r9d
jne 0x9565
movl %edx, %r8d
imull %ebp, %r8d
leaq (%rsi,%r8,4), %rsi
addq $0x1c, %rsi
leaq (%rdi,%r8,4), %rdi
addq $0x1c, %rdi
movl $0x3, %r8d
cmpq %rdx, %r8
setae %r15b
jae 0x95ba
movl -0xc(%rsi,%r8,4), %r9d
leaq 0x3(%r8), %r10
cmpl -0xc(%rdi,%r8,4), %r9d
movq %r10, %r8
je 0x9549
movq 0x8(%rsp), %rsi
leal 0x1(%rsi), %r13d
movslq %ecx, %rcx
imulq $0x2aaaaaab, %rcx, %rcx # imm = 0x2AAAAAAB
movq %rcx, %rdx
shrq $0x3f, %rdx
shrq $0x20, %rcx
addl %edx, %ecx
cmpl %ecx, %esi
jge 0x95a5
movl 0x4(%rsp), %edx
movzbl %dl, %edx
testb $0x1, %dl
movl $0x1, %ecx
cmovel %ecx, %edx
movl %edx, 0x4(%rsp)
cmovel %ebp, %r14d
jmp 0x95b2
movq %rbx, %rdi
callq 0x9cd8
movl %eax, %r11d
xorl %eax, %eax
movl %r13d, %ecx
movq %rcx, 0x8(%rsp)
testb %al, %al
je 0x968c
movb $0x1, %r13b
testb $0x1, 0x4(%rsp)
je 0x968f
movl 0xc(%rbx), %eax
leal -0x2(%rax), %ecx
cmpl %ecx, %ebp
setne %dl
notb %r15b
testb %dl, %r15b
jne 0x968f
movq 0x18(%rsp), %rdx
incw 0x2(%rdx)
cmpl %ecx, %ebp
movq 0x50(%rbx), %rdi
cmovnel %ebp, %eax
movslq (%rsp), %rdx
subl %r14d, %eax
movw $0x0, (%rdi,%rdx)
movl %r14d, %ecx
rolw $0x8, %cx
movw %cx, 0x2(%rdi,%rdx)
movl 0x8(%rbx), %ecx
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 (%rdx,%rax,4), %r15d
addl $0xc, %r15d
addl $0xc, %edx
cmpl 0x58(%rbx), %r15d
setle %r13b
jg 0x96a8
shll $0x2, %eax
movslq %edx, %rdx
addq %rdx, %rdi
movslq 0x48(%rbx), %rdx
movq 0x38(%rbx,%rdx,8), %rdx
imull %r14d, %ecx
movslq %ecx, %rcx
leaq (%rdx,%rcx,4), %rsi
addq $0x10, %rsi
movslq %eax, %rdx
movl %r11d, 0x14(%rsp)
callq 0x4140
movl 0x14(%rsp), %r11d
movl $0x0, 0x4(%rsp)
movl %r15d, (%rsp)
jmp 0x968f
xorl %r13d, %r13d
testb %r13b, %r13b
je 0x96b8
addl $0x2, %ebp
movl 0xc(%rbx), %ecx
cmpl %ecx, %ebp
setl %r12b
jl 0x94f3
jmp 0x96b8
movl %edx, (%rsp)
movq %rbx, %rdi
callq 0x9cd8
movl %eax, %r11d
jmp 0x968f
cmpl $0x0, 0x8(%rsp)
sete %al
movl (%rsp), %ebp
jmp 0x96cc
movb $0x1, %al
movl $0x4, %ebp
orb %r12b, %al
xorl %ecx, %ecx
testb $0x1, %r12b
cmovel %ecx, %r11d
testb $0x1, %al
jne 0x9723
movq 0x18(%rsp), %rax
rolw $0x8, 0x2(%rax)
movq 0x50(%rbx), %rsi
addq $0x20, %rbx
movq %rbx, %rdi
movl %ebp, %edx
callq 0x719c
movl $0x0, %r11d
cmpl %ebp, %eax
je 0x9723
leaq 0x1d7c(%rip), %rdi      # 0xb486
leaq 0x1d8f(%rip), %rsi      # 0xb4a0
movl $0x1d8, %edx            # imm = 0x1D8
xorl %eax, %eax
callq 0x4050
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
movl %r11d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop