// File: 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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movq %rdi, -0x38(%rbp)
movl %esi, -0x3c(%rbp)
movl %edx, -0x40(%rbp)
movl %ecx, -0x44(%rbp)
movq %r8, -0x50(%rbp)
movb $0x1, %al
cmpl $0x2, -0x44(%rbp)
movb %al, -0x93(%rbp)
je 0x37a50
cmpl $0x4, -0x44(%rbp)
sete %al
movb %al, -0x93(%rbp)
movb -0x93(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x54(%rbp)
cmpl $0x0, -0x54(%rbp)
je 0x37a72
movl -0x44(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x98(%rbp)
jmp 0x37a7b
movl -0x44(%rbp), %eax
movl %eax, -0x98(%rbp)
movl -0x98(%rbp), %eax
movl %eax, -0x58(%rbp)
movl -0x58(%rbp), %edx
movl $0x2, %eax
movl $0x3, %ecx
cmpl $0x2, %edx
cmovll %ecx, %eax
movl %eax, -0x5c(%rbp)
cmpl $0x0, -0x40(%rbp)
jl 0x37aa6
cmpl $0x0, -0x3c(%rbp)
jge 0x37ab2
movl $0x0, -0x2c(%rbp)
jmp 0x37ea8
cmpl $0x0, 0x24c87(%rip)     # 0x5c740
jne 0x37b82
movq -0x38(%rbp), %rdi
movl -0x3c(%rbp), %ecx
movl -0x40(%rbp), %r8d
movl -0x44(%rbp), %r9d
movq -0x50(%rbp), %r13
movl -0x54(%rbp), %r12d
movl -0x5c(%rbp), %r14d
movl -0x3c(%rbp), %ebx
movl -0x40(%rbp), %r11d
movl -0x58(%rbp), %r10d
addl -0x54(%rbp), %r10d
shll $0x3, %r10d
movl -0x54(%rbp), %eax
shll $0x3, %eax
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %esi, %esi
leaq 0x1101f(%rip), %r15     # 0x48b20
movl %edx, %esi
movl $0x0, (%rsp)
movq %r13, 0x8(%rsp)
movl %r12d, 0x10(%rsp)
movl $0x0, 0x18(%rsp)
movq %r15, 0x20(%rsp)
movl $0x0, 0x28(%rsp)
movl $0x0, 0x30(%rsp)
movl %r14d, 0x38(%rsp)
movl $0x0, 0x40(%rsp)
movl $0x0, 0x48(%rsp)
movl $0x0, 0x50(%rsp)
movl $0x0, 0x58(%rsp)
movl $0x0, 0x60(%rsp)
movl %ebx, 0x68(%rsp)
movl %r11d, 0x70(%rsp)
movl %r10d, 0x78(%rsp)
movl %eax, 0x80(%rsp)
movb $0x0, %al
callq 0x3a3b0
movl %eax, -0x2c(%rbp)
jmp 0x37ea8
movq -0x38(%rbp), %rdi
movl -0x5c(%rbp), %r8d
addl $0x8, %r8d
movl -0x3c(%rbp), %ebx
movl -0x40(%rbp), %r11d
movl -0x58(%rbp), %r10d
addl -0x54(%rbp), %r10d
shll $0x3, %r10d
movl -0x54(%rbp), %eax
shll $0x3, %eax
leaq 0x10f72(%rip), %rsi     # 0x48b20
xorl %r9d, %r9d
movl %r9d, %edx
movl %r9d, %ecx
movl $0x0, (%rsp)
movl $0x0, 0x8(%rsp)
movl $0x0, 0x10(%rsp)
movl $0x0, 0x18(%rsp)
movl %ebx, 0x20(%rsp)
movl %r11d, 0x28(%rsp)
movl %r10d, 0x30(%rsp)
movl %eax, 0x38(%rsp)
movb $0x0, %al
callq 0x3aa50
movl -0x40(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x64(%rbp)
cmpl $0x0, -0x64(%rbp)
jl 0x37e9f
movq -0x50(%rbp), %rax
movl -0x64(%rbp), %ecx
imull -0x3c(%rbp), %ecx
imull -0x44(%rbp), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movl $0x0, -0x60(%rbp)
movl -0x60(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jge 0x37e8f
movq -0x70(%rbp), %rax
movl -0x60(%rbp), %ecx
imull -0x44(%rbp), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x80(%rbp)
movl $0x1, -0x84(%rbp)
movl $0x1, -0x74(%rbp)
movl -0x60(%rbp), %eax
movl -0x3c(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x37dba
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
movq -0x80(%rbp), %rdi
movq -0x70(%rbp), %rsi
movl -0x60(%rbp), %eax
addl $0x1, %eax
imull -0x44(%rbp), %eax
cltq
addq %rax, %rsi
movslq -0x44(%rbp), %rdx
callq 0x51d0
movl %eax, -0x84(%rbp)
cmpl $0x0, -0x84(%rbp)
je 0x37d45
movq -0x80(%rbp), %rax
movq %rax, -0x90(%rbp)
movl -0x60(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x68(%rbp)
movl -0x68(%rbp), %ecx
xorl %eax, %eax
cmpl -0x3c(%rbp), %ecx
movb %al, -0x99(%rbp)
jge 0x37cd5
cmpl $0x80, -0x74(%rbp)
setl %al
movb %al, -0x99(%rbp)
movb -0x99(%rbp), %al
testb $0x1, %al
jne 0x37ce1
jmp 0x37d43
movq -0x90(%rbp), %rdi
movq -0x70(%rbp), %rsi
movl -0x68(%rbp), %eax
imull -0x44(%rbp), %eax
cltq
addq %rax, %rsi
movslq -0x44(%rbp), %rdx
callq 0x51d0
cmpl $0x0, %eax
je 0x37d28
movl -0x44(%rbp), %ecx
movq -0x90(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x90(%rbp)
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x37d33
movl -0x74(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x37d43
jmp 0x37d35
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x37cb5
jmp 0x37db8
movl -0x60(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x68(%rbp)
movl -0x68(%rbp), %ecx
xorl %eax, %eax
cmpl -0x3c(%rbp), %ecx
movb %al, -0x9a(%rbp)
jge 0x37d6e
cmpl $0x80, -0x74(%rbp)
setl %al
movb %al, -0x9a(%rbp)
movb -0x9a(%rbp), %al
testb $0x1, %al
jne 0x37d7a
jmp 0x37db6
movq -0x80(%rbp), %rdi
movq -0x70(%rbp), %rsi
movl -0x68(%rbp), %eax
imull -0x44(%rbp), %eax
cltq
addq %rax, %rsi
movslq -0x44(%rbp), %rdx
callq 0x51d0
cmpl $0x0, %eax
jne 0x37da7
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x37da9
jmp 0x37db6
jmp 0x37dab
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x37d4e
jmp 0x37db8
jmp 0x37dba
cmpl $0x0, -0x84(%rbp)
je 0x37e34
movl -0x74(%rbp), %eax
subl $0x1, %eax
andl $0xff, %eax
movb %al, -0x91(%rbp)
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq -0x38(%rbp), %rcx
movq 0x8(%rcx), %rdi
leaq -0x91(%rbp), %rsi
movl $0x1, %edx
callq *%rax
movl $0x0, -0x68(%rbp)
movl -0x68(%rbp), %eax
cmpl -0x74(%rbp), %eax
jge 0x37e32
movq -0x38(%rbp), %rdi
movl -0x44(%rbp), %edx
movl -0x54(%rbp), %ecx
movq -0x80(%rbp), %r9
movl -0x68(%rbp), %eax
imull -0x44(%rbp), %eax
cltq
addq %rax, %r9
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
callq 0x3a7d0
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x37df8
jmp 0x37e7f
movl -0x74(%rbp), %eax
subl $0x81, %eax
andl $0xff, %eax
movb %al, -0x92(%rbp)
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq -0x38(%rbp), %rcx
movq 0x8(%rcx), %rdi
leaq -0x92(%rbp), %rsi
movl $0x1, %edx
callq *%rax
movq -0x38(%rbp), %rdi
movl -0x44(%rbp), %edx
movl -0x54(%rbp), %ecx
movq -0x80(%rbp), %r9
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
callq 0x3a7d0
jmp 0x37e81
movl -0x74(%rbp), %eax
addl -0x60(%rbp), %eax
movl %eax, -0x60(%rbp)
jmp 0x37c22
jmp 0x37e91
movl -0x64(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x37bf8
jmp 0x37ea1
movl $0x1, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)