<p>I've been digging through some parts of the Linux kernel, and found calls like this:</p>

<pre><code>if (unlikely(fd &lt; 0))
{
    /* Do something */
}
</code></pre>

<p>or</p>

<pre><code>if (likely(!err))
{
    /* Do something */
}
</code></pre>

<p>I've found the definition of them:</p>

<pre><code>#define likely(x)       __builtin_expect((x),1)
#define unlikely(x)     __builtin_expect((x),0)
</code></pre>

<p>I know that they are for optimization, but how do they work? And how much performance/size decrease can be expected from using them? And is it worth the hassle (and losing the portability probably) at least in bottleneck code (in userspace, of course).</p>

They're hints to the compiler to generate the hint prefixes on branches.  On x86/x64, they take up one byte, so you'll get at most a one-byte increase for each branch.  As for performance, it entirely depends on the application -- in most cases, the branch predictor on the processor will ignore them, these days.
Edit: Forgot about one place they can actually really help with.  It can allow the compiler to reorder the control-flow graph to reduce the number of branches taken for the 'likely' path.  This can have a marked improvement in loops where you're checking multiple exit cases.