// File: /workspace/github_cpp_makefile_star2_O2/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 %r15
pushq %r14
pushq %rbx
subq $0x38, %rsp
movl %esi, (%rdi)
movl %edx, 0x4(%rdi)
movq %rcx, 0x10(%rdi)
andq $0x0, 0x18(%rdi)
andw $0x0, 0x24(%rdi)
movl $0x1, 0x2c(%rdi)
testl %esi, %esi
je 0xb872
movl %edx, %r14d
testl %edx, %edx
je 0xb872
cmpl $0x55555555, %esi       # imm = 0x55555555
jae 0xb89c
movq %rdi, %rbx
leal (%rsi,%rsi,2), %ecx
incl %ecx
movl %ecx, 0x8(%rdi)
imull %r14d, %ecx
movl %ecx, 0x20(%rdi)
movl $0xffff, %edi           # imm = 0xFFFF
movl %ecx, %eax
xorl %edx, %edx
divl %edi
cmpl $0x1, %edx
sbbl $-0x1, %eax
leal (%rax,%rax,4), %eax
leaq (%rax,%rcx), %r15
addq $0x6, %r15
movq %r15, %rax
andq $-0x80000000, %rax      # imm = 0x80000000
jne 0xb8b8
movups 0x1531(%rip), %xmm0     # 0xcd1b
leaq -0x40(%rbp), %rax
movups %xmm0, 0xb(%rax)
movaps 0x1517(%rip), %xmm0     # 0xcd10
movaps %xmm0, (%rax)
movaps 0x14fd(%rip), %xmm0     # 0xcd00
movaps %xmm0, -0x10(%rax)
movl %esi, %edi
movq %rax, %rsi
callq 0xb900
leaq -0x3c(%rbp), %rsi
movl %r14d, %edi
callq 0xb900
leaq -0x2f(%rbp), %rsi
movl %r15d, %edi
callq 0xb900
andl $0x0, 0x28(%rbx)
leaq -0x44(%rbp), %rsi
pushq $0x11
popq %rdx
movq %rbx, %rdi
callq 0xb922
movl 0x28(%rbx), %edi
leaq -0x33(%rbp), %rsi
callq 0xb900
leaq -0x50(%rbp), %rsi
movq %rbx, %rdi
callq 0xbcaa
andl $0x0, 0x28(%rbx)
leaq -0x2b(%rbp), %rsi
pushq $0x6
popq %rdx
movq %rbx, %rdi
callq 0xb922
addq $0x38, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
pushq $0x10
popq %rdi
callq 0x3130
movq %rax, %rbx
leaq 0x138c(%rip), %rsi      # 0xcc10
movq %rax, %rdi
callq 0x3190
movq 0x66e5(%rip), %rsi      # 0x11f78
movq 0x66fe(%rip), %rdx      # 0x11f98
jmp 0xb8e0
pushq $0x10
popq %rdi
callq 0x3130
movq %rax, %rbx
leaq 0x1377(%rip), %rsi      # 0xcc25
movq %rax, %rdi
callq 0x3300
jmp 0xb8d2
pushq $0x10
popq %rdi
callq 0x3130
movq %rax, %rbx
leaq 0x135b(%rip), %rsi      # 0xcc25
movq %rax, %rdi
callq 0x3300
movq 0x66ef(%rip), %rsi      # 0x11fc8
movq 0x66c0(%rip), %rdx      # 0x11fa0
movq %rbx, %rdi
callq 0x3430
jmp 0xb8ec
jmp 0xb8ec
movq %rax, %r14
movq %rbx, %rdi
callq 0x31e0
movq %r14, %rdi
callq 0x3460
nop