#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 /*<<< orphan*/ printf(char *, int);
int square_digits(int);
int main(void) {
  int cache[567] = {0};
  int i;
  for (i = 1; i < 567; ++i) {
    int t = i;
    while (t != 89 && t != 1)
      t = square_digits(t);
    if (t == 89)
      cache[i] = 1;
  }
  int sum = 0;
  for (i = 1; i < 10000000; ++i) {
    int t = square_digits(i);
    if (cache[t])
      ++sum;
  }
  printf("%d\n", sum);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x900,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x8e0(%rbp),%rdi
xor    %esi,%esi
mov    $0x8dc,%edx
call   1040 <memset@plt>
movl   $0x1,-0x8e4(%rbp)
cmpl   $0x237,-0x8e4(%rbp)
jge    1201 <main+0xd1>
mov    -0x8e4(%rbp),%eax
mov    %eax,-0x8e8(%rbp)
xor    %eax,%eax
cmpl   $0x59,-0x8e8(%rbp)
mov    %al,-0x8f1(%rbp)
je     11a0 <main+0x70>
cmpl   $0x1,-0x8e8(%rbp)
setne  %al
mov    %al,-0x8f1(%rbp)
mov    -0x8f1(%rbp),%al
test   $0x1,%al
jne    11b3 <main+0x83>
jmp    11c9 <main+0x99>
mov    -0x8e8(%rbp),%edi
call   1050 <square_digits@plt>
mov    %eax,-0x8e8(%rbp)
jmp    117b <main+0x4b>
cmpl   $0x59,-0x8e8(%rbp)
jne    11e8 <main+0xb8>
movslq -0x8e4(%rbp),%rax
movl   $0x1,-0x8e0(%rbp,%rax,4)
jmp    11ed <main+0xbd>
mov    -0x8e4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8e4(%rbp)
jmp    115f <main+0x2f>
movl   $0x0,-0x8ec(%rbp)
movl   $0x1,-0x8e4(%rbp)
cmpl   $0x989680,-0x8e4(%rbp)
jge    1273 <main+0x143>
mov    -0x8e4(%rbp),%edi
call   1050 <square_digits@plt>
mov    %eax,-0x8f0(%rbp)
movslq -0x8f0(%rbp),%rax
cmpl   $0x0,-0x8e0(%rbp,%rax,4)
je     125a <main+0x12a>
mov    -0x8ec(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8ec(%rbp)
jmp    125f <main+0x12f>
mov    -0x8e4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8e4(%rbp)
jmp    1215 <main+0xe5>
mov    -0x8ec(%rbp),%esi
lea    0xd80(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x900,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x8e8,%rsp
mov    %rsp,%rdi
mov    $0x8dc,%edx
xor    %esi,%esi
call   1040 <memset@plt>
mov    $0x1,%ebx
jmp    1164 <main+0x34>
nop
movl   $0x1,(%rsp,%rbx,4)
add    $0x1,%rbx
cmp    $0x237,%rbx
je     1183 <main+0x53>
mov    %ebx,%eax
cmp    $0x1,%eax
je     1157 <main+0x27>
nopl   0x0(%rax,%rax,1)
cmp    $0x59,%eax
je     1150 <main+0x20>
mov    %eax,%edi
call   1050 <square_digits@plt>
cmp    $0x1,%eax
jne    1170 <main+0x40>
jmp    1157 <main+0x27>
xor    %ebx,%ebx
mov    $0x1,%ebp
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
call   1050 <square_digits@plt>
cltq
cmpl   $0x1,(%rsp,%rax,4)
sbb    $0xffffffff,%ebx
add    $0x1,%ebp
cmp    $0x989680,%ebp
jne    1190 <main+0x60>
lea    0xe4e(%rip),%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x8e8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [567 x i32]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = bitcast [567 x i32]* %2 to i8*
store i32 1, i32* %3
br label %8
8: ; preds = %32, %0
%9 = load i32, i32* %3
%10 = icmp slt i32 %9, 567
br i1 %10, label %11, label %35
11: ; preds = %8
%12 = load i32, i32* %3
store i32 %12, i32* %4
br label %13
13: ; preds = %21, %11
%14 = load i32, i32* %4
%15 = icmp ne i32 %14, 89
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = load i32, i32* %4
%18 = icmp ne i32 %17, 1
br label %19
19: ; preds = %16, %13
%20 = phi i1 [ false, %13 ], [ %18, %16 ]
br i1 %20, label %21, label %24
21: ; preds = %19
%22 = load i32, i32* %4
%23 = call i32 @square_digits(i32 %22)
store i32 %23, i32* %4
br label %13
24: ; preds = %19
%25 = load i32, i32* %4
%26 = icmp eq i32 %25, 89
br i1 %26, label %27, label %31
27: ; preds = %24
%28 = load i32, i32* %3
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [567 x i32], [567 x i32]* %2, i64 0, i64 %29
store i32 1, i32* %30
br label %31
31: ; preds = %27, %24
br label %32
32: ; preds = %31
%33 = load i32, i32* %3
%34 = add nsw i32 %33, 1
store i32 %34, i32* %3
br label %8
35: ; preds = %8
store i32 0, i32* %5
store i32 1, i32* %3
br label %36
36: ; preds = %51, %35
%37 = load i32, i32* %3
%38 = icmp slt i32 %37, 10000000
br i1 %38, label %39, label %54
39: ; preds = %36
%40 = load i32, i32* %3
%41 = call i32 @square_digits(i32 %40)
store i32 %41, i32* %6
%42 = load i32, i32* %6
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds [567 x i32], [567 x i32]* %2, i64 0, i64 %43
%45 = load i32, i32* %44
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %39
%48 = load i32, i32* %5
%49 = add nsw i32 %48, 1
store i32 %49, i32* %5
br label %50
50: ; preds = %47, %39
br label %51
51: ; preds = %50
%52 = load i32, i32* %3
%53 = add nsw i32 %52, 1
store i32 %53, i32* %3
br label %36
54: ; preds = %36
%55 = load i32, i32* %5
%56 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %55)
ret i32 0
}

LLVM IR (-O2):
@.str = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca [567 x i32]
%2 = bitcast [567 x i32]* %1 to i8*
br label %3
3: ; preds = %0, %12
%4 = phi i64 [ 1, %0 ], [ %13, %12 ]
%5 = trunc i64 %4 to i32
br label %6
6: ; preds = %3, %8
%7 = phi i32 [ %9, %8 ], [ %5, %3 ]
switch i32 %7, label %8 [
i32 89, label %10
i32 1, label %12
]
8: ; preds = %6
%9 = tail call i32 @square_digits(i32 %7)
br label %6
10: ; preds = %6
%11 = getelementptr inbounds [567 x i32], [567 x i32]* %1, i64 0, i64 %4
store i32 1, i32* %11
br label %12
12: ; preds = %6, %10
%13 = add nuw nsw i64 %4, 1
%14 = icmp eq i64 %13, 567
br i1 %14, label %15, label %3
15: ; preds = %12, %15
%16 = phi i32 [ %25, %15 ], [ 1, %12 ]
%17 = phi i32 [ %24, %15 ], [ 0, %12 ]
%18 = tail call i32 @square_digits(i32 %16)
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [567 x i32], [567 x i32]* %1, i64 0, i64 %19
%21 = load i32, i32* %20
%22 = icmp ne i32 %21, 0
%23 = zext i1 %22 to i32
%24 = add nuw nsw i32 %17, %23
%25 = add nuw nsw i32 %16, 1
%26 = icmp eq i32 %25, 10000000
br i1 %26, label %27, label %15
27: ; preds = %15
%28 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %24)
ret i32 0
}