// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x268, %rsp            # imm = 0x268
movl %ecx, %r14d
leal -0x1(%r14), %eax
cmpl $0x2, %eax
jae 0xc127
movq %rdi, 0x18(%rsp)
movq 0x40(%rsi), %rax
movq %rsi, 0x8(%rsp)
subq 0x38(%rsi), %rax
cmpq $0x18, %rax
jne 0xc146
leaq 0x48(%rsp), %rbx
leaq 0x68(%rsp), %rax
movq %rbx, %rdi
movq %rdx, %rsi
movq %rax, %rdx
callq 0x7fcc
movq %rbx, %rdi
callq 0xa382
xorl %ecx, %ecx
cmpl $0x1, %r14d
movl $0xff, %eax
movl $0xffff, %edx           # imm = 0xFFFF
cmovel %eax, %edx
movl %edx, 0x10(%rsp)
setne %cl
incl %ecx
movl %ecx, 0x14(%rsp)
xorl %ebx, %ebx
leaq 0x68(%rsp), %r15
movq %r14, 0x20(%rsp)
cmpl 0x14(%rsp), %ebx
je 0xc0d0
leaq 0x28(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x33b0
cmpl $0x2, %r14d
jne 0xbe88
leaq 0x28(%rsp), %rdi
leaq 0x1734a(%rip), %rsi     # 0x231b8
callq 0x3510
testl %ebx, %ebx
je 0xbe88
leaq 0x28(%rsp), %rdi
leaq 0x1733a(%rip), %rsi     # 0x231bd
callq 0x3510
leaq 0x28(%rsp), %rdi
leaq 0x17526(%rip), %rsi     # 0x233ba
callq 0x3510
movq %r15, %rdi
callq 0x34a0
movq 0x28(%rsp), %rsi
movq %r15, %rdi
pushq $0x10
popq %rdx
callq 0x3050
movq 0x68(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x88(%rsp,%rax)
jne 0xc108
movq 0x24101(%rip), %rdi     # 0x2ffd0
leaq 0x174ed(%rip), %rsi     # 0x233c3
callq 0x32c0
movq %rax, %rdi
leaq 0x28(%rsp), %rsi
callq 0x3270
movq %rax, %rdi
callq 0x3120
movq %r15, %rdi
leaq 0x1744f(%rip), %rsi     # 0x23349
callq 0x32c0
movq %rax, %rdi
callq 0x3120
movq %rax, %rdi
leaq 0x172e3(%rip), %rsi     # 0x231f4
callq 0x32c0
movq %rax, %rdi
callq 0x3120
movq 0x8(%rsp), %rcx
movl 0x28(%rcx), %esi
movq %rax, %rdi
callq 0x34b0
movq %rax, %rdi
leaq 0x169a3(%rip), %rsi     # 0x228db
callq 0x32c0
movq 0x8(%rsp), %rcx
movl 0x2c(%rcx), %esi
movq %rax, %rdi
callq 0x34b0
movq %rax, %rdi
callq 0x3120
movq %rax, %rdi
movl 0x10(%rsp), %esi
callq 0x34b0
movq %rax, %rdi
callq 0x3120
cmpl $0x1, %r14d
movq %rbx, %r13
jne 0xbfee
movq 0x24057(%rip), %rdi     # 0x2ffd0
leaq 0x17461(%rip), %rsi     # 0x233e1
callq 0x32c0
movq %rax, %rdi
callq 0x3120
xorl %r14d, %r14d
movq 0x8(%rsp), %rax
movslq 0x30(%rax), %rax
cmpq %rax, %r14
jge 0xc09e
xorl %r12d, %r12d
movq 0x8(%rsp), %rbx
cmpq $0x3, %r12
je 0xbfe6
movq 0x38(%rbx), %rax
movq (%rax,%r12,8), %rax
movq 0x38(%rax), %rax
movsd (%rax,%r14,8), %xmm0
mulsd 0x16ffe(%rip), %xmm0    # 0x22fc8
xorl %edi, %edi
movl $0xff, %esi
callq 0x3c32
movsbl %al, %esi
movq %r15, %rdi
callq 0x3040
incq %r12
jmp 0xbfaa
incq %r14
movq %r13, %rbx
jmp 0xbf90
testl %ebx, %ebx
leaq 0x17404(%rip), %rsi     # 0x233fb
leaq 0x17427(%rip), %rax     # 0x23425
cmoveq %rax, %rsi
movq 0x23fc7(%rip), %rdi     # 0x2ffd0
callq 0x32c0
movq %rax, %rdi
callq 0x3120
xorl %r14d, %r14d
movq 0x8(%rsp), %rax
movslq 0x30(%rax), %rax
cmpq %rax, %r14
jge 0xc09e
xorl %r12d, %r12d
cmpq $0x3, %r12
movq 0x8(%rsp), %rax
je 0xc093
movq 0x38(%rax), %rax
movq (%rax,%r12,8), %rax
movq 0x38(%rax), %rax
movsd (%rax,%r14,8), %xmm0
mulsd 0x16f71(%rip), %xmm0    # 0x22fc0
xorl %edi, %edi
movl $0xffff, %esi           # imm = 0xFFFF
callq 0x3c32
movl %eax, %ebx
movl %eax, %ebp
shrl $0x8, %ebp
testl %r13d, %r13d
je 0xc077
movsbl %bpl, %esi
movq %r15, %rdi
callq 0x3040
movl %ebx, %ebp
jmp 0xc082
movsbl %bl, %esi
movq %r15, %rdi
callq 0x3040
movsbl %bpl, %esi
movq %r15, %rdi
callq 0x3040
incq %r12
jmp 0xc02a
incq %r14
movq %r13, %rbx
jmp 0xc019
movq 0x23f2b(%rip), %rdi     # 0x2ffd0
callq 0x3120
movq 0x20(%rsp), %r14
movq %r15, %rdi
callq 0x33c0
movq %r15, %rdi
callq 0x33e0
leaq 0x28(%rsp), %rdi
callq 0x3548
incl %ebx
jmp 0xbe43
leaq 0x1676d(%rip), %rsi     # 0x22844
leaq 0x68(%rsp), %rdx
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x7fcc
leaq 0x48(%rsp), %rdi
callq 0x3548
movq %rbx, %rax
addq $0x268, %rsp            # imm = 0x268
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x170c3(%rip), %rdi     # 0x231d2
leaq 0x1715c(%rip), %rsi     # 0x23272
leaq 0x1725b(%rip), %rcx     # 0x23378
movl $0x140, %edx            # imm = 0x140
callq 0x31a0
leaq 0x17022(%rip), %rdi     # 0x23150
leaq 0x1713d(%rip), %rsi     # 0x23272
leaq 0x1723c(%rip), %rcx     # 0x23378
movl $0x12a, %edx            # imm = 0x12A
callq 0x31a0
leaq 0x17114(%rip), %rdi     # 0x23261
leaq 0x1711e(%rip), %rsi     # 0x23272
leaq 0x1721d(%rip), %rcx     # 0x23378
movl $0x12b, %edx            # imm = 0x12B
callq 0x31a0
jmp 0xc169
jmp 0xc169
movq %rax, %rbx
jmp 0xc190
jmp 0xc170
movq %rax, %rbx
jmp 0xc186
jmp 0xc179
jmp 0xc179
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x33e0
leaq 0x28(%rsp), %rdi
callq 0x3548
leaq 0x48(%rsp), %rdi
callq 0x3548
movq %rbx, %rdi
callq 0x34d0