#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);
int isprime(unsigned long int n) {
  unsigned long int sqrt_n, i, residue;
  sqrt_n = (unsigned long int)sqrt((double)n) + 1;
  if ((n & 0x1) == 0 || n % 3 == 0)
    return 0;
  residue = 5;
  for (i = 5; i <= sqrt_n;) {
    if (n % i == 0)
      return 0;
    if (residue == 1)
      i += 4;
    else
      i += 2;
    residue = i % 6;
  }
  return 1;
}


x86 assembly (clang, -O0):
<isprime>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movaps 0xedd(%rip),%xmm1
movq   -0x10(%rbp),%xmm0
punpckldq %xmm1,%xmm0
movapd 0xedc(%rip),%xmm1
subpd  %xmm1,%xmm0
movaps %xmm0,%xmm1
unpckhpd %xmm0,%xmm0
addsd  %xmm1,%xmm0
call   1030 <sqrt@plt>
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
and    $0x1,%rax
cmp    $0x0,%rax
je     117a <isprime+0x6a>
mov    -0x10(%rbp),%rax
mov    $0x3,%ecx
xor    %edx,%edx
div    %rcx
cmp    $0x0,%rdx
jne    1186 <isprime+0x76>
movl   $0x0,-0x4(%rbp)
jmp    120c <isprime+0xfc>
movq   $0x5,-0x28(%rbp)
movq   $0x5,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
ja     1205 <isprime+0xf5>
mov    -0x10(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x20(%rbp)
cmp    $0x0,%rdx
jne    11c6 <isprime+0xb6>
movl   $0x0,-0x4(%rbp)
jmp    120c <isprime+0xfc>
cmpq   $0x1,-0x28(%rbp)
jne    11e2 <isprime+0xd2>
mov    -0x20(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x20(%rbp)
jmp    11ee <isprime+0xde>
mov    -0x20(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    $0x6,%ecx
xor    %edx,%edx
div    %rcx
mov    %rdx,-0x28(%rbp)
jmp    1196 <isprime+0x86>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<isprime>:
push   %rbx
mov    %rdi,%rbx
movq   %rdi,%xmm1
punpckldq 0xedf(%rip),%xmm1
subpd  0xee7(%rip),%xmm1
movapd %xmm1,%xmm0
unpckhpd %xmm1,%xmm0
addsd  %xmm1,%xmm0
call   1030 <sqrt@plt>
mov    %rax,%rsi
movabs $0xaaaaaaaaaaaaaaab,%r9
aa aa aa
mov    %rbx,%rax
mul    %r9
xor    %r8d,%r8d
test   $0x1,%bl
je     11ed <isprime+0xdd>
shr    %rdx
lea    (%rdx,%rdx,2),%rax
mov    %rbx,%rcx
sub    %rax,%rcx
je     11ed <isprime+0xdd>
add    $0x1,%rsi
mov    $0x1,%r8d
cmp    $0x5,%rsi
jb     11ed <isprime+0xdd>
mov    $0x5,%edi
mov    $0x5,%ecx
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rax
or     %rcx,%rax
shr    $0x20,%rax
je     11b0 <isprime+0xa0>
mov    %rbx,%rax
xor    %edx,%edx
div    %rcx
test   %rdx,%rdx
jne    11bb <isprime+0xab>
jmp    11ea <isprime+0xda>
nopl   0x0(%rax,%rax,1)
mov    %ebx,%eax
xor    %edx,%edx
div    %ecx
test   %rdx,%rdx
je     11ea <isprime+0xda>
xor    %eax,%eax
cmp    $0x1,%rdi
sete   %al
lea    (%rcx,%rax,2),%rcx
add    $0x2,%rcx
mov    %rcx,%rax
mul    %r9
shr    %rdx
and    $0xfffffffffffffffe,%rdx
lea    (%rdx,%rdx,2),%rax
mov    %rcx,%rdi
sub    %rax,%rdi
cmp    %rsi,%rcx
jbe    1190 <isprime+0x80>
jmp    11ed <isprime+0xdd>
xor    %r8d,%r8d
mov    %r8d,%eax
pop    %rbx
ret

LLVM IR (-O0):
define i32 @isprime(i64 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
store i64 %0, i64* %3
%7 = load i64, i64* %3
%8 = uitofp i64 %7 to double
%9 = call i64 @sqrt(double %8)
%10 = add i64 %9, 1
store i64 %10, i64* %4
%11 = load i64, i64* %3
%12 = and i64 %11, 1
%13 = icmp eq i64 %12, 0
br i1 %13, label %18, label %14
14: ; preds = %1
%15 = load i64, i64* %3
%16 = urem i64 %15, 3
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %14, %1
store i32 0, i32* %2
br label %43
19: ; preds = %14
store i64 5, i64* %6
store i64 5, i64* %5
br label %20
20: ; preds = %39, %19
%21 = load i64, i64* %5
%22 = load i64, i64* %4
%23 = icmp ule i64 %21, %22
br i1 %23, label %24, label %42
24: ; preds = %20
%25 = load i64, i64* %3
%26 = load i64, i64* %5
%27 = urem i64 %25, %26
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %24
store i32 0, i32* %2
br label %43
30: ; preds = %24
%31 = load i64, i64* %6
%32 = icmp eq i64 %31, 1
br i1 %32, label %33, label %36
33: ; preds = %30
%34 = load i64, i64* %5
%35 = add i64 %34, 4
store i64 %35, i64* %5
br label %39
36: ; preds = %30
%37 = load i64, i64* %5
%38 = add i64 %37, 2
store i64 %38, i64* %5
br label %39
39: ; preds = %36, %33
%40 = load i64, i64* %5
%41 = urem i64 %40, 6
store i64 %41, i64* %6
br label %20
42: ; preds = %20
store i32 1, i32* %2
br label %43
43: ; preds = %42, %29, %18
%44 = load i32, i32* %2
ret i32 %44
}

LLVM IR (-O2):
define i32 @isprime(i64 %0) {
%2 = uitofp i64 %0 to double
%3 = tail call i64 @sqrt(double %2)
%4 = add i64 %3, 1
%5 = and i64 %0, 1
%6 = icmp eq i64 %5, 0
%7 = urem i64 %0, 3
%8 = icmp eq i64 %7, 0
%9 = or i1 %6, %8
br i1 %9, label %23, label %10
10: ; preds = %1
%11 = icmp ult i64 %4, 5
br i1 %11, label %23, label %12
12: ; preds = %10, %17
%13 = phi i64 [ %21, %17 ], [ 5, %10 ]
%14 = phi i64 [ %20, %17 ], [ 5, %10 ]
%15 = urem i64 %0, %14
%16 = icmp eq i64 %15, 0
br i1 %16, label %23, label %17
17: ; preds = %12
%18 = icmp eq i64 %13, 1
%19 = select i1 %18, i64 4, i64 2
%20 = add i64 %19, %14
%21 = urem i64 %20, 6
%22 = icmp ugt i64 %20, %4
br i1 %22, label %23, label %12
23: ; preds = %12, %17, %10, %1
%24 = phi i32 [ 0, %1 ], [ 1, %10 ], [ 0, %12 ], [ 1, %17 ]
ret i32 %24
}