// File: /workspace/github_cpp_makefile_star2_O1/iZhangHui[P]CppPrimer/CH17_SpecializedLibraryFacilities/examples/seek.cc
// Function: main
int main()
{
    // open for input and output and preposition file pointers to end-of-file
	// file mode argument 
    fstream inOut("data/copyOut", 
                   fstream::ate | fstream::in | fstream::out);
    if (!inOut) {
        cerr << "Unable to open file!" << endl;
        return EXIT_FAILURE; // EXIT_FAILURE 
    } 

    // inOut is opened in ate mode, so it starts out positioned at the end
    auto end_mark = inOut.tellg();// remember original end-of-file position
    inOut.seekg(0, fstream::beg); // reposition to the start of the file
    size_t cnt = 0;               // accumulator for the byte count
    string line;                  // hold each line of input

    // while we haven't hit an error and are still reading the original data 
    while (inOut && inOut.tellg() != end_mark
           && getline(inOut, line)) { // and can get another line of input
        cnt += line.size() + 1;       // add 1 to account for the newline
		auto mark = inOut.tellg();    // remember the read position
        inOut.seekp(0, fstream::end); // set the write marker to the end
        inOut << cnt;                 // write the accumulated length
        // print a separator if this is not the last line
        if (mark != end_mark) inOut << " "; 
        inOut.seekg(mark);            // restore the read position
    }
    inOut.seekp(0, fstream::end);     // seek to the end
    inOut << "\n";                    // write a newline at end-of-file

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x238, %rsp            # imm = 0x238
leaq 0xd7d(%rip), %rsi       # 0x2004
leaq -0x260(%rbp), %rbx
movq %rbx, %rdi
movl $0x1a, %edx
callq 0x10d0
movq (%rbx), %rax
movq -0x18(%rax), %rax
testb $0x5, -0x240(%rbp,%rax)
je 0x12fc
movq 0x2d3d(%rip), %rbx      # 0x3ff0
leaq 0xd57(%rip), %rsi       # 0x2011
movl $0x14, %edx
movq %rbx, %rdi
callq 0x10b0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x1080
movsbl %al, %esi
movq 0x2d0b(%rip), %rdi      # 0x3ff0
callq 0x1030
movl $0x1, %ebx
movq %rax, %rdi
callq 0x1060
jmp 0x1445
leaq -0x260(%rbp), %rdi
callq 0x1100
movq %rax, -0x30(%rbp)
leaq -0x260(%rbp), %r14
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x10e0
leaq -0x40(%rbp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
xorl %r12d, %r12d
movq -0x260(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x5, -0x240(%rbp,%rax)
jne 0x1400
movq %r14, %rdi
callq 0x1100
cmpq -0x30(%rbp), %rax
je 0x1400
movq -0x260(%rbp), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x1080
movsbl %al, %edx
movq %r14, %rdi
leaq -0x50(%rbp), %rsi
callq 0x1120
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x1400
movq -0x48(%rbp), %rbx
movq %r14, %rdi
callq 0x1100
movq %rax, %r13
movq %rdx, %r15
leaq -0x250(%rbp), %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x1090
addq %rbx, %r12
incq %r12
leaq -0x250(%rbp), %rdi
movq %r12, %rsi
callq 0x1050
cmpq -0x30(%rbp), %r13
je 0x13ed
movl $0x1, %edx
leaq -0x250(%rbp), %rdi
leaq 0xc3e(%rip), %rsi       # 0x2026
callq 0x10b0
movq %r14, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x10c0
jmp 0x1335
leaq -0x250(%rbp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x1090
leaq 0xc0b(%rip), %rsi       # 0x2028
movl $0x1, %edx
movq %rbx, %rdi
callq 0x10b0
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x1443
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x10a0
xorl %ebx, %ebx
leaq -0x260(%rbp), %rdi
callq 0x1040
movl %ebx, %eax
addq $0x238, %rsp            # imm = 0x238
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1470
jmp 0x1469
movq %rax, %rbx
jmp 0x148c
jmp 0x1470
movq %rax, %rbx
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x148c
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x10a0
leaq -0x260(%rbp), %rdi
callq 0x1040
movq %rbx, %rdi
callq 0x1110