#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 */
typedef int /*<<< orphan*/ unhappy_seq;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ EOF;
int /*<<< orphan*/ *fopen(char const *, char *);
scalar_t__ fscanf(int /*<<< orphan*/ *, char *, int *);
scalar_t__ is_value_in_array(int, int *, int);
int /*<<< orphan*/ printf(char *);
int sum_sqr_digits(int);
int main(int argc, const char *argv[]) {
  FILE *in_file = fopen(argv[1], "r");
  int unhappy_seq[] = {4, 16, 37, 58, 89, 145, 42, 20};
  int unhappy_seq_size = sizeof(unhappy_seq) / sizeof(unhappy_seq[0]);
  int num, sqr_sum;
  while (fscanf(in_file, "%d\n", &num) != EOF) {
    ;
    sqr_sum = sum_sqr_digits(num);
    while (true) {
      if (sqr_sum == 1) {
        printf("1\n");
        break;
      } else if (is_value_in_array(sqr_sum, unhappy_seq, unhappy_seq_size)) {
        printf("0\n");
        break;
      } else {
        sqr_sum = sum_sqr_digits(sqr_sum);
      }
    }
  }
  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)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe8b(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    0xe8b(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xe88(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0xe85(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe82(%rip),%rax
mov    %rax,-0x28(%rbp)
movl   $0x8,-0x44(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe46(%rip),%rsi
lea    -0x48(%rbp),%rdx
call   1050 <fscanf@plt>
mov    0x2e1c(%rip),%rcx
cmp    (%rcx),%rax
je     123f <main+0xef>
mov    -0x48(%rbp),%edi
call   1070 <sum_sqr_digits@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x1,-0x4c(%rbp)
jne    11fb <main+0xab>
lea    0xe15(%rip),%rdi
call   1040 <printf@plt>
jmp    123a <main+0xea>
mov    -0x4c(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    -0x44(%rbp),%edx
call   1030 <is_value_in_array@plt>
cmp    $0x0,%rax
je     1225 <main+0xd5>
lea    0xdee(%rip),%rdi
call   1040 <printf@plt>
jmp    123a <main+0xea>
mov    -0x4c(%rbp),%edi
call   1070 <sum_sqr_digits@plt>
mov    %eax,-0x4c(%rbp)
jmp    1235 <main+0xe5>
jmp    11e0 <main+0x90>
jmp    11b1 <main+0x61>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    0x8(%rsi),%rdi
lea    0xe97(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%r12
movaps 0xea8(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xe8c(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
lea    0xe72(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rax,%rdi
call   1050 <fscanf@plt>
mov    0x2e44(%rip),%rcx
cmp    (%rcx),%rax
jne    11ba <main+0x6a>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe47(%rip),%r13
lea    0xe3a(%rip),%r14
lea    0xc(%rsp),%r15
lea    0x10(%rsp),%rbx
jmp    1206 <main+0xb6>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
lea    0xe1f(%rip),%rdi
call   1040 <puts@plt>
mov    %r12,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1050 <fscanf@plt>
mov    0x2de7(%rip),%rcx
cmp    (%rcx),%rax
je     11a9 <main+0x59>
mov    0xc(%rsp),%edi
call   1070 <sum_sqr_digits@plt>
mov    %r13,%rdi
cmp    $0x1,%eax
je     11e7 <main+0x97>
mov    %eax,%ebp
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x8,%edx
call   1030 <is_value_in_array@plt>
test   %rax,%rax
jne    11e0 <main+0x90>
mov    %ebp,%edi
call   1070 <sum_sqr_digits@plt>
mov    %eax,%ebp
cmp    $0x1,%eax
jne    1220 <main+0xd0>
mov    %r13,%rdi
jmp    11e7 <main+0x97>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@__const.main.unhappy_seq = [8 x i32] [i32 4, i32 16, i32 37, i32 58, i32 89, i32 145, i32 42, i32 20]
@.str.1 = [4 x i8] c"%d\0A\00"
@EOF = global i64 0
@.str.2 = [3 x i8] c"1\0A\00"
@.str.3 = [3 x i8] c"0\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca [8 x i32]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 1
%13 = load i8*, i8** %12
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %6
%15 = bitcast [8 x i32]* %7 to i8*
store i32 8, i32* %8
br label %16
16: ; preds = %42, %2
%17 = load i32*, i32** %6
%18 = call i64 @fscanf(i32* %17, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32* %9)
%19 = load i64, i64* @EOF
%20 = icmp ne i64 %18, %19
br i1 %20, label %21, label %43
21: ; preds = %16
%22 = load i32, i32* %9
%23 = call i32 @sum_sqr_digits(i32 %22)
store i32 %23, i32* %10
br label %24
24: ; preds = %21, %41
%25 = load i32, i32* %10
%26 = icmp eq i32 %25, 1
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %42
29: ; preds = %24
%30 = load i32, i32* %10
%31 = getelementptr inbounds [8 x i32], [8 x i32]* %7, i64 0, i64 0
%32 = load i32, i32* %8
%33 = call i64 @is_value_in_array(i32 %30, i32* %31, i32 %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %29
%36 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
br label %42
37: ; preds = %29
%38 = load i32, i32* %10
%39 = call i32 @sum_sqr_digits(i32 %38)
store i32 %39, i32* %10
br label %40
40: ; preds = %37
br label %41
41: ; preds = %40
br label %24
42: ; preds = %35, %27
br label %16
43: ; preds = %16
ret i32 0
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@__const.main.unhappy_seq = [8 x i32] [i32 4, i32 16, i32 37, i32 58, i32 89, i32 145, i32 42, i32 20]
@.str.1 = [4 x i8] c"%d\0A\00"
@EOF = global i64 0
@str = [2 x i8] c"0\00"
@str.4 = [2 x i8] c"1\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [8 x i32]
%4 = alloca i32
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = bitcast [8 x i32]* %3 to i8*
%9 = bitcast i32* %4 to i8*
%10 = call i64 @fscanf(i32* %7, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32* %4)
%11 = load i64, i64* @EOF
%12 = icmp eq i64 %10, %11
br i1 %12, label %32, label %13
13: ; preds = %2
%14 = getelementptr inbounds [8 x i32], [8 x i32]* %3, i64 0, i64 0
br label %15
15: ; preds = %13, %26
%16 = load i32, i32* %4
%17 = call i32 @sum_sqr_digits(i32 %16)
%18 = icmp eq i32 %17, 1
br i1 %18, label %26, label %19
19: ; preds = %15, %23
%20 = phi i32 [ %24, %23 ], [ %17, %15 ]
%21 = call i64 @is_value_in_array(i32 %20, i32* %14, i32 8)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = call i32 @sum_sqr_digits(i32 %20)
%25 = icmp eq i32 %24, 1
br i1 %25, label %26, label %19
26: ; preds = %19, %23, %15
%27 = phi i8* [ getelementptr inbounds ([2 x i8], [2 x i8]* @str.4, i64 0, i64 0), %15 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @str.4, i64 0, i64 0), %23 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @str, i64 0, i64 0), %19 ]
%28 = call i32 @puts(i8* dereferenceable(1) %27)
%29 = call i64 @fscanf(i32* %7, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32* %4)
%30 = load i64, i64* @EOF
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %15
32: ; preds = %26, %2
ret i32 0
}