#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 *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(int *);
long get_run_sum(char, int, int, int *, size_t);
int /*<<< orphan*/ power(int, int);
int *primes_under(int /*<<< orphan*/ *, scalar_t__, size_t *);
int /*<<< orphan*/ printf(char *, long);
scalar_t__ sqrt(int /*<<< orphan*/);
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 < 1)
    return 1;
  size_t primes_count = 0;
  int *primes = primes_under(NULL, sqrt(power(10, N)) + 1, &primes_count);
  long sum = 0;
  for (char run_digit = '0'; run_digit <= '9'; run_digit++)
    for (int run_len = N; run_len >= 1; run_len--) {
      long run_sum = get_run_sum(run_digit, run_len, N, primes, primes_count);
      if (run_sum > 0) {
        sum += run_sum;
        break;
      }
    }
  free(primes);
  printf("%ld\n", sum);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11c8 <main+0x48>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe49(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c9 <main+0x149>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jge    11ee <main+0x6e>
movl   $0x1,-0x4(%rbp)
jmp    12c9 <main+0x149>
movq   $0x0,-0x20(%rbp)
mov    -0x14(%rbp),%esi
mov    $0xa,%edi
call   1040 <power@plt>
mov    %eax,%edi
call   10a0 <sqrt@plt>
mov    %rax,%rsi
add    $0x1,%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    -0x20(%rbp),%rdx
call   1050 <primes_under@plt>
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movb   $0x30,-0x31(%rbp)
movsbl -0x31(%rbp),%eax
cmp    $0x39,%eax
jg     12a9 <main+0x129>
mov    -0x14(%rbp),%eax
mov    %eax,-0x38(%rbp)
cmpl   $0x1,-0x38(%rbp)
jl     1297 <main+0x117>
mov    -0x31(%rbp),%al
mov    -0x38(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%r8
movsbl %al,%edi
call   1070 <get_run_sum@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jle    1284 <main+0x104>
mov    -0x40(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1297 <main+0x117>
jmp    1289 <main+0x109>
mov    -0x38(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x38(%rbp)
jmp    1241 <main+0xc1>
jmp    129c <main+0x11c>
mov    -0x31(%rbp),%al
add    $0x1,%al
mov    %al,-0x31(%rbp)
jmp    122e <main+0xae>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rsi
lea    0xd52(%rip),%rdi
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
cmp    $0x2,%edi
jne    1216 <main+0x96>
mov    0x8(%rsi),%rdi
call   1090 <atoi@plt>
test   %eax,%eax
jle    122e <main+0xae>
mov    %eax,%r15d
movq   $0x0,0x8(%rsp)
mov    $0xa,%edi
mov    %eax,%esi
call   1040 <power@plt>
mov    %eax,%edi
call   10a0 <sqrt@plt>
lea    0x1(%rax),%rsi
xor    %r14d,%r14d
lea    0x8(%rsp),%rdx
xor    %edi,%edi
call   1050 <primes_under@plt>
mov    %rax,%r12
mov    $0x30,%bl
jmp    11eb <main+0x6b>
nop
add    %rax,%r14
add    $0x1,%bl
cmp    $0x3a,%bl
je     1235 <main+0xb5>
mov    %r15d,%ebp
xchg   %ax,%ax
mov    0x8(%rsp),%r8
movsbl %bl,%edi
mov    %ebp,%esi
mov    %r15d,%edx
mov    %r12,%rcx
call   1070 <get_run_sum@plt>
test   %rax,%rax
jg     11e0 <main+0x60>
lea    -0x1(%rbp),%eax
cmp    $0x2,%ebp
mov    %eax,%ebp
jge    11f0 <main+0x70>
jmp    11e3 <main+0x63>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xdd7(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%eax
jmp    124e <main+0xce>
mov    %r12,%rdi
call   1030 <free@plt>
lea    0xdcb(%rip),%rdi
mov    %r14,%rsi
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
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 = [5 x i8] c"%ld\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i64
%10 = alloca i8
%11 = alloca i32
%12 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 2
br i1 %14, label %15, label %21
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %19)
store i32 1, i32* %3
br label %70
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32 @atoi(i8* %24)
store i32 %25, i32* %6
%26 = load i32, i32* %6
%27 = icmp slt i32 %26, 1
br i1 %27, label %28, label %29
28: ; preds = %21
store i32 1, i32* %3
br label %70
29: ; preds = %21
store i64 0, i64* %7
%30 = load i32, i32* %6
%31 = call i32 @power(i32 10, i32 %30)
%32 = call i64 @sqrt(i32 %31)
%33 = add nsw i64 %32, 1
%34 = call i32* @primes_under(i32* null, i64 %33, i64* %7)
store i32* %34, i32** %8
store i64 0, i64* %9
store i8 48, i8* %10
br label %35
35: ; preds = %62, %29
%36 = load i8, i8* %10
%37 = sext i8 %36 to i32
%38 = icmp sle i32 %37, 57
br i1 %38, label %39, label %65
39: ; preds = %35
%40 = load i32, i32* %6
store i32 %40, i32* %11
br label %41
41: ; preds = %58, %39
%42 = load i32, i32* %11
%43 = icmp sge i32 %42, 1
br i1 %43, label %44, label %61
44: ; preds = %41
%45 = load i8, i8* %10
%46 = load i32, i32* %11
%47 = load i32, i32* %6
%48 = load i32*, i32** %8
%49 = load i64, i64* %7
%50 = call i64 @get_run_sum(i8 signext %45, i32 %46, i32 %47, i32* %48, i64 %49)
store i64 %50, i64* %12
%51 = load i64, i64* %12
%52 = icmp sgt i64 %51, 0
br i1 %52, label %53, label %57
53: ; preds = %44
%54 = load i64, i64* %12
%55 = load i64, i64* %9
%56 = add nsw i64 %55, %54
store i64 %56, i64* %9
br label %61
57: ; preds = %44
br label %58
58: ; preds = %57
%59 = load i32, i32* %11
%60 = add nsw i32 %59, -1
store i32 %60, i32* %11
br label %41
61: ; preds = %53, %41
br label %62
62: ; preds = %61
%63 = load i8, i8* %10
%64 = add i8 %63, 1
store i8 %64, i8* %10
br label %35
65: ; preds = %35
%66 = load i32*, i32** %8
%67 = call i32 @free(i32* %66)
%68 = load i64, i64* %9
%69 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64 %68)
store i32 0, i32* %3
br label %70
70: ; preds = %65, %28, %15
%71 = load i32, i32* %3
ret i32 %71
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [15 x i8] c"usage: %s <N>\0A\00"
@.str.1 = [5 x i8] c"%ld\0A\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 %40
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, 1
br i1 %13, label %40, label %14
14: ; preds = %9
%15 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%16 = tail call i32 @power(i32 10, i32 %12)
%17 = tail call i64 @sqrt(i32 %16)
%18 = add nsw i64 %17, 1
%19 = call i32* @primes_under(i32* null, i64 %18, i64* %3)
br label %20
20: ; preds = %36, %14
%21 = phi i8 [ 48, %14 ], [ %38, %36 ]
%22 = phi i64 [ 0, %14 ], [ %37, %36 ]
br label %29
23: ; preds = %36
%24 = call i32 @free(i32* %19)
%25 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64 %37)
br label %40
26: ; preds = %29
%27 = add nsw i32 %30, -1
%28 = icmp sgt i32 %30, 1
br i1 %28, label %29, label %36
29: ; preds = %20, %26
%30 = phi i32 [ %27, %26 ], [ %12, %20 ]
%31 = load i64, i64* %3
%32 = call i64 @get_run_sum(i8 signext %21, i32 %30, i32 %12, i32* %19, i64 %31)
%33 = icmp sgt i64 %32, 0
br i1 %33, label %34, label %26
34: ; preds = %29
%35 = add nsw i64 %32, %22
br label %36
36: ; preds = %26, %34
%37 = phi i64 [ %35, %34 ], [ %22, %26 ]
%38 = add nuw nsw i8 %21, 1
%39 = icmp eq i8 %38, 58
br i1 %39, label %23, label %20
40: ; preds = %23, %9, %5
%41 = phi i32 [ 1, %5 ], [ 0, %23 ], [ 1, %9 ]
ret i32 %41
}