// File: /workspace/github_cpp_makefile_star2_O3/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 0xcf38
testl %edx, %edx
je 0xcf38
cmpl $0x55555555, %esi       # imm = 0x55555555
jae 0xcf64
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 0xcf82
movups 0x1e89(%rip), %xmm0     # 0xecdb
movups %xmm0, -0x25(%rbp)
movaps 0x1e73(%rip), %xmm0     # 0xecd0
movaps %xmm0, -0x30(%rbp)
movaps 0x1e58(%rip), %xmm0     # 0xecc0
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 0xce76
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 0xce97
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 0xceb6
movl $0x0, 0x28(%rbx)
leaq -0x34(%rbp), %rsi
movl $0x11, %edx
movq %rbx, %rdi
callq 0xcfec
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 0xcef0
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 0xcfec
addq $0x30, %rsp
popq %rbx
popq %r14
popq %rbp
retq
movl $0x10, %edi
callq 0x3130
movq %rax, %rbx
leaq 0x1c84(%rip), %rsi      # 0xebd0
movq %rax, %rdi
callq 0x3190
movq 0x601d(%rip), %rsi      # 0x12f78
movq 0x6036(%rip), %rdx      # 0x12f98
jmp 0xcfac
movl $0x10, %edi
callq 0x3130
movq %rax, %rbx
leaq 0x1c6d(%rip), %rsi      # 0xebe5
movq %rax, %rdi
callq 0x3330
jmp 0xcf9e
movl $0x10, %edi
callq 0x3130
movq %rax, %rbx
leaq 0x1c4f(%rip), %rsi      # 0xebe5
movq %rax, %rdi
callq 0x3330
movq 0x601b(%rip), %rsi      # 0x12fc0
movq 0x5ff4(%rip), %rdx      # 0x12fa0
movq %rbx, %rdi
callq 0x3450
jmp 0xcfb8
jmp 0xcfb8
movq %rax, %r14
movq %rbx, %rdi
callq 0x31e0
movq %r14, %rdi
callq 0x3490
nop