// File: /workspace/github_cpp_makefile_star2_O1/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 $0x88, %rsp
movq %r8, 0x48(%rsp)
leal -0x2(%rcx), %eax
xorl %ebp, %ebp
testl $0xfffffffd, %eax       # imm = 0xFFFFFFFD
sete %bpl
movq %rcx, 0x30(%rsp)
movl %ecx, %r10d
subl %ebp, %r10d
xorl %r14d, %r14d
cmpl $0x2, %r10d
setl %al
movl %edx, %ecx
orl %esi, %ecx
js 0x23c1c
movl %edx, %r12d
movq %rdi, %r15
movq %rsi, 0x10(%rsp)
movb %al, %r14b
orl $0x2, %r14d
cmpl $0x0, 0x14a52(%rip)     # 0x3838c
je 0x23c27
orl $0x8, %r14d
addl %ebp, %r10d
shll $0x3, %r10d
leal (,%rbp,8), %r11d
xorl %ebx, %ebx
movq %r15, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
movl %r14d, %r8d
xorl %r9d, %r9d
xorl %eax, %eax
pushq %r11
pushq %r10
pushq %r12
movq 0x28(%rsp), %r14
pushq %r14
pushq %rbx
pushq %rbx
pushq %rbx
pushq %rbx
callq 0x25798
addq $0x40, %rsp
testl %r12d, %r12d
jle 0x23c15
movq %r15, 0x58(%rsp)
movq 0x30(%rsp), %r13
movl %r13d, %ecx
imull %r14d, %ecx
leal -0x1(%r14), %r8d
movslq %r13d, %rdx
movslq %r14d, %rsi
movq %rsi, 0x68(%rsp)
movl %r12d, %esi
movq %rsi, 0x38(%rsp)
decl %r12d
movl %ecx, 0x2c(%rsp)
imull %ecx, %r12d
movq %rbp, 0x20(%rsp)
movq %r14, %rbx
movq %rdx, 0x18(%rsp)
movl %r8d, 0x8(%rsp)
movq 0x38(%rsp), %rax
movq %rax, 0x70(%rsp)
decq %rax
movq %rax, 0x38(%rsp)
movslq %r12d, %r12
movq %r12, 0x50(%rsp)
testl %ebx, %ebx
jle 0x23bff
movl 0x2c(%rsp), %eax
imull 0x38(%rsp), %eax
movslq %eax, %rdi
movq 0x48(%rsp), %rax
addq %rax, %rdi
movq 0x50(%rsp), %rcx
addq %rcx, %rax
movq %rax, 0x40(%rsp)
xorl %r14d, %r14d
movq %rdi, 0x78(%rsp)
movl %r14d, %eax
imull %r13d, %eax
movslq %eax, %r12
leaq (%rdi,%r12), %r15
cmpl %r8d, %r14d
movq %r14, 0x80(%rsp)
jge 0x23aad
leal 0x1(%r14), %eax
imull %r13d, %eax
movslq %eax, %rsi
addq %rdi, %rsi
movq %r15, %rdi
callq 0x4330
leal 0x2(%r14), %ecx
testl %eax, %eax
je 0x23ab8
cmpl %ebx, %ecx
jge 0x23b1f
movslq %ecx, %rbp
movq 0x18(%rsp), %rdx
movq %rdx, %rbx
imulq %rbp, %rbx
addq 0x40(%rsp), %rbx
incq %rbp
movl $0x1, %r13d
movq %r15, %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x4330
testl %eax, %eax
je 0x23aa8
incl %r13d
cmpq 0x68(%rsp), %rbp
movq 0x18(%rsp), %rdx
jge 0x23aa5
addq %rdx, %r14
addq %rdx, %rbx
incq %rbp
cmpl $0x7f, %r13d
jb 0x23a78
incl %r13d
xorl %r14d, %r14d
jmp 0x23b13
movl $0x1, %r13d
xorl %r14d, %r14d
jmp 0x23b2d
movb $0x1, %r14b
movl $0x2, %r13d
cmpl %ebx, %ecx
jge 0x23b28
movslq %ecx, %rbp
movq 0x18(%rsp), %rdx
movq %rdx, %rbx
imulq %rbp, %rbx
addq 0x40(%rsp), %rbx
incq %rbp
movl $0x2, %r13d
movq %r15, %rdi
movq %rbx, %rsi
callq 0x4330
testl %eax, %eax
jne 0x23b13
leal 0x1(%r13), %eax
cmpq 0x68(%rsp), %rbp
jge 0x23b10
movq 0x18(%rsp), %rdx
addq %rdx, %rbx
incq %rbp
cmpl $0x7f, %r13d
movl %eax, %r13d
jb 0x23ae2
movl %eax, %r13d
movq 0x20(%rsp), %rbp
movq 0x10(%rsp), %rbx
jmp 0x23b28
movl $0x2, %r13d
xorl %r14d, %r14d
movl 0x8(%rsp), %r8d
testb %r14b, %r14b
je 0x23b7a
leal 0x7f(%r13), %eax
movb %al, 0xe(%rsp)
movq 0x58(%rsp), %r14
movq 0x8(%r14), %rdi
leaq 0xe(%rsp), %rsi
movl $0x1, %edx
movl %r8d, %r12d
callq *(%r14)
movq %r14, %rdi
movq 0x30(%rsp), %r14
movl %r14d, %esi
movl %ebp, %edx
xorl %ecx, %ecx
movq %r15, %r8
callq 0x25684
movq %r13, %rax
movq %r14, %r13
movl %r12d, %r8d
movq 0x18(%rsp), %rdx
jmp 0x23be8
leal -0x1(%r13), %eax
movb %al, 0xf(%rsp)
movq %rbp, %r15
movq 0x58(%rsp), %rbp
movq 0x8(%rbp), %rdi
leaq 0xf(%rsp), %rsi
movl $0x1, %edx
callq *(%rbp)
movq %r13, 0x60(%rsp)
testl %r13d, %r13d
movq 0x30(%rsp), %r13
movq 0x18(%rsp), %r14
jle 0x23bd3
movl 0x60(%rsp), %ebx
addq 0x40(%rsp), %r12
movq %rbp, %rdi
movl %r13d, %esi
movl %r15d, %edx
xorl %ecx, %ecx
movq %r12, %r8
callq 0x25684
addq %r14, %r12
decq %rbx
jne 0x23bb8
movq 0x10(%rsp), %rbx
movq %r15, %rbp
movl 0x8(%rsp), %r8d
movq %r14, %rdx
movq 0x60(%rsp), %rax
addl 0x80(%rsp), %eax
movl %eax, %r14d
cmpl %ebx, %eax
movq 0x78(%rsp), %rdi
jl 0x23a17
movq 0x50(%rsp), %r12
subl 0x2c(%rsp), %r12d
cmpq $0x2, 0x70(%rsp)
jge 0x239cc
movl $0x1, %eax
jmp 0x23c1e
xorl %eax, %eax
addq $0x88, %rsp
jmp 0x23c7b
addl %ebp, %r10d
shll $0x3, %r10d
leal (,%rbp,8), %ebx
subq $0x8, %rsp
xorl %edx, %edx
leaq 0xa552(%rip), %r11      # 0x2e194
movq %r15, %rdi
movq 0x18(%rsp), %rcx
movl %r12d, %r8d
movq 0x38(%rsp), %r9
xorl %eax, %eax
pushq %rbx
pushq %r10
pushq %r12
pushq %rcx
pushq %rdx
pushq %rdx
pushq %rdx
pushq %rdx
pushq %rdx
pushq %r14
pushq %rdx
pushq %rdx
pushq %r11
pushq %rdx
pushq %rbp
pushq 0xc8(%rsp)
pushq %rdx
callq 0x253dc
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq