#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__ sqrt(double);
unsigned long NearestPrime(unsigned long n) {
  long position;
  long index;
  for (position = n;; position++) {
    for (index = 2;
         index <= (long)sqrt((double)position) && position % index != 0;
         index++)
      ;
    if (position % index != 0) {
      break;
    }
  }
  return position;
}


x86 assembly (clang, -O0):
<NearestPrime>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
movq   $0x2,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
cvtsi2sdq -0x10(%rbp),%xmm0
call   1030 <sqrt@plt>
mov    -0x28(%rbp),%rcx
mov    %rax,%rdx
xor    %eax,%eax
cmp    %rdx,%rcx
mov    %al,-0x19(%rbp)
jg     1168 <NearestPrime+0x58>
mov    -0x10(%rbp),%rax
cqto
idivq  -0x18(%rbp)
cmp    $0x0,%rdx
setne  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    1178 <NearestPrime+0x68>
jmp    118e <NearestPrime+0x7e>
jmp    117d <NearestPrime+0x6d>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    112c <NearestPrime+0x1c>
mov    -0x10(%rbp),%rax
cqto
idivq  -0x18(%rbp)
cmp    $0x0,%rdx
je     11a7 <NearestPrime+0x97>
jmp    11bd <NearestPrime+0xad>
jmp    11ac <NearestPrime+0x9c>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1124 <NearestPrime+0x14>
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<NearestPrime>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
jmp    1148 <NearestPrime+0x38>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%r15
mov    %r14,%rax
or     %r15,%rax
shr    $0x20,%rax
je     11c0 <NearestPrime+0xb0>
mov    %r14,%rax
cqto
idiv   %r15
lea    0x1(%r14),%rdi
test   %rdx,%rdx
jne    11cd <NearestPrime+0xbd>
mov    %rdi,%r14
xorps  %xmm0,%xmm0
cvtsi2sd %rdi,%xmm0
movsd  %xmm0,0x8(%rsp)
call   1030 <sqrt@plt>
mov    $0x2,%r15d
cmp    $0x2,%rax
jl     1123 <NearestPrime+0x13>
mov    $0x2,%ebx
nop
mov    %r14,%rax
or     %rbx,%rax
shr    $0x20,%rax
je     1190 <NearestPrime+0x80>
mov    %r14,%rax
cqto
idiv   %rbx
test   %rdx,%rdx
jne    119c <NearestPrime+0x8c>
jmp    1120 <NearestPrime+0x10>
nopl   0x0(%rax,%rax,1)
mov    %r14d,%eax
xor    %edx,%edx
div    %ebx
test   %rdx,%rdx
je     1120 <NearestPrime+0x10>
lea    0x1(%rbx),%r15
movsd  0x8(%rsp),%xmm0
call   1030 <sqrt@plt>
cmp    %rax,%rbx
mov    %r15,%rbx
jl     1170 <NearestPrime+0x60>
jmp    1123 <NearestPrime+0x13>
nopl   0x0(%rax,%rax,1)
mov    %r14d,%eax
xor    %edx,%edx
div    %r15d
jmp    113b <NearestPrime+0x2b>
mov    %r14,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @NearestPrime(i64 %0) {
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
store i64 %0, i64* %2
%5 = load i64, i64* %2
store i64 %5, i64* %3
br label %6
6: ; preds = %31, %1
store i64 2, i64* %4
br label %7
7: ; preds = %21, %6
%8 = load i64, i64* %4
%9 = load i64, i64* %3
%10 = sitofp i64 %9 to double
%11 = call i64 @sqrt(double %10)
%12 = icmp sle i64 %8, %11
br i1 %12, label %13, label %18
13: ; preds = %7
%14 = load i64, i64* %3
%15 = load i64, i64* %4
%16 = srem i64 %14, %15
%17 = icmp ne i64 %16, 0
br label %18
18: ; preds = %13, %7
%19 = phi i1 [ false, %7 ], [ %17, %13 ]
br i1 %19, label %20, label %24
20: ; preds = %18
br label %21
21: ; preds = %20
%22 = load i64, i64* %4
%23 = add nsw i64 %22, 1
store i64 %23, i64* %4
br label %7
24: ; preds = %18
%25 = load i64, i64* %3
%26 = load i64, i64* %4
%27 = srem i64 %25, %26
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %24
br label %34
30: ; preds = %24
br label %31
31: ; preds = %30
%32 = load i64, i64* %3
%33 = add nsw i64 %32, 1
store i64 %33, i64* %3
br label %6
34: ; preds = %29
%35 = load i64, i64* %3
ret i64 %35
}

LLVM IR (-O2):
define i64 @NearestPrime(i64 %0) {
br label %2
2: ; preds = %15, %1
%3 = phi i64 [ %0, %1 ], [ %19, %15 ]
%4 = sitofp i64 %3 to double
%5 = tail call i64 @sqrt(double %4)
%6 = icmp slt i64 %5, 2
br i1 %6, label %15, label %7
7: ; preds = %2, %11
%8 = phi i64 [ %12, %11 ], [ 2, %2 ]
%9 = srem i64 %3, %8
%10 = icmp eq i64 %9, 0
br i1 %10, label %15, label %11
11: ; preds = %7
%12 = add nuw nsw i64 %8, 1
%13 = tail call i64 @sqrt(double %4)
%14 = icmp slt i64 %8, %13
br i1 %14, label %7, label %15
15: ; preds = %7, %11, %2
%16 = phi i64 [ 2, %2 ], [ %12, %11 ], [ %8, %7 ]
%17 = srem i64 %3, %16
%18 = icmp eq i64 %17, 0
%19 = add nsw i64 %3, 1
br i1 %18, label %2, label %20
20: ; preds = %15
ret i64 %3
}