Tanix2
2022-04-23 00:00:11 +08:00
源代码
```c++
#include <iostream>
#include <vector>
using namespace std;
int main(){
    vector<int> a{1,2,3};
    for(int i = 0; i < a.size(); i++){
        cout << a[i] << ' ';
    }
}
```
g++版本
```shell
g++ --version
g++.exe (tdm64-1) 10.3.0
```
分别用不同优化等级编译,然后反编译,循环部分如下
-O0
```c
for( i = 0; ; ++i ){
    v3 = i;
    if ( v3 >= std::vector<int>::size(v8) )break;
    v4 = (unsigned int *)std::vector<int>::operator[](v8, i);
    v5 = std::ostream::operator<<(refptr__ZSt4cout, *v4);
    std::operator<<<std::char_traits<char>>(v5, 32i64);
}
```
-O1
```c
do{
    v5 = ((__int64 (__fastcall *)(void *, _QWORD))std::ostream::operator<<)(
           refptr__ZSt4cout,
           *(unsigned int *)(v3 + 4 * v4));
    v7 = 32;
    std::__ostream_insert<char,std::char_traits<char>>(v5, &v7, 1i64);
    v3 = v8;
    ++v4;
}while ( (v9 - v8) >> 2 > v4 );
```
-O2
```c
do{
    v5 = (std::ostream *)std::ostream::operator<<(refptr__ZSt4cout, *(unsigned int *)(v3 + 4 * v4));
    std::__ostream_insert<char,std::char_traits<char>>(v5);
    v3 = v7;
    ++v4;
}while ( (v8 - v7) >> 2 > v4 );
```
-O3
```c
do{
    v5 = (std::ostream *)std::ostream::operator<<(refptr__ZSt4cout, *v4);
    std::__ostream_insert<char,std::char_traits<char>>(v5);
    ++v4;
}while ( v4 != v3 + 3 );
```