// File: /workspace/github_cpp_makefile_star2_O2/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 $0x258, %rsp            # imm = 0x258
movq %rcx, (%rsp)
leal -0x1(%rcx), %eax
cmpl $0x2, %eax
jae 0xb333
movq %rsi, %r15
movq %rdi, %r14
leaq 0x38(%rsp), %rbx
leaq 0x58(%rsp), %rax
movq %rbx, %rdi
movq %rdx, %rsi
movq %rax, %rdx
callq 0x7fcc
movq %rbx, %rdi
callq 0xa382
xorl %ecx, %ecx
cmpl $0x1, (%rsp)
movl $0xff, %eax
movl $0xffff, %edx           # imm = 0xFFFF
cmovel %eax, %edx
movl %edx, 0x8(%rsp)
setne %cl
incl %ecx
movl %ecx, 0xc(%rsp)
xorl %ebp, %ebp
leaq 0x58(%rsp), %r13
movq %r14, 0x10(%rsp)
cmpl 0xc(%rsp), %ebp
je 0xb2e1
leaq 0x18(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x33b0
cmpl $0x2, (%rsp)
jne 0xb0f6
leaq 0x18(%rsp), %rdi
leaq 0x180dc(%rip), %rsi     # 0x231b8
callq 0x3510
testl %ebp, %ebp
je 0xb0f6
leaq 0x18(%rsp), %rdi
leaq 0x180cc(%rip), %rsi     # 0x231bd
callq 0x3510
leaq 0x18(%rsp), %rdi
leaq 0x180c7(%rip), %rsi     # 0x231c9
callq 0x3510
movq %r13, %rdi
callq 0x34a0
movq 0x18(%rsp), %rsi
movq %r13, %rdi
pushq $0x10
popq %rdx
callq 0x3050
movq 0x58(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x78(%rsp,%rax)
jne 0xb314
movq 0x24e96(%rip), %rdi     # 0x2ffd0
leaq 0x18095(%rip), %rsi     # 0x231d6
callq 0x32c0
movq %rax, %rdi
leaq 0x18(%rsp), %rsi
callq 0x3270
movq %rax, %rdi
callq 0x3120
movq %rax, %rdi
callq 0x3120
movq %r13, %rdi
leaq 0x17faf(%rip), %rsi     # 0x2311c
callq 0x32c0
movq %rax, %rdi
callq 0x3120
movq %rax, %rdi
leaq 0x18070(%rip), %rsi     # 0x231f4
callq 0x32c0
movq %rax, %rdi
callq 0x3120
movl 0x28(%r15), %esi
movq %rax, %rdi
callq 0x34b0
movq %rax, %rdi
leaq 0x17734(%rip), %rsi     # 0x228db
callq 0x32c0
movl 0x2c(%r15), %esi
movq %rax, %rdi
callq 0x34b0
movq %rax, %rdi
callq 0x3120
movq %rax, %rdi
movl 0x8(%rsp), %esi
callq 0x34b0
movq %rax, %rdi
callq 0x3120
cmpl $0x1, (%rsp)
jne 0xb231
movq 0x24def(%rip), %rdi     # 0x2ffd0
leaq 0x1800f(%rip), %rsi     # 0x231f7
callq 0x32c0
movq %rax, %rdi
callq 0x3120
xorl %ebx, %ebx
movslq 0x30(%r15), %rax
cmpq %rax, %rbx
jge 0xb2bb
movq 0x38(%r15), %rax
movsd (%rax,%rbx,8), %xmm0
mulsd 0x17db3(%rip), %xmm0    # 0x22fc8
xorl %edi, %edi
movl $0xff, %esi
callq 0x3c32
movsbl %al, %esi
movq %r13, %rdi
callq 0x3040
incq %rbx
jmp 0xb1f7
testl %ebp, %ebp
leaq 0x17fd7(%rip), %rsi     # 0x23211
leaq 0x17ffa(%rip), %rax     # 0x2323b
cmoveq %rax, %rsi
movq 0x24d84(%rip), %rdi     # 0x2ffd0
callq 0x32c0
movq %rax, %rdi
callq 0x3120
xorl %r14d, %r14d
movslq 0x30(%r15), %rax
cmpq %rax, %r14
jge 0xb2bb
movq 0x38(%r15), %rax
movsd (%rax,%r14,8), %xmm0
mulsd 0x17d49(%rip), %xmm0    # 0x22fc0
xorl %edi, %edi
movl $0xffff, %esi           # imm = 0xFFFF
callq 0x3c32
movl %eax, %r12d
movl %eax, %ebx
shrl $0x8, %ebx
testl %ebp, %ebp
je 0xb29f
movsbl %bl, %esi
movq %r13, %rdi
callq 0x3040
movl %r12d, %ebx
jmp 0xb2ab
movsbl %r12b, %esi
movq %r13, %rdi
callq 0x3040
movsbl %bl, %esi
movq %r13, %rdi
callq 0x3040
incq %r14
jmp 0xb25c
movq %r13, %rdi
callq 0x33c0
movq 0x10(%rsp), %r14
movq %r13, %rdi
callq 0x33e0
leaq 0x18(%rsp), %rdi
callq 0x3548
incl %ebp
jmp 0xb0b1
leaq 0x1755c(%rip), %rsi     # 0x22844
leaq 0x58(%rsp), %rdx
movq %r14, %rdi
callq 0x7fcc
leaq 0x38(%rsp), %rdi
callq 0x3548
movq %r14, %rax
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x17eb7(%rip), %rdi     # 0x231d2
leaq 0x17cd2(%rip), %rsi     # 0x22ff4
leaq 0x17e52(%rip), %rcx     # 0x2317b
movl $0x146, %edx            # imm = 0x146
callq 0x31a0
leaq 0x17e16(%rip), %rdi     # 0x23150
leaq 0x17cb3(%rip), %rsi     # 0x22ff4
leaq 0x17e33(%rip), %rcx     # 0x2317b
movl $0x131, %edx            # imm = 0x131
callq 0x31a0
jmp 0xb356
jmp 0xb356
movq %rax, %rbx
jmp 0xb37d
jmp 0xb35d
movq %rax, %rbx
jmp 0xb373
jmp 0xb366
jmp 0xb366
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x33e0
leaq 0x18(%rsp), %rdi
callq 0x3548
leaq 0x38(%rsp), %rdi
callq 0x3548
movq %rbx, %rdi
callq 0x34d0
nop