unsigned int next_prime(unsigned int num) {
  if (num == 1)
    return 2;
  else if (num == 2)
    return 3;
  else if (num == 3 || num == 4)
    return 5;
  else if (num == 5 || num == 6)
    return 7;
  else if (num >= 7 && num <= 9)
    return 1;
  return 1;
}


x86 assembly (clang, -O0):
<next_prime>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x8(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    111d <next_prime+0x1d>
movl   $0x2,-0x4(%rbp)
jmp    11ae <next_prime+0xae>
cmpl   $0x2,-0x8(%rbp)
jne    1133 <next_prime+0x33>
movl   $0x3,-0x4(%rbp)
jmp    11ae <next_prime+0xae>
cmpl   $0x3,-0x8(%rbp)
je     1147 <next_prime+0x47>
cmpl   $0x4,-0x8(%rbp)
jne    1153 <next_prime+0x53>
movl   $0x5,-0x4(%rbp)
jmp    11ae <next_prime+0xae>
cmpl   $0x5,-0x8(%rbp)
je     1167 <next_prime+0x67>
cmpl   $0x6,-0x8(%rbp)
jne    1173 <next_prime+0x73>
movl   $0x7,-0x4(%rbp)
jmp    11ae <next_prime+0xae>
cmpl   $0x7,-0x8(%rbp)
jb     1193 <next_prime+0x93>
cmpl   $0x9,-0x8(%rbp)
ja     1193 <next_prime+0x93>
movl   $0x1,-0x4(%rbp)
jmp    11ae <next_prime+0xae>
jmp    1198 <next_prime+0x98>
jmp    119d <next_prime+0x9d>
jmp    11a2 <next_prime+0xa2>
jmp    11a7 <next_prime+0xa7>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<next_prime>:
cmp    $0x1,%edi
je     1110 <next_prime+0x10>
cmp    $0x2,%edi
jne    1116 <next_prime+0x16>
mov    $0x3,%eax
ret
mov    $0x2,%eax
ret
lea    -0x3(%rdi),%ecx
mov    $0x5,%eax
cmp    $0x2,%ecx
jb     1136 <next_prime+0x36>
add    $0xfffffffb,%edi
cmp    $0x2,%edi
mov    $0x7,%ecx
mov    $0x1,%eax
cmovb  %ecx,%eax
ret

LLVM IR (-O0):
define i32 @next_prime(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %3
%4 = load i32, i32* %3
%5 = icmp eq i32 %4, 1
br i1 %5, label %6, label %7
6: ; preds = %1
store i32 2, i32* %2
br label %37
7: ; preds = %1
%8 = load i32, i32* %3
%9 = icmp eq i32 %8, 2
br i1 %9, label %10, label %11
10: ; preds = %7
store i32 3, i32* %2
br label %37
11: ; preds = %7
%12 = load i32, i32* %3
%13 = icmp eq i32 %12, 3
br i1 %13, label %17, label %14
14: ; preds = %11
%15 = load i32, i32* %3
%16 = icmp eq i32 %15, 4
br i1 %16, label %17, label %18
17: ; preds = %14, %11
store i32 5, i32* %2
br label %37
18: ; preds = %14
%19 = load i32, i32* %3
%20 = icmp eq i32 %19, 5
br i1 %20, label %24, label %21
21: ; preds = %18
%22 = load i32, i32* %3
%23 = icmp eq i32 %22, 6
br i1 %23, label %24, label %25
24: ; preds = %21, %18
store i32 7, i32* %2
br label %37
25: ; preds = %21
%26 = load i32, i32* %3
%27 = icmp uge i32 %26, 7
br i1 %27, label %28, label %32
28: ; preds = %25
%29 = load i32, i32* %3
%30 = icmp ule i32 %29, 9
br i1 %30, label %31, label %32
31: ; preds = %28
store i32 1, i32* %2
br label %37
32: ; preds = %28, %25
br label %33
33: ; preds = %32
br label %34
34: ; preds = %33
br label %35
35: ; preds = %34
br label %36
36: ; preds = %35
store i32 1, i32* %2
br label %37
37: ; preds = %36, %31, %24, %17, %10, %6
%38 = load i32, i32* %2
ret i32 %38
}

LLVM IR (-O2):
define i32 @next_prime(i32 %0) {
switch i32 %0, label %3 [
i32 1, label %10
i32 2, label %2
]
2: ; preds = %1
br label %10
3: ; preds = %1
%4 = add i32 %0, -3
%5 = icmp ult i32 %4, 2
br i1 %5, label %10, label %6
6: ; preds = %3
%7 = add i32 %0, -5
%8 = icmp ult i32 %7, 2
%9 = select i1 %8, i32 7, i32 1
br label %10
10: ; preds = %6, %3, %1, %2
%11 = phi i32 [ 3, %2 ], [ 2, %1 ], [ 5, %3 ], [ %9, %6 ]
ret i32 %11
}