// File: /workspace/github_cpp_makefile_star2_O3/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, 0x10(%rsp)
movl %ecx, %r10d
subl %ebp, %r10d
xorl %r14d, %r14d
cmpl $0x2, %r10d
setl %al
movl %edx, %ecx
orl %esi, %ecx
js 0x24245
movl %edx, %r12d
movq %rdi, %r13
movq %rsi, 0x30(%rsp)
movb %al, %r14b
orl $0x2, %r14d
cmpl $0x0, 0x133d6(%rip)     # 0x3736c
je 0x24250
orl $0x8, %r14d
addl %ebp, %r10d
shll $0x3, %r10d
leal (,%rbp,8), %r11d
xorl %ebx, %ebx
movq %r13, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
movl %r14d, %r8d
xorl %r9d, %r9d
xorl %eax, %eax
pushq %r11
pushq %r10
pushq %r12
movq 0x48(%rsp), %r15
pushq %r15
pushq %rbx
pushq %rbx
pushq %rbx
pushq %rbx
callq 0x25cf4
addq $0x40, %rsp
testl %r12d, %r12d
jle 0x2423e
movq %r13, 0x58(%rsp)
movq 0x10(%rsp), %rax
movl %eax, %ecx
imull %r15d, %ecx
leal -0x1(%r15), %edx
movl %edx, 0x44(%rsp)
movslq %eax, %r14
movslq %r15d, %r13
movl %r12d, %eax
movq %rax, 0x20(%rsp)
decl %r12d
movl %ecx, 0x1c(%rsp)
imull %ecx, %r12d
movq %rbp, 0x60(%rsp)
movq %r15, %rbx
movq %r13, 0x68(%rsp)
movq 0x20(%rsp), %rax
movq %rax, 0x70(%rsp)
decq %rax
movq %rax, 0x20(%rsp)
movslq %r12d, %r12
movq %r12, 0x50(%rsp)
testl %ebx, %ebx
movq 0x10(%rsp), %r12
jle 0x24228
movl 0x1c(%rsp), %eax
imull 0x20(%rsp), %eax
movslq %eax, %rcx
movq 0x48(%rsp), %rax
addq %rax, %rcx
movq %rcx, 0x78(%rsp)
movq 0x50(%rsp), %rcx
addq %rcx, %rax
movq %rax, 0x28(%rsp)
xorl %ebp, %ebp
movl %ebp, %eax
imull %r12d, %eax
movslq %eax, %r15
movl $0x1, %eax
cmpl 0x44(%rsp), %ebp
movq %rbp, 0x80(%rsp)
jge 0x24171
movq 0x78(%rsp), %rcx
leaq (%rcx,%r15), %rdi
leal 0x1(%rbp), %eax
imull 0x10(%rsp), %eax
movslq %eax, %rsi
addq %rcx, %rsi
movq %rdi, %r12
movq %r14, %rdx
callq 0x4320
leal 0x2(%rbp), %ecx
testl %eax, %eax
je 0x24119
movl $0x2, %eax
cmpl %ebx, %ecx
jge 0x24171
movslq %ecx, %rbp
movq %r14, %rbx
imulq %rbp, %rbx
addq 0x28(%rsp), %rbx
incq %rbp
movl $0x1, %eax
movq %r12, %rdi
movq %r14, %rdx
movq %rax, %r13
movq %rdi, %r12
movq %rbx, %rsi
callq 0x4320
testl %eax, %eax
je 0x2416e
movq %r13, %rax
incl %eax
movq 0x68(%rsp), %r13
cmpq %r13, %rbp
jge 0x24115
movq %r12, %rdi
addq %r14, %rdi
addq %r14, %rbx
incq %rbp
cmpl $0x7f, %eax
jb 0x240e0
incl %eax
jmp 0x24171
movl $0x2, %eax
cmpl %ebx, %ecx
jge 0x241eb
movslq %ecx, %r15
movq %r14, %rbx
imulq %r15, %rbx
addq 0x28(%rsp), %rbx
incq %r15
movl $0x2, %ebp
movq %r12, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x4320
testl %eax, %eax
jne 0x241e9
leal 0x1(%rbp), %eax
cmpq %r13, %r15
jge 0x241eb
addq %r14, %rbx
incq %r15
cmpl $0x7f, %ebp
movl %eax, %ebp
jb 0x2413d
jmp 0x241eb
movq %r13, %rax
movq %rax, %rbx
decl %eax
movb %al, 0xf(%rsp)
movq 0x58(%rsp), %rbp
movq 0x8(%rbp), %rdi
leaq 0xf(%rsp), %rsi
movl $0x1, %edx
callq *(%rbp)
movq %rbx, 0x38(%rsp)
testl %ebx, %ebx
movq 0x10(%rsp), %r12
movq 0x60(%rsp), %r13
jle 0x241c7
movl 0x38(%rsp), %ebx
addq 0x28(%rsp), %r15
movq %rbp, %rdi
movl %r12d, %esi
movl %r13d, %edx
xorl %ecx, %ecx
movq %r15, %r8
callq 0x25bc5
addq %r14, %r15
decq %rbx
jne 0x241ac
movq 0x68(%rsp), %r13
movq 0x38(%rsp), %rax
addl 0x80(%rsp), %eax
movl %eax, %ebp
movq 0x30(%rsp), %rbx
cmpl %ebx, %eax
jl 0x24070
jmp 0x24228
movl %ebp, %eax
movq %rax, 0x38(%rsp)
addl $0x7f, %eax
movb %al, 0xe(%rsp)
movq 0x58(%rsp), %rbx
movq 0x8(%rbx), %rdi
leaq 0xe(%rsp), %rsi
movl $0x1, %edx
callq *(%rbx)
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
movq 0x60(%rsp), %rdx
xorl %ecx, %ecx
movq %r12, %r8
movq %rsi, %r12
callq 0x25bc5
jmp 0x241cc
movq 0x50(%rsp), %r12
subl 0x1c(%rsp), %r12d
cmpq $0x2, 0x70(%rsp)
jge 0x24021
movl $0x1, %eax
jmp 0x24247
xorl %eax, %eax
addq $0x88, %rsp
jmp 0x242a4
addl %ebp, %r10d
shll $0x3, %r10d
leal (,%rbp,8), %ebx
subq $0x8, %rsp
xorl %edx, %edx
leaq 0x8fe9(%rip), %r11      # 0x2d254
movq %r13, %rdi
movq 0x38(%rsp), %rcx
movl %r12d, %r8d
movq 0x18(%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 0x2591d
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq