// File: /workspace/github_cpp_makefile_star2_O2/tonymugen[P]sampleSNPs/varfiles.cpp
// Function: sampFiles::TpedFileI::_famLines(std::basic_fstream<char, std::char_traits<char>>&)
uint64_t TpedFileI::_famLines(fstream &fam){
	uint64_t N = 0;

	/*
	 * I am using the line-end counting method. It is > 2-fold faster than reading lines with getline().
	 */
	if (_tfamFile.is_open()) {
        _tfamFile.close();
    }
    string tfamName = _fileStub + ".tfam";
    try {
        _tfamFile.open(tfamName.c_str(), ios::in);
		if (!_tfamFile.is_open()) {
			cerr << "ERROR: failed to open .tfam file " << tfamName << " for input" << endl;
			exit(2);
		}

    } catch (system_error &error) {
        cerr << "ERROR: cannot open .tfam file " << tfamName << " for input: " << error.code().message() << flush;
        perror(" ");
        exit(1);
    }
    if (!fam.is_open()) {
        throw string("Output .fam filestream not open");
    }
    const size_t bufSize = BUF_SIZE;
    char *buf;
    try {
        buf = new char[bufSize];
    } catch (bad_alloc& error) {
        cerr << "ERROR: failed to allocate buffer in TpedFile::_famLines(): " << error.what() << endl;
        exit(4);
    }

    while (_tfamFile) {
        _tfamFile.read(buf, bufSize);
        for (size_t i = 0; i < _tfamFile.gcount(); i++) {
            if (buf[i] == '\n') {
                N++;
            }
        }
        fam.write(buf, _tfamFile.gcount());
    }
    _tfamFile.close();
    fam.close();
    delete [] buf;

	return N;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %rbx
movq %rdi, %r15
leaq 0x240(%rdi), %r14
leaq 0x2c0(%rdi), %r12
movq %r12, %rdi
callq 0x40b0
testb %al, %al
je 0xef18
movq %r14, %rdi
callq 0x4050
leaq 0x450(%r15), %rsi
leaq 0x5d19(%rip), %rdx      # 0x14c3f
leaq 0x28(%rsp), %r13
movq %r13, %rdi
callq 0x5e08
movq (%r13), %rsi
pushq $0x8
popq %rdx
movq %r14, %rdi
callq 0x4280
movq %r12, %rdi
callq 0x40b0
testb %al, %al
je 0xf002
leaq 0x80(%rbx), %rdi
callq 0x40b0
testb %al, %al
je 0xf040
movl $0xa00000, %edi         # imm = 0xA00000
callq 0x4030
movq %rax, %r13
leaq 0x10(%rbx), %rbp
xorl %r12d, %r12d
movq 0x240(%r15), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x260(%r15,%rax)
jne 0xefce
movl $0xa00000, %edx         # imm = 0xA00000
movq %r14, %rdi
movq %r13, %rsi
callq 0x4340
movq 0x248(%r15), %rdx
xorl %eax, %eax
cmpq %rax, %rdx
je 0xefc1
xorl %ecx, %ecx
cmpb $0xa, (%r13,%rax)
sete %cl
addq %rcx, %r12
incq %rax
jmp 0xefa9
movq %rbp, %rdi
movq %r13, %rsi
callq 0x4390
jmp 0xef7a
movq %r14, %rdi
callq 0x4050
movq %rbx, %rdi
callq 0x4050
movq %r13, %rdi
callq 0x4370
leaq 0x28(%rsp), %rdi
callq 0x4558
movq %r12, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xcfe7(%rip), %rdi      # 0x1bff0
leaq 0x5c35(%rip), %rsi      # 0x14c45
callq 0x42a0
leaq 0x28(%rsp), %rsi
movq %rax, %rdi
callq 0x4270
leaq 0x5543(%rip), %rsi      # 0x1456c
movq %rax, %rdi
callq 0x42a0
movq %rax, %rdi
callq 0x4140
pushq $0x2
jmp 0xf0d5
pushq $0x20
popq %rdi
callq 0x4150
movq %rax, %r14
leaq 0x57c2(%rip), %rsi      # 0x14814
leaq 0x8(%rsp), %rdx
movq %rax, %rdi
callq 0x5d1e
leaq 0xc862(%rip), %rsi      # 0x1b8c8
movq 0xcf53(%rip), %rdx      # 0x1bfc0
movq %r14, %rdi
callq 0x44a0
jmp 0xf19a
movq %rax, %rbx
movq %r14, %rdi
callq 0x4200
jmp 0xf19d
movq %rax, %rbx
cmpl $0x2, %edx
jne 0xf19d
movq %rbx, %rdi
callq 0x4110
movq %rax, %rbx
movq 0xcf48(%rip), %rdi      # 0x1bff0
leaq 0x5c13(%rip), %rsi      # 0x14cc2
callq 0x42a0
movq %rax, %r14
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x10(%rax)
movq %r14, %rdi
movq %rax, %rsi
callq 0x42a0
movq %rax, %rdi
callq 0x4140
pushq $0x4
popq %rdi
callq 0x4310
movq %rax, %rbx
callq 0x4470
jmp 0xf19d
movq %rax, %rbx
cmpl $0x1, %edx
jne 0xf19d
movq %rbx, %rdi
callq 0x4110
movq %rax, %rbx
movq 0xceea(%rip), %rdi      # 0x1bff0
leaq 0x5b5a(%rip), %rsi      # 0x14c67
callq 0x42a0
leaq 0x28(%rsp), %rsi
movq %rax, %rdi
callq 0x4270
leaq 0x58d7(%rip), %rsi      # 0x149fd
movq %rax, %rdi
callq 0x42a0
movq %rax, %r14
movq 0x18(%rbx), %rsi
movl 0x10(%rbx), %edx
movq (%rsi), %rax
leaq 0x8(%rsp), %rdi
callq *0x20(%rax)
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0x4270
movq %rax, %rdi
callq 0x41c0
leaq 0x8(%rsp), %rdi
callq 0x4558
leaq 0x56a9(%rip), %rdi      # 0x14812
callq 0x4250
pushq $0x1
jmp 0xf0d5
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x4558
jmp 0xf189
jmp 0xf186
movq %rax, %rbx
callq 0x4470
jmp 0xf19d
movq %rax, %rdi
callq 0x6194
jmp 0xf19a
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x4558
movq %rbx, %rdi
callq 0x44d0
nop