// File: /workspace/github_cpp_makefile_star2_O1/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 $0x278, %rsp            # imm = 0x278
movl %ecx, %r14d
leal -0x1(%r14), %eax
cmpl $0x2, %eax
jae 0xf05e
movq %rsi, %r15
movq %rdi, 0x48(%rsp)
movq 0x40(%rsi), %rax
subq 0x38(%rsi), %rax
cmpq $0x18, %rax
jne 0xf07d
leaq 0x58(%rsp), %rbx
leaq 0x78(%rsp), %r12
movq %rbx, %rdi
movq %rdx, %rsi
movq %r12, %rdx
callq 0x9f34
movq %rbx, %rdi
callq 0xcd42
cmpl $0x1, %r14d
movl $0xffff, %eax           # imm = 0xFFFF
movl $0xff, %ecx
cmovnel %eax, %ecx
movl %ecx, 0x14(%rsp)
movq 0x28431(%rip), %rax     # 0x36fc8
movq (%rax), %rcx
movq %rcx, 0x40(%rsp)
movq 0x18(%rax), %rax
movq %rax, 0x38(%rsp)
movq $0x0, 0x8(%rsp)
leaq 0x18(%rsp), %rbx
movq %r14, 0x50(%rsp)
leaq 0x28(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x58(%rsp), %rsi
movq 0x60(%rsp), %rdx
addq %rsi, %rdx
movq %rbx, %rdi
callq 0xa97e
cmpl $0x2, %r14d
jne 0xebef
movq %rbx, %rdi
leaq 0x1d606(%rip), %rsi     # 0x2c1f0
callq 0x3540
cmpl $0x0, 0x8(%rsp)
sete %al
cmpl $0x2, %r14d
setne %cl
orb %al, %cl
jne 0xec11
movq %rbx, %rdi
leaq 0x1d5e9(%rip), %rsi     # 0x2c1f5
callq 0x3540
movq %rbx, %rdi
leaq 0x1d7d7(%rip), %rsi     # 0x2c3f2
callq 0x3540
movq %r12, %rdi
callq 0x34d0
movq 0x18(%rsp), %rsi
movq %r12, %rdi
movl $0x10, %edx
callq 0x3050
movq 0x78(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x98(%rsp,%rax)
jne 0xf03f
movl $0x1d, %edx
movq 0x28373(%rip), %rdi     # 0x36fd0
leaq 0x1d797(%rip), %rsi     # 0x2c3fb
callq 0x3360
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq 0x28356(%rip), %rdi     # 0x36fd0
callq 0x3360
movq %rax, %rbp
movq (%rax), %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
movl $0x2, %edx
movq %r12, %rdi
leaq 0x1d6c9(%rip), %rsi     # 0x2c381
callq 0x3360
movq 0x78(%rsp), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x32e0
movsbl %al, %esi
movq %r12, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31f0
movq %rax, %rbp
movl $0x2, %edx
movq %rax, %rdi
leaq 0x1d534(%rip), %rsi     # 0x2c22c
callq 0x3360
movq (%rbp), %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
movl 0x28(%r15), %esi
movq %rax, %rdi
callq 0x34e0
movq %rax, %rbp
movl $0x1, %edx
movq %rax, %rdi
leaq 0x1cbce(%rip), %rsi     # 0x2b911
callq 0x3360
movl 0x2c(%r15), %esi
movq %rbp, %rdi
callq 0x34e0
movq %rax, %rbp
movq (%rax), %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, %rdi
movl 0x14(%rsp), %esi
callq 0x34e0
movq %rax, %rbp
movq (%rax), %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
cmpl $0x1, %r14d
je 0xeea9
cmpl $0x0, 0x8(%rsp)
leaq 0x1d693(%rip), %rsi     # 0x2c45d
leaq 0x1d662(%rip), %rax     # 0x2c433
cmovneq %rax, %rsi
movl $0x1a, %edx
movl $0x29, %eax
cmovneq %rax, %rdx
movq 0x281e6(%rip), %rdi     # 0x36fd0
callq 0x3360
movq 0x281da(%rip), %rcx     # 0x36fd0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x32e0
movsbl %al, %esi
movq 0x281bc(%rip), %rdi     # 0x36fd0
callq 0x3040
movq %rax, %rdi
callq 0x31f0
cmpl $0x0, 0x30(%r15)
jle 0xef44
xorl %r14d, %r14d
xorl %ebx, %ebx
movq 0x38(%r15), %rax
movq (%rax,%rbx,8), %rax
movq 0x38(%rax), %rax
movsd (%rax,%r14,8), %xmm0
mulsd 0x1d1ad(%rip), %xmm0    # 0x2bff8
xorl %edi, %edi
movl $0xffff, %esi           # imm = 0xFFFF
callq 0x4249
movl %eax, %r13d
movl %eax, %ebp
shrl $0x8, %ebp
cmpl $0x0, 0x8(%rsp)
je 0xee77
movsbl %bpl, %esi
movq %r12, %rdi
callq 0x3040
movl %r13d, %ebp
jmp 0xee83
movsbl %r13b, %esi
movq %r12, %rdi
callq 0x3040
movsbl %bpl, %esi
movq %r12, %rdi
callq 0x3040
incq %rbx
cmpq $0x3, %rbx
jne 0xee31
incq %r14
movslq 0x30(%r15), %rax
cmpq %rax, %r14
jl 0xee2f
jmp 0xef44
movl $0x19, %edx
movq 0x2811b(%rip), %rdi     # 0x36fd0
leaq 0x1d55d(%rip), %rsi     # 0x2c419
callq 0x3360
movq 0x28108(%rip), %rcx     # 0x36fd0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x32e0
movsbl %al, %esi
movq 0x280ea(%rip), %rdi     # 0x36fd0
callq 0x3040
movq %rax, %rdi
callq 0x31f0
cmpl $0x0, 0x30(%r15)
jle 0xef44
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 0x1d0e8(%rip), %xmm0    # 0x2c000
xorl %edi, %edi
movl $0xff, %esi
callq 0x4249
movsbl %al, %esi
movq %r12, %rdi
callq 0x3040
incq %r14
cmpq $0x3, %r14
jne 0xeeff
incq %rbx
movslq 0x30(%r15), %rax
cmpq %rax, %rbx
jl 0xeefc
movq 0x28085(%rip), %rcx     # 0x36fd0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x32e0
movq 0x50(%rsp), %r14
leaq 0x18(%rsp), %rbx
movsbl %al, %esi
movq 0x2805d(%rip), %rdi     # 0x36fd0
callq 0x3040
movq %rax, %rdi
callq 0x31f0
movq %r12, %rdi
callq 0x33e0
movq 0x40(%rsp), %rax
movq %rax, 0x78(%rsp)
movq -0x18(%rax), %rax
movq 0x38(%rsp), %rcx
movq %rcx, 0x78(%rsp,%rax)
leaq 0x80(%rsp), %rdi
callq 0x3270
leaq 0x170(%rsp), %rdi
callq 0x30e0
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0xefd6
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x3310
cmpl $0x1, %r14d
je 0xeff1
movq 0x8(%rsp), %rcx
leal 0x1(%rcx), %eax
testl %ecx, %ecx
movq %rax, 0x8(%rsp)
je 0xebbb
movq 0x48(%rsp), %rbx
leaq 0x10(%rbx), %rax
movq %rax, (%rbx)
leaq 0x1c876(%rip), %rdx     # 0x2b87a
movq %rbx, %rdi
movq %rdx, %rsi
callq 0xaa0e
leaq 0x68(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xf02a
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x3310
movq %rbx, %rax
addq $0x278, %rsp            # imm = 0x278
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1d1c4(%rip), %rdi     # 0x2c20a
leaq 0x1d25d(%rip), %rsi     # 0x2c2aa
leaq 0x1d35c(%rip), %rcx     # 0x2c3b0
movl $0x140, %edx            # imm = 0x140
callq 0x31c0
leaq 0x1d123(%rip), %rdi     # 0x2c188
leaq 0x1d23e(%rip), %rsi     # 0x2c2aa
leaq 0x1d33d(%rip), %rcx     # 0x2c3b0
movl $0x12a, %edx            # imm = 0x12A
callq 0x31c0
leaq 0x1d215(%rip), %rdi     # 0x2c299
leaq 0x1d21f(%rip), %rsi     # 0x2c2aa
leaq 0x1d31e(%rip), %rcx     # 0x2c3b0
movl $0x12b, %edx            # imm = 0x12B
callq 0x31c0
jmp 0xf0a2
jmp 0xf0a2
jmp 0xf0a7
movq %rax, %rbx
jmp 0xf100
movq %rax, %rbx
jmp 0xf0e4
jmp 0xf0b0
jmp 0xf0b0
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
movq 0x40(%rsp), %rax
movq %rax, -0x8(%rdi)
movq -0x18(%rax), %rax
movq 0x38(%rsp), %rcx
movq %rcx, 0x78(%rsp,%rax)
callq 0x3270
leaq 0x170(%rsp), %rdi
callq 0x30e0
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0xf100
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x3310
leaq 0x68(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xf11b
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x3310
movq %rbx, %rdi
callq 0x3500
nop