// File: /workspace/github_cpp_makefile_star2_O2/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 0xd9d(%rip), %rsi       # 0x2004
leaq -0x260(%rbp), %rbx
pushq $0x1a
popq %rdx
movq %rbx, %rdi
callq 0x10c0
movq (%rbx), %rax
movq -0x18(%rax), %rax
testb $0x5, -0x240(%rbp,%rax)
je 0x12ad
movq 0x2d5f(%rip), %rdi      # 0x3ff0
leaq 0xd79(%rip), %rsi       # 0x2011
callq 0x1090
movq %rax, %rdi
callq 0x1060
pushq $0x1
popq %rbx
jmp 0x13ba
leaq -0x260(%rbp), %rdi
callq 0x10f0
movq %rax, -0x30(%rbp)
leaq -0x260(%rbp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x10d0
leaq -0x40(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
xorl %r15d, %r15d
leaq -0x260(%rbp), %r12
movq -0x260(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x5, -0x240(%rbp,%rax)
leaq -0x250(%rbp), %rbx
jne 0x1393
movq %r12, %rdi
callq 0x10f0
cmpq -0x30(%rbp), %rax
je 0x1393
movq %r12, %rdi
leaq -0x50(%rbp), %rsi
callq 0x1030
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x1393
movq -0x48(%rbp), %rbx
movq %r12, %rdi
callq 0x10f0
movq %rax, %r13
movq %rdx, %r14
leaq -0x250(%rbp), %rdi
xorl %esi, %esi
pushq $0x2
popq %rdx
callq 0x10a0
addq %rbx, %r15
incq %r15
leaq -0x250(%rbp), %rdi
movq %r15, %rsi
callq 0x1070
cmpq -0x30(%rbp), %r13
je 0x1380
leaq -0x250(%rbp), %rdi
leaq 0xcab(%rip), %rsi       # 0x2026
callq 0x1090
movq %r12, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x10b0
jmp 0x12e7
pushq $0x2
popq %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x10a0
leaq 0xc81(%rip), %rsi       # 0x2028
movq %rbx, %rdi
callq 0x1090
leaq -0x50(%rbp), %rdi
callq 0x1050
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 0x13e5
jmp 0x13de
movq %rax, %rbx
jmp 0x13f1
jmp 0x13e5
movq %rax, %rbx
leaq -0x50(%rbp), %rdi
callq 0x1050
leaq -0x260(%rbp), %rdi
callq 0x1040
movq %rbx, %rdi
callq 0x1100