// File: srajangarg[P]raytracer/src/core/stb_image_write.hpp
// Function: stbiw__write_pixel(stbi__write_context*, int, int, int, int, unsigned char*)
static void stbiw__write_pixel(stbi__write_context *s, int rgb_dir, int comp, int write_alpha, int expand_mono, unsigned char *d)
{
   unsigned char bg[3] = { 255, 0, 255}, px[3];
   int k;

   if (write_alpha < 0)
      s->func(s->context, &d[comp - 1], 1);

   switch (comp) {
      case 1:
         s->func(s->context,d,1);
         break;
      case 2:
         if (expand_mono)
            stbiw__write3(s, d[0], d[0], d[0]); // monochrome bmp
         else
            s->func(s->context, d, 1);  // monochrome TGA
         break;
      case 4:
         if (!write_alpha) {
            // composite against pink background
            for (k = 0; k < 3; ++k)
               px[k] = bg[k] + ((d[k] - bg[k]) * d[3]) / 255;
            stbiw__write3(s, px[1 - rgb_dir], px[1], px[1 + rgb_dir]);
            break;
         }
         /* FALLTHROUGH */
      case 3:
         stbiw__write3(s, d[1 - rgb_dir], d[1], d[1 + rgb_dir]);
         break;
   }
   if (write_alpha > 0)
      s->func(s->context, &d[comp - 1], 1);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movl %ecx, -0x14(%rbp)
movl %r8d, -0x18(%rbp)
movq %r9, -0x20(%rbp)
movw 0xe130(%rip), %ax       # 0x48924
movw %ax, -0x23(%rbp)
movb 0xe128(%rip), %al       # 0x48926
movb %al, -0x21(%rbp)
cmpl $0x0, -0x14(%rbp)
jge 0x3a82d
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x8(%rcx), %rdi
movq -0x20(%rbp), %rsi
movl -0x10(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
addq %rcx, %rsi
movl $0x1, %edx
callq *%rax
movl -0x10(%rbp), %eax
decl %eax
movl %eax, %ecx
movq %rcx, -0x38(%rbp)
subl $0x3, %eax
ja 0x3a992
movq -0x38(%rbp), %rax
leaq 0xe0b0(%rip), %rcx      # 0x488fc
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x8(%rcx), %rdi
movq -0x20(%rbp), %rsi
movl $0x1, %edx
callq *%rax
jmp 0x3a992
cmpl $0x0, -0x18(%rbp)
je 0x3a89e
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movb (%rax), %dl
movq -0x20(%rbp), %rax
movb (%rax), %cl
movq -0x20(%rbp), %rax
movzbl %dl, %esi
movzbl %cl, %edx
movzbl (%rax), %ecx
callq 0x3a9d0
jmp 0x3a8b8
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x8(%rcx), %rdi
movq -0x20(%rbp), %rsi
movl $0x1, %edx
callq *%rax
jmp 0x3a992
cmpl $0x0, -0x14(%rbp)
jne 0x3a955
movl $0x0, -0x2c(%rbp)
cmpl $0x3, -0x2c(%rbp)
jge 0x3a926
movslq -0x2c(%rbp), %rax
movzbl -0x23(%rbp,%rax), %eax
movl %eax, -0x3c(%rbp)
movq -0x20(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movslq -0x2c(%rbp), %rcx
movzbl -0x23(%rbp,%rcx), %ecx
subl %ecx, %eax
movq -0x20(%rbp), %rcx
movzbl 0x3(%rcx), %ecx
imull %ecx, %eax
movl $0xff, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0x3c(%rbp), %eax
addl %ecx, %eax
movb %al, %cl
movslq -0x2c(%rbp), %rax
movb %cl, -0x26(%rbp,%rax)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x3a8ce
movq -0x8(%rbp), %rdi
movl $0x1, %eax
subl -0xc(%rbp), %eax
cltq
movb -0x26(%rbp,%rax), %dl
movb -0x25(%rbp), %cl
movl -0xc(%rbp), %eax
addl $0x1, %eax
cltq
movzbl %dl, %esi
movzbl %cl, %edx
movzbl -0x26(%rbp,%rax), %ecx
callq 0x3a9d0
jmp 0x3a992
jmp 0x3a957
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movl $0x1, %ecx
subl -0xc(%rbp), %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %sil
movq -0x20(%rbp), %rax
movb 0x1(%rax), %dl
movq -0x20(%rbp), %rax
movl -0xc(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movzbl %sil, %esi
movzbl %dl, %edx
movzbl (%rax,%rcx), %ecx
callq 0x3a9d0
cmpl $0x0, -0x14(%rbp)
jle 0x3a9be
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x8(%rcx), %rdi
movq -0x20(%rbp), %rsi
movl -0x10(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
addq %rcx, %rsi
movl $0x1, %edx
callq *%rax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)