#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 */
short asc_digits(short);
short desc_digits(short);
char kaprekar(short num) {
  if (num == 6174)
    return 0;
  short big = desc_digits(num);
  short small = asc_digits(big);
  char count = 1;
  while (big - small != 6174) {
    big = desc_digits(big - small);
    small = asc_digits(big);
    count++;
  }
  return count;
}


x86 assembly (clang, -O0):
<kaprekar>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %di,%ax
mov    %ax,-0x4(%rbp)
movswl -0x4(%rbp),%eax
cmp    $0x181e,%eax
jne    1147 <kaprekar+0x27>
movb   $0x0,-0x1(%rbp)
jmp    11b0 <kaprekar+0x90>
movswl -0x4(%rbp),%edi
call   1030 <desc_digits@plt>
mov    %ax,-0x6(%rbp)
movswl -0x6(%rbp),%edi
call   1040 <asc_digits@plt>
mov    %ax,-0x8(%rbp)
movb   $0x1,-0x9(%rbp)
movswl -0x6(%rbp),%eax
movswl -0x8(%rbp),%ecx
sub    %ecx,%eax
cmp    $0x181e,%eax
je     11aa <kaprekar+0x8a>
movswl -0x6(%rbp),%eax
movswl -0x8(%rbp),%ecx
sub    %ecx,%eax
movswl %ax,%edi
call   1030 <desc_digits@plt>
mov    %ax,-0x6(%rbp)
movswl -0x6(%rbp),%edi
call   1040 <asc_digits@plt>
mov    %ax,-0x8(%rbp)
mov    -0x9(%rbp),%al
add    $0x1,%al
mov    %al,-0x9(%rbp)
jmp    1165 <kaprekar+0x45>
mov    -0x9(%rbp),%al
mov    %al,-0x1(%rbp)
movsbl -0x1(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kaprekar>:
push   %rbp
push   %rbx
push   %rax
cmp    $0x181e,%edi
jne    112f <kaprekar+0xf>
xor    %ebx,%ebx
jmp    1170 <kaprekar+0x50>
movswl %di,%edi
call   1030 <desc_digits@plt>
movswl %ax,%ebp
mov    %ebp,%edi
call   1040 <asc_digits@plt>
cwtl
sub    %eax,%ebp
mov    $0x1,%bl
cmp    $0x181e,%ebp
je     1170 <kaprekar+0x50>
mov    $0x1,%bl
movswl %bp,%edi
call   1030 <desc_digits@plt>
movswl %ax,%ebp
mov    %ebp,%edi
call   1040 <asc_digits@plt>
add    $0x1,%bl
cwtl
sub    %eax,%ebp
cmp    $0x181e,%ebp
jne    1150 <kaprekar+0x30>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define signext i8 @kaprekar(i16 signext %0) {
%2 = alloca i8
%3 = alloca i16
%4 = alloca i16
%5 = alloca i16
%6 = alloca i8
store i16 %0, i16* %3
%7 = load i16, i16* %3
%8 = sext i16 %7 to i32
%9 = icmp eq i32 %8, 6174
br i1 %9, label %10, label %11
10: ; preds = %1
store i8 0, i8* %2
br label %37
11: ; preds = %1
%12 = load i16, i16* %3
%13 = call signext i16 @desc_digits(i16 signext %12)
store i16 %13, i16* %4
%14 = load i16, i16* %4
%15 = call signext i16 @asc_digits(i16 signext %14)
store i16 %15, i16* %5
store i8 1, i8* %6
br label %16
16: ; preds = %23, %11
%17 = load i16, i16* %4
%18 = sext i16 %17 to i32
%19 = load i16, i16* %5
%20 = sext i16 %19 to i32
%21 = sub nsw i32 %18, %20
%22 = icmp ne i32 %21, 6174
br i1 %22, label %23, label %35
23: ; preds = %16
%24 = load i16, i16* %4
%25 = sext i16 %24 to i32
%26 = load i16, i16* %5
%27 = sext i16 %26 to i32
%28 = sub nsw i32 %25, %27
%29 = trunc i32 %28 to i16
%30 = call signext i16 @desc_digits(i16 signext %29)
store i16 %30, i16* %4
%31 = load i16, i16* %4
%32 = call signext i16 @asc_digits(i16 signext %31)
store i16 %32, i16* %5
%33 = load i8, i8* %6
%34 = add i8 %33, 1
store i8 %34, i8* %6
br label %16
35: ; preds = %16
%36 = load i8, i8* %6
store i8 %36, i8* %2
br label %37
37: ; preds = %35, %10
%38 = load i8, i8* %2
ret i8 %38
}

LLVM IR (-O2):
define signext i8 @kaprekar(i16 signext %0) {
%2 = icmp eq i16 %0, 6174
br i1 %2, label %21, label %3
3: ; preds = %1
%4 = tail call signext i16 @desc_digits(i16 signext %0)
%5 = tail call signext i16 @asc_digits(i16 signext %4)
%6 = sext i16 %4 to i32
%7 = sext i16 %5 to i32
%8 = sub nsw i32 %6, %7
%9 = icmp eq i32 %8, 6174
br i1 %9, label %21, label %10
10: ; preds = %3, %10
%11 = phi i32 [ %19, %10 ], [ %8, %3 ]
%12 = phi i8 [ %16, %10 ], [ 1, %3 ]
%13 = trunc i32 %11 to i16
%14 = tail call signext i16 @desc_digits(i16 signext %13)
%15 = tail call signext i16 @asc_digits(i16 signext %14)
%16 = add i8 %12, 1
%17 = sext i16 %14 to i32
%18 = sext i16 %15 to i32
%19 = sub nsw i32 %17, %18
%20 = icmp eq i32 %19, 6174
br i1 %20, label %21, label %10
21: ; preds = %10, %3, %1
%22 = phi i8 [ 0, %1 ], [ 1, %3 ], [ %16, %10 ]
ret i8 %22
}