// File: /workspace/github_cpp_makefile_star2_O2/srajangarg[P]raytracer/src/core/stb_image_write.hpp
// Function: stbi_write_tga_core(stbi__write_context*, int, int, int, void*)
static int stbi_write_tga_core(stbi__write_context *s, int x, int y, int comp, void *data)
{
   int has_alpha = (comp == 2 || comp == 4);
   int colorbytes = has_alpha ? comp-1 : comp;
   int format = colorbytes < 2 ? 3 : 2; // 3 color channels (RGB/RGBA) = 2, 1 color channel (Y/YA) = 3

   if (y < 0 || x < 0)
      return 0;

   if (!stbi_write_tga_with_rle) {
      return stbiw__outfile(s, -1, -1, x, y, comp, 0, (void *) data, has_alpha, 0,
         "111 221 2222 11", 0, 0, format, 0, 0, 0, 0, 0, x, y, (colorbytes + has_alpha) * 8, has_alpha * 8);
   } else {
      int i,j,k;

      stbiw__writef(s, "111 221 2222 11", 0,0,format+8, 0,0,0, 0,0,x,y, (colorbytes + has_alpha) * 8, has_alpha * 8);

      for (j = y - 1; j >= 0; --j) {
          unsigned char *row = (unsigned char *) data + j * x * comp;
         int len;

         for (i = 0; i < x; i += len) {
            unsigned char *begin = row + i * comp;
            int diff = 1;
            len = 1;

            if (i < x - 1) {
               ++len;
               diff = memcmp(begin, row + (i + 1) * comp, comp);
               if (diff) {
                  const unsigned char *prev = begin;
                  for (k = i + 2; k < x && len < 128; ++k) {
                     if (memcmp(prev, row + k * comp, comp)) {
                        prev += comp;
                        ++len;
                     } else {
                        --len;
                        break;
                     }
                  }
               } else {
                  for (k = i + 2; k < x && len < 128; ++k) {
                     if (!memcmp(begin, row + k * comp, comp)) {
                        ++len;
                     } else {
                        break;
                     }
                  }
               }
            }

            if (diff) {
               unsigned char header = STBIW_UCHAR(len - 1);
               s->func(s->context, &header, 1);
               for (k = 0; k < len; ++k) {
                  stbiw__write_pixel(s, -1, comp, has_alpha, 0, begin + k * comp);
               }
            } else {
               unsigned char header = STBIW_UCHAR(len - 129);
               s->func(s->context, &header, 1);
               stbiw__write_pixel(s, -1, comp, has_alpha, 0, begin);
            }
         }
      }
   }
   return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %r8, 0xd0(%rsp)
movl %ecx, %ebp
movl %edx, %r14d
movq %rdi, 0x90(%rsp)
leal -0x2(%rbp), %eax
xorl %ecx, %ecx
testl $0xfffffffd, %eax       # imm = 0xFFFFFFFD
sete %cl
movl %ebp, %eax
movq %rcx, 0xb0(%rsp)
subl %ecx, %eax
xorl %r8d, %r8d
cmpl $0x2, %eax
setl %cl
orl %esi, %edx
js 0x1b1d5
movl %esi, %r15d
movb %cl, %r8b
orl $0x2, %r8d
cmpl $0x0, 0x14497(%rip)     # 0x2f3ac
je 0x1b1de
orl $0x8, %r8d
movq 0xb0(%rsp), %rcx
addl %ecx, %eax
shll $0x3, %eax
leal (,%rcx,8), %ecx
andl $0x0, 0x18(%rsp)
andl $0x0, 0x10(%rsp)
movl %ecx, 0x38(%rsp)
andl $0x0, 0x8(%rsp)
movl %r14d, 0x28(%rsp)
andl $0x0, (%rsp)
movl %r15d, 0x20(%rsp)
movl %eax, 0x30(%rsp)
movq 0x90(%rsp), %rdi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r9d, %r9d
xorl %eax, %eax
callq 0x1cad2
movl %ebp, %eax
imull %r15d, %eax
leal -0x1(%r15), %ecx
movl %ecx, 0xcc(%rsp)
movslq %ebp, %r13
movslq %r15d, %rcx
movq %rcx, 0xe0(%rsp)
movslq %r14d, %rcx
leal -0x1(%rcx), %edx
movl %eax, 0x9c(%rsp)
imull %eax, %edx
movq %rdx, 0xa0(%rsp)
pushq $0x1
popq %rdx
movq %rbp, 0xb8(%rsp)
movq %r15, 0xa8(%rsp)
testq %rcx, %rcx
jle 0x1b1d7
movq 0xa0(%rsp), %rsi
movslq %esi, %rsi
decq %rcx
movl 0x9c(%rsp), %eax
imull %ecx, %eax
movslq %eax, %rdi
movq 0xd0(%rsp), %rax
addq %rax, %rdi
movq %rdi, 0xe8(%rsp)
movq %rsi, 0xa0(%rsp)
addq %rax, %rsi
movq %rsi, 0xc0(%rsp)
xorl %esi, %esi
movq %rcx, 0xf0(%rsp)
cmpl %r15d, %esi
jge 0x1b1b9
movl %esi, %eax
imull %ebp, %eax
movslq %eax, %r12
movl %edx, %ebx
movq %rsi, 0xd8(%rsp)
cmpl 0xcc(%rsp), %esi
jge 0x1b167
movq 0xe8(%rsp), %rcx
leaq (%rcx,%r12), %r14
movq 0xd8(%rsp), %rbx
leal 0x1(%rbx), %eax
imull %ebp, %eax
movslq %eax, %rsi
addq %rcx, %rsi
movq %r14, %rdi
movq %r13, %rdx
callq 0x4340
leal 0x2(%rbx), %ecx
movslq %ecx, %r15
testl %eax, %eax
je 0x1b0b7
movq %r13, %rbp
imulq %r15, %rbp
addq 0xc0(%rsp), %rbp
pushq $0x2
popq %rax
movl %eax, %ebx
cmpq 0xe0(%rsp), %r15
jge 0x1b154
cmpl $0x7f, %ebx
ja 0x1b154
movq %r14, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x4340
testl %eax, %eax
je 0x1b152
addq %r13, %r14
incl %ebx
incq %r15
addq %r13, %rbp
jmp 0x1b07d
movq %r13, %r12
imulq %r15, %r12
addq 0xc0(%rsp), %r12
pushq $0x2
popq %rax
movl %eax, %ebx
cmpq 0xe0(%rsp), %r15
jge 0x1b0f6
cmpl $0x7f, %ebx
ja 0x1b0f6
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x4340
testl %eax, %eax
jne 0x1b0f6
incl %ebx
incq %r15
addq %r13, %r12
jmp 0x1b0cb
leal 0x7f(%rbx), %eax
movb %al, 0x8e(%rsp)
movq 0x90(%rsp), %r15
movq 0x8(%r15), %rdi
leaq 0x8e(%rsp), %rsi
pushq $0x1
popq %rdx
callq *(%r15)
movq %r15, %rdi
movl %ebp, %esi
movq 0xb0(%rsp), %rdx
xorl %ecx, %ecx
movq %r14, %r8
callq 0x1c9da
movq 0xa8(%rsp), %r15
addl 0xd8(%rsp), %ebx
movl %ebx, %esi
pushq $0x1
popq %rdx
movq 0xf0(%rsp), %rcx
jmp 0x1b00c
decl %ebx
movq 0xb8(%rsp), %rbp
movq 0xa8(%rsp), %r15
pushq $0x1
popq %rdx
leal -0x1(%rbx), %eax
movb %al, 0x8f(%rsp)
movq 0x90(%rsp), %rax
movq 0x8(%rax), %rdi
leaq 0x8f(%rsp), %rsi
callq *(%rax)
movl %ebx, %r14d
addq 0xc0(%rsp), %r12
subq $0x1, %r14
jb 0x1b139
movq 0x90(%rsp), %rdi
movl %ebp, %esi
movq 0xb0(%rsp), %rdx
xorl %ecx, %ecx
movq %r12, %r8
callq 0x1c9da
addq %r13, %r12
jmp 0x1b192
movq 0xa0(%rsp), %rax
subl 0x9c(%rsp), %eax
movq %rax, 0xa0(%rsp)
jmp 0x1afb8
xorl %edx, %edx
movl %edx, %eax
jmp 0x1b26d
movq 0xb0(%rsp), %rdx
addl %edx, %eax
shll $0x3, %eax
leal (,%rdx,8), %ecx
movl %ecx, 0x80(%rsp)
movl %r14d, 0x70(%rsp)
andl $0x0, 0x60(%rsp)
movl %r15d, 0x68(%rsp)
andl $0x0, 0x58(%rsp)
movl %r8d, 0x38(%rsp)
andl $0x0, 0x50(%rsp)
leaq 0x7f36(%rip), %rcx      # 0x23154
andl $0x0, 0x48(%rsp)
movq %rcx, 0x20(%rsp)
andl $0x0, 0x40(%rsp)
movl %edx, 0x10(%rsp)
andl $0x0, 0x30(%rsp)
movq 0xd0(%rsp), %rcx
movq %rcx, 0x8(%rsp)
andl $0x0, 0x28(%rsp)
movl %eax, 0x78(%rsp)
andl $0x0, 0x18(%rsp)
andl $0x0, (%rsp)
movq 0x90(%rsp), %rdi
movl %r15d, %ecx
movl %r14d, %r8d
movl %ebp, %r9d
xorl %eax, %eax
callq 0x1c752
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq