#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 */
int atoi(char *);
size_t find_largest_prime(int *, size_t, size_t, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(int *);
int *primes_under(int /*<<< orphan*/ *, int, size_t *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  if (argc != 2) {
    fprintf(stderr, "usage: %s <N>\n", argv[0]);
    return 1;
  }
  int N = atoi(argv[1]);
  if (N <= 4) {
    printf("0\n");
    return 0;
  }
  int max_factor = (N - 1) / 2;
  size_t primes_count = 0;
  int *primes = primes_under(NULL, max_factor + 1, &primes_count);
  int count = 0;
  for (size_t i = 0; i < primes_count; i++) {
    int largest_factor = (N - 1) / primes[i];
    if (largest_factor < primes[i])
      break;
    size_t pi = find_largest_prime(primes, i, primes_count - 1, largest_factor);
    count += pi - i + 1;
  }
  printf("%d\n", count);
  free(primes);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11a8 <main+0x48>
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe69(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12bb <main+0x15b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x4,-0x14(%rbp)
jg     11dc <main+0x7c>
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12bb <main+0x15b>
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%esi
add    $0x1,%esi
xor    %eax,%eax
mov    %eax,%edi
lea    -0x20(%rbp),%rdx
call   1040 <primes_under@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    129a <main+0x13a>
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    -0x28(%rbp),%rcx
mov    -0x38(%rbp),%rsi
cltd
idivl  (%rcx,%rsi,4)
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    -0x28(%rbp),%rcx
mov    -0x38(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
jge    1257 <main+0xf7>
jmp    129a <main+0x13a>
mov    -0x28(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    $0x1,%rdx
mov    -0x3c(%rbp),%ecx
call   1050 <find_largest_prime@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rcx
sub    -0x38(%rbp),%rcx
add    $0x1,%rcx
movslq -0x2c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    121b <main+0xbb>
mov    -0x2c(%rbp),%esi
lea    0xd6e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    11a2 <main+0x32>
mov    0x8(%rsi),%rdi
call   1090 <atoi@plt>
cmp    $0x4,%eax
jg     11c5 <main+0x55>
lea    0xe7e(%rip),%rdi
call   1040 <puts@plt>
xor    %r14d,%r14d
jmp    126e <main+0xfe>
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xe4b(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%r14d
jmp    126e <main+0xfe>
mov    %eax,%r15d
add    $0xffffffff,%r15d
mov    %r15d,%esi
shr    %esi
movq   $0x0,(%rsp)
add    $0x1,%esi
xor    %r14d,%r14d
mov    %rsp,%rdx
xor    %edi,%edi
call   1050 <primes_under@plt>
mov    %rax,%r12
mov    (%rsp),%rcx
mov    $0x0,%r13d
test   %rcx,%rcx
je     1255 <main+0xe5>
xor    %ebx,%ebx
mov    $0x2,%ebp
xor    %edx,%edx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %edx,%r13d
mov    (%r12,%rbx,4),%esi
mov    %r15d,%eax
cltd
idiv   %esi
cmp    %esi,%eax
jl     1255 <main+0xe5>
add    $0xffffffffffffffff,%rcx
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %rcx,%rdx
mov    %eax,%ecx
call   1060 <find_largest_prime@plt>
lea    (%rax,%r13,1),%ecx
lea    (%rcx,%rbp,1),%edx
add    $0xffffffff,%edx
add    $0x1,%rbx
mov    (%rsp),%rcx
add    $0xffffffff,%ebp
cmp    %rcx,%rbx
jb     1210 <main+0xa0>
add    %eax,%r13d
add    %ebp,%r13d
lea    0xdb3(%rip),%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [15 x i8] c"usage: %s <N>\0A\00"
@.str.1 = [3 x i8] c"0\0A\00"
@.str.2 = [4 x i8] c"%d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i64
%12 = alloca i32
%13 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = icmp ne i32 %14, 2
br i1 %15, label %16, label %22
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %20)
store i32 1, i32* %3
br label %80
22: ; preds = %2
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = call i32 @atoi(i8* %25)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = icmp sle i32 %27, 4
br i1 %28, label %29, label %31
29: ; preds = %22
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %80
31: ; preds = %22
%32 = load i32, i32* %6
%33 = sub nsw i32 %32, 1
%34 = sdiv i32 %33, 2
store i32 %34, i32* %7
store i64 0, i64* %8
%35 = load i32, i32* %7
%36 = add nsw i32 %35, 1
%37 = call i32* @primes_under(i32* null, i32 %36, i64* %8)
store i32* %37, i32** %9
store i32 0, i32* %10
store i64 0, i64* %11
br label %38
38: ; preds = %72, %31
%39 = load i64, i64* %11
%40 = load i64, i64* %8
%41 = icmp ult i64 %39, %40
br i1 %41, label %42, label %75
42: ; preds = %38
%43 = load i32, i32* %6
%44 = sub nsw i32 %43, 1
%45 = load i32*, i32** %9
%46 = load i64, i64* %11
%47 = getelementptr inbounds i32, i32* %45, i64 %46
%48 = load i32, i32* %47
%49 = sdiv i32 %44, %48
store i32 %49, i32* %12
%50 = load i32, i32* %12
%51 = load i32*, i32** %9
%52 = load i64, i64* %11
%53 = getelementptr inbounds i32, i32* %51, i64 %52
%54 = load i32, i32* %53
%55 = icmp slt i32 %50, %54
br i1 %55, label %56, label %57
56: ; preds = %42
br label %75
57: ; preds = %42
%58 = load i32*, i32** %9
%59 = load i64, i64* %11
%60 = load i64, i64* %8
%61 = sub i64 %60, 1
%62 = load i32, i32* %12
%63 = call i64 @find_largest_prime(i32* %58, i64 %59, i64 %61, i32 %62)
store i64 %63, i64* %13
%64 = load i64, i64* %13
%65 = load i64, i64* %11
%66 = sub i64 %64, %65
%67 = add i64 %66, 1
%68 = load i32, i32* %10
%69 = sext i32 %68 to i64
%70 = add i64 %69, %67
%71 = trunc i64 %70 to i32
store i32 %71, i32* %10
br label %72
72: ; preds = %57
%73 = load i64, i64* %11
%74 = add i64 %73, 1
store i64 %74, i64* %11
br label %38
75: ; preds = %56, %38
%76 = load i32, i32* %10
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %76)
%78 = load i32*, i32** %9
%79 = call i32 @free(i32* %78)
store i32 0, i32* %3
br label %80
80: ; preds = %75, %29, %16
%81 = load i32, i32* %3
ret i32 %81
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [15 x i8] c"usage: %s <N>\0A\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@str = [2 x i8] c"0\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i64
%4 = icmp eq i32 %0, 2
br i1 %4, label %9, label %5
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = load i8*, i8** %1
%8 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %7)
br label %46
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32 @atoi(i8* %11)
%13 = icmp slt i32 %12, 5
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str, i64 0, i64 0))
br label %46
16: ; preds = %9
%17 = add nsw i32 %12, -1
%18 = lshr i32 %17, 1
%19 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%20 = add nuw nsw i32 %18, 1
%21 = call i32* @primes_under(i32* null, i32 %20, i64* %3)
%22 = load i64, i64* %3
%23 = icmp eq i64 %22, 0
br i1 %23, label %42, label %24
24: ; preds = %16, %32
%25 = phi i64 [ %40, %32 ], [ %22, %16 ]
%26 = phi i64 [ %39, %32 ], [ 0, %16 ]
%27 = phi i32 [ %38, %32 ], [ 0, %16 ]
%28 = getelementptr inbounds i32, i32* %21, i64 %26
%29 = load i32, i32* %28
%30 = sdiv i32 %17, %29
%31 = icmp slt i32 %30, %29
br i1 %31, label %42, label %32
32: ; preds = %24
%33 = add i64 %25, -1
%34 = call i64 @find_largest_prime(i32* %21, i64 %26, i64 %33, i32 %30)
%35 = sub i64 %34, %26
%36 = trunc i64 %35 to i32
%37 = add i32 %27, 1
%38 = add i32 %37, %36
%39 = add nuw i64 %26, 1
%40 = load i64, i64* %3
%41 = icmp ult i64 %39, %40
br i1 %41, label %24, label %42
42: ; preds = %32, %24, %16
%43 = phi i32 [ 0, %16 ], [ %27, %24 ], [ %38, %32 ]
%44 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %43)
%45 = call i32 @free(i32* %21)
br label %46
46: ; preds = %14, %42, %5
%47 = phi i32 [ 1, %5 ], [ 0, %42 ], [ 0, %14 ]
ret i32 %47
}