// File: 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
subq $0x300, %rsp            # imm = 0x300
movl $0x0, -0x4(%rbp)
movl $0x2, %edi
movl $0x8, %esi
callq 0x1640
movl %eax, %edi
movl $0x10, %esi
callq 0x1640
movl %eax, %edx
leaq 0xd3e(%rip), %rsi       # 0x2004
leaq -0x218(%rbp), %rdi
callq 0x10f0
movq -0x218(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x218(%rbp,%rax), %rdi
callq 0x1060
movb %al, -0x291(%rbp)
jmp 0x12f2
movb -0x291(%rbp), %al
testb $0x1, %al
jne 0x12fe
jmp 0x135c
movq 0x2ceb(%rip), %rdi      # 0x3ff0
leaq 0xd05(%rip), %rsi       # 0x2011
callq 0x10a0
movq %rax, -0x2a0(%rbp)
jmp 0x131a
movq -0x2a0(%rbp), %rdi
movq 0x2ca0(%rip), %rsi      # 0x3fc8
callq 0x10c0
jmp 0x132f
movl $0x1, -0x4(%rbp)
movl $0x1, -0x228(%rbp)
jmp 0x1610
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
jmp 0x1628
leaq -0x218(%rbp), %rdi
callq 0x1130
movq %rdx, -0x2b0(%rbp)
movq %rax, -0x2a8(%rbp)
jmp 0x1378
movq -0x2b0(%rbp), %rax
movq -0x2a8(%rbp), %rcx
movq %rcx, -0x238(%rbp)
movq %rax, -0x230(%rbp)
xorl %edx, %edx
movl %edx, %esi
leaq -0x218(%rbp), %rdi
callq 0x1100
jmp 0x13a6
movq $0x0, -0x240(%rbp)
leaq -0x260(%rbp), %rdi
callq 0x1110
movq -0x218(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x218(%rbp,%rax), %rdi
callq 0x10d0
movb %al, -0x2b1(%rbp)
jmp 0x13dd
movb -0x2b1(%rbp), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x2b2(%rbp)
jne 0x13f5
jmp 0x14a5
leaq -0x218(%rbp), %rdi
callq 0x1130
movq %rdx, -0x2c8(%rbp)
movq %rax, -0x2c0(%rbp)
jmp 0x1411
movq -0x2c8(%rbp), %rax
movq -0x2c0(%rbp), %rcx
movq %rcx, -0x270(%rbp)
movq %rax, -0x268(%rbp)
leaq -0x270(%rbp), %rdi
leaq -0x238(%rbp), %rsi
callq 0x1660
movb %al, -0x2c9(%rbp)
jmp 0x1448
movb -0x2c9(%rbp), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x2b2(%rbp)
jne 0x145d
jmp 0x14a5
leaq -0x218(%rbp), %rdi
leaq -0x260(%rbp), %rsi
callq 0x1030
movq %rax, -0x2d8(%rbp)
jmp 0x1479
movq -0x2d8(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x10d0
movb %al, -0x2d9(%rbp)
jmp 0x1497
movb -0x2d9(%rbp), %al
movb %al, -0x2b2(%rbp)
jmp 0x14a5
movb -0x2b2(%rbp), %al
testb $0x1, %al
jne 0x14b4
jmp 0x15c7
leaq -0x260(%rbp), %rdi
callq 0x1080
movq -0x240(%rbp), %rcx
leaq 0x1(%rax,%rcx), %rax
movq %rax, -0x240(%rbp)
leaq -0x218(%rbp), %rdi
callq 0x1130
movq %rdx, -0x2f0(%rbp)
movq %rax, -0x2e8(%rbp)
jmp 0x14ef
movq -0x2f0(%rbp), %rax
movq -0x2e8(%rbp), %rcx
movq %rcx, -0x280(%rbp)
movq %rax, -0x278(%rbp)
leaq -0x208(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x10b0
jmp 0x1522
leaq -0x208(%rbp), %rdi
movq -0x240(%rbp), %rsi
callq 0x1050
jmp 0x1537
leaq -0x280(%rbp), %rdi
leaq -0x238(%rbp), %rsi
callq 0x1660
movb %al, -0x2f1(%rbp)
jmp 0x1552
movb -0x2f1(%rbp), %al
testb $0x1, %al
jne 0x155e
jmp 0x1598
leaq -0x208(%rbp), %rdi
leaq 0xaba(%rip), %rsi       # 0x2026
callq 0x10a0
jmp 0x1573
jmp 0x1598
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
leaq -0x260(%rbp), %rdi
callq 0x1070
jmp 0x1628
movups -0x280(%rbp), %xmm0
movaps %xmm0, -0x290(%rbp)
movq -0x290(%rbp), %rsi
movq -0x288(%rbp), %rdx
leaq -0x218(%rbp), %rdi
callq 0x10e0
jmp 0x15c2
jmp 0x13bd
leaq -0x208(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x10b0
jmp 0x15de
leaq -0x208(%rbp), %rdi
leaq 0xa3c(%rip), %rsi       # 0x2028
callq 0x10a0
jmp 0x15f3
movl $0x0, -0x4(%rbp)
movl $0x1, -0x228(%rbp)
leaq -0x260(%rbp), %rdi
callq 0x1070
leaq -0x218(%rbp), %rdi
callq 0x1040
movl -0x4(%rbp), %eax
addq $0x300, %rsp            # imm = 0x300
popq %rbp
retq
leaq -0x218(%rbp), %rdi
callq 0x1040
movq -0x220(%rbp), %rdi
callq 0x1140