void *foo(int offset) {
  switch (offset) {
  case 0:
    return __builtin_return_address(0);
  case 1:
    return __builtin_return_address(1);
  case 2:
    return __builtin_return_address(2);
  case 3:
    return __builtin_return_address(3);
  case 4:
    return __builtin_return_address(4);
  }
  return 0;
}


x86 assembly (clang, -O0):
<foo>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %rax,-0x18(%rbp)
sub    $0x4,%rax
ja     1197 <foo+0x97>
mov    -0x18(%rbp),%rax
lea    0xedd(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    0x8(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    119f <foo+0x9f>
mov    %rbp,%rax
mov    (%rax),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    119f <foo+0x9f>
mov    %rbp,%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    119f <foo+0x9f>
mov    %rbp,%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    119f <foo+0x9f>
mov    %rbp,%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    119f <foo+0x9f>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<foo>:
push   %rbp
mov    %rsp,%rbp
cmp    $0x4,%edi
ja     1121 <foo+0x21>
mov    %edi,%eax
lea    0xeee(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    0x8(%rbp),%rax
pop    %rbp
ret
xor    %eax,%eax
pop    %rbp
ret
mov    0x0(%rbp),%rax
jmp    1144 <foo+0x44>
mov    0x0(%rbp),%rax
jmp    1141 <foo+0x41>
mov    0x0(%rbp),%rax
jmp    113e <foo+0x3e>
mov    0x0(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    0x8(%rax),%rax
pop    %rbp
ret

LLVM IR (-O0):
define i8* @foo(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
store i32 %0, i32* %3
%4 = load i32, i32* %3
switch i32 %4, label %15 [
i32 0, label %5
i32 1, label %7
i32 2, label %9
i32 3, label %11
i32 4, label %13
]
5: ; preds = %1
%6 = call i8* @llvm.returnaddress(i32 0)
store i8* %6, i8** %2
br label %16
7: ; preds = %1
%8 = call i8* @llvm.returnaddress(i32 1)
store i8* %8, i8** %2
br label %16
9: ; preds = %1
%10 = call i8* @llvm.returnaddress(i32 2)
store i8* %10, i8** %2
br label %16
11: ; preds = %1
%12 = call i8* @llvm.returnaddress(i32 3)
store i8* %12, i8** %2
br label %16
13: ; preds = %1
%14 = call i8* @llvm.returnaddress(i32 4)
store i8* %14, i8** %2
br label %16
15: ; preds = %1
store i8* null, i8** %2
br label %16
16: ; preds = %15, %13, %11, %9, %7, %5
%17 = load i8*, i8** %2
ret i8* %17
}

LLVM IR (-O2):
define i8* @foo(i32 %0) {
switch i32 %0, label %12 [
i32 0, label %2
i32 1, label %4
i32 2, label %6
i32 3, label %8
i32 4, label %10
]
2: ; preds = %1
%3 = tail call i8* @llvm.returnaddress(i32 0)
br label %12
4: ; preds = %1
%5 = tail call i8* @llvm.returnaddress(i32 1)
br label %12
6: ; preds = %1
%7 = tail call i8* @llvm.returnaddress(i32 2)
br label %12
8: ; preds = %1
%9 = tail call i8* @llvm.returnaddress(i32 3)
br label %12
10: ; preds = %1
%11 = tail call i8* @llvm.returnaddress(i32 4)
br label %12
12: ; preds = %1, %10, %8, %6, %4, %2
%13 = phi i8* [ %11, %10 ], [ %9, %8 ], [ %7, %6 ], [ %5, %4 ], [ %3, %2 ], [ null, %1 ]
ret i8* %13
}