// File: alan0201tw[P]RayTracer/include/stb_image_write.h
// 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;
      int jend, jdir;

      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);

      if (stbi__flip_vertically_on_write) {
         j = 0;
         jend = y;
         jdir = 1;
      } else {
         j = y-1;
         jend = -1;
         jdir = -1;
      }
      for (; j != jend; j += jdir) {
         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, -0x9b(%rbp)
je 0x57c30
cmpl $0x4, -0x44(%rbp)
sete %al
movb %al, -0x9b(%rbp)
movb -0x9b(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x54(%rbp)
cmpl $0x0, -0x54(%rbp)
je 0x57c52
movl -0x44(%rbp), %eax
subl $0x1, %eax
movl %eax, -0xa0(%rbp)
jmp 0x57c5b
movl -0x44(%rbp), %eax
movl %eax, -0xa0(%rbp)
movl -0xa0(%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 0x57c86
cmpl $0x0, -0x3c(%rbp)
jge 0x57c92
movl $0x0, -0x2c(%rbp)
jmp 0x580cb
cmpl $0x0, 0x5884b(%rip)     # 0xb04e4
jne 0x57d62
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 0x29e6b(%rip), %r15     # 0x81b4c
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 0x71ec0
movl %eax, -0x2c(%rbp)
jmp 0x580cb
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 0x29dbe(%rip), %rsi     # 0x81b4c
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 0x72550
cmpl $0x0, 0x58d66(%rip)     # 0xb0b3c
je 0x57dee
movl $0x0, -0x64(%rbp)
movl -0x40(%rbp), %eax
movl %eax, -0x6c(%rbp)
movl $0x1, -0x70(%rbp)
jmp 0x57e05
movl -0x40(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x64(%rbp)
movl $0xffffffff, -0x6c(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x70(%rbp) # imm = 0xFFFFFFFF
jmp 0x57e07
movl -0x64(%rbp), %eax
cmpl -0x6c(%rbp), %eax
je 0x580c2
movq -0x50(%rbp), %rax
movl -0x64(%rbp), %ecx
imull -0x3c(%rbp), %ecx
imull -0x44(%rbp), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movl $0x0, -0x60(%rbp)
movl -0x60(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jge 0x580b2
movq -0x78(%rbp), %rax
movl -0x60(%rbp), %ecx
imull -0x44(%rbp), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x88(%rbp)
movl $0x1, -0x8c(%rbp)
movl $0x1, -0x7c(%rbp)
movl -0x60(%rbp), %eax
movl -0x3c(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x57fd7
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
movq -0x88(%rbp), %rdi
movq -0x78(%rbp), %rsi
movl -0x60(%rbp), %eax
addl $0x1, %eax
imull -0x44(%rbp), %eax
cltq
addq %rax, %rsi
movslq -0x44(%rbp), %rdx
callq 0x7210
movl %eax, -0x8c(%rbp)
cmpl $0x0, -0x8c(%rbp)
je 0x57f5f
movq -0x88(%rbp), %rax
movq %rax, -0x98(%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, -0xa1(%rbp)
jge 0x57eef
cmpl $0x80, -0x7c(%rbp)
setl %al
movb %al, -0xa1(%rbp)
movb -0xa1(%rbp), %al
testb $0x1, %al
jne 0x57efb
jmp 0x57f5d
movq -0x98(%rbp), %rdi
movq -0x78(%rbp), %rsi
movl -0x68(%rbp), %eax
imull -0x44(%rbp), %eax
cltq
addq %rax, %rsi
movslq -0x44(%rbp), %rdx
callq 0x7210
cmpl $0x0, %eax
je 0x57f42
movl -0x44(%rbp), %ecx
movq -0x98(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x98(%rbp)
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x57f4d
movl -0x7c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x57f5d
jmp 0x57f4f
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x57ecf
jmp 0x57fd5
movl -0x60(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x68(%rbp)
movl -0x68(%rbp), %ecx
xorl %eax, %eax
cmpl -0x3c(%rbp), %ecx
movb %al, -0xa2(%rbp)
jge 0x57f88
cmpl $0x80, -0x7c(%rbp)
setl %al
movb %al, -0xa2(%rbp)
movb -0xa2(%rbp), %al
testb $0x1, %al
jne 0x57f94
jmp 0x57fd3
movq -0x88(%rbp), %rdi
movq -0x78(%rbp), %rsi
movl -0x68(%rbp), %eax
imull -0x44(%rbp), %eax
cltq
addq %rax, %rsi
movslq -0x44(%rbp), %rdx
callq 0x7210
cmpl $0x0, %eax
jne 0x57fc4
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x57fc6
jmp 0x57fd3
jmp 0x57fc8
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x57f68
jmp 0x57fd5
jmp 0x57fd7
cmpl $0x0, -0x8c(%rbp)
je 0x58054
movl -0x7c(%rbp), %eax
subl $0x1, %eax
andl $0xff, %eax
movb %al, -0x99(%rbp)
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq -0x38(%rbp), %rcx
movq 0x8(%rcx), %rdi
leaq -0x99(%rbp), %rsi
movl $0x1, %edx
callq *%rax
movl $0x0, -0x68(%rbp)
movl -0x68(%rbp), %eax
cmpl -0x7c(%rbp), %eax
jge 0x58052
movq -0x38(%rbp), %rdi
movl -0x44(%rbp), %edx
movl -0x54(%rbp), %ecx
movq -0x88(%rbp), %r9
movl -0x68(%rbp), %eax
imull -0x44(%rbp), %eax
cltq
addq %rax, %r9
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
callq 0x722f0
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x58015
jmp 0x580a2
movl -0x7c(%rbp), %eax
subl $0x81, %eax
andl $0xff, %eax
movb %al, -0x9a(%rbp)
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq -0x38(%rbp), %rcx
movq 0x8(%rcx), %rdi
leaq -0x9a(%rbp), %rsi
movl $0x1, %edx
callq *%rax
movq -0x38(%rbp), %rdi
movl -0x44(%rbp), %edx
movl -0x54(%rbp), %ecx
movq -0x88(%rbp), %r9
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
callq 0x722f0
jmp 0x580a4
movl -0x7c(%rbp), %eax
addl -0x60(%rbp), %eax
movl %eax, -0x60(%rbp)
jmp 0x57e33
jmp 0x580b4
movl -0x70(%rbp), %eax
addl -0x64(%rbp), %eax
movl %eax, -0x64(%rbp)
jmp 0x57e07
jmp 0x580c4
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