#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
scalar_t__ isPrime(unsigned short);
unsigned short getNextPrime(unsigned short n) {
  if (n > 65521) {
    return 0;
  }
  unsigned short primeGapUpperBound = 4680;
  for (unsigned short i = n; i < n + primeGapUpperBound; i++) {
    if (isPrime(i)) {
      return i;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<getNextPrime>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %di,%ax
mov    %ax,-0x4(%rbp)
movzwl -0x4(%rbp),%eax
cmp    $0xfff1,%eax
jle    1139 <getNextPrime+0x29>
movw   $0x0,-0x2(%rbp)
jmp    1199 <getNextPrime+0x89>
movw   $0x1248,-0x6(%rbp)
mov    -0x4(%rbp),%ax
mov    %ax,-0x8(%rbp)
movzwl -0x8(%rbp),%eax
movzwl -0x4(%rbp),%ecx
movzwl -0x6(%rbp),%edx
add    %edx,%ecx
cmp    %ecx,%eax
jge    1193 <getNextPrime+0x83>
movzwl -0x8(%rbp),%edi
call   1030 <isPrime@plt>
cmp    $0x0,%rax
je     117d <getNextPrime+0x6d>
mov    -0x8(%rbp),%ax
mov    %ax,-0x2(%rbp)
jmp    1199 <getNextPrime+0x89>
jmp    1182 <getNextPrime+0x72>
mov    -0x8(%rbp),%ax
add    $0x1,%ax
mov    %ax,-0x8(%rbp)
jmp    1147 <getNextPrime+0x37>
movw   $0x0,-0x2(%rbp)
movzwl -0x2(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getNextPrime>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
cmp    $0xfff1,%bx
ja     1147 <getNextPrime+0x37>
movzwl %bx,%ebp
add    $0x1248,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movzwl %bx,%edi
call   1030 <isPrime@plt>
test   %rax,%rax
jne    1149 <getNextPrime+0x39>
add    $0x1,%ebx
movzwl %bx,%eax
cmp    %eax,%ebp
ja     1130 <getNextPrime+0x20>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define zeroext i16 @getNextPrime(i16 zeroext %0) {
%2 = alloca i16
%3 = alloca i16
%4 = alloca i16
%5 = alloca i16
store i16 %0, i16* %3
%6 = load i16, i16* %3
%7 = zext i16 %6 to i32
%8 = icmp sgt i32 %7, 65521
br i1 %8, label %9, label %10
9: ; preds = %1
store i16 0, i16* %2
br label %32
10: ; preds = %1
store i16 4680, i16* %4
%11 = load i16, i16* %3
store i16 %11, i16* %5
br label %12
12: ; preds = %28, %10
%13 = load i16, i16* %5
%14 = zext i16 %13 to i32
%15 = load i16, i16* %3
%16 = zext i16 %15 to i32
%17 = load i16, i16* %4
%18 = zext i16 %17 to i32
%19 = add nsw i32 %16, %18
%20 = icmp slt i32 %14, %19
br i1 %20, label %21, label %31
21: ; preds = %12
%22 = load i16, i16* %5
%23 = call i64 @isPrime(i16 zeroext %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = load i16, i16* %5
store i16 %26, i16* %2
br label %32
27: ; preds = %21
br label %28
28: ; preds = %27
%29 = load i16, i16* %5
%30 = add i16 %29, 1
store i16 %30, i16* %5
br label %12
31: ; preds = %12
store i16 0, i16* %2
br label %32
32: ; preds = %31, %25, %9
%33 = load i16, i16* %2
ret i16 %33
}

LLVM IR (-O2):
define zeroext i16 @getNextPrime(i16 zeroext %0) {
%2 = icmp ugt i16 %0, -15
br i1 %2, label %14, label %3
3: ; preds = %1
%4 = zext i16 %0 to i32
%5 = add nuw nsw i32 %4, 4680
br label %6
6: ; preds = %3, %10
%7 = phi i16 [ %11, %10 ], [ %0, %3 ]
%8 = tail call i64 @isPrime(i16 zeroext %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %6
%11 = add i16 %7, 1
%12 = zext i16 %11 to i32
%13 = icmp ugt i32 %5, %12
br i1 %13, label %6, label %14
14: ; preds = %6, %10, %1
%15 = phi i16 [ 0, %1 ], [ %7, %6 ], [ 0, %10 ]
ret i16 %15
}