// File: /workspace/github_cpp_makefile_star2_O1/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
testl %esi, %esi
js 0x4336
movl %edx, %ebp
testl %edx, %edx
js 0x4336
movl %esi, %r14d
movq %rdi, %rbx
cmpl %esi, 0xc(%rdi)
jle 0x4336
cmpl %ebp, 0x8(%rbx)
jle 0x4336
movl %r9d, %r15d
movl %r8d, %r12d
movl %ecx, %r13d
cmpb $0x1, 0x11(%rbx)
jne 0x3f0c
movb 0x42ea(%rip), %al       # 0x81d0
testb %al, %al
je 0x4345
movzbl %r13b, %eax
movzbl 0x12(%rbx), %ecx
imulq $0xc8, %rax, %rax
addq 0x42c4(%rip), %rax      # 0x81c8
movzwl -0x2(%rax,%rcx,2), %r13d
jmp 0x3f2c
movzbl %r13b, %eax
movzbl 0x12(%rbx), %ecx
imull %eax, %ecx
shrl $0x2, %ecx
imull $0x147b, %ecx, %r13d    # imm = 0x147B
shrl $0xe, %r13d
andl $0x7f8, %r13d           # imm = 0x7F8
cmpb $0x1, 0x11(%rbx)
jne 0x3f5e
movb 0x4298(%rip), %al       # 0x81d0
testb %al, %al
je 0x4376
movzbl %r12b, %eax
movzbl 0x12(%rbx), %ecx
imulq $0xc8, %rax, %rax
addq 0x4272(%rip), %rax      # 0x81c8
movzwl -0x2(%rax,%rcx,2), %r12d
jmp 0x3f7e
movzbl %r12b, %eax
movzbl 0x12(%rbx), %ecx
imull %eax, %ecx
shrl $0x2, %ecx
imull $0x147b, %ecx, %r12d    # imm = 0x147B
shrl $0xe, %r12d
andl $0x7f8, %r12d           # imm = 0x7F8
cmpb $0x1, 0x11(%rbx)
jne 0x3faf
movb 0x4246(%rip), %al       # 0x81d0
testb %al, %al
je 0x43a7
movzbl %r15b, %eax
movzbl 0x12(%rbx), %ecx
imulq $0xc8, %rax, %rax
addq 0x4220(%rip), %rax      # 0x81c8
movzwl -0x2(%rax,%rcx,2), %edx
jmp 0x3fcc
movzbl %r15b, %eax
movzbl 0x12(%rbx), %ecx
imull %eax, %ecx
shrl $0x2, %ecx
imull $0x147b, %ecx, %edx     # imm = 0x147B
shrl $0xe, %edx
andl $0x7f8, %edx            # imm = 0x7F8
movzbl 0x10(%rbx), %esi
movl $0xb, %ecx
subl %esi, %ecx
movzbl 0x18(%rbx), %eax
andl %ebp, %eax
leal (%rax,%rax,4), %edi
leal (%rax,%rdi,2), %eax
addl %ecx, %eax
imull 0xc(%rbx), %eax
addl %r14d, %eax
cltq
shlq $0x2, %rax
addq 0x20(%rbx), %rax
movl (%rbx), %r8d
movl %ebp, %edi
subl %r8d, %edi
jge 0x4091
cmpl %ebp, 0x14(%rbx)
jle 0x412e
testb %sil, %sil
je 0x4336
movzwl %r13w, %esi
movzwl %r12w, %edi
movzwl %dx, %edx
movl $0xf7fff77f, %r8d       # imm = 0xF7FFF77F
movl $0x1, %r9d
shll %cl, %r9d
movzwl %r9w, %r10d
movl %r9d, %r11d
andl %esi, %r11d
xorl %ebp, %ebp
cmpl %r10d, %r11d
sete %bpl
shll $0xb, %ebp
movl (%rax), %r11d
andl %r8d, %r11d
movl %r9d, %r14d
andl %edi, %r14d
xorl %r15d, %r15d
cmpl %r10d, %r14d
sete %r15b
shll $0x1b, %r15d
andl %edx, %r9d
xorl %r14d, %r14d
cmpl %r10d, %r9d
sete %r14b
shll $0x7, %r14d
orl %ebp, %r15d
orl %r14d, %r15d
orl %r11d, %r15d
movl %r15d, (%rax)
movslq 0xc(%rbx), %r9
leaq (%rax,%r9,4), %rax
incl %ecx
cmpl $0xb, %ecx
jne 0x4027
jmp 0x4336
addl %r8d, %r8d
movl 0x14(%rbx), %r9d
subl %r8d, %ebp
jge 0x41b2
cmpl %r9d, %edi
jge 0x423a
testb %sil, %sil
je 0x4336
movzwl %r13w, %esi
movzwl %r12w, %edi
movzwl %dx, %edx
movl $0xffffef9f, %r8d       # imm = 0xFFFFEF9F
movl $0x1, %r9d
shll %cl, %r9d
movzwl %r9w, %r10d
movl %r9d, %r11d
andl %esi, %r11d
xorl %ebp, %ebp
cmpl %r10d, %r11d
sete %bpl
shll $0xc, %ebp
movl (%rax), %r11d
andl %r8d, %r11d
movl %r9d, %r14d
andl %edi, %r14d
xorl %r15d, %r15d
cmpl %r10d, %r14d
sete %r15b
shll $0x5, %r15d
andl %edx, %r9d
xorl %r14d, %r14d
cmpl %r10d, %r9d
sete %r14b
shll $0x6, %r14d
orl %ebp, %r15d
orl %r14d, %r15d
orl %r11d, %r15d
movl %r15d, (%rax)
movslq 0xc(%rbx), %r9
leaq (%rax,%r9,4), %rax
incl %ecx
cmpl $0xb, %ecx
jne 0x40c4
jmp 0x4336
testb %sil, %sil
je 0x4336
movzwl %r13w, %esi
movzwl %r12w, %edi
movzwl %dx, %edx
movl $0xfffff8ff, %r8d       # imm = 0xFFFFF8FF
movl $0x1, %r9d
shll %cl, %r9d
movzwl %r9w, %r10d
movl %r9d, %r11d
andl %esi, %r11d
xorl %ebp, %ebp
cmpl %r10d, %r11d
sete %bpl
shll $0x8, %ebp
movl (%rax), %r11d
andl %r8d, %r11d
movl %r9d, %r14d
andl %edi, %r14d
xorl %r15d, %r15d
cmpl %r10d, %r14d
sete %r15b
shll $0x9, %r15d
andl %edx, %r9d
xorl %r14d, %r14d
cmpl %r10d, %r9d
sete %r14b
shll $0xa, %r14d
orl %ebp, %r15d
orl %r14d, %r15d
orl %r11d, %r15d
movl %r15d, (%rax)
movslq 0xc(%rbx), %r9
leaq (%rax,%r9,4), %rax
incl %ecx
cmpl $0xb, %ecx
jne 0x4148
jmp 0x4336
cmpl %r9d, %ebp
jge 0x42bb
testb %sil, %sil
je 0x4336
movzwl %r13w, %esi
movzwl %r12w, %edi
movzwl %dx, %edx
movl $0xffffbff3, %r8d       # imm = 0xFFFFBFF3
movl $0x1, %r9d
shll %cl, %r9d
movzwl %r9w, %r10d
movl %r9d, %r11d
andl %esi, %r11d
xorl %r14d, %r14d
cmpl %r10d, %r11d
sete %r14b
shll $0xe, %r14d
movl (%rax), %r11d
andl %r8d, %r11d
movl %r9d, %ebp
andl %edi, %ebp
xorl %r15d, %r15d
cmpl %r10d, %ebp
sete %r15b
andl %edx, %r9d
xorl %r12d, %r12d
cmpl %r10d, %r9d
sete %r12b
leal (%r14,%r15,4), %r9d
leal (%r9,%r12,8), %r9d
orl %r11d, %r9d
movl %r9d, (%rax)
movslq 0xc(%rbx), %r9
leaq (%rax,%r9,4), %rax
incl %ecx
cmpl $0xb, %ecx
jne 0x41d5
jmp 0x4336
testb %sil, %sil
je 0x4336
movzwl %r13w, %esi
movzwl %r12w, %edi
movzwl %dx, %edx
movl $0xffe7dfff, %r8d       # imm = 0xFFE7DFFF
movl $0x1, %r9d
shll %cl, %r9d
movzwl %r9w, %r10d
movl %r9d, %r11d
andl %esi, %r11d
xorl %ebp, %ebp
cmpl %r10d, %r11d
sete %bpl
shll $0x13, %ebp
movl (%rax), %r11d
andl %r8d, %r11d
movl %r9d, %r14d
andl %edi, %r14d
xorl %r15d, %r15d
cmpl %r10d, %r14d
sete %r15b
shll $0xd, %r15d
andl %edx, %r9d
xorl %r14d, %r14d
cmpl %r10d, %r9d
sete %r14b
shll $0x14, %r14d
orl %ebp, %r15d
orl %r14d, %r15d
orl %r11d, %r15d
movl %r15d, (%rax)
movslq 0xc(%rbx), %r9
leaq (%rax,%r9,4), %rax
incl %ecx
cmpl $0xb, %ecx
jne 0x4254
jmp 0x4336
testb %sil, %sil
je 0x4336
movzwl %r13w, %esi
movzwl %r12w, %edi
movzwl %dx, %edx
movl $0xfbdeffff, %r8d       # imm = 0xFBDEFFFF
movl $0x1, %r9d
shll %cl, %r9d
movzwl %r9w, %r10d
movl %r9d, %r11d
andl %esi, %r11d
xorl %ebp, %ebp
cmpl %r10d, %r11d
sete %bpl
shll $0x1a, %ebp
movl (%rax), %r11d
andl %r8d, %r11d
movl %r9d, %r14d
andl %edi, %r14d
xorl %r15d, %r15d
cmpl %r10d, %r14d
sete %r15b
shll $0x10, %r15d
andl %edx, %r9d
xorl %r14d, %r14d
cmpl %r10d, %r9d
sete %r14b
shll $0x15, %r14d
orl %ebp, %r15d
orl %r14d, %r15d
orl %r11d, %r15d
movl %r15d, (%rax)
movslq 0xc(%rbx), %r9
leaq (%rax,%r9,4), %rax
incl %ecx
cmpl $0xb, %ecx
jne 0x42d1
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3e84(%rip), %rdi      # 0x81d0
callq 0x22c0
testl %eax, %eax
je 0x3eee
callq 0x455f
movq %rax, 0x3e63(%rip)      # 0x81c8
leaq 0x3e64(%rip), %rdi      # 0x81d0
callq 0x2100
jmp 0x3eee
leaq 0x3e53(%rip), %rdi      # 0x81d0
callq 0x22c0
testl %eax, %eax
je 0x3f40
callq 0x455f
movq %rax, 0x3e32(%rip)      # 0x81c8
leaq 0x3e33(%rip), %rdi      # 0x81d0
callq 0x2100
jmp 0x3f40
leaq 0x3e22(%rip), %rdi      # 0x81d0
callq 0x22c0
testl %eax, %eax
je 0x3f92
callq 0x455f
movq %rax, 0x3e01(%rip)      # 0x81c8
leaq 0x3e02(%rip), %rdi      # 0x81d0
callq 0x2100
jmp 0x3f92
jmp 0x43dc
jmp 0x43dc
movq %rax, %rbx
leaq 0x3dea(%rip), %rdi      # 0x81d0
callq 0x20f0
movq %rbx, %rdi
callq 0x22a0
nop