#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 */
long atol(char *);
int /*<<< orphan*/ fill_candidate(char *, char, size_t, int *, size_t);
scalar_t__ is_prime_long(long, int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int const *, size_t);
scalar_t__ next_number_non_decreasing(int *, size_t, int);
scalar_t__ next_permutation(char *);
long get_run_sum(char run_digit, size_t run_len, int N, const int *primes,
                 size_t primes_count) {
  long sum = 0;
  char candidate[N + 1];
  candidate[N] = '\0';
  size_t rest_len = N - run_len;
  int rest_digits[rest_len];
  for (size_t i = 0; i < rest_len; i++)
    rest_digits[i] = 0;
  do {
    fill_candidate(candidate, run_digit, run_len, rest_digits, rest_len);
    do {
      if (candidate[0] == '0')
        continue;
      long candidate_num = atol(candidate);
      if (is_prime_long(candidate_num, NULL, 0, primes, primes_count))
        sum += candidate_num;
    } while (next_permutation(candidate));
  } while (next_number_non_decreasing(rest_digits, rest_len, 10));
  return sum;
}


x86 assembly (clang, -O0):
<get_run_sum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %dil,%al
mov    %al,-0x1(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x70(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
movslq -0x14(%rbp),%rax
movb   $0x0,(%rax,%rcx,1)
movslq -0x14(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x68(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x50(%rbp)
movq   $0x0,-0x58(%rbp)
mov    -0x58(%rbp),%rax
cmp    -0x48(%rbp),%rax
jae    120c <get_run_sum+0xbc>
mov    -0x68(%rbp),%rax
mov    -0x58(%rbp),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x58(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x58(%rbp)
jmp    11de <get_run_sum+0x8e>
jmp    1211 <get_run_sum+0xc1>
mov    -0x68(%rbp),%rcx
mov    -0x70(%rbp),%rdi
mov    -0x1(%rbp),%al
mov    -0x10(%rbp),%rdx
mov    -0x48(%rbp),%r8
movsbl %al,%esi
call   1050 <fill_candidate@plt>
mov    -0x70(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x30,%eax
jne    1241 <get_run_sum+0xf1>
jmp    1280 <get_run_sum+0x130>
mov    -0x70(%rbp),%rdi
call   1070 <atol@plt>
mov    %rax,-0x60(%rbp)
mov    -0x60(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1040 <is_prime_long@plt>
cmp    $0x0,%rax
je     127b <get_run_sum+0x12b>
mov    -0x60(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1280 <get_run_sum+0x130>
mov    -0x70(%rbp),%rdi
call   1030 <next_permutation@plt>
cmp    $0x0,%rax
jne    122c <get_run_sum+0xdc>
jmp    1298 <get_run_sum+0x148>
mov    -0x68(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    $0xa,%edx
call   1060 <next_number_non_decreasing@plt>
cmp    $0x0,%rax
jne    1211 <get_run_sum+0xc1>
mov    -0x30(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_run_sum>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r8,-0x38(%rbp)
mov    %rcx,%r12
lea    0x1(%rdx),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
movslq %edx,%r15
movb   $0x0,(%r15,%rbx,1)
mov    %rsi,-0x50(%rbp)
sub    %rsi,%r15
lea    0xf(,%r15,4),%rax
and    $0xfffffffffffffff0,%rax
mov    %rsp,%r14
sub    %rax,%r14
mov    %r14,%rsp
test   %r15,%r15
je     11cd <get_run_sum+0x6d>
lea    0x0(,%r15,4),%rdx
mov    %edi,%r13d
mov    %r14,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
mov    %r13d,%edi
xor    %r13d,%r13d
movsbl %dil,%eax
mov    %eax,-0x2c(%rbp)
mov    %r15,-0x48(%rbp)
mov    %r14,-0x40(%rbp)
jmp    120d <get_run_sum+0xad>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    -0x40(%rbp),%r14
mov    %r14,%rdi
mov    -0x48(%rbp),%r15
mov    %r15,%rsi
mov    $0xa,%edx
call   1070 <next_number_non_decreasing@plt>
test   %rax,%rax
je     126b <get_run_sum+0x10b>
mov    %rbx,%rdi
mov    -0x2c(%rbp),%esi
mov    -0x50(%rbp),%rdx
mov    %r14,%rcx
mov    %r15,%r8
call   1060 <fill_candidate@plt>
mov    -0x38(%rbp),%r14
jmp    123d <get_run_sum+0xdd>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1030 <next_permutation@plt>
test   %rax,%rax
je     11f0 <get_run_sum+0x90>
cmpb   $0x30,(%rbx)
je     1230 <get_run_sum+0xd0>
mov    %rbx,%rdi
call   1080 <atol@plt>
mov    %rax,%r15
mov    %rax,%rdi
xor    %esi,%esi
xor    %edx,%edx
mov    %r12,%rcx
mov    %r14,%r8
call   1040 <is_prime_long@plt>
test   %rax,%rax
cmove  %rax,%r15
add    %r15,%r13
jmp    1230 <get_run_sum+0xd0>
mov    %r13,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i64 @get_run_sum(i8 signext %0, i64 %1, i32 %2, i32* %3, i64 %4) {
%6 = alloca i8
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i64
%11 = alloca i64
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i64
%15 = alloca i64
%16 = alloca i64
%17 = alloca i64
store i8 %0, i8* %6
store i64 %1, i64* %7
store i32 %2, i32* %8
store i32* %3, i32** %9
store i64 %4, i64* %10
store i64 0, i64* %11
%18 = load i32, i32* %8
%19 = add nsw i32 %18, 1
%20 = zext i32 %19 to i64
%21 = call i8* @llvm.stacksave()
store i8* %21, i8** %12
%22 = alloca i8, i64 %20
store i64 %20, i64* %13
%23 = load i32, i32* %8
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
store i8 0, i8* %25
%26 = load i32, i32* %8
%27 = sext i32 %26 to i64
%28 = load i64, i64* %7
%29 = sub i64 %27, %28
store i64 %29, i64* %14
%30 = load i64, i64* %14
%31 = alloca i32, i64 %30
store i64 %30, i64* %15
store i64 0, i64* %16
br label %32
32: ; preds = %39, %5
%33 = load i64, i64* %16
%34 = load i64, i64* %14
%35 = icmp ult i64 %33, %34
br i1 %35, label %36, label %42
36: ; preds = %32
%37 = load i64, i64* %16
%38 = getelementptr inbounds i32, i32* %31, i64 %37
store i32 0, i32* %38
br label %39
39: ; preds = %36
%40 = load i64, i64* %16
%41 = add i64 %40, 1
store i64 %41, i64* %16
br label %32
42: ; preds = %32
br label %43
43: ; preds = %70, %42
%44 = load i8, i8* %6
%45 = load i64, i64* %7
%46 = load i64, i64* %14
%47 = call i32 @fill_candidate(i8* %22, i8 signext %44, i64 %45, i32* %31, i64 %46)
br label %48
48: ; preds = %66, %43
%49 = getelementptr inbounds i8, i8* %22, i64 0
%50 = load i8, i8* %49
%51 = sext i8 %50 to i32
%52 = icmp eq i32 %51, 48
br i1 %52, label %53, label %54
53: ; preds = %48
br label %66
54: ; preds = %48
%55 = call i64 @atol(i8* %22)
store i64 %55, i64* %17
%56 = load i64, i64* %17
%57 = load i32*, i32** %9
%58 = load i64, i64* %10
%59 = call i64 @is_prime_long(i64 %56, i32* null, i32 0, i32* %57, i64 %58)
%60 = icmp ne i64 %59, 0
br i1 %60, label %61, label %65
61: ; preds = %54
%62 = load i64, i64* %17
%63 = load i64, i64* %11
%64 = add nsw i64 %63, %62
store i64 %64, i64* %11
br label %65
65: ; preds = %61, %54
br label %66
66: ; preds = %65, %53
%67 = call i64 @next_permutation(i8* %22)
%68 = icmp ne i64 %67, 0
br i1 %68, label %48, label %69
69: ; preds = %66
br label %70
70: ; preds = %69
%71 = load i64, i64* %14
%72 = call i64 @next_number_non_decreasing(i32* %31, i64 %71, i32 10)
%73 = icmp ne i64 %72, 0
br i1 %73, label %43, label %74
74: ; preds = %70
%75 = load i64, i64* %11
%76 = load i8*, i8** %12
ret i64 %75
}

LLVM IR (-O2):
define i64 @get_run_sum(i8 signext %0, i64 %1, i32 %2, i32* %3, i64 %4) {
%6 = add nsw i32 %2, 1
%7 = zext i32 %6 to i64
%8 = alloca i8, i64 %7
%9 = sext i32 %2 to i64
%10 = getelementptr inbounds i8, i8* %8, i64 %9
store i8 0, i8* %10
%11 = sub i64 %9, %1
%12 = alloca i32, i64 %11
%13 = icmp eq i64 %11, 0
br i1 %13, label %18, label %14
14: ; preds = %5
%15 = bitcast i32* %12 to i8*
%16 = sub i64 %9, %1
%17 = shl i64 %16, 2
br label %18
18: ; preds = %14, %5
br label %19
19: ; preds = %18, %36
%20 = phi i64 [ %33, %36 ], [ 0, %18 ]
%21 = call i32 @fill_candidate(i8* %8, i8 signext %0, i64 %1, i32* %12, i64 %11)
br label %22
22: ; preds = %32, %19
%23 = phi i64 [ %20, %19 ], [ %33, %32 ]
%24 = load i8, i8* %8
%25 = icmp eq i8 %24, 48
br i1 %25, label %32, label %26
26: ; preds = %22
%27 = call i64 @atol(i8* %8)
%28 = call i64 @is_prime_long(i64 %27, i32* null, i32 0, i32* %3, i64 %4)
%29 = icmp eq i64 %28, 0
%30 = select i1 %29, i64 0, i64 %27
%31 = add nsw i64 %30, %23
br label %32
32: ; preds = %22, %26
%33 = phi i64 [ %23, %22 ], [ %31, %26 ]
%34 = call i64 @next_permutation(i8* %8)
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %22
36: ; preds = %32
%37 = call i64 @next_number_non_decreasing(i32* %12, i64 %11, i32 10)
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %19
39: ; preds = %36
ret i64 %33
}