// File: kebwi[P]Trifid/src/Image3ChnlReal.cpp
// Function: Image3ChnlReal::writeFile[abi:cxx11](char const*, int) const
string Image3ChnlReal::writeFile(const char *filename, int bytesPerSample) const
{
	assert(bytesPerSample == 1 || bytesPerSample == 2);
	assert(img_.size() == 3);
	
	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.ppm";
		
		ofstream ofs;
		ofs.open(filenameOut.c_str());
		assert(ofs);
		
		//Write ppm header
		cout << "PPM file opened for writing: " << filenameOut << endl;
		ofs << "P6" << endl
			<< "#." << endl
			<< width_ << " " << height_ << endl
			<< ((bytesPerSample == 1) ? 255 : 65535) << endl;
		
		//Write the data in interlaced RGB order
		if (bytesPerSample == 1)
		{
			cout << "Writing 8-bit PPM file..." << endl;
			
			char pixelC;
			for (int i = 0; i < totalPixels_; i++)
				for (int j = 0; j < 3; j++)
				{
					pixelC = roundToNearestInt((*img_[j])[i] * 255.0, 0, 255);
					ofs.put(pixelC);
				}
		}
		else    //bytesPerSample == 2
		{
			if (flipEndian)
				cout << "Writing Endian-flipped 16-bit PPM file..." << endl;
			else cout << "Writing 16-bit PPM file..." << endl;
			
			char *charPtr;
			unsigned short pixelS;
			for (int i = 0; i < totalPixels_; i++)
				for (int j = 0; j < 3; j++)
				{
					pixelS = roundToNearestInt((*img_[j])[i] * 65535.0, 0, 65535);
					
					charPtr = (char*)&pixelS;
					
					if (!flipEndian)
					{
						ofs.put(*charPtr++);
						ofs.put(*charPtr);
					}
					else
					{
						ofs.put(*++charPtr);
						ofs.put(*--charPtr);
					}
				}
		}
		
		cout << endl;
		ofs.close();
	}
	
	return string("");//filenameOut;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x370, %rsp            # imm = 0x370
movq %rdi, -0x2c0(%rbp)
movq %rdi, %rax
movq %rax, -0x2b8(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x2b0(%rbp)
movb $0x1, %al
cmpl $0x1, -0x1c(%rbp)
movb %al, -0x2a6(%rbp)
je 0x184d1
cmpl $0x2, -0x1c(%rbp)
sete %al
movb %al, -0x2a6(%rbp)
movb -0x2a6(%rbp), %al
testb $0x1, %al
jne 0x184dd
jmp 0x184df
jmp 0x184fe
leaq 0x31d52(%rip), %rdi     # 0x4a238
leaq 0x31e6d(%rip), %rsi     # 0x4a35a
movl $0x12a, %edx            # imm = 0x12A
leaq 0x31f67(%rip), %rcx     # 0x4a460
callq 0x4240
movq -0x2b0(%rbp), %rdi
addq $0x38, %rdi
callq 0x18cd0
cmpq $0x3, %rax
jne 0x18516
jmp 0x18535
leaq 0x31e2c(%rip), %rdi     # 0x4a349
leaq 0x31e36(%rip), %rsi     # 0x4a35a
movl $0x12b, %edx            # imm = 0x12B
leaq 0x31f30(%rip), %rcx     # 0x4a460
callq 0x4240
movq -0x18(%rbp), %rax
movq %rax, -0x2d0(%rbp)
leaq -0x41(%rbp), %rdi
movq %rdi, -0x2c8(%rbp)
callq 0x4630
movq -0x2d0(%rbp), %rsi
movq -0x2c8(%rbp), %rdx
leaq -0x40(%rbp), %rdi
callq 0xe110
jmp 0x18569
leaq -0x41(%rbp), %rdi
callq 0x43f0
leaq -0x40(%rbp), %rdi
callq 0x151a0
jmp 0x1857d
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 0x18b97
movb $0x0, -0x5d(%rbp)
cmpl $0x0, -0x5c(%rbp)
je 0x185e1
movb $0x1, -0x5d(%rbp)
jmp 0x185e1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x41(%rbp), %rdi
callq 0x43f0
jmp 0x18c0c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x18c03
leaq -0x80(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x4180
jmp 0x185f0
cmpl $0x2, -0x1c(%rbp)
jne 0x1861b
leaq 0x31ca3(%rip), %rsi     # 0x4a2a0
leaq -0x80(%rbp), %rdi
callq 0x4360
jmp 0x18608
jmp 0x1861b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x18b8c
testb $0x1, -0x5d(%rbp)
je 0x1863b
cmpl $0x2, -0x1c(%rbp)
jne 0x1863b
leaq 0x31c77(%rip), %rsi     # 0x4a2a5
leaq -0x80(%rbp), %rdi
callq 0x4360
jmp 0x18639
jmp 0x1863b
leaq 0x31e60(%rip), %rsi     # 0x4a4a2
leaq -0x80(%rbp), %rdi
callq 0x4360
jmp 0x1864d
leaq -0x280(%rbp), %rdi
callq 0x45e0
jmp 0x1865b
leaq -0x80(%rbp), %rdi
callq 0x4160
movq %rax, %rsi
leaq -0x280(%rbp), %rdi
movl $0x10, %edx
callq 0x4070
jmp 0x1867a
movq -0x280(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x280(%rbp,%rax), %rdi
callq 0x44a0
movb %al, -0x2d1(%rbp)
jmp 0x1869a
movb -0x2d1(%rbp), %al
testb $0x1, %al
jne 0x186a6
jmp 0x186a8
jmp 0x186e4
leaq 0x31c0b(%rip), %rdi     # 0x4a2ba
leaq 0x31ca4(%rip), %rsi     # 0x4a35a
movl $0x140, %edx            # imm = 0x140
leaq 0x31d9e(%rip), %rcx     # 0x4a460
callq 0x4240
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x280(%rbp), %rdi
callq 0x4540
jmp 0x18b8c
movq 0x418e5(%rip), %rdi     # 0x59fd0
leaq 0x31db9(%rip), %rsi     # 0x4a4ab
callq 0x4390
movq %rax, -0x2e0(%rbp)
jmp 0x18700
movq -0x2e0(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x4330
movq %rax, -0x2e8(%rbp)
jmp 0x18719
movq -0x2e8(%rbp), %rdi
movq 0x41891(%rip), %rsi     # 0x59fb8
callq 0x43d0
jmp 0x1872e
leaq 0x31cfc(%rip), %rsi     # 0x4a431
leaq -0x280(%rbp), %rdi
callq 0x4390
movq %rax, -0x2f0(%rbp)
jmp 0x1874a
movq -0x2f0(%rbp), %rdi
movq 0x41860(%rip), %rsi     # 0x59fb8
callq 0x43d0
movq %rax, -0x2f8(%rbp)
jmp 0x18766
movq -0x2f8(%rbp), %rdi
leaq 0x31b68(%rip), %rsi     # 0x4a2dc
callq 0x4390
movq %rax, -0x300(%rbp)
jmp 0x18782
movq -0x300(%rbp), %rdi
movq 0x41828(%rip), %rsi     # 0x59fb8
callq 0x43d0
movq %rax, -0x308(%rbp)
jmp 0x1879e
movq -0x308(%rbp), %rdi
movq -0x2b0(%rbp), %rax
movl 0x28(%rax), %esi
callq 0x4600
movq %rax, -0x310(%rbp)
jmp 0x187bd
movq -0x310(%rbp), %rdi
leaq 0x311e3(%rip), %rsi     # 0x499ae
callq 0x4390
movq %rax, -0x318(%rbp)
jmp 0x187d9
movq -0x318(%rbp), %rdi
movq -0x2b0(%rbp), %rax
movl 0x2c(%rax), %esi
callq 0x4600
movq %rax, -0x320(%rbp)
jmp 0x187f8
movq -0x320(%rbp), %rdi
movq 0x417b2(%rip), %rsi     # 0x59fb8
callq 0x43d0
movq %rax, -0x328(%rbp)
jmp 0x18814
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 0x1883c
movq -0x330(%rbp), %rdi
movq 0x4176e(%rip), %rsi     # 0x59fb8
callq 0x43d0
jmp 0x18851
cmpl $0x1, -0x1c(%rbp)
jne 0x1896e
movq 0x4176e(%rip), %rdi     # 0x59fd0
leaq 0x31c60(%rip), %rsi     # 0x4a4c9
callq 0x4390
movq %rax, -0x338(%rbp)
jmp 0x18877
movq -0x338(%rbp), %rdi
movq 0x41733(%rip), %rsi     # 0x59fb8
callq 0x43d0
jmp 0x1888c
movl $0x0, -0x288(%rbp)
movq -0x2b0(%rbp), %rcx
movl -0x288(%rbp), %eax
cmpl 0x30(%rcx), %eax
jge 0x18969
movl $0x0, -0x28c(%rbp)
cmpl $0x3, -0x28c(%rbp)
jge 0x18953
movq -0x2b0(%rbp), %rdi
addq $0x38, %rdi
movslq -0x28c(%rbp), %rsi
callq 0x18cf0
movq (%rax), %rdi
movl -0x288(%rbp), %esi
callq 0xef70
movq %rax, -0x340(%rbp)
jmp 0x188f1
movq -0x340(%rbp), %rax
movsd (%rax), %xmm0
movsd 0x317ac(%rip), %xmm1    # 0x4a0b0
mulsd %xmm1, %xmm0
xorl %edi, %edi
movl $0xff, %esi
callq 0x5520
movl %eax, -0x344(%rbp)
jmp 0x1891c
movl -0x344(%rbp), %eax
movb %al, -0x281(%rbp)
movsbl -0x281(%rbp), %esi
leaq -0x280(%rbp), %rdi
callq 0x4050
jmp 0x1893d
jmp 0x1893f
movl -0x28c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28c(%rbp)
jmp 0x188b6
jmp 0x18955
movl -0x288(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x288(%rbp)
jmp 0x18896
jmp 0x18b46
testb $0x1, -0x5d(%rbp)
je 0x189a7
movq 0x41655(%rip), %rdi     # 0x59fd0
leaq 0x31b61(%rip), %rsi     # 0x4a4e3
callq 0x4390
movq %rax, -0x350(%rbp)
jmp 0x18990
movq -0x350(%rbp), %rdi
movq 0x4161a(%rip), %rsi     # 0x59fb8
callq 0x43d0
jmp 0x189a5
jmp 0x189da
movq 0x41622(%rip), %rdi     # 0x59fd0
leaq 0x31b58(%rip), %rsi     # 0x4a50d
callq 0x4390
movq %rax, -0x358(%rbp)
jmp 0x189c3
movq -0x358(%rbp), %rdi
movq 0x415e7(%rip), %rsi     # 0x59fb8
callq 0x43d0
jmp 0x189d8
jmp 0x189da
movl $0x0, -0x2a0(%rbp)
movq -0x2b0(%rbp), %rcx
movl -0x2a0(%rbp), %eax
cmpl 0x30(%rcx), %eax
jge 0x18b44
movl $0x0, -0x2a4(%rbp)
cmpl $0x3, -0x2a4(%rbp)
jge 0x18b2e
movq -0x2b0(%rbp), %rdi
addq $0x38, %rdi
movslq -0x2a4(%rbp), %rsi
callq 0x18cf0
movq (%rax), %rdi
movl -0x2a0(%rbp), %esi
callq 0xef70
movq %rax, -0x360(%rbp)
jmp 0x18a3f
movq -0x360(%rbp), %rax
movsd (%rax), %xmm0
movsd 0x31656(%rip), %xmm1    # 0x4a0a8
mulsd %xmm1, %xmm0
xorl %edi, %edi
movl $0xffff, %esi           # imm = 0xFFFF
callq 0x5520
movl %eax, -0x364(%rbp)
jmp 0x18a6a
movl -0x364(%rbp), %eax
movw %ax, -0x29a(%rbp)
leaq -0x29a(%rbp), %rax
movq %rax, -0x298(%rbp)
testb $0x1, -0x5d(%rbp)
jne 0x18aca
movq -0x298(%rbp), %rax
movq %rax, %rcx
incq %rcx
movq %rcx, -0x298(%rbp)
movsbl (%rax), %esi
leaq -0x280(%rbp), %rdi
callq 0x4050
jmp 0x18ab0
movq -0x298(%rbp), %rax
movsbl (%rax), %esi
leaq -0x280(%rbp), %rdi
callq 0x4050
jmp 0x18ac8
jmp 0x18b18
movq -0x298(%rbp), %rax
movq %rax, %rcx
incq %rcx
movq %rcx, -0x298(%rbp)
movsbl 0x1(%rax), %esi
leaq -0x280(%rbp), %rdi
callq 0x4050
jmp 0x18af0
movq -0x298(%rbp), %rax
movq %rax, %rcx
decq %rcx
movq %rcx, -0x298(%rbp)
movsbl -0x1(%rax), %esi
leaq -0x280(%rbp), %rdi
callq 0x4050
jmp 0x18b16
jmp 0x18b18
jmp 0x18b1a
movl -0x2a4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2a4(%rbp)
jmp 0x18a04
jmp 0x18b30
movl -0x2a0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2a0(%rbp)
jmp 0x189e4
jmp 0x18b46
movq 0x41483(%rip), %rdi     # 0x59fd0
movq 0x41464(%rip), %rsi     # 0x59fb8
callq 0x43d0
jmp 0x18b5b
leaq -0x280(%rbp), %rdi
callq 0x4520
jmp 0x18b69
leaq -0x280(%rbp), %rdi
callq 0x4540
leaq -0x80(%rbp), %rdi
callq 0x46c8
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x1859a
leaq -0x80(%rbp), %rdi
callq 0x46c8
jmp 0x18c03
leaq -0x2a5(%rbp), %rdi
movq %rdi, -0x370(%rbp)
callq 0x4630
movq -0x2c0(%rbp), %rdi
movq -0x370(%rbp), %rdx
leaq 0x30d58(%rip), %rsi     # 0x49917
callq 0xe110
jmp 0x18bc6
leaq -0x2a5(%rbp), %rdi
callq 0x43f0
leaq -0x40(%rbp), %rdi
callq 0x46c8
movq -0x2b8(%rbp), %rax
addq $0x370, %rsp            # imm = 0x370
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x2a5(%rbp), %rdi
callq 0x43f0
leaq -0x40(%rbp), %rdi
callq 0x46c8
movq -0x50(%rbp), %rdi
callq 0x4620
nopw %cs:(%rax,%rax)
nop