title: Simple example where [[likely]] and [[unlikely]] affect program assembly?
body: <p>C++20 introduced the attributes <code>[[likely]]</code> and <code>[[unlikely]]</code> to the language, which can be used to allow the compiler to optimize for the case where one execution path is either much <em>more</em> likely or much <em>less</em> likely than the other ones. </p>
  
  <p>Given the cost of an incorrect branch prediction, this seems like a feature that's potentially extremely useful in performance-critical sections of code, but I don't know what it would actually cause the compiler to do. </p>
  
  <p><strong>Is there a simple piece of code for which adding <code>[[likely]]</code> and <code>[[unlikely]]</code> attributes changes the assembly output by the compiler?</strong> And perhaps more importantly, what do these changes do?</p>
  
  <p>I created a simple example for my own understanding to see if there was any difference in the assembly, but it appears that this example is too simple to actually show any changes to the assembly:</p>
  
  <pre><code>void true_path();
  void false_path();
  
  void foo(int i) {
      if(i) {
          true_path();
      } else {
          false_path();
      }
  }
  void bar(int i) {
      if(i) [[likely]] {
          true_path();
      } else [[unlikely]] {
          false_path();
      }
  }
  </code></pre>
  
  <p><a href="https://godbolt.org/z/x8GczP" rel="noreferrer">View the compiled assembly here.</a></p>