int foo(int x) {
  static void *labptr[] = {&&lab0, &&lab1, &&lab2};
  if (x < 0 || x > 2)
    return -1;
  goto *labptr[x];
lab0:
  return bar(x);
lab1:
  return baz(x) + 1;
lab2:
  return spaz(x) / 2;
}


x86 assembly (clang, -O0):
<foo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     114f <foo+0x1f>
cmpl   $0x2,-0x8(%rbp)
jle    115b <foo+0x2b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11af <foo+0x7f>
movslq -0x8(%rbp),%rcx
lea    0x2eda(%rip),%rax
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x10(%rbp)
jmp    11b8 <foo+0x88>
mov    -0x8(%rbp),%edi
mov    $0x0,%al
call   1040 <bar@plt>
mov    %eax,-0x4(%rbp)
jmp    11af <foo+0x7f>
mov    -0x8(%rbp),%edi
mov    $0x0,%al
call   1030 <baz@plt>
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    11af <foo+0x7f>
mov    -0x8(%rbp),%edi
mov    $0x0,%al
call   1050 <spaz@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret
mov    -0x10(%rbp),%rax
jmp    *%rax

x86 assembly (clang, -O2):
<foo>:
push   %rax
mov    $0xffffffff,%eax
cmp    $0x2,%edi
ja     116b <foo+0x3b>
mov    %edi,%eax
lea    0x2cbc(%rip),%rcx
jmp    *(%rcx,%rax,8)
xor    %eax,%eax
pop    %rcx
jmp    1040 <bar@plt>
xor    %eax,%eax
call   1030 <baz@plt>
add    $0x1,%eax
pop    %rcx
ret
xor    %eax,%eax
call   1050 <spaz@plt>
mov    %eax,%ecx
shr    $0x1f,%eax
add    %ecx,%eax
sar    %eax
pop    %rcx
ret

LLVM IR (-O0):
@foo.labptr = internal global [3 x i8*] [i8* blockaddress(@foo, %15), i8* blockaddress(@foo, %18), i8* blockaddress(@foo, %22)]
define i32 @foo(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %3
%4 = load i32, i32* %3
%5 = icmp slt i32 %4, 0
br i1 %5, label %9, label %6
6: ; preds = %1
%7 = load i32, i32* %3
%8 = icmp sgt i32 %7, 2
br i1 %8, label %9, label %10
9: ; preds = %6, %1
store i32 -1, i32* %2
br label %26
10: ; preds = %6
%11 = load i32, i32* %3
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds [3 x i8*], [3 x i8*]* @foo.labptr, i64 0, i64 %12
%14 = load i8*, i8** %13
br label %28
15: ; preds = %28
%16 = load i32, i32* %3
%17 = call i32 (i32, ...) bitcast (i32 (...)* @bar to i32 (i32, ...)*)(i32 %16)
store i32 %17, i32* %2
br label %26
18: ; preds = %28
%19 = load i32, i32* %3
%20 = call i32 (i32, ...) bitcast (i32 (...)* @baz to i32 (i32, ...)*)(i32 %19)
%21 = add nsw i32 %20, 1
store i32 %21, i32* %2
br label %26
22: ; preds = %28
%23 = load i32, i32* %3
%24 = call i32 (i32, ...) bitcast (i32 (...)* @spaz to i32 (i32, ...)*)(i32 %23)
%25 = sdiv i32 %24, 2
store i32 %25, i32* %2
br label %26
26: ; preds = %22, %18, %15, %9
%27 = load i32, i32* %2
ret i32 %27
28: ; preds = %10
%29 = phi i8* [ %14, %10 ]
indirectbr i8* %29, [label %15, label %18, label %22]
}

LLVM IR (-O2):
@foo.labptr = internal unnamed_addr constant [3 x i8*] [i8* blockaddress(@foo, %7), i8* blockaddress(@foo, %9), i8* blockaddress(@foo, %12)]
define i32 @foo(i32 %0) {
%2 = icmp ugt i32 %0, 2
br i1 %2, label %15, label %3
3: ; preds = %1
%4 = zext i32 %0 to i64
%5 = getelementptr inbounds [3 x i8*], [3 x i8*]* @foo.labptr, i64 0, i64 %4
%6 = load i8*, i8** %5
indirectbr i8* %6, [label %7, label %9, label %12]
7: ; preds = %3
%8 = tail call i32 (i32, ...) bitcast (i32 (...)* @bar to i32 (i32, ...)*)(i32 %0)
br label %15
9: ; preds = %3
%10 = tail call i32 (i32, ...) bitcast (i32 (...)* @baz to i32 (i32, ...)*)(i32 %0)
%11 = add nsw i32 %10, 1
br label %15
12: ; preds = %3
%13 = tail call i32 (i32, ...) bitcast (i32 (...)* @spaz to i32 (i32, ...)*)(i32 %0)
%14 = sdiv i32 %13, 2
br label %15
15: ; preds = %1, %12, %9, %7
%16 = phi i32 [ %8, %7 ], [ %14, %12 ], [ %11, %9 ], [ -1, %1 ]
ret i32 %16
}