// File: Lut99[P]RayTracer/src/lib/LodePNG.cpp
// Function: deflateNoCompression(ucvector*, unsigned char const*, unsigned long)
static unsigned deflateNoCompression(ucvector* out, const unsigned char* data, size_t datasize) {
  /*non compressed deflate block data: 1 bit BFINAL,2 bits BTYPE,(5 bits): it jumps to start of next byte,
  2 bytes LEN, 2 bytes NLEN, LEN bytes literal DATA*/

  size_t i, j, numdeflateblocks = (datasize + 65534u) / 65535u;
  unsigned datapos = 0;
  for(i = 0; i != numdeflateblocks; ++i) {
    unsigned BFINAL, BTYPE, LEN, NLEN;
    unsigned char firstbyte;

    BFINAL = (i == numdeflateblocks - 1);
    BTYPE = 0;

    firstbyte = (unsigned char)(BFINAL + ((BTYPE & 1u) << 1u) + ((BTYPE & 2u) << 1u));
    ucvector_push_back(out, firstbyte);

    LEN = 65535;
    if(datasize - datapos < 65535u) LEN = (unsigned)datasize - datapos;
    NLEN = 65535 - LEN;

    ucvector_push_back(out, (unsigned char)(LEN & 255));
    ucvector_push_back(out, (unsigned char)(LEN >> 8u));
    ucvector_push_back(out, (unsigned char)(NLEN & 255));
    ucvector_push_back(out, (unsigned char)(NLEN >> 8u));

    /*Decompressed data*/
    for(j = 0; j < 65535 && datapos < datasize; ++j) {
      ucvector_push_back(out, data[datapos++]);
    }
  }

  return 0;
}

// Assembly:
subq $0x48, %rsp
movq %rdi, 0x40(%rsp)
movq %rsi, 0x38(%rsp)
movq %rdx, 0x30(%rsp)
movq 0x30(%rsp), %rax
addq $0xfffe, %rax           # imm = 0xFFFE
movl $0xffff, %ecx           # imm = 0xFFFF
xorl %edx, %edx
divq %rcx
movq %rax, 0x18(%rsp)
movl $0x0, 0x14(%rsp)
movq $0x0, 0x28(%rsp)
movq 0x28(%rsp), %rax
cmpq 0x18(%rsp), %rax
je 0x14d525
movq 0x28(%rsp), %rax
movq 0x18(%rsp), %rcx
subq $0x1, %rcx
cmpq %rcx, %rax
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x10(%rsp)
movl $0x0, 0xc(%rsp)
movl 0x10(%rsp), %eax
movl 0xc(%rsp), %ecx
andl $0x1, %ecx
shll %ecx
addl %ecx, %eax
movl 0xc(%rsp), %ecx
andl $0x2, %ecx
shll %ecx
addl %ecx, %eax
movb %al, 0x3(%rsp)
movq 0x40(%rsp), %rdi
movzbl 0x3(%rsp), %esi
callq 0x14e670
movl $0xffff, 0x8(%rsp)      # imm = 0xFFFF
movq 0x30(%rsp), %rax
movl 0x14(%rsp), %ecx
subq %rcx, %rax
cmpq $0xffff, %rax           # imm = 0xFFFF
jae 0x14d449
movq 0x30(%rsp), %rax
subl 0x14(%rsp), %eax
movl %eax, 0x8(%rsp)
movl $0xffff, %eax           # imm = 0xFFFF
subl 0x8(%rsp), %eax
movl %eax, 0x4(%rsp)
movq 0x40(%rsp), %rdi
movl 0x8(%rsp), %eax
andl $0xff, %eax
movzbl %al, %esi
callq 0x14e670
movq 0x40(%rsp), %rdi
movl 0x8(%rsp), %eax
shrl $0x8, %eax
movzbl %al, %esi
callq 0x14e670
movq 0x40(%rsp), %rdi
movl 0x4(%rsp), %eax
andl $0xff, %eax
movzbl %al, %esi
callq 0x14e670
movq 0x40(%rsp), %rdi
movl 0x4(%rsp), %eax
shrl $0x8, %eax
movzbl %al, %esi
callq 0x14e670
movq $0x0, 0x20(%rsp)
xorl %eax, %eax
cmpq $0xffff, 0x20(%rsp)     # imm = 0xFFFF
movb %al, 0x2(%rsp)
jae 0x14d4d4
movl 0x14(%rsp), %eax
cmpq 0x30(%rsp), %rax
setb %al
movb %al, 0x2(%rsp)
movb 0x2(%rsp), %al
testb $0x1, %al
jne 0x14d4de
jmp 0x14d510
movq 0x40(%rsp), %rdi
movq 0x38(%rsp), %rax
movl 0x14(%rsp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, 0x14(%rsp)
movl %ecx, %ecx
movzbl (%rax,%rcx), %esi
callq 0x14e670
movq 0x20(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x20(%rsp)
jmp 0x14d4b3
jmp 0x14d512
movq 0x28(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x28(%rsp)
jmp 0x14d3be
xorl %eax, %eax
addq $0x48, %rsp
retq
nopl (%rax)