// File: /workspace/github_cpp_makefile_star2_O3/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, (%rsp)
leal -0x1(%rcx), %eax
cmpl $0x2, %eax
jae 0xdee5
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 0x9ee4
movq %rbx, %rdi
callq 0xcc86
cmpl $0x1, (%rsp)
leaq 0x18(%rsp), %r14
movl $0xffff, %eax           # imm = 0xFFFF
movl $0xff, %ecx
cmovnel %eax, %ecx
movl %ecx, 0x2c(%rsp)
movq 0x2954c(%rip), %rax     # 0x36fc8
movq (%rax), %rcx
movq %rcx, 0x40(%rsp)
movq 0x18(%rax), %rax
movq %rax, 0x38(%rsp)
movb $0x1, %r13b
movq %r14, 0x8(%rsp)
movq 0x48(%rsp), %rsi
movq 0x50(%rsp), %rdx
addq %rsi, %rdx
leaq 0x8(%rsp), %rdi
callq 0xa92c
cmpl $0x2, (%rsp)
jne 0xdada
leaq 0x8(%rsp), %rdi
leaq 0x1d70a(%rip), %rsi     # 0x2b1c8
callq 0x3540
testb $0x1, %r13b
jne 0xdada
leaq 0x8(%rsp), %rdi
leaq 0x1d6f8(%rip), %rsi     # 0x2b1cd
callq 0x3540
leaq 0x8(%rsp), %rdi
leaq 0x1d6f3(%rip), %rsi     # 0x2b1d9
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 0xdec6
movl $0x1d, %edx
movq 0x294a8(%rip), %rdi     # 0x36fd0
leaq 0x1d6b7(%rip), %rsi     # 0x2b1e6
callq 0x3360
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq 0x2948b(%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 0x1d57f(%rip), %rsi     # 0x2b12c
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 0x1d617(%rip), %rsi     # 0x2b204
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 0x1ccb7(%rip), %rsi     # 0x2a8ef
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 0x2c(%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, (%rsp)
je 0xdd86
testb $0x1, %r13b
leaq 0x1d563(%rip), %rsi     # 0x2b221
leaq 0x1d586(%rip), %rax     # 0x2b24b
cmovneq %rax, %rsi
movl $0x29, %edx
movl $0x1a, %eax
cmovneq %rax, %rdx
movq 0x292f2(%rip), %rdi     # 0x36fd0
callq 0x3360
movq 0x292e6(%rip), %rcx     # 0x36fd0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x32e0
movsbl %al, %esi
movq 0x292c8(%rip), %rdi     # 0x36fd0
callq 0x3040
movq %rax, %rdi
callq 0x31f0
cmpl $0x0, 0x30(%r15)
jle 0xde0f
xorl %r14d, %r14d
movq 0x38(%r15), %rax
movsd (%rax,%r14,8), %xmm0
mulsd 0x1d29b(%rip), %xmm0    # 0x2afd0
xorl %edi, %edi
movl $0xffff, %esi           # imm = 0xFFFF
callq 0x4249
movl %eax, %ebx
movl %eax, %ebp
shrl $0x8, %ebp
testb $0x1, %r13b
je 0xdd5b
movsbl %bl, %esi
movq %r12, %rdi
callq 0x3040
jmp 0xdd69
movsbl %bpl, %esi
movq %r12, %rdi
callq 0x3040
movl %ebx, %ebp
movsbl %bpl, %esi
movq %r12, %rdi
callq 0x3040
incq %r14
movslq 0x30(%r15), %rax
cmpq %rax, %r14
jl 0xdd23
jmp 0xde0f
movl $0x19, %edx
movq 0x2923e(%rip), %rdi     # 0x36fd0
leaq 0x1d46e(%rip), %rsi     # 0x2b207
callq 0x3360
movq 0x2922b(%rip), %rcx     # 0x36fd0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x32e0
movsbl %al, %esi
movq 0x2920d(%rip), %rdi     # 0x36fd0
callq 0x3040
movq %rax, %rdi
callq 0x31f0
cmpl $0x0, 0x30(%r15)
jle 0xde0f
xorl %r14d, %r14d
movq 0x38(%r15), %rax
movsd (%rax,%r14,8), %xmm0
mulsd 0x1d1ec(%rip), %xmm0    # 0x2afd8
xorl %edi, %edi
movl $0xff, %esi
callq 0x4249
movsbl %al, %esi
movq %r12, %rdi
callq 0x3040
incq %r14
movslq 0x30(%r15), %rax
cmpq %rax, %r14
jl 0xddda
movq %r12, %rdi
leaq 0x18(%rsp), %r14
callq 0x33e0
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
cmpq %r14, %rdi
je 0xde62
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3310
cmpl $0x1, (%rsp)
setne %al
testb %r13b, %al
movl $0x0, %r13d
jne 0xda90
movq 0x30(%rsp), %rbx
leaq 0x10(%rbx), %rax
movq %rax, (%rbx)
leaq 0x1c9cd(%rip), %rdx     # 0x2a858
movq %rbx, %rdi
movq %rdx, %rsi
callq 0xa9c2
leaq 0x58(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xdeb1
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 0x1d315(%rip), %rdi     # 0x2b1e2
leaq 0x1d130(%rip), %rsi     # 0x2b004
leaq 0x1d2b0(%rip), %rcx     # 0x2b18b
movl $0x146, %edx            # imm = 0x146
callq 0x31c0
leaq 0x1d274(%rip), %rdi     # 0x2b160
leaq 0x1d111(%rip), %rsi     # 0x2b004
leaq 0x1d291(%rip), %rcx     # 0x2b18b
movl $0x131, %edx            # imm = 0x131
callq 0x31c0
jmp 0xdf08
jmp 0xdf08
movq %rax, %rbx
jmp 0xdf44
jmp 0xdf0f
movq %rax, %rbx
jmp 0xdf2d
jmp 0xdf16
movq %rax, %rbx
leaq 0x18(%rsp), %r14
jmp 0xdf23
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x3400
movq 0x8(%rsp), %rdi
cmpq %r14, %rdi
je 0xdf44
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3310
leaq 0x58(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xdf5f
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x3310
movq %rbx, %rdi
callq 0x3500
nop