// File: 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
subq $0xb0, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movq %rcx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x88(%rbp)
movl -0xc(%rbp), %ecx
movl %ecx, (%rax)
movl -0x10(%rbp), %ecx
movl %ecx, 0x4(%rax)
movq -0x18(%rbp), %rcx
movq %rcx, 0x10(%rax)
movl $0x0, 0x18(%rax)
movl $0x0, 0x1c(%rax)
movw $0x0, 0x24(%rax)
movl $0x1, 0x2c(%rax)
cmpl $0x0, (%rax)
je 0x1b734
movq -0x88(%rbp), %rax
cmpl $0x0, 0x4(%rax)
jne 0x1b790
movl $0x10, %edi
callq 0x4160
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x90(%rbp)
leaq 0x245e(%rip), %rsi      # 0x1dbb0
callq 0x4210
jmp 0x1b759
movq -0x90(%rbp), %rdi
movq 0xb809(%rip), %rsi      # 0x26f70
movq 0xb82a(%rip), %rdx      # 0x26f98
callq 0x4590
movq -0x90(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
callq 0x4280
jmp 0x1ba1a
movq -0x88(%rbp), %rax
movl (%rax), %eax
imulq $0x3, %rax, %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, -0x30(%rbp)
jbe 0x1b80c
movl $0x10, %edi
callq 0x4160
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x98(%rbp)
leaq 0x23f7(%rip), %rsi      # 0x1dbc5
callq 0x4410
jmp 0x1b7d5
movq -0x98(%rbp), %rdi
movq 0xb7e5(%rip), %rsi      # 0x26fc8
movq 0xb7b6(%rip), %rdx      # 0x26fa0
callq 0x4590
movq -0x98(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
callq 0x4280
jmp 0x1ba1a
movq -0x88(%rbp), %rcx
movq -0x30(%rbp), %rax
movl %eax, 0x8(%rcx)
movl 0x8(%rcx), %eax
imull 0x4(%rcx), %eax
movl %eax, %eax
movq %rax, -0x38(%rbp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, -0x38(%rbp)
jbe 0x1b88e
movl $0x10, %edi
callq 0x4160
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xa0(%rbp)
leaq 0x2375(%rip), %rsi      # 0x1dbc5
callq 0x4410
jmp 0x1b857
movq -0xa0(%rbp), %rdi
movq 0xb763(%rip), %rsi      # 0x26fc8
movq 0xb734(%rip), %rdx      # 0x26fa0
callq 0x4590
movq -0xa0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
callq 0x4280
jmp 0x1ba1a
movq -0x88(%rbp), %rax
movq -0x38(%rbp), %rcx
movl %ecx, 0x20(%rax)
movl 0x20(%rax), %eax
movl $0xffff, %ecx           # imm = 0xFFFF
xorl %edx, %edx
divl %ecx
movl %eax, %ecx
movq -0x88(%rbp), %rax
movl %ecx, -0x3c(%rbp)
movl 0x20(%rax), %eax
movl $0xffff, %ecx           # imm = 0xFFFF
xorl %edx, %edx
divl %ecx
cmpl $0x0, %edx
je 0x1b8ce
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
movq -0x88(%rbp), %rax
movl -0x3c(%rbp), %ecx
imulq $0x5, %rcx, %rcx
addq $0x6, %rcx
movq %rcx, -0x48(%rbp)
movl 0x20(%rax), %eax
addq -0x48(%rbp), %rax
movq %rax, -0x48(%rbp)
cmpq $0x7fffffff, -0x48(%rbp) # imm = 0x7FFFFFFF
jbe 0x1b955
movl $0x10, %edi
callq 0x4160
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xa8(%rbp)
leaq 0x22ae(%rip), %rsi      # 0x1dbc5
callq 0x4410
jmp 0x1b91e
movq -0xa8(%rbp), %rdi
movq 0xb69c(%rip), %rsi      # 0x26fc8
movq 0xb66d(%rip), %rdx      # 0x26fa0
callq 0x4590
movq -0xa8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
callq 0x4280
jmp 0x1ba1a
leaq -0x80(%rbp), %rdi
leaq 0x2340(%rip), %rsi      # 0x1dca0
movl $0x2b, %edx
callq 0x42d0
movq -0x88(%rbp), %rax
movl (%rax), %edi
leaq -0x80(%rbp), %rsi
addq $0x10, %rsi
callq 0x1ba30
movq -0x88(%rbp), %rax
movl 0x4(%rax), %edi
leaq -0x80(%rbp), %rsi
addq $0x14, %rsi
callq 0x1ba30
movq -0x48(%rbp), %rax
movl %eax, %edi
leaq -0x80(%rbp), %rsi
addq $0x21, %rsi
callq 0x1ba30
movq -0x88(%rbp), %rdi
movl $0x0, 0x28(%rdi)
leaq -0x80(%rbp), %rsi
addq $0xc, %rsi
movl $0x11, %edx
callq 0x1ba80
movq -0x88(%rbp), %rax
movl 0x28(%rax), %edi
leaq -0x80(%rbp), %rsi
addq $0x1d, %rsi
callq 0x1ba30
movq -0x88(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x1c020
movq -0x88(%rbp), %rdi
movl $0x0, 0x28(%rdi)
leaq -0x80(%rbp), %rsi
addq $0x25, %rsi
movl $0x6, %edx
callq 0x1ba80
addq $0xb0, %rsp
popq %rbp
retq
movq -0x20(%rbp), %rdi
callq 0x45d0
nopw %cs:(%rax,%rax)