// File: /workspace/github_cpp_makefile_star2_O1/soybin[P]idyll/src/lib/TinyPngOut.cpp
// Function: TinyPngOut::TinyPngOut(unsigned int, unsigned int, std::ostream&)
TinyPngOut::TinyPngOut(uint32_t w, uint32_t h, std::ostream &out) :
		// Set most of the fields
		width(w),
		height(h),
		output(out),
		positionX(0),
		positionY(0),
		deflateFilled(0),
		adler(1) {
	
	// Check arguments
	if (width == 0 || height == 0)
		throw std::domain_error("Zero width or height");
	
	// Compute and check data siezs
	uint64_t lineSz = static_cast<uint64_t>(width) * 3 + 1;
	if (lineSz > UINT32_MAX)
		throw std::length_error("Image too large");
	lineSize = static_cast<uint32_t>(lineSz);
	
	uint64_t uncompRm = lineSize * height;
	if (uncompRm > UINT32_MAX)
		throw std::length_error("Image too large");
	uncompRemain = static_cast<uint32_t>(uncompRm);
	
	uint32_t numBlocks = uncompRemain / DEFLATE_MAX_BLOCK_SIZE;
	if (uncompRemain % DEFLATE_MAX_BLOCK_SIZE != 0)
		numBlocks++;  // Round up
	// 5 bytes per DEFLATE uncompressed block header, 2 bytes for zlib header, 4 bytes for zlib Adler-32 footer
	uint64_t idatSize = static_cast<uint64_t>(numBlocks) * 5 + 6;
	idatSize += uncompRemain;
	if (idatSize > static_cast<uint32_t>(INT32_MAX))
		throw std::length_error("Image too large");
	
	// Write header (not a pure header, but a couple of things concatenated together)
	uint8_t header[] = {  // 43 bytes long
		// PNG header
		0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A,
		// IHDR chunk
		0x00, 0x00, 0x00, 0x0D,
		0x49, 0x48, 0x44, 0x52,
		0, 0, 0, 0,  // 'width' placeholder
		0, 0, 0, 0,  // 'height' placeholder
		0x08, 0x02, 0x00, 0x00, 0x00,
		0, 0, 0, 0,  // IHDR CRC-32 placeholder
		// IDAT chunk
		0, 0, 0, 0,  // 'idatSize' placeholder
		0x49, 0x44, 0x41, 0x54,
		// DEFLATE data
		0x08, 0x1D,
	};
	putBigUint32(width, &header[16]);
	putBigUint32(height, &header[20]);
	putBigUint32(idatSize, &header[33]);
	crc = 0;
	crc32(&header[12], 17);
	putBigUint32(crc, &header[29]);
	write(header);
	
	crc = 0;
	crc32(&header[37], 6);  // 0xD7245B6B
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r14
pushq %rbx
subq $0x30, %rsp
movl %esi, (%rdi)
movl %edx, 0x4(%rdi)
movq %rcx, 0x10(%rdi)
movq $0x0, 0x18(%rdi)
movw $0x0, 0x24(%rdi)
movl $0x1, 0x2c(%rdi)
testl %esi, %esi
je 0xd0e0
testl %edx, %edx
je 0xd0e0
cmpl $0x55555555, %esi       # imm = 0x55555555
jae 0xd10c
movq %rdi, %rbx
leal (%rsi,%rsi,2), %eax
incl %eax
movl %eax, 0x8(%rdi)
imull %edx, %eax
movl %eax, 0x20(%rdi)
movl $0x80008001, %ecx       # imm = 0x80008001
imulq %rax, %rcx
shrq $0x2f, %rcx
movl %eax, %edi
shll $0x10, %edi
addl %eax, %edi
negl %edi
cmpl $0x10002, %edi          # imm = 0x10002
sbbl $-0x1, %ecx
leal (%rcx,%rcx,4), %ecx
addq %rcx, %rax
addq $0x6, %rax
movq %rax, %rcx
andq $-0x80000000, %rcx      # imm = 0x80000000
jne 0xd12a
movups 0x1cf1(%rip), %xmm0     # 0xeceb
movups %xmm0, -0x25(%rbp)
movaps 0x1cdb(%rip), %xmm0     # 0xece0
movaps %xmm0, -0x30(%rbp)
movaps 0x1cc0(%rip), %xmm0     # 0xecd0
movaps %xmm0, -0x40(%rbp)
movl $0x10, %edi
movl $0x18, %ecx
movl %esi, %r8d
shrl %cl, %r8d
movb %r8b, -0x40(%rbp,%rdi)
incq %rdi
addl $-0x8, %ecx
cmpq $0x14, %rdi
jne 0xd01e
movl $0x14, %esi
movl $0x18, %ecx
movl %edx, %edi
shrl %cl, %edi
movb %dil, -0x40(%rbp,%rsi)
incq %rsi
addl $-0x8, %ecx
cmpq $0x18, %rsi
jne 0xd03f
movl $0x21, %edx
movl $0x18, %ecx
movl %eax, %esi
shrl %cl, %esi
movb %sil, -0x40(%rbp,%rdx)
incq %rdx
addl $-0x8, %ecx
cmpq $0x25, %rdx
jne 0xd05e
movl $0x0, 0x28(%rbx)
leaq -0x34(%rbp), %rsi
movl $0x11, %edx
movq %rbx, %rdi
callq 0xd194
movl 0x28(%rbx), %eax
movl $0x1d, %edx
movl $0x18, %ecx
movl %eax, %esi
shrl %cl, %esi
movb %sil, -0x40(%rbp,%rdx)
incq %rdx
addl $-0x8, %ecx
cmpq $0x21, %rdx
jne 0xd098
movq 0x10(%rbx), %rdi
leaq -0x40(%rbp), %rsi
movl $0x2b, %edx
callq 0x3350
movl $0x0, 0x28(%rbx)
leaq -0x1b(%rbp), %rsi
movl $0x6, %edx
movq %rbx, %rdi
callq 0xd194
addq $0x30, %rsp
popq %rbx
popq %r14
popq %rbp
retq
movl $0x10, %edi
callq 0x3130
movq %rax, %rbx
leaq 0x1aec(%rip), %rsi      # 0xebe0
movq %rax, %rdi
callq 0x3190
movq 0x5e7d(%rip), %rsi      # 0x12f80
movq 0x5e96(%rip), %rdx      # 0x12fa0
jmp 0xd154
movl $0x10, %edi
callq 0x3130
movq %rax, %rbx
leaq 0x1ad5(%rip), %rsi      # 0xebf5
movq %rax, %rdi
callq 0x3330
jmp 0xd146
movl $0x10, %edi
callq 0x3130
movq %rax, %rbx
leaq 0x1ab7(%rip), %rsi      # 0xebf5
movq %rax, %rdi
callq 0x3330
movq 0x5e7b(%rip), %rsi      # 0x12fc8
movq 0x5e54(%rip), %rdx      # 0x12fa8
movq %rbx, %rdi
callq 0x3450
jmp 0xd160
jmp 0xd160
movq %rax, %r14
movq %rbx, %rdi
callq 0x31e0
movq %r14, %rdi
callq 0x3490
nop