// File: /workspace/github_cpp_makefile_star2_O3/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 0xef7e
movq %rsi, %r15
movq %rdi, 0x38(%rsp)
movq 0x40(%rsi), %rax
subq 0x38(%rsi), %rax
cmpq $0x18, %rax
jne 0xef9d
leaq 0x48(%rsp), %rbx
leaq 0x68(%rsp), %rbp
movq %rbx, %rdi
movq %rdx, %rsi
movq %rbp, %rdx
callq 0x9ee4
movq %rbx, %rdi
callq 0xcc86
cmpl $0x1, %r14d
movl $0xffff, %eax           # imm = 0xFFFF
movl $0xff, %ecx
cmovnel %eax, %ecx
movl %ecx, 0x4(%rsp)
movq 0x284f3(%rip), %rax     # 0x36fc8
movq (%rax), %rcx
movq %rcx, 0x30(%rsp)
movq 0x18(%rax), %rax
movq %rax, 0x28(%rsp)
movb $0x1, %al
movl %eax, (%rsp)
movq %r14, 0x40(%rsp)
leaq 0x18(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x48(%rsp), %rsi
movq 0x50(%rsp), %rdx
addq %rsi, %rdx
leaq 0x8(%rsp), %rdi
callq 0xa92c
cmpl $0x2, %r14d
jne 0xeb3f
leaq 0x8(%rsp), %rdi
leaq 0x1c6a5(%rip), %rsi     # 0x2b1c8
callq 0x3540
testb $0x1, (%rsp)
jne 0xeb3f
leaq 0x8(%rsp), %rdi
leaq 0x1c693(%rip), %rsi     # 0x2b1cd
callq 0x3540
leaq 0x8(%rsp), %rdi
leaq 0x1c87f(%rip), %rsi     # 0x2b3ca
callq 0x3540
movq %rbp, %rdi
callq 0x34d0
movq 0x8(%rsp), %rsi
movq %rbp, %rdi
movl $0x10, %edx
callq 0x3050
movq 0x68(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x88(%rsp,%rax)
jne 0xef5f
movl $0x1d, %edx
movq 0x28443(%rip), %rdi     # 0x36fd0
leaq 0x1c83f(%rip), %rsi     # 0x2b3d3
callq 0x3360
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq 0x28426(%rip), %rdi     # 0x36fd0
callq 0x3360
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x32e0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31f0
movl $0x2, %edx
movq %rbp, %rdi
leaq 0x1c771(%rip), %rsi     # 0x2b359
callq 0x3360
movq 0x68(%rsp), %rax
movq -0x18(%rax), %rdi
addq %rbp, %rdi
movl $0xa, %esi
callq 0x32e0
movsbl %al, %esi
movq %rbp, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31f0
movq %rax, %rbx
movl $0x2, %edx
movq %rax, %rdi
leaq 0x1c5dc(%rip), %rsi     # 0x2b204
callq 0x3360
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x32e0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31f0
movl 0x28(%r15), %esi
movq %rax, %rdi
callq 0x34e0
movq %rax, %rbx
movl $0x1, %edx
movq %rax, %rdi
leaq 0x1bc7d(%rip), %rsi     # 0x2a8ef
callq 0x3360
movl 0x2c(%r15), %esi
movq %rbx, %rdi
callq 0x34e0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x32e0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31f0
movq %rax, %rdi
movl 0x4(%rsp), %esi
callq 0x34e0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x32e0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31f0
cmpl $0x1, %r14d
je 0xedd5
testb $0x1, (%rsp)
leaq 0x1c713(%rip), %rsi     # 0x2b40b
leaq 0x1c736(%rip), %rax     # 0x2b435
cmovneq %rax, %rsi
movl $0x29, %edx
movl $0x1a, %eax
cmovneq %rax, %rdx
movq 0x282b8(%rip), %rdi     # 0x36fd0
callq 0x3360
movq 0x282ac(%rip), %rcx     # 0x36fd0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x32e0
movsbl %al, %esi
movq 0x2828e(%rip), %rdi     # 0x36fd0
callq 0x3040
movq %rax, %rdi
callq 0x31f0
cmpl $0x0, 0x30(%r15)
jle 0xee70
xorl %r14d, %r14d
xorl %r13d, %r13d
movq 0x38(%r15), %rax
movq (%rax,%r13,8), %rax
movq 0x38(%rax), %rax
movsd (%rax,%r14,8), %xmm0
mulsd 0x1c256(%rip), %xmm0    # 0x2afd0
xorl %edi, %edi
movl $0xffff, %esi           # imm = 0xFFFF
callq 0x4249
movl %eax, %r12d
movl %eax, %ebx
shrl $0x8, %ebx
testb $0x1, (%rsp)
je 0xeda2
movsbl %r12b, %esi
movq %rbp, %rdi
callq 0x3040
jmp 0xedb0
movsbl %bl, %esi
movq %rbp, %rdi
callq 0x3040
movl %r12d, %ebx
movsbl %bl, %esi
movq %rbp, %rdi
callq 0x3040
incq %r13
cmpq $0x3, %r13
jne 0xed60
incq %r14
movslq 0x30(%r15), %rax
cmpq %rax, %r14
jl 0xed5d
jmp 0xee70
movl $0x19, %edx
movq 0x281ef(%rip), %rdi     # 0x36fd0
leaq 0x1c609(%rip), %rsi     # 0x2b3f1
callq 0x3360
movq 0x281dc(%rip), %rcx     # 0x36fd0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x32e0
movsbl %al, %esi
movq 0x281be(%rip), %rdi     # 0x36fd0
callq 0x3040
movq %rax, %rdi
callq 0x31f0
cmpl $0x0, 0x30(%r15)
jle 0xee70
xorl %ebx, %ebx
xorl %r14d, %r14d
movq 0x38(%r15), %rax
movq (%rax,%r14,8), %rax
movq 0x38(%rax), %rax
movsd (%rax,%rbx,8), %xmm0
mulsd 0x1c194(%rip), %xmm0    # 0x2afd8
xorl %edi, %edi
movl $0xff, %esi
callq 0x4249
movsbl %al, %esi
movq %rbp, %rdi
callq 0x3040
incq %r14
cmpq $0x3, %r14
jne 0xee2b
incq %rbx
movslq 0x30(%r15), %rax
cmpq %rax, %rbx
jl 0xee28
movq 0x28159(%rip), %rcx     # 0x36fd0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x32e0
movq 0x40(%rsp), %r14
movsbl %al, %esi
movq 0x28136(%rip), %rdi     # 0x36fd0
callq 0x3040
movq %rax, %rdi
callq 0x31f0
movq %rbp, %rdi
callq 0x33e0
movq 0x30(%rsp), %rax
movq %rax, 0x68(%rsp)
movq -0x18(%rax), %rax
movq 0x28(%rsp), %rcx
movq %rcx, 0x68(%rsp,%rax)
leaq 0x70(%rsp), %rdi
callq 0x3270
leaq 0x160(%rsp), %rdi
callq 0x30e0
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0xeefa
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3310
cmpl $0x1, %r14d
setne %al
testb %al, (%rsp)
movl $0x0, (%rsp)
jne 0xeaf0
movq 0x38(%rsp), %rbx
leaq 0x10(%rbx), %rax
movq %rax, (%rbx)
leaq 0x1b934(%rip), %rdx     # 0x2a858
movq %rbx, %rdi
movq %rdx, %rsi
callq 0xa9c2
leaq 0x58(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xef4a
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x3310
movq %rbx, %rax
addq $0x268, %rsp            # imm = 0x268
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1c27c(%rip), %rdi     # 0x2b1e2
leaq 0x1c315(%rip), %rsi     # 0x2b282
leaq 0x1c414(%rip), %rcx     # 0x2b388
movl $0x140, %edx            # imm = 0x140
callq 0x31c0
leaq 0x1c1db(%rip), %rdi     # 0x2b160
leaq 0x1c2f6(%rip), %rsi     # 0x2b282
leaq 0x1c3f5(%rip), %rcx     # 0x2b388
movl $0x12a, %edx            # imm = 0x12A
callq 0x31c0
leaq 0x1c2cd(%rip), %rdi     # 0x2b271
leaq 0x1c2d7(%rip), %rsi     # 0x2b282
leaq 0x1c3d6(%rip), %rcx     # 0x2b388
movl $0x12b, %edx            # imm = 0x12B
callq 0x31c0
jmp 0xefc2
jmp 0xefc2
jmp 0xefc7
movq %rax, %rbx
jmp 0xf01e
movq %rax, %rbx
jmp 0xf002
jmp 0xefd0
jmp 0xefd0
movq %rax, %rbx
movq 0x30(%rsp), %rax
movq %rax, 0x68(%rsp)
movq -0x18(%rax), %rax
movq 0x28(%rsp), %rcx
movq %rcx, 0x68(%rsp,%rax)
leaq 0x70(%rsp), %rdi
callq 0x3270
leaq 0x160(%rsp), %rdi
callq 0x30e0
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0xf01e
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3310
leaq 0x58(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xf039
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x3310
movq %rbx, %rdi
callq 0x3500
nop