void *__attribute__((noclone, no)) test_builtin_return_address(unsigned i) {
  void *const ra[] = {__builtin_return_address(0), __builtin_return_address(1),
                      __builtin_return_address(2), __builtin_return_address(3),
                      __builtin_return_address(4)};
  return ra[i];
}


x86 assembly (clang, -O0):
<test_builtin_return_address>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    %rbp,%rax
mov    (%rax),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    %rbp,%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    %rbp,%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    %rbp,%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x10(%rbp)
mov    -0x4(%rbp),%eax
mov    -0x30(%rbp,%rax,8),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_builtin_return_address>:
push   %rbp
mov    %rsp,%rbp
mov    0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x0(%rbp),%rax
mov    (%rax),%rcx
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    (%rcx),%rax
mov    0x8(%rcx),%rcx
mov    %rcx,-0x20(%rbp)
mov    (%rax),%rcx
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    0x8(%rcx),%rax
mov    %rax,-0x10(%rbp)
mov    %edi,%eax
mov    -0x30(%rbp,%rax,8),%rax
pop    %rbp
ret

LLVM IR (-O0):
define i8* @test_builtin_return_address(i32 %0) {
%2 = alloca i32
%3 = alloca [5 x i8*]
store i32 %0, i32* %2
%4 = getelementptr inbounds [5 x i8*], [5 x i8*]* %3, i64 0, i64 0
%5 = call i8* @llvm.returnaddress(i32 0)
store i8* %5, i8** %4
%6 = getelementptr inbounds i8*, i8** %4, i64 1
%7 = call i8* @llvm.returnaddress(i32 1)
store i8* %7, i8** %6
%8 = getelementptr inbounds i8*, i8** %6, i64 1
%9 = call i8* @llvm.returnaddress(i32 2)
store i8* %9, i8** %8
%10 = getelementptr inbounds i8*, i8** %8, i64 1
%11 = call i8* @llvm.returnaddress(i32 3)
store i8* %11, i8** %10
%12 = getelementptr inbounds i8*, i8** %10, i64 1
%13 = call i8* @llvm.returnaddress(i32 4)
store i8* %13, i8** %12
%14 = load i32, i32* %2
%15 = zext i32 %14 to i64
%16 = getelementptr inbounds [5 x i8*], [5 x i8*]* %3, i64 0, i64 %15
%17 = load i8*, i8** %16
ret i8* %17
}

LLVM IR (-O2):
define i8* @test_builtin_return_address(i32 %0) {
%2 = alloca [5 x i8*]
%3 = bitcast [5 x i8*]* %2 to i8*
%4 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 0
%5 = tail call i8* @llvm.returnaddress(i32 0)
store i8* %5, i8** %4
%6 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 1
%7 = tail call i8* @llvm.returnaddress(i32 1)
store i8* %7, i8** %6
%8 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 2
%9 = tail call i8* @llvm.returnaddress(i32 2)
store i8* %9, i8** %8
%10 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 3
%11 = tail call i8* @llvm.returnaddress(i32 3)
store i8* %11, i8** %10
%12 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 4
%13 = tail call i8* @llvm.returnaddress(i32 4)
store i8* %13, i8** %12
%14 = zext i32 %0 to i64
%15 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 %14
%16 = load i8*, i8** %15
ret i8* %16
}