// File: /workspace/github_cpp_makefile_star2_O2/eggnot[P]pixel_camera/matrix/lib/framebuffer.cc
// Function: rgb_matrix::internal::Framebuffer::SetPixel(int, int, unsigned char, unsigned char, unsigned char)
void Framebuffer::SetPixel(int x, int y, uint8_t r, uint8_t g, uint8_t b) {
  if (x < 0 || x >= columns_ || y < 0 || y >= height_) return;

  const uint16_t red   = MapColor(r);
  const uint16_t green = MapColor(PANEL_SWAP_G_B_ ? b : g);
  const uint16_t blue  = MapColor(PANEL_SWAP_G_B_ ? g : b);

  const int min_bit_plane = kBitPlanes - pwm_bits_;
  IoBits *bits = ValueAt(y & row_mask_, x, min_bit_plane);

  // Manually expand the three cases for better performance.
  // TODO(hzeller): This is a bit repetetive. Test if it pays off to just
  // pre-calc rgb mask and apply.
  if (y < rows_) {
    // Parallel chain #1
    if (y < double_rows_) {   // Upper sub-panel.
      for (int b = min_bit_plane; b < kBitPlanes; ++b) {
        const uint16_t mask = 1 << b;
        bits->bits.p0_r1 = (red & mask) == mask;
        bits->bits.p0_g1 = (green & mask) == mask;
        bits->bits.p0_b1 = (blue & mask) == mask;
        bits += columns_;
      }
    } else {
      for (int b = min_bit_plane; b < kBitPlanes; ++b) {
        const uint16_t mask = 1 << b;
        bits->bits.p0_r2 = (red & mask) == mask;
        bits->bits.p0_g2 = (green & mask) == mask;
        bits->bits.p0_b2 = (blue & mask) == mask;
        bits += columns_;
      }
    }
#ifndef ONLY_SINGLE_CHAIN
  } else if (y >= rows_ && y < 2 * rows_) {
    // Parallel chain #2
    if (y - rows_ < double_rows_) {   // Upper sub-panel.
      for (int b = min_bit_plane; b < kBitPlanes; ++b) {
        const uint16_t mask = 1 << b;
        bits->bits.p1_r1 = (red & mask) == mask;
        bits->bits.p1_g1 = (green & mask) == mask;
        bits->bits.p1_b1 = (blue & mask) == mask;
        bits += columns_;
      }
    } else {
      for (int b = min_bit_plane; b < kBitPlanes; ++b) {
        const uint16_t mask = 1 << b;
        bits->bits.p1_r2 = (red & mask) == mask;
        bits->bits.p1_g2 = (green & mask) == mask;
        bits->bits.p1_b2 = (blue & mask) == mask;
        bits += columns_;
      }
    }
  } else {
    // Parallel chain #3
    if (y - 2*rows_ < double_rows_) {   // Upper sub-panel.
      for (int b = min_bit_plane; b < kBitPlanes; ++b) {
        const uint16_t mask = 1 << b;
        bits->bits.p2_r1 = (red & mask) == mask;
        bits->bits.p2_g1 = (green & mask) == mask;
        bits->bits.p2_b1 = (blue & mask) == mask;
        bits += columns_;
      }
    } else {
      for (int b = min_bit_plane; b < kBitPlanes; ++b) {
        const uint16_t mask = 1 << b;
        bits->bits.p2_r2 = (red & mask) == mask;
        bits->bits.p2_g2 = (green & mask) == mask;
        bits->bits.p2_b2 = (blue & mask) == mask;
        bits += columns_;
      }
    }
#endif
  }
}

// Assembly:
testl %esi, %esi
js 0x3f88
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %edx, %ebp
testl %edx, %edx
js 0x3f7a
movl %esi, %r14d
movq %rdi, %rbx
cmpl %esi, 0xc(%rdi)
jle 0x3f7a
cmpl %ebp, 0x8(%rbx)
jle 0x3f7a
movl %r9d, %r13d
movl %r8d, %r12d
movzbl %cl, %esi
movq %rbx, %rdi
callq 0x40b4
movl %eax, %r15d
movzbl %r12b, %esi
movq %rbx, %rdi
callq 0x40b4
movl %eax, %r12d
movzbl %r13b, %esi
movq %rbx, %rdi
callq 0x40b4
movzbl 0x10(%rbx), %edx
pushq $0xb
popq %rcx
subl %edx, %ecx
movzbl 0x18(%rbx), %edx
andl %ebp, %edx
imull $0xb, %edx, %edx
addl %ecx, %edx
imull 0xc(%rbx), %edx
addl %r14d, %edx
movslq %edx, %rdx
shlq $0x2, %rdx
addq 0x20(%rbx), %rdx
movl (%rbx), %r9d
movl %ebp, %r8d
subl %r9d, %r8d
jge 0x3d21
movzwl %r15w, %esi
movzwl %r12w, %edi
movzwl %ax, %eax
cmpl %ebp, 0x14(%rbx)
jle 0x3db7
pushq $0x1
popq %r8
movl $0xf7fff77f, %r9d       # imm = 0xF7FFF77F
cmpl $0xb, %ecx
je 0x3f7a
movl %r8d, %r10d
shll %cl, %r10d
movzwl %r10w, %r11d
movl %r10d, %ebp
andl %esi, %ebp
xorl %r14d, %r14d
cmpl %r11d, %ebp
sete %r14b
shll $0xb, %r14d
movl (%rdx), %ebp
andl %r9d, %ebp
movl %r10d, %r15d
andl %edi, %r15d
xorl %r12d, %r12d
cmpl %r11d, %r15d
sete %r12b
shll $0x1b, %r12d
andl %eax, %r10d
xorl %r15d, %r15d
cmpl %r11d, %r10d
sete %r15b
shll $0x7, %r15d
orl %r14d, %r12d
orl %r15d, %r12d
orl %ebp, %r12d
movl %r12d, (%rdx)
movslq 0xc(%rbx), %r10
leaq (%rdx,%r10,4), %rdx
incl %ecx
jmp 0x3cb9
addl %r9d, %r9d
movl 0x14(%rbx), %r10d
movzwl %r15w, %esi
movzwl %r12w, %edi
movzwl %ax, %eax
subl %r9d, %ebp
jge 0x3e29
cmpl %r10d, %r8d
jge 0x3e9e
pushq $0x1
popq %r8
movl $0xffffef9f, %r9d       # imm = 0xFFFFEF9F
cmpl $0xb, %ecx
je 0x3f7a
movl %r8d, %r10d
shll %cl, %r10d
movzwl %r10w, %r11d
movl %r10d, %ebp
andl %esi, %ebp
xorl %r14d, %r14d
cmpl %r11d, %ebp
sete %r14b
shll $0xc, %r14d
movl (%rdx), %ebp
andl %r9d, %ebp
movl %r10d, %r15d
andl %edi, %r15d
xorl %r12d, %r12d
cmpl %r11d, %r15d
sete %r12b
shll $0x5, %r12d
andl %eax, %r10d
xorl %r15d, %r15d
cmpl %r11d, %r10d
sete %r15b
shll $0x6, %r15d
orl %r14d, %r12d
orl %r15d, %r12d
orl %ebp, %r12d
movl %r12d, (%rdx)
movslq 0xc(%rbx), %r10
leaq (%rdx,%r10,4), %rdx
incl %ecx
jmp 0x3d4f
pushq $0x1
popq %r8
movl $0xfffff8ff, %r9d       # imm = 0xFFFFF8FF
cmpl $0xb, %ecx
je 0x3f7a
movl %r8d, %r10d
shll %cl, %r10d
movzwl %r10w, %r11d
movl %r10d, %ebp
andl %esi, %ebp
xorl %r14d, %r14d
cmpl %r11d, %ebp
sete %r14b
shll $0x8, %r14d
movl (%rdx), %ebp
andl %r9d, %ebp
movl %r10d, %r15d
andl %edi, %r15d
xorl %r12d, %r12d
cmpl %r11d, %r15d
sete %r12b
shll $0x9, %r12d
andl %eax, %r10d
xorl %r15d, %r15d
cmpl %r11d, %r10d
sete %r15b
shll $0xa, %r15d
orl %r14d, %r12d
orl %r15d, %r12d
orl %ebp, %r12d
movl %r12d, (%rdx)
movslq 0xc(%rbx), %r10
leaq (%rdx,%r10,4), %rdx
incl %ecx
jmp 0x3dc1
pushq $0x1
popq %r8
cmpl %r10d, %ebp
jge 0x3f10
movl $0xffffbff3, %r9d       # imm = 0xFFFFBFF3
cmpl $0xb, %ecx
je 0x3f7a
movl %r8d, %r10d
shll %cl, %r10d
movzwl %r10w, %r11d
movl %r10d, %ebp
andl %esi, %ebp
xorl %r14d, %r14d
cmpl %r11d, %ebp
sete %r14b
shll $0xe, %r14d
movl (%rdx), %ebp
andl %r9d, %ebp
movl %r10d, %r15d
andl %edi, %r15d
xorl %r12d, %r12d
cmpl %r11d, %r15d
sete %r12b
andl %eax, %r10d
xorl %r15d, %r15d
cmpl %r11d, %r10d
sete %r15b
leal (%r14,%r12,4), %r10d
leal (%r10,%r15,8), %r10d
orl %ebp, %r10d
movl %r10d, (%rdx)
movslq 0xc(%rbx), %r10
leaq (%rdx,%r10,4), %rdx
incl %ecx
jmp 0x3e3c
pushq $0x1
popq %r8
movl $0xffe7dfff, %r9d       # imm = 0xFFE7DFFF
cmpl $0xb, %ecx
je 0x3f7a
movl %r8d, %r10d
shll %cl, %r10d
movzwl %r10w, %r11d
movl %r10d, %ebp
andl %esi, %ebp
xorl %r14d, %r14d
cmpl %r11d, %ebp
sete %r14b
shll $0x13, %r14d
movl (%rdx), %ebp
andl %r9d, %ebp
movl %r10d, %r15d
andl %edi, %r15d
xorl %r12d, %r12d
cmpl %r11d, %r15d
sete %r12b
shll $0xd, %r12d
andl %eax, %r10d
xorl %r15d, %r15d
cmpl %r11d, %r10d
sete %r15b
shll $0x14, %r15d
orl %r14d, %r12d
orl %r15d, %r12d
orl %ebp, %r12d
movl %r12d, (%rdx)
movslq 0xc(%rbx), %r10
leaq (%rdx,%r10,4), %rdx
incl %ecx
jmp 0x3ea8
movl $0xfbdeffff, %r9d       # imm = 0xFBDEFFFF
cmpl $0xb, %ecx
je 0x3f7a
movl %r8d, %r10d
shll %cl, %r10d
movzwl %r10w, %r11d
movl %r10d, %ebp
andl %esi, %ebp
xorl %r14d, %r14d
cmpl %r11d, %ebp
sete %r14b
shll $0x1a, %r14d
movl (%rdx), %ebp
andl %r9d, %ebp
movl %r10d, %r15d
andl %edi, %r15d
xorl %r12d, %r12d
cmpl %r11d, %r15d
sete %r12b
shll $0x10, %r12d
andl %eax, %r10d
xorl %r15d, %r15d
cmpl %r11d, %r10d
sete %r15b
shll $0x15, %r15d
orl %r14d, %r12d
orl %r15d, %r12d
orl %ebp, %r12d
movl %r12d, (%rdx)
movslq 0xc(%rbx), %r10
leaq (%rdx,%r10,4), %rdx
incl %ecx
jmp 0x3f16
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop