#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 */
scalar_t__ EOF;
int check_num_table(int, int *, int);
int /*<<< orphan*/ convert_int_to_string(int, char *);
int convert_string_to_int(char *);
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ reverse_string(char *);
scalar_t__ scanf(char *, int *);
int /*<<< orphan*/ sort_string(char *);
int main() {
  int num;
  int op1, op2, count;
  int num_table[1024];
  char buf[16];
  while (scanf("%d\n", &num) != EOF && num != 0) {
    printf("Original number was %d\n", num);
    count = 0;
    num_table[count++] = num;
    while (1) {
      convert_int_to_string(num, buf);
      sort_string(buf);
      op1 = convert_string_to_int(buf);
      reverse_string(buf);
      op2 = convert_string_to_int(buf);
      num = op2 - op1;
      printf("%d - %d = %d\n", op2, op1, num);
      if (check_num_table(num, num_table, count) == 1) {
        printf("Chain length %d\n\n", count);
        break;
      } else {
        num_table[count++] = num;
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe99(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1070 <scanf@plt>
mov    %rax,%rcx
xor    %eax,%eax
mov    0x2e4a(%rip),%rdx
cmp    (%rdx),%rcx
mov    %al,-0x1031(%rbp)
je     11ba <main+0x4a>
cmpl   $0x0,-0x8(%rbp)
setne  %al
mov    %al,-0x1031(%rbp)
mov    -0x1031(%rbp),%al
test   $0x1,%al
jne    11cd <main+0x5d>
jmp    12b3 <main+0x143>
mov    -0x8(%rbp),%esi
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%ecx
mov    -0x14(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0x14(%rbp)
cltq
mov    %ecx,-0x1020(%rbp,%rax,4)
mov    -0x8(%rbp),%edi
lea    -0x1030(%rbp),%rsi
call   1060 <convert_int_to_string@plt>
lea    -0x1030(%rbp),%rdi
call   1090 <sort_string@plt>
lea    -0x1030(%rbp),%rdi
call   1080 <convert_string_to_int@plt>
mov    %eax,-0xc(%rbp)
lea    -0x1030(%rbp),%rdi
call   1030 <reverse_string@plt>
lea    -0x1030(%rbp),%rdi
call   1080 <convert_string_to_int@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
sub    -0xc(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x10(%rbp),%esi
mov    -0xc(%rbp),%edx
mov    -0x8(%rbp),%ecx
lea    0xdbe(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <check_num_table@plt>
cmp    $0x1,%eax
jne    1292 <main+0x122>
mov    -0x14(%rbp),%esi
lea    0xda0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12ae <main+0x13e>
mov    -0x8(%rbp),%ecx
mov    -0x14(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0x14(%rbp)
cltq
mov    %ecx,-0x1020(%rbp,%rax,4)
jmp    11fc <main+0x8c>
jmp    1182 <main+0x12>
xor    %eax,%eax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1028,%rsp
lea    0xe9a(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1070 <scanf@plt>
mov    0x2e4f(%rip),%rcx
cmp    (%rcx),%rax
je     1298 <main+0x128>
mov    0xc(%rsp),%esi
test   %esi,%esi
je     1298 <main+0x128>
lea    0x10(%rsp),%rbx
lea    0xe5e(%rip),%r15
lea    0x20(%rsp),%r12
lea    0xc(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
lea    0xe29(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0xc(%rsp),%edi
mov    %edi,0x20(%rsp)
mov    $0x1,%r13d
nopl   0x0(%rax)
mov    %rbx,%rsi
call   1060 <convert_int_to_string@plt>
mov    %rbx,%rdi
call   1090 <sort_string@plt>
mov    %rbx,%rdi
call   1080 <convert_string_to_int@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <reverse_string@plt>
mov    %rbx,%rdi
call   1080 <convert_string_to_int@plt>
mov    %eax,%ecx
sub    %ebp,%ecx
mov    %ecx,0xc(%rsp)
mov    %r15,%rdi
mov    %eax,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    0xc(%rsp),%edi
mov    %r12,%rsi
mov    %r13d,%edx
call   1050 <check_num_table@plt>
cmp    $0x1,%eax
je     1260 <main+0xf0>
mov    0xc(%rsp),%edi
mov    %edi,0x20(%rsp,%r13,4)
add    $0x1,%r13
jmp    11f0 <main+0x80>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    0xdbf(%rip),%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xdaa(%rip),%rdi
mov    %r14,%rsi
call   1070 <scanf@plt>
mov    0x2d61(%rip),%rcx
cmp    (%rcx),%rax
je     1298 <main+0x128>
mov    0xc(%rsp),%esi
test   %esi,%esi
jne    11d0 <main+0x60>
xor    %eax,%eax
add    $0x1028,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [4 x i8] c"%d\0A\00"
@EOF = global i64 0
@.str.1 = [24 x i8] c"Original number was %d\0A\00"
@.str.2 = [14 x i8] c"%d - %d = %d\0A\00"
@.str.3 = [18 x i8] c"Chain length %d\0A\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [1024 x i32]
%3 = alloca [16 x i8]
%4 = bitcast i32* %1 to i8*
%5 = bitcast [1024 x i32]* %2 to i8*
%6 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%7 = call i64 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %1)
%8 = load i64, i64* @EOF
%9 = icmp ne i64 %7, %8
%10 = load i32, i32* %1
%11 = icmp ne i32 %10, 0
%12 = select i1 %9, i1 %11, i1 false
br i1 %12, label %13, label %46
13: ; preds = %0
%14 = getelementptr inbounds [1024 x i32], [1024 x i32]* %2, i64 0, i64 0
br label %15
15: ; preds = %13, %33
%16 = phi i32 [ %10, %13 ], [ %39, %33 ]
%17 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%18 = load i32, i32* %1
store i32 %18, i32* %14
br label %19
19: ; preds = %42, %15
%20 = phi i32 [ %43, %42 ], [ %18, %15 ]
%21 = phi i64 [ %44, %42 ], [ 1, %15 ]
%22 = call i32 @convert_int_to_string(i32 %20, i8* %6)
%23 = call i32 @sort_string(i8* %6)
%24 = call i32 @convert_string_to_int(i8* %6)
%25 = call i32 @reverse_string(i8* %6)
%26 = call i32 @convert_string_to_int(i8* %6)
%27 = sub nsw i32 %26, %24
store i32 %27, i32* %1
%28 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %26, i32 %24, i32 %27)
%29 = load i32, i32* %1
%30 = trunc i64 %21 to i32
%31 = call i32 @check_num_table(i32 %29, i32* %14, i32 %30)
%32 = icmp eq i32 %31, 1
br i1 %32, label %33, label %42
33: ; preds = %19
%34 = trunc i64 %21 to i32
%35 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %34)
%36 = call i64 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %1)
%37 = load i64, i64* @EOF
%38 = icmp ne i64 %36, %37
%39 = load i32, i32* %1
%40 = icmp ne i32 %39, 0
%41 = select i1 %38, i1 %40, i1 false
br i1 %41, label %15, label %46
42: ; preds = %19
%43 = load i32, i32* %1
%44 = add nuw i64 %21, 1
%45 = getelementptr inbounds [1024 x i32], [1024 x i32]* %2, i64 0, i64 %21
store i32 %43, i32* %45
br label %19
46: ; preds = %33, %0
ret i32 0
}