// File: 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
movq %rsp, %rbp
subq $0x170, %rsp            # imm = 0x170
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0xa0(%rbp)
movq $0x0, -0x18(%rbp)
addq $0x240, %rdi            # imm = 0x240
callq 0x45e0
testb $0x1, %al
jne 0x19308
jmp 0x1931b
movq -0xa0(%rbp), %rdi
addq $0x240, %rdi            # imm = 0x240
callq 0x4060
movq -0xa0(%rbp), %rsi
addq $0x450, %rsi            # imm = 0x450
leaq 0xa8f7(%rip), %rdx      # 0x23c27
leaq -0x38(%rbp), %rdi
movq %rdi, -0xb0(%rbp)
callq 0x7920
movq -0xa0(%rbp), %rax
movq -0xb0(%rbp), %rdi
addq $0x240, %rax            # imm = 0x240
movq %rax, -0xa8(%rbp)
callq 0x4140
movq -0xa8(%rbp), %rdi
movq %rax, %rsi
movl $0x8, %edx
callq 0x42f0
jmp 0x19376
movq -0xa0(%rbp), %rdi
addq $0x240, %rdi            # imm = 0x240
callq 0x45e0
movb %al, -0xb1(%rbp)
jmp 0x19391
movb -0xb1(%rbp), %al
testb $0x1, %al
jne 0x194f1
jmp 0x193a1
movq 0x12c48(%rip), %rdi     # 0x2bff0
leaq 0xa87e(%rip), %rsi      # 0x23c2d
callq 0x4310
movq %rax, -0xc0(%rbp)
jmp 0x193bd
movq -0xc0(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x42e0
movq %rax, -0xc8(%rbp)
jmp 0x193d6
movq -0xc8(%rbp), %rdi
leaq 0xa170(%rip), %rsi      # 0x23554
callq 0x4310
movq %rax, -0xd0(%rbp)
jmp 0x193f2
movq -0xd0(%rbp), %rdi
movq 0x12bb0(%rip), %rsi     # 0x2bfb0
callq 0x4350
jmp 0x19407
movl $0x2, %edi
callq 0x4400
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x1985a
movq -0x40(%rbp), %rdi
callq 0x4130
movq %rax, -0x50(%rbp)
movq 0x12baf(%rip), %rdi     # 0x2bff0
leaq 0xa807(%rip), %rsi      # 0x23c4f
callq 0x4310
movq %rax, -0xd8(%rbp)
jmp 0x19456
movq -0xd8(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x42e0
movq %rax, -0xe0(%rbp)
jmp 0x1946f
movq -0xe0(%rbp), %rdi
leaq 0xa568(%rip), %rsi      # 0x239e5
callq 0x4310
movq %rax, -0xe8(%rbp)
jmp 0x1948b
movq -0x50(%rbp), %rdi
callq 0x1ee90
movq %rax, %rsi
leaq -0x70(%rbp), %rdi
callq 0x1eeb0
jmp 0x194a2
movq -0xe8(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x42e0
movq %rax, -0xf0(%rbp)
jmp 0x194bb
movq -0xf0(%rbp), %rdi
movq 0x12aff(%rip), %rsi     # 0x2bfc8
callq 0x4350
jmp 0x194d0
leaq -0x70(%rbp), %rdi
callq 0x46d8
leaq 0xa31a(%rip), %rdi      # 0x237fa
callq 0x42a0
jmp 0x194e7
movl $0x1, %edi
callq 0x4400
jmp 0x19522
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x19516
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x46d8
callq 0x4580
jmp 0x1951d
jmp 0x1985a
movq -0x10(%rbp), %rdi
callq 0x45e0
movb %al, -0xf1(%rbp)
jmp 0x19533
movb -0xf1(%rbp), %al
testb $0x1, %al
jne 0x195ed
jmp 0x19543
movb $0x1, -0x72(%rbp)
movl $0x20, %edi
callq 0x4180
movq %rax, -0x108(%rbp)
movq %rax, -0x110(%rbp)
leaq -0x71(%rbp), %rdi
movq %rdi, -0x100(%rbp)
callq 0x4620
movq -0x108(%rbp), %rdi
movq -0x100(%rbp), %rdx
leaq 0xa278(%rip), %rsi      # 0x237fc
callq 0x7470
jmp 0x1958b
movq -0x110(%rbp), %rdi
movb $0x0, -0x72(%rbp)
leaq 0x122f3(%rip), %rsi     # 0x2b890
movq 0x12a14(%rip), %rdx     # 0x2bfb8
callq 0x45c0
jmp 0x19874
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x1985a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x71(%rbp), %rdi
callq 0x4380
testb $0x1, -0x72(%rbp)
jne 0x195dc
jmp 0x195e8
movq -0x110(%rbp), %rdi
callq 0x4250
jmp 0x1985a
movq $0xa00000, -0x80(%rbp)  # imm = 0xA00000
movl $0xa00000, %edi         # imm = 0xA00000
callq 0x4030
movq %rax, -0x118(%rbp)
jmp 0x19608
movq -0x118(%rbp), %rax
movq %rax, -0x88(%rbp)
jmp 0x196c2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %eax
movl $0x2, %ecx
cmpl %ecx, %eax
jne 0x1985a
movq -0x40(%rbp), %rdi
callq 0x4130
movq %rax, -0x90(%rbp)
movq 0x129a2(%rip), %rdi     # 0x2bff0
leaq 0xa655(%rip), %rsi      # 0x23caa
callq 0x4310
movq %rax, -0x120(%rbp)
jmp 0x19663
movq -0x90(%rbp), %rdi
movq (%rdi), %rax
movq 0x10(%rax), %rax
callq *%rax
movq -0x120(%rbp), %rdi
movq %rax, %rsi
callq 0x4310
movq %rax, -0x128(%rbp)
jmp 0x1968b
movq -0x128(%rbp), %rdi
movq 0x12917(%rip), %rsi     # 0x2bfb0
callq 0x4350
jmp 0x196a0
movl $0x4, %edi
callq 0x4400
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
callq 0x4580
jmp 0x196bd
jmp 0x1985a
jmp 0x196c4
movq -0xa0(%rbp), %rax
movq 0x240(%rax), %rcx
movq -0x18(%rcx), %rcx
leaq 0x240(%rax,%rcx), %rdi
callq 0x4440
movb %al, -0x129(%rbp)
jmp 0x196eb
movb -0x129(%rbp), %al
testb $0x1, %al
jne 0x196fa
jmp 0x197f6
movq -0xa0(%rbp), %rdi
addq $0x240, %rdi            # imm = 0x240
movq -0x88(%rbp), %rsi
movl $0xa00000, %edx         # imm = 0xA00000
callq 0x4450
jmp 0x1971b
movq $0x0, -0x98(%rbp)
movq -0xa0(%rbp), %rdi
movq -0x98(%rbp), %rax
movq %rax, -0x140(%rbp)
addq $0x240, %rdi            # imm = 0x240
callq 0x46c0
movq %rax, -0x138(%rbp)
jmp 0x19750
movq -0x140(%rbp), %rax
movq -0x138(%rbp), %rcx
cmpq %rcx, %rax
jae 0x1979c
movq -0x88(%rbp), %rax
movq -0x98(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xa, %eax
jne 0x19786
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
jmp 0x19788
movq -0x98(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x98(%rbp)
jmp 0x19726
movq -0xa0(%rbp), %rdi
movq -0x10(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x158(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0x150(%rbp)
addq $0x240, %rdi            # imm = 0x240
callq 0x46c0
movq %rax, -0x148(%rbp)
jmp 0x197d5
movq -0x148(%rbp), %rdx
movq -0x150(%rbp), %rsi
movq -0x158(%rbp), %rdi
callq 0x44a0
jmp 0x197f1
jmp 0x196c4
movq -0xa0(%rbp), %rdi
addq $0x240, %rdi            # imm = 0x240
callq 0x4060
jmp 0x1980b
movq -0x10(%rbp), %rdi
callq 0x4060
jmp 0x19816
movq -0x88(%rbp), %rax
movq %rax, -0x160(%rbp)
cmpq $0x0, %rax
je 0x19836
movq -0x160(%rbp), %rdi
callq 0x4480
movq -0x18(%rbp), %rax
movq %rax, -0x168(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x46d8
movq -0x168(%rbp), %rax
addq $0x170, %rsp            # imm = 0x170
popq %rbp
retq
leaq -0x38(%rbp), %rdi
callq 0x46d8
movq -0x40(%rbp), %rdi
callq 0x4610
movq %rax, %rdi
callq 0x82c0
nopw %cs:(%rax,%rax)