// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x268, %rsp            # imm = 0x268
movq %rcx, 0x28(%rsp)
leal -0x1(%rcx), %eax
cmpl $0x2, %eax
jae 0xdfb5
movq %rsi, %r15
movq %rdi, 0x30(%rsp)
leaq 0x48(%rsp), %rbx
leaq 0x68(%rsp), %r12
movq %rbx, %rdi
movq %rdx, %rsi
movq %r12, %rdx
callq 0x9f34
movq %rbx, %rdi
callq 0xcd42
movq 0x28(%rsp), %rbx
cmpl $0x1, %ebx
movl $0xffff, %eax           # imm = 0xFFFF
movl $0xff, %ecx
cmovnel %eax, %ecx
movl %ecx, 0x4(%rsp)
movq 0x2948a(%rip), %rax     # 0x36fc8
movq (%rax), %rcx
movq %rcx, 0x40(%rsp)
movq 0x18(%rax), %rax
movq %rax, 0x38(%rsp)
xorl %r13d, %r13d
leaq 0x18(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x48(%rsp), %rsi
movq 0x50(%rsp), %rdx
addq %rsi, %rdx
leaq 0x8(%rsp), %rdi
callq 0xa97e
cmpl $0x2, %ebx
jne 0xdb89
leaq 0x8(%rsp), %rdi
leaq 0x1e66c(%rip), %rsi     # 0x2c1f0
callq 0x3540
testl %r13d, %r13d
sete %al
cmpl $0x2, %ebx
setne %cl
orb %al, %cl
jne 0xdbaa
leaq 0x8(%rsp), %rdi
leaq 0x1e650(%rip), %rsi     # 0x2c1f5
callq 0x3540
leaq 0x8(%rsp), %rdi
leaq 0x1e64b(%rip), %rsi     # 0x2c201
callq 0x3540
movq %r12, %rdi
callq 0x34d0
movq 0x8(%rsp), %rsi
movq %r12, %rdi
movl $0x10, %edx
callq 0x3050
movq 0x68(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x88(%rsp,%rax)
jne 0xdf96
movl $0x1d, %edx
movq 0x293d8(%rip), %rdi     # 0x36fd0
leaq 0x1e60f(%rip), %rsi     # 0x2c20e
callq 0x3360
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq 0x293bb(%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
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 0x1e4d7(%rip), %rsi     # 0x2c154
callq 0x3360
movq 0x68(%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 0x1e56f(%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 0x1dc09(%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 0x4(%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, %ebx
je 0xde54
testl %r13d, %r13d
leaq 0x1e4e7(%rip), %rsi     # 0x2c273
leaq 0x1e4b6(%rip), %rax     # 0x2c249
cmovneq %rax, %rsi
movl $0x1a, %edx
movl $0x29, %eax
cmovneq %rax, %rdx
movq 0x29224(%rip), %rdi     # 0x36fd0
callq 0x3360
movq 0x29218(%rip), %rcx     # 0x36fd0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x32e0
movsbl %al, %esi
movq 0x291fa(%rip), %rdi     # 0x36fd0
callq 0x3040
movq %rax, %rdi
callq 0x31f0
cmpl $0x0, 0x30(%r15)
jle 0xdedb
xorl %ebx, %ebx
movq 0x38(%r15), %rax
movsd (%rax,%rbx,8), %xmm0
mulsd 0x1e1f7(%rip), %xmm0    # 0x2bff8
xorl %edi, %edi
movl $0xffff, %esi           # imm = 0xFFFF
callq 0x4249
movl %eax, %r14d
movl %eax, %ebp
shrl $0x8, %ebp
testl %r13d, %r13d
je 0xde2b
movsbl %bpl, %esi
movq %r12, %rdi
callq 0x3040
movl %r14d, %ebp
jmp 0xde37
movsbl %r14b, %esi
movq %r12, %rdi
callq 0x3040
movsbl %bpl, %esi
movq %r12, %rdi
callq 0x3040
incq %rbx
movslq 0x30(%r15), %rax
cmpq %rax, %rbx
jl 0xddf0
jmp 0xdedb
movl $0x19, %edx
movq 0x29170(%rip), %rdi     # 0x36fd0
leaq 0x1e3c8(%rip), %rsi     # 0x2c22f
callq 0x3360
movq 0x2915d(%rip), %rcx     # 0x36fd0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x32e0
movsbl %al, %esi
movq 0x2913f(%rip), %rdi     # 0x36fd0
callq 0x3040
movq %rax, %rdi
callq 0x31f0
cmpl $0x0, 0x30(%r15)
jle 0xdedb
xorl %ebx, %ebx
movq 0x38(%r15), %rax
movsd (%rax,%rbx,8), %xmm0
mulsd 0x1e148(%rip), %xmm0    # 0x2c000
xorl %edi, %edi
movl $0xff, %esi
callq 0x4249
movsbl %al, %esi
movq %r12, %rdi
callq 0x3040
incq %rbx
movslq 0x30(%r15), %rax
cmpq %rax, %rbx
jl 0xdea7
movq %r12, %rdi
callq 0x33e0
movq 0x28(%rsp), %rbx
movq 0x40(%rsp), %rax
movq %rax, 0x68(%rsp)
movq -0x18(%rax), %rax
movq 0x38(%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 0xdf33
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3310
cmpl $0x1, %ebx
je 0xdf48
leal 0x1(%r13), %eax
testl %r13d, %r13d
movl %eax, %r13d
je 0xdb52
movq 0x30(%rsp), %rbx
leaq 0x10(%rbx), %rax
movq %rax, (%rbx)
leaq 0x1d91f(%rip), %rdx     # 0x2b87a
movq %rbx, %rdi
movq %rdx, %rsi
callq 0xaa0e
leaq 0x58(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xdf81
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 0x1e26d(%rip), %rdi     # 0x2c20a
leaq 0x1e088(%rip), %rsi     # 0x2c02c
leaq 0x1e208(%rip), %rcx     # 0x2c1b3
movl $0x146, %edx            # imm = 0x146
callq 0x31c0
leaq 0x1e1cc(%rip), %rdi     # 0x2c188
leaq 0x1e069(%rip), %rsi     # 0x2c02c
leaq 0x1e1e9(%rip), %rcx     # 0x2c1b3
movl $0x131, %edx            # imm = 0x131
callq 0x31c0
jmp 0xdfd8
jmp 0xdfd8
movq %rax, %rbx
jmp 0xe011
jmp 0xdfdf
movq %rax, %rbx
jmp 0xdff5
jmp 0xdfe8
jmp 0xdfe8
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x3400
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0xe011
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3310
leaq 0x58(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xe02c
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x3310
movq %rbx, %rdi
callq 0x3500