void foo(unsigned int v) {
  const char *s;
  switch (v) {
  case 0:
    s = "foo";
    break;
  case 1:
    s = "bar";
    break;
  case 2:
    s = "spam";
    break;
  default:
    for (int i = 0; i < v; i++)
      __builtin_printf("baz\n");
    return;
  }
  __builtin_printf("%s\n", s);
}


x86 assembly (clang, -O0):
<foo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x18(%rbp)
test   %eax,%eax
je     1150 <foo+0x40>
jmp    112e <foo+0x1e>
mov    -0x18(%rbp),%eax
sub    $0x1,%eax
je     1160 <foo+0x50>
jmp    113f <foo+0x2f>
mov    -0x18(%rbp),%eax
sub    $0x2,%eax
je     1170 <foo+0x60>
jmp    1180 <foo+0x70>
lea    0xea9(%rip),%rax
mov    %rax,-0x10(%rbp)
jmp    11b4 <foo+0xa4>
lea    0xe9d(%rip),%rax
mov    %rax,-0x10(%rbp)
jmp    11b4 <foo+0xa4>
lea    0xe91(%rip),%rax
mov    %rax,-0x10(%rbp)
jmp    11b4 <foo+0xa4>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x4(%rbp),%eax
jae    11af <foo+0x9f>
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1187 <foo+0x77>
jmp    11c6 <foo+0xb6>
mov    -0x10(%rbp),%rsi
lea    0xe53(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<foo>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
cmp    $0x3,%edi
jae    1138 <foo+0x28>
movslq %ebx,%rax
lea    0xeef(%rip),%rcx
movslq (%rcx,%rax,4),%rdi
add    %rcx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <puts@plt>
lea    0xece(%rip),%r14
nop
mov    %r14,%rdi
call   1030 <puts@plt>
add    $0xffffffff,%ebx
jne    1140 <foo+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [4 x i8] c"foo\00"
@.str.1 = [4 x i8] c"bar\00"
@.str.2 = [5 x i8] c"spam\00"
@.str.3 = [5 x i8] c"baz\0A\00"
@.str.4 = [4 x i8] c"%s\0A\00"
define void @foo(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i32 %0, i32* %2
%5 = load i32, i32* %2
switch i32 %5, label %9 [
i32 0, label %6
i32 1, label %7
i32 2, label %8
]
6: ; preds = %1
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8** %3
br label %20
7: ; preds = %1
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8** %3
br label %20
8: ; preds = %1
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8** %3
br label %20
9: ; preds = %1
store i32 0, i32* %4
br label %10
10: ; preds = %16, %9
%11 = load i32, i32* %4
%12 = load i32, i32* %2
%13 = icmp ult i32 %11, %12
br i1 %13, label %14, label %19
14: ; preds = %10
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %16
16: ; preds = %14
%17 = load i32, i32* %4
%18 = add nsw i32 %17, 1
store i32 %18, i32* %4
br label %10
19: ; preds = %10
br label %23
20: ; preds = %8, %7, %6
%21 = load i8*, i8** %3
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %21)
br label %23
23: ; preds = %20, %19
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c"foo\00"
@.str.1 = [4 x i8] c"bar\00"
@.str.2 = [5 x i8] c"spam\00"
@str = [4 x i8] c"baz\00"
@reltable.foo = [3 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([4 x i8]* @.str to i64), i64 ptrtoint ([3 x i32]* @reltable.foo to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([4 x i8]* @.str.1 to i64), i64 ptrtoint ([3 x i32]* @reltable.foo to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([5 x i8]* @.str.2 to i64), i64 ptrtoint ([3 x i32]* @reltable.foo to i64)) to i32)]
define void @foo(i32 %0) {
%2 = icmp ult i32 %0, 3
br i1 %2, label %8, label %3
3: ; preds = %1, %3
%4 = phi i32 [ %6, %3 ], [ 0, %1 ]
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @str, i64 0, i64 0))
%6 = add nuw i32 %4, 1
%7 = icmp eq i32 %6, %0
br i1 %7, label %13, label %3
8: ; preds = %1
%9 = sext i32 %0 to i64
%10 = shl i64 %9, 2
%11 = call i8* @llvm.load.relative.i64(i8* bitcast ([3 x i32]* @reltable.foo to i8*), i64 %10)
%12 = tail call i32 @puts(i8* dereferenceable(1) %11)
br label %13
13: ; preds = %3, %8
ret void
}