// File: kebwi[P]Trifid/src/ImageReal.cpp
// Function: ImageReal::writeFile[abi:cxx11](char const*, int) const
string ImageReal::writeFile(const char *filename, int bytesPerSample) const
{
	assert(bytesPerSample == 1 || bytesPerSample == 2);
	
	string filenameStr(filename);
	stripString(filenameStr);
	
	int numPasses = (bytesPerSample == 1 ? 1 : 2);
	for (int pass = 0; pass < numPasses; pass++)
	{
		bool flipEndian = false;
		if (pass != 0)
			flipEndian = true;
		
		string filenameOut = filenameStr;
		if (bytesPerSample == 2)
			filenameOut += "_16b";
		if (flipEndian && bytesPerSample == 2)
			filenameOut += "_endianFlip";
		filenameOut += "_out.pgm";
		
		ofstream ofs;
		ofs.open(filenameOut.c_str());
		assert(ofs);
		
		//Write pgm header
		cout << "PGM file opened for writing: " << filenameOut << endl << endl;
		ofs << "P5" << endl
			<< "#." << endl
			<< width_ << " " << height_ << endl
			<< ((bytesPerSample == 1) ? 255 : 65535) << endl;
		
		if (bytesPerSample == 1)
		{
			cout << "Writing 8-bit PGM file..." << endl;
			
			char pixelC;
			for (int i = 0; i < totalPixels_; i++)
			{
				pixelC = roundToNearestInt(img_[i] * 255.0, 0, 255);
				ofs.put(pixelC);
			}
		}
		else    //bytesPerSample == 2
		{
			if (flipEndian)
				cout << "Writing Endian-flipped 16-bit PGM file..." << endl;
			else cout << "Writing 16-bit PGM file..." << endl;
			
			char *charPtr;
			unsigned short pixelS;
			for (int i = 0; i < totalPixels_; i++)
			{
				pixelS = roundToNearestInt(img_[i] * 65535.0, 0, 65535);
				
				charPtr = (char*)&pixelS;
				
				if (!flipEndian)
				{
					ofs.put(*charPtr++);
					ofs.put(*charPtr);
				}
				else
				{
					ofs.put(*++charPtr);
					ofs.put(*--charPtr);
				}
			}
		}
		
		ofs.close();
	}
	
	return string("");//filenameOut;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x360, %rsp            # imm = 0x360
movq %rdi, -0x2b8(%rbp)
movq %rdi, %rax
movq %rax, -0x2b0(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x2a8(%rbp)
movb $0x1, %al
cmpl $0x1, -0x1c(%rbp)
movb %al, -0x29a(%rbp)
je 0x16961
cmpl $0x2, -0x1c(%rbp)
sete %al
movb %al, -0x29a(%rbp)
movb -0x29a(%rbp), %al
testb $0x1, %al
jne 0x1696d
jmp 0x1696f
jmp 0x1698e
leaq 0x338c2(%rip), %rdi     # 0x4a238
leaq 0x3375f(%rip), %rsi     # 0x4a0dc
movl $0x131, %edx            # imm = 0x131
leaq 0x338da(%rip), %rcx     # 0x4a263
callq 0x4240
movq -0x18(%rbp), %rax
movq %rax, -0x2c8(%rbp)
leaq -0x41(%rbp), %rdi
movq %rdi, -0x2c0(%rbp)
callq 0x4630
movq -0x2c8(%rbp), %rsi
movq -0x2c0(%rbp), %rdx
leaq -0x40(%rbp), %rdi
callq 0xe110
jmp 0x169c2
leaq -0x41(%rbp), %rdi
callq 0x43f0
leaq -0x40(%rbp), %rdi
callq 0x151a0
jmp 0x169d6
movl -0x1c(%rbp), %edx
movl $0x2, %eax
movl $0x1, %ecx
cmpl $0x1, %edx
cmovel %ecx, %eax
movl %eax, -0x58(%rbp)
movl $0x0, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x58(%rbp), %eax
jge 0x16f52
movb $0x0, -0x5d(%rbp)
cmpl $0x0, -0x5c(%rbp)
je 0x16a3a
movb $0x1, -0x5d(%rbp)
jmp 0x16a3a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x41(%rbp), %rdi
callq 0x43f0
jmp 0x16fc7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x16fbe
leaq -0x80(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x4180
jmp 0x16a49
cmpl $0x2, -0x1c(%rbp)
jne 0x16a74
leaq 0x3384a(%rip), %rsi     # 0x4a2a0
leaq -0x80(%rbp), %rdi
callq 0x4360
jmp 0x16a61
jmp 0x16a74
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x16f47
testb $0x1, -0x5d(%rbp)
je 0x16a94
cmpl $0x2, -0x1c(%rbp)
jne 0x16a94
leaq 0x3381e(%rip), %rsi     # 0x4a2a5
leaq -0x80(%rbp), %rdi
callq 0x4360
jmp 0x16a92
jmp 0x16a94
leaq 0x33816(%rip), %rsi     # 0x4a2b1
leaq -0x80(%rbp), %rdi
callq 0x4360
jmp 0x16aa6
leaq -0x280(%rbp), %rdi
callq 0x45e0
jmp 0x16ab4
leaq -0x80(%rbp), %rdi
callq 0x4160
movq %rax, %rsi
leaq -0x280(%rbp), %rdi
movl $0x10, %edx
callq 0x4070
jmp 0x16ad3
movq -0x280(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x280(%rbp,%rax), %rdi
callq 0x44a0
movb %al, -0x2c9(%rbp)
jmp 0x16af3
movb -0x2c9(%rbp), %al
testb $0x1, %al
jne 0x16aff
jmp 0x16b01
jmp 0x16b3d
leaq 0x337b2(%rip), %rdi     # 0x4a2ba
leaq 0x335cd(%rip), %rsi     # 0x4a0dc
movl $0x146, %edx            # imm = 0x146
leaq 0x33748(%rip), %rcx     # 0x4a263
callq 0x4240
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x280(%rbp), %rdi
callq 0x4540
jmp 0x16f47
movq 0x4348c(%rip), %rdi     # 0x59fd0
leaq 0x33773(%rip), %rsi     # 0x4a2be
callq 0x4390
movq %rax, -0x2d8(%rbp)
jmp 0x16b59
movq -0x2d8(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x4330
movq %rax, -0x2e0(%rbp)
jmp 0x16b72
movq -0x2e0(%rbp), %rdi
movq 0x43438(%rip), %rsi     # 0x59fb8
callq 0x43d0
movq %rax, -0x2e8(%rbp)
jmp 0x16b8e
movq -0x2e8(%rbp), %rdi
movq 0x4341c(%rip), %rsi     # 0x59fb8
callq 0x43d0
jmp 0x16ba3
leaq 0x3365a(%rip), %rsi     # 0x4a204
leaq -0x280(%rbp), %rdi
callq 0x4390
movq %rax, -0x2f0(%rbp)
jmp 0x16bbf
movq -0x2f0(%rbp), %rdi
movq 0x433eb(%rip), %rsi     # 0x59fb8
callq 0x43d0
movq %rax, -0x2f8(%rbp)
jmp 0x16bdb
movq -0x2f8(%rbp), %rdi
leaq 0x336f3(%rip), %rsi     # 0x4a2dc
callq 0x4390
movq %rax, -0x300(%rbp)
jmp 0x16bf7
movq -0x300(%rbp), %rdi
movq 0x433b3(%rip), %rsi     # 0x59fb8
callq 0x43d0
movq %rax, -0x308(%rbp)
jmp 0x16c13
movq -0x308(%rbp), %rdi
movq -0x2a8(%rbp), %rax
movl 0x28(%rax), %esi
callq 0x4600
movq %rax, -0x310(%rbp)
jmp 0x16c32
movq -0x310(%rbp), %rdi
leaq 0x32d6e(%rip), %rsi     # 0x499ae
callq 0x4390
movq %rax, -0x318(%rbp)
jmp 0x16c4e
movq -0x318(%rbp), %rdi
movq -0x2a8(%rbp), %rax
movl 0x2c(%rax), %esi
callq 0x4600
movq %rax, -0x320(%rbp)
jmp 0x16c6d
movq -0x320(%rbp), %rdi
movq 0x4333d(%rip), %rsi     # 0x59fb8
callq 0x43d0
movq %rax, -0x328(%rbp)
jmp 0x16c89
movq -0x328(%rbp), %rdi
movl -0x1c(%rbp), %eax
subl $0x1, %eax
movl $0xff, %eax
movl $0xffff, %esi           # imm = 0xFFFF
cmovel %eax, %esi
callq 0x4600
movq %rax, -0x330(%rbp)
jmp 0x16cb1
movq -0x330(%rbp), %rdi
movq 0x432f9(%rip), %rsi     # 0x59fb8
callq 0x43d0
jmp 0x16cc6
cmpl $0x1, -0x1c(%rbp)
jne 0x16d8d
movq 0x432f9(%rip), %rdi     # 0x59fd0
leaq 0x33601(%rip), %rsi     # 0x4a2df
callq 0x4390
movq %rax, -0x338(%rbp)
jmp 0x16cec
movq -0x338(%rbp), %rdi
movq 0x432be(%rip), %rsi     # 0x59fb8
callq 0x43d0
jmp 0x16d01
movl $0x0, -0x288(%rbp)
movq -0x2a8(%rbp), %rcx
movl -0x288(%rbp), %eax
cmpl 0x30(%rcx), %eax
jge 0x16d88
movq -0x2a8(%rbp), %rax
movq 0x38(%rax), %rax
movslq -0x288(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd 0x33374(%rip), %xmm1    # 0x4a0b0
mulsd %xmm1, %xmm0
xorl %edi, %edi
movl $0xff, %esi
callq 0x5520
movl %eax, -0x33c(%rbp)
jmp 0x16d54
movl -0x33c(%rbp), %eax
movb %al, -0x281(%rbp)
movsbl -0x281(%rbp), %esi
leaq -0x280(%rbp), %rdi
callq 0x4050
jmp 0x16d75
jmp 0x16d77
movl -0x288(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x288(%rbp)
jmp 0x16d0b
jmp 0x16f16
testb $0x1, -0x5d(%rbp)
je 0x16dc6
movq 0x43236(%rip), %rdi     # 0x59fd0
leaq 0x33558(%rip), %rsi     # 0x4a2f9
callq 0x4390
movq %rax, -0x348(%rbp)
jmp 0x16daf
movq -0x348(%rbp), %rdi
movq 0x431fb(%rip), %rsi     # 0x59fb8
callq 0x43d0
jmp 0x16dc4
jmp 0x16df9
movq 0x43203(%rip), %rdi     # 0x59fd0
leaq 0x3354f(%rip), %rsi     # 0x4a323
callq 0x4390
movq %rax, -0x350(%rbp)
jmp 0x16de2
movq -0x350(%rbp), %rdi
movq 0x431c8(%rip), %rsi     # 0x59fb8
callq 0x43d0
jmp 0x16df7
jmp 0x16df9
movl $0x0, -0x298(%rbp)
movq -0x2a8(%rbp), %rcx
movl -0x298(%rbp), %eax
cmpl 0x30(%rcx), %eax
jge 0x16f14
movq -0x2a8(%rbp), %rax
movq 0x38(%rax), %rax
movslq -0x298(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd 0x33270(%rip), %xmm1    # 0x4a0a8
mulsd %xmm1, %xmm0
xorl %edi, %edi
movl $0xffff, %esi           # imm = 0xFFFF
callq 0x5520
movl %eax, -0x354(%rbp)
jmp 0x16e50
movl -0x354(%rbp), %eax
movw %ax, -0x292(%rbp)
leaq -0x292(%rbp), %rax
movq %rax, -0x290(%rbp)
testb $0x1, -0x5d(%rbp)
jne 0x16eb0
movq -0x290(%rbp), %rax
movq %rax, %rcx
incq %rcx
movq %rcx, -0x290(%rbp)
movsbl (%rax), %esi
leaq -0x280(%rbp), %rdi
callq 0x4050
jmp 0x16e96
movq -0x290(%rbp), %rax
movsbl (%rax), %esi
leaq -0x280(%rbp), %rdi
callq 0x4050
jmp 0x16eae
jmp 0x16efe
movq -0x290(%rbp), %rax
movq %rax, %rcx
incq %rcx
movq %rcx, -0x290(%rbp)
movsbl 0x1(%rax), %esi
leaq -0x280(%rbp), %rdi
callq 0x4050
jmp 0x16ed6
movq -0x290(%rbp), %rax
movq %rax, %rcx
decq %rcx
movq %rcx, -0x290(%rbp)
movsbl -0x1(%rax), %esi
leaq -0x280(%rbp), %rdi
callq 0x4050
jmp 0x16efc
jmp 0x16efe
jmp 0x16f00
movl -0x298(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x298(%rbp)
jmp 0x16e03
jmp 0x16f16
leaq -0x280(%rbp), %rdi
callq 0x4520
jmp 0x16f24
leaq -0x280(%rbp), %rdi
callq 0x4540
leaq -0x80(%rbp), %rdi
callq 0x46c8
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x169f3
leaq -0x80(%rbp), %rdi
callq 0x46c8
jmp 0x16fbe
leaq -0x299(%rbp), %rdi
movq %rdi, -0x360(%rbp)
callq 0x4630
movq -0x2b8(%rbp), %rdi
movq -0x360(%rbp), %rdx
leaq 0x3299d(%rip), %rsi     # 0x49917
callq 0xe110
jmp 0x16f81
leaq -0x299(%rbp), %rdi
callq 0x43f0
leaq -0x40(%rbp), %rdi
callq 0x46c8
movq -0x2b0(%rbp), %rax
addq $0x360, %rsp            # imm = 0x360
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x299(%rbp), %rdi
callq 0x43f0
leaq -0x40(%rbp), %rdi
callq 0x46c8
movq -0x50(%rbp), %rdi
callq 0x4620