// File: /workspace/github_cpp_makefile_star2_O2/soybin[P]idyll/src/lib/TinyPngOut.cpp
// Function: TinyPngOut::write(unsigned char const*, unsigned long)
void TinyPngOut::write(const uint8_t pixels[], size_t count) {
	if (count > SIZE_MAX / 3)
		throw std::length_error("Invalid argument");
	count *= 3;  // Convert pixel count to byte count
	while (count > 0) {
		if (pixels == nullptr)
			throw std::invalid_argument("Null pointer");
		if (positionY >= height)
			throw std::logic_error("All image pixels already written");
		
		if (deflateFilled == 0) {  // Start DEFLATE block
			uint16_t size = DEFLATE_MAX_BLOCK_SIZE;
			if (uncompRemain < size)
				size = static_cast<uint16_t>(uncompRemain);
			const uint8_t header[] = {  // 5 bytes long
				static_cast<uint8_t>(uncompRemain <= DEFLATE_MAX_BLOCK_SIZE ? 1 : 0),
				static_cast<uint8_t>(size >> 0),
				static_cast<uint8_t>(size >> 8),
				static_cast<uint8_t>((size >> 0) ^ 0xFF),
				static_cast<uint8_t>((size >> 8) ^ 0xFF),
			};
			write(header);
			crc32(header, sizeof(header) / sizeof(header[0]));
		}
		assert(positionX < lineSize && deflateFilled < DEFLATE_MAX_BLOCK_SIZE);
		
		if (positionX == 0) {  // Beginning of line - write filter method byte
			uint8_t b[] = {0};
			write(b);
			crc32(b, 1);
			adler32(b, 1);
			positionX++;
			uncompRemain--;
			deflateFilled++;
			
		} else {  // Write some pixel bytes for current line
			uint16_t n = DEFLATE_MAX_BLOCK_SIZE - deflateFilled;
			if (lineSize - positionX < n)
				n = static_cast<uint16_t>(lineSize - positionX);
			if (count < n)
				n = static_cast<uint16_t>(count);
			if (static_cast<std::make_unsigned<std::streamsize>::type>(std::numeric_limits<std::streamsize>::max()) < std::numeric_limits<decltype(n)>::max())
				n = std::min(n, static_cast<decltype(n)>(std::numeric_limits<std::streamsize>::max()));
			assert(n > 0);
			output.write(reinterpret_cast<const char*>(pixels), static_cast<std::streamsize>(n));
			
			// Update checksums
			crc32(pixels, n);
			adler32(pixels, n);
			
			// Increment positions
			count -= n;
			pixels += n;
			positionX += n;
			uncompRemain -= n;
			deflateFilled += n;
		}
		
		if (deflateFilled >= DEFLATE_MAX_BLOCK_SIZE)
			deflateFilled = 0;  // End current block
		
		if (positionX == lineSize) {  // Increment line
			positionX = 0;
			positionY++;
			if (positionY == height) {  // Reached end of pixels
				uint8_t footer[] = {  // 20 bytes long
					0, 0, 0, 0,  // DEFLATE Adler-32 placeholder
					0, 0, 0, 0,  // IDAT CRC-32 placeholder
					// IEND chunk
					0x00, 0x00, 0x00, 0x00,
					0x49, 0x45, 0x4E, 0x44,
					0xAE, 0x42, 0x60, 0x82,
				};
				putBigUint32(adler, &footer[0]);
				crc32(&footer[0], 4);
				putBigUint32(crc, &footer[4]);
				write(footer);
			}
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movabsq $0x5555555555555556, %rax # imm = 0x5555555555555556
cmpq %rax, %rdx
jae 0xbc14
movq %rsi, %rbx
movq %rdi, %r14
leaq (%rdx,%rdx,2), %r15
testq %r15, %r15
je 0xbb6f
testq %rbx, %rbx
je 0xbb9b
movl 0x1c(%r14), %eax
cmpl 0x4(%r14), %eax
jae 0xbbc5
cmpw $0x0, 0x24(%r14)
jne 0xba0f
movl 0x20(%r14), %eax
movl $0xffff, %ecx           # imm = 0xFFFF
cmpl %ecx, %eax
cmovbl %eax, %ecx
cmpl $0x10000, %eax          # imm = 0x10000
setb -0x40(%rbp)
movb %cl, -0x3f(%rbp)
movl %ecx, %eax
shrl $0x8, %eax
movb %al, -0x3e(%rbp)
notb %cl
movb %cl, -0x3d(%rbp)
notb %al
movb %al, -0x3c(%rbp)
movq %r14, %rdi
leaq -0x40(%rbp), %r12
movq %r12, %rsi
callq 0xbcc2
movq %r14, %rdi
movq %r12, %rsi
pushq $0x5
popq %rdx
callq 0xb922
movl 0x8(%r14), %eax
movl 0x18(%r14), %edx
subl %edx, %eax
jbe 0xbb7e
movzwl 0x24(%r14), %ecx
cmpw $-0x1, %cx
je 0xbb7e
testl %edx, %edx
je 0xbaa5
notl %ecx
movl %ecx, %edx
movl $0xffff, %esi           # imm = 0xFFFF
andl %esi, %edx
cmpl %edx, %eax
cmovbl %eax, %ecx
movl %ecx, %eax
andl %esi, %eax
cmpq %rax, %r15
movl %r15d, %r12d
cmovael %ecx, %r12d
testw %r12w, %r12w
je 0xbbf5
movzwl %r12w, %r13d
movq 0x10(%r14), %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x3340
movq %r14, %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0xb922
movq %r14, %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0xbc56
subq %r13, %r15
movl 0x18(%r14), %eax
addl %r13d, %eax
movl %eax, 0x18(%r14)
subl %r13d, 0x20(%r14)
addq %r13, %rbx
addw 0x24(%r14), %r12w
jmp 0xbaed
movb $0x0, -0x40(%rbp)
movq %r14, %rdi
leaq -0x40(%rbp), %r12
movq %r12, %rsi
callq 0xbcda
movq %r14, %rdi
movq %r12, %rsi
pushq $0x1
popq %r13
movq %r13, %rdx
callq 0xb922
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0xbc56
movl 0x18(%r14), %eax
movl 0x24(%r14), %r12d
incl %eax
movl %eax, 0x18(%r14)
decl 0x20(%r14)
incl %r12d
movw %r12w, 0x24(%r14)
cmpw $-0x1, %r12w
jne 0xbaff
andw $0x0, 0x24(%r14)
cmpl 0x8(%r14), %eax
jne 0xb99e
andl $0x0, 0x18(%r14)
movl 0x1c(%r14), %eax
incl %eax
movl %eax, 0x1c(%r14)
cmpl 0x4(%r14), %eax
jne 0xb99e
movaps 0x1207(%rip), %xmm0     # 0xcd30
movaps %xmm0, -0x40(%rbp)
movl $0x826042ae, -0x30(%rbp) # imm = 0x826042AE
movl 0x2c(%r14), %edi
leaq -0x40(%rbp), %r12
movq %r12, %rsi
callq 0xb900
movq %r14, %rdi
movq %r12, %rsi
pushq $0x4
popq %rdx
callq 0xb922
movl 0x28(%r14), %edi
leaq -0x3c(%rbp), %rsi
callq 0xb900
movq %r14, %rdi
movq %r12, %rsi
callq 0xbcf2
jmp 0xb99e
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x10ef(%rip), %rdi      # 0xcc74
leaq 0x1127(%rip), %rsi      # 0xccb3
leaq 0x1137(%rip), %rcx      # 0xccca
pushq $0x7a
popq %rdx
callq 0x31a0
pushq $0x10
popq %rdi
callq 0x3130
movq %rax, %r14
leaq 0x1099(%rip), %rsi      # 0xcc46
movq %rax, %rdi
callq 0x34b0
movq 0x6424(%rip), %rsi      # 0x11fe0
movq 0x63fd(%rip), %rdx      # 0x11fc0
jmp 0xbbed
pushq $0x10
popq %rdi
callq 0x3130
movq %rax, %r14
leaq 0x107c(%rip), %rsi      # 0xcc53
movq %rax, %rdi
callq 0x3370
movq 0x63f2(%rip), %rsi      # 0x11fd8
movq 0x63bb(%rip), %rdx      # 0x11fa8
movq %r14, %rdi
callq 0x3430
leaq 0x10fe(%rip), %rdi      # 0xccfa
leaq 0x10b0(%rip), %rsi      # 0xccb3
leaq 0x10c0(%rip), %rcx      # 0xccca
movl $0x8d, %edx
callq 0x31a0
pushq $0x10
popq %rdi
callq 0x3130
movq %rax, %r14
leaq 0x100f(%rip), %rsi      # 0xcc35
movq %rax, %rdi
callq 0x3300
movq 0x6393(%rip), %rsi      # 0x11fc8
movq 0x6364(%rip), %rdx      # 0x11fa0
jmp 0xbbed
jmp 0xbc42
jmp 0xbc42
movq %rax, %rbx
movq %r14, %rdi
callq 0x31e0
movq %rbx, %rdi
callq 0x3460
nop