#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*/ executeInstruction(int *, int *, int *, int *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ memset(int *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
int /*<<< orphan*/ stdin;
int main(void) {
  int mRegister[10], mRAM[1000], T, numInstruction, isHalt, ind, j,
      programCounter;
  char instruction[3 + 2], trash[3 + 2];
  scanf("%d", &T);
  fgets(trash, 3 + 2, stdin);
  fgets(trash, 3 + 2, stdin);
  while (T) {
    memset(mRAM, 0, 1000 * sizeof(int));
    memset(mRegister, 0, 10 * sizeof(int));
    numInstruction = 0;
    isHalt = 0;
    programCounter = 0;
    ind = 0;
    while (1) {
      if (fgets(instruction, 3 + 2, stdin) == NULL)
        break;
      if (instruction[0] == '\n' || instruction[0] == '\0')
        break;
      for (j = 0; j < 3; j++)
        mRAM[ind] = mRAM[ind] * 10 + (instruction[j] - '0');
      ind++;
    }
    while (!isHalt && programCounter < 1000) {
      executeInstruction(mRegister, mRAM, &programCounter, &isHalt);
      numInstruction++;
    }
    printf("%d\n", numInstruction);
    if (T != 1)
      printf("\n");
    T--;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1000,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe97(%rip),%rdi
lea    -0xfd4(%rbp),%rsi
call   1060 <scanf@plt>
lea    -0xff2(%rbp),%rdi
mov    0x2e65(%rip),%rax
mov    (%rax),%edx
mov    $0x5,%esi
call   1050 <fgets@plt>
lea    -0xff2(%rbp),%rdi
mov    0x2e4b(%rip),%rax
mov    (%rax),%edx
mov    $0x5,%esi
call   1050 <fgets@plt>
cmpl   $0x0,-0xfd4(%rbp)
je     1374 <main+0x224>
lea    -0xfd0(%rbp),%rdi
xor    %esi,%esi
mov    $0xfa0,%edx
call   1040 <memset@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
movl   $0x0,-0xfd8(%rbp)
movl   $0x0,-0xfdc(%rbp)
movl   $0x0,-0xfe8(%rbp)
movl   $0x0,-0xfe0(%rbp)
lea    -0xfed(%rbp),%rdi
mov    0x2dd9(%rip),%rax
mov    (%rax),%edx
mov    $0x5,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
jne    122a <main+0xda>
jmp    12bf <main+0x16f>
movsbl -0xfed(%rbp),%eax
cmp    $0xa,%eax
je     124a <main+0xfa>
movsbl -0xfed(%rbp),%eax
cmp    $0x0,%eax
jne    124f <main+0xff>
jmp    12bf <main+0x16f>
movl   $0x0,-0xfe4(%rbp)
cmpl   $0x3,-0xfe4(%rbp)
jge    12ab <main+0x15b>
movslq -0xfe0(%rbp),%rax
imul   $0xa,-0xfd0(%rbp,%rax,4),%ecx
0a
movslq -0xfe4(%rbp),%rax
movsbl -0xfed(%rbp,%rax,1),%eax
ff
sub    $0x30,%eax
add    %eax,%ecx
movslq -0xfe0(%rbp),%rax
mov    %ecx,-0xfd0(%rbp,%rax,4)
mov    -0xfe4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xfe4(%rbp)
jmp    1259 <main+0x109>
mov    -0xfe0(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xfe0(%rbp)
jmp    1201 <main+0xb1>
jmp    12c4 <main+0x174>
xor    %eax,%eax
cmpl   $0x0,-0xfdc(%rbp)
mov    %al,-0xff3(%rbp)
jne    12ec <main+0x19c>
cmpl   $0x3e8,-0xfe8(%rbp)
setl   %al
mov    %al,-0xff3(%rbp)
mov    -0xff3(%rbp),%al
test   $0x1,%al
jne    12ff <main+0x1af>
jmp    1331 <main+0x1e1>
lea    -0x30(%rbp),%rdi
lea    -0xfd0(%rbp),%rsi
lea    -0xfe8(%rbp),%rdx
lea    -0xfdc(%rbp),%rcx
call   1070 <executeInstruction@plt>
mov    -0xfd8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xfd8(%rbp)
jmp    12c4 <main+0x174>
mov    -0xfd8(%rbp),%esi
lea    0xcc5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x1,-0xfd4(%rbp)
je     1360 <main+0x210>
lea    0xcac(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xfd4(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xfd4(%rbp)
jmp    11a9 <main+0x59>
xor    %eax,%eax
add    $0x1000,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xff8,%rsp
lea    0xe88(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1070 <scanf@plt>
mov    0x2e5f(%rip),%rbp
mov    0x0(%rbp),%edx
lea    0x1b(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x5,%esi
call   1060 <fgets@plt>
mov    0x0(%rbp),%edx
mov    %rbx,%rdi
mov    $0x5,%esi
call   1060 <fgets@plt>
cmpl   $0x0,0x10(%rsp)
je     1301 <main+0x1a1>
lea    0x50(%rsp),%rbx
lea    0x20(%rsp),%r14
lea    0x16(%rsp),%r15
lea    0x8(%rsp),%r12
lea    0xc(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xfa0,%edx
call   1050 <memset@plt>
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
movl   $0x0,0xc(%rsp)
movl   $0x0,0x8(%rsp)
mov    0x2dd3(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
mov    $0x5,%esi
call   1060 <fgets@plt>
test   %rax,%rax
je     1290 <main+0x130>
mov    %rbx,%rbp
nopl   0x0(%rax)
movsbl 0x16(%rsp),%eax
test   %eax,%eax
je     1290 <main+0x130>
cmp    $0xa,%al
je     1290 <main+0x130>
mov    0x0(%rbp),%ecx
lea    (%rcx,%rcx,4),%ecx
lea    (%rax,%rcx,2),%eax
lea    (%rax,%rax,4),%eax
movsbl 0x17(%rsp),%ecx
lea    (%rcx,%rax,2),%eax
lea    (%rax,%rax,4),%eax
movsbl 0x18(%rsp),%ecx
lea    -0x14d0(%rcx,%rax,2),%eax
mov    %eax,0x0(%rbp)
mov    0x2d7e(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
mov    $0x5,%esi
call   1060 <fgets@plt>
add    $0x4,%rbp
test   %rax,%rax
jne    1230 <main+0xd0>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %ebp,%ebp
cmpl   $0x0,0xc(%rsp)
jne    12d5 <main+0x175>
cmpl   $0x3e7,0x8(%rsp)
jg     12d5 <main+0x175>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
mov    %r13,%rcx
call   1080 <executeInstruction@plt>
add    $0x1,%ebp
cmpl   $0x0,0xc(%rsp)
jne    12d5 <main+0x175>
cmpl   $0x3e8,0x8(%rsp)
jl     12b0 <main+0x150>
lea    0xd27(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
cmpl   $0x1,0x10(%rsp)
je     1301 <main+0x1a1>
mov    $0xa,%edi
call   1030 <putchar@plt>
addl   $0xffffffff,0x10(%rsp)
jne    11e0 <main+0x80>
xor    %eax,%eax
add    $0xff8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@stdin = global i32 0
@.str.1 = [4 x i8] c"%d\0A\00"
@.str.2 = [2 x i8] c"\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [10 x i32]
%3 = alloca [1000 x i32]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [5 x i8]
%11 = alloca [5 x i8]
store i32 0, i32* %1
%12 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %4)
%13 = getelementptr inbounds [5 x i8], [5 x i8]* %11, i64 0, i64 0
%14 = load i32, i32* @stdin
%15 = call i32* @fgets(i8* %13, i32 5, i32 %14)
%16 = getelementptr inbounds [5 x i8], [5 x i8]* %11, i64 0, i64 0
%17 = load i32, i32* @stdin
%18 = call i32* @fgets(i8* %16, i32 5, i32 %17)
br label %19
19: ; preds = %92, %0
%20 = load i32, i32* %4
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %95
22: ; preds = %19
%23 = getelementptr inbounds [1000 x i32], [1000 x i32]* %3, i64 0, i64 0
%24 = call i32 @memset(i32* %23, i32 0, i32 4000)
%25 = getelementptr inbounds [10 x i32], [10 x i32]* %2, i64 0, i64 0
%26 = call i32 @memset(i32* %25, i32 0, i32 40)
store i32 0, i32* %5
store i32 0, i32* %6
store i32 0, i32* %9
store i32 0, i32* %7
br label %27
27: ; preds = %22, %67
%28 = getelementptr inbounds [5 x i8], [5 x i8]* %10, i64 0, i64 0
%29 = load i32, i32* @stdin
%30 = call i32* @fgets(i8* %28, i32 5, i32 %29)
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %33
32: ; preds = %27
br label %70
33: ; preds = %27
%34 = getelementptr inbounds [5 x i8], [5 x i8]* %10, i64 0, i64 0
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp eq i32 %36, 10
br i1 %37, label %43, label %38
38: ; preds = %33
%39 = getelementptr inbounds [5 x i8], [5 x i8]* %10, i64 0, i64 0
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %38, %33
br label %70
44: ; preds = %38
store i32 0, i32* %8
br label %45
45: ; preds = %64, %44
%46 = load i32, i32* %8
%47 = icmp slt i32 %46, 3
br i1 %47, label %48, label %67
48: ; preds = %45
%49 = load i32, i32* %7
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds [1000 x i32], [1000 x i32]* %3, i64 0, i64 %50
%52 = load i32, i32* %51
%53 = mul nsw i32 %52, 10
%54 = load i32, i32* %8
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds [5 x i8], [5 x i8]* %10, i64 0, i64 %55
%57 = load i8, i8* %56
%58 = sext i8 %57 to i32
%59 = sub nsw i32 %58, 48
%60 = add nsw i32 %53, %59
%61 = load i32, i32* %7
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds [1000 x i32], [1000 x i32]* %3, i64 0, i64 %62
store i32 %60, i32* %63
br label %64
64: ; preds = %48
%65 = load i32, i32* %8
%66 = add nsw i32 %65, 1
store i32 %66, i32* %8
br label %45
67: ; preds = %45
%68 = load i32, i32* %7
%69 = add nsw i32 %68, 1
store i32 %69, i32* %7
br label %27
70: ; preds = %43, %32
br label %71
71: ; preds = %79, %70
%72 = load i32, i32* %6
%73 = icmp ne i32 %72, 0
br i1 %73, label %77, label %74
74: ; preds = %71
%75 = load i32, i32* %9
%76 = icmp slt i32 %75, 1000
br label %77
77: ; preds = %74, %71
%78 = phi i1 [ false, %71 ], [ %76, %74 ]
br i1 %78, label %79, label %85
79: ; preds = %77
%80 = getelementptr inbounds [10 x i32], [10 x i32]* %2, i64 0, i64 0
%81 = getelementptr inbounds [1000 x i32], [1000 x i32]* %3, i64 0, i64 0
%82 = call i32 @executeInstruction(i32* %80, i32* %81, i32* %9, i32* %6)
%83 = load i32, i32* %5
%84 = add nsw i32 %83, 1
store i32 %84, i32* %5
br label %71
85: ; preds = %77
%86 = load i32, i32* %5
%87 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %86)
%88 = load i32, i32* %4
%89 = icmp ne i32 %88, 1
br i1 %89, label %90, label %92
90: ; preds = %85
%91 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %92
92: ; preds = %90, %85
%93 = load i32, i32* %4
%94 = add nsw i32 %93, -1
store i32 %94, i32* %4
br label %19
95: ; preds = %19
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@stdin = global i32 0
@.str.1 = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca [10 x i32]
%2 = alloca [1000 x i32]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [5 x i8]
%7 = alloca [5 x i8]
%8 = bitcast [10 x i32]* %1 to i8*
%9 = bitcast [1000 x i32]* %2 to i8*
%10 = bitcast i32* %3 to i8*
%11 = bitcast i32* %4 to i8*
%12 = bitcast i32* %5 to i8*
%13 = getelementptr inbounds [5 x i8], [5 x i8]* %6, i64 0, i64 0
%14 = getelementptr inbounds [5 x i8], [5 x i8]* %7, i64 0, i64 0
%15 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %3)
%16 = load i32, i32* @stdin
%17 = call i32* @fgets(i8* %14, i32 5, i32 %16)
%18 = load i32, i32* @stdin
%19 = call i32* @fgets(i8* %14, i32 5, i32 %18)
%20 = load i32, i32* %3
%21 = icmp eq i32 %20, 0
br i1 %21, label %82, label %22
22: ; preds = %0
%23 = getelementptr inbounds [1000 x i32], [1000 x i32]* %2, i64 0, i64 0
%24 = getelementptr inbounds [10 x i32], [10 x i32]* %1, i64 0, i64 0
%25 = getelementptr inbounds [5 x i8], [5 x i8]* %6, i64 0, i64 1
%26 = getelementptr inbounds [5 x i8], [5 x i8]* %6, i64 0, i64 2
br label %27
27: ; preds = %22, %77
%28 = call i32 @memset(i32* %23, i32 0, i32 4000)
%29 = call i32 @memset(i32* %24, i32 0, i32 40)
store i32 0, i32* %4
store i32 0, i32* %5
%30 = load i32, i32* @stdin
%31 = call i32* @fgets(i8* %13, i32 5, i32 %30)
%32 = icmp eq i32* %31, null
br i1 %32, label %57, label %33
33: ; preds = %27, %36
%34 = phi i64 [ %53, %36 ], [ 0, %27 ]
%35 = load i8, i8* %13
switch i8 %35, label %36 [
i8 10, label %57
i8 0, label %57
]
36: ; preds = %33
%37 = getelementptr inbounds [1000 x i32], [1000 x i32]* %2, i64 0, i64 %34
%38 = load i32, i32* %37
%39 = mul nsw i32 %38, 10
%40 = sext i8 %35 to i32
%41 = add i32 %39, -48
%42 = add i32 %41, %40
%43 = mul nsw i32 %42, 10
%44 = load i8, i8* %25
%45 = sext i8 %44 to i32
%46 = add i32 %43, -48
%47 = add i32 %46, %45
%48 = mul nsw i32 %47, 10
%49 = load i8, i8* %26
%50 = sext i8 %49 to i32
%51 = add i32 %48, -48
%52 = add i32 %51, %50
store i32 %52, i32* %37
%53 = add nuw i64 %34, 1
%54 = load i32, i32* @stdin
%55 = call i32* @fgets(i8* %13, i32 5, i32 %54)
%56 = icmp eq i32* %55, null
br i1 %56, label %57, label %33
57: ; preds = %36, %33, %33, %27
%58 = load i32, i32* %4
%59 = icmp eq i32 %58, 0
%60 = load i32, i32* %5
%61 = icmp slt i32 %60, 1000
%62 = select i1 %59, i1 %61, i1 false
br i1 %62, label %63, label %72
63: ; preds = %57, %63
%64 = phi i32 [ %66, %63 ], [ 0, %57 ]
%65 = call i32 @executeInstruction(i32* %24, i32* %23, i32* %5, i32* %4)
%66 = add nuw nsw i32 %64, 1
%67 = load i32, i32* %4
%68 = icmp eq i32 %67, 0
%69 = load i32, i32* %5
%70 = icmp slt i32 %69, 1000
%71 = select i1 %68, i1 %70, i1 false
br i1 %71, label %63, label %72
72: ; preds = %63, %57
%73 = phi i32 [ 0, %57 ], [ %66, %63 ]
%74 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %73)
%75 = load i32, i32* %3
%76 = icmp eq i32 %75, 1
br i1 %76, label %82, label %77
77: ; preds = %72
%78 = call i32 @putchar(i32 10)
%79 = load i32, i32* %3
%80 = add nsw i32 %79, -1
store i32 %80, i32* %3
%81 = icmp eq i32 %80, 0
br i1 %81, label %82, label %27
82: ; preds = %77, %72, %0
ret i32 0
}