#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 change(int);
int /*<<< orphan*/ getchar();
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ scanf(char *, int *);
int main(int argc, char const *argv[]) {
  int lines, num, count;
  scanf("%d", &lines);
  getchar();
  while (lines--) {
    scanf("%d", &num);
    getchar();
    for (count = 1; num != 6174; count++)
      num = change(num);
    printf("%d\n", count);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xea3(%rip),%rdi
lea    -0x14(%rbp),%rsi
call   1060 <scanf@plt>
mov    $0x0,%al
call   1050 <getchar@plt>
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x14(%rbp)
cmp    $0x0,%eax
je     11d9 <main+0x99>
lea    0xe78(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <scanf@plt>
mov    $0x0,%al
call   1050 <getchar@plt>
movl   $0x1,-0x1c(%rbp)
cmpl   $0x181e,-0x18(%rbp)
je     11c5 <main+0x85>
mov    -0x18(%rbp),%edi
call   1030 <change@plt>
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    119f <main+0x5f>
mov    -0x1c(%rbp),%esi
lea    0xe34(%rip),%rdi
call   1040 <printf@plt>
jmp    116d <main+0x2d>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
lea    0xeb2(%rip),%rdi
lea    0x4(%rsp),%rsi
call   1060 <scanf@plt>
xor    %eax,%eax
call   1050 <getchar@plt>
subl   $0x1,0x4(%rsp)
jae    1173 <main+0x33>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe86(%rip),%r14
mov    %rsp,%r15
lea    0xe7f(%rip),%rbx
jmp    11a1 <main+0x61>
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
subl   $0x1,0x4(%rsp)
jb     1166 <main+0x26>
mov    %r14,%rdi
mov    %r15,%rsi
call   1060 <scanf@plt>
xor    %eax,%eax
call   1050 <getchar@plt>
mov    (%rsp),%eax
mov    $0x1,%ebp
cmp    $0x181e,%eax
je     1190 <main+0x50>
mov    $0x1,%ebp
nopw   0x0(%rax,%rax,1)
mov    %eax,%edi
call   1030 <change@plt>
mov    %eax,(%rsp)
add    $0x1,%ebp
cmp    $0x181e,%eax
jne    11d0 <main+0x90>
jmp    1190 <main+0x50>

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [4 x i8] c"%d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %6)
%10 = call i32 (...) @getchar()
br label %11
11: ; preds = %27, %2
%12 = load i32, i32* %6
%13 = add nsw i32 %12, -1
store i32 %13, i32* %6
%14 = icmp ne i32 %12, 0
br i1 %14, label %15, label %30
15: ; preds = %11
%16 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %7)
%17 = call i32 (...) @getchar()
store i32 1, i32* %8
br label %18
18: ; preds = %24, %15
%19 = load i32, i32* %7
%20 = icmp ne i32 %19, 6174
br i1 %20, label %21, label %27
21: ; preds = %18
%22 = load i32, i32* %7
%23 = call i32 @change(i32 %22)
store i32 %23, i32* %7
br label %24
24: ; preds = %21
%25 = load i32, i32* %8
%26 = add nsw i32 %25, 1
store i32 %26, i32* %8
br label %18
27: ; preds = %18
%28 = load i32, i32* %8
%29 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %28)
br label %11
30: ; preds = %11
ret i32 0
}

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