int aapatternnum(int gridnum) {
  int pattern = (-1);
  static int patternnum[] = {
      1,  3,  4,  7,  3,  8,  7,  19, 4,  7,  11, 24, 9,  23, 20, 39, 4,  9,
      11, 20, 7,  23, 24, 39, 12, 22, 27, 47, 22, 48, 47, 29, 3,  8,  9,  23,
      10, 25, 21, 42, 7,  19, 20, 39, 21, 42, 44, 34, 9,  26, 28, 41, 21, 50,
      49, 30, 22, 43, 45, 33, 40, 32, 31, 13, 4,  9,  12, 22, 9,  26, 22, 43,
      11, 20, 27, 47, 28, 41, 45, 33, 11, 28, 27, 45, 20, 41, 47, 33, 27, 45,
      51, 35, 45, 36, 35, 14, 7,  23, 22, 48, 21, 50, 40, 32, 24, 39, 47, 29,
      49, 30, 31, 13, 20, 41, 45, 36, 44, 38, 31, 15, 47, 33, 35, 14, 31, 15,
      16, 5,  3,  10, 9,  21, 8,  25, 23, 42, 9,  21, 28, 49, 26, 50, 41, 30,
      7,  21, 20, 44, 19, 42, 39, 34, 22, 40, 45, 31, 43, 32, 33, 13, 8,  25,
      26, 50, 25, 46, 50, 37, 23, 42, 41, 30, 50, 37, 38, 17, 23, 50, 41, 38,
      42, 37, 30, 17, 48, 32, 36, 15, 32, 18, 15, 6,  7,  21, 22, 40, 23, 50,
      48, 32, 20, 44, 45, 31, 41, 38, 36, 15, 24, 49, 47, 31, 39, 30, 29, 13,
      47, 31, 35, 16, 33, 15, 14, 5,  19, 42, 43, 32, 42, 37, 32, 18, 39, 34,
      33, 13, 30, 17, 15, 6,  39, 30, 33, 15, 34, 17, 13, 6,  29, 13, 14, 5,
      13, 6,  5,  2,  -1};
  if (gridnum < 0 || gridnum > 511)
    goto end_of_job;
  pattern = patternnum[gridnum / 2];
  if (pattern < 1 || pattern > 51)
    pattern = (-1);
end_of_job:
  return (pattern);
}


x86 assembly (clang, -O0):
<aapatternnum>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
movl   $0xffffffff,-0x8(%rbp)
cmpl   $0x0,-0x4(%rbp)
jl     1125 <aapatternnum+0x25>
cmpl   $0x1ff,-0x4(%rbp)
jle    112a <aapatternnum+0x2a>
jmp    1165 <aapatternnum+0x65>
mov    -0x4(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
movslq %eax,%rcx
lea    0x2ef1(%rip),%rax
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x8(%rbp)
cmpl   $0x1,-0x8(%rbp)
jl     1159 <aapatternnum+0x59>
cmpl   $0x33,-0x8(%rbp)
jle    1160 <aapatternnum+0x60>
movl   $0xffffffff,-0x8(%rbp)
jmp    1165 <aapatternnum+0x65>
mov    -0x8(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<aapatternnum>:
mov    $0xffffffff,%eax
cmp    $0x1ff,%edi
ja     1119 <aapatternnum+0x19>
shr    %edi
lea    0xeea(%rip),%rax
mov    (%rax,%rdi,4),%eax
ret

LLVM IR (-O0):
@aapatternnum.patternnum = internal global [257 x i32] [i32 1, i32 3, i32 4, i32 7, i32 3, i32 8, i32 7, i32 19, i32 4, i32 7, i32 11, i32 24, i32 9, i32 23, i32 20, i32 39, i32 4, i32 9, i32 11, i32 20, i32 7, i32 23, i32 24, i32 39, i32 12, i32 22, i32 27, i32 47, i32 22, i32 48, i32 47, i32 29, i32 3, i32 8, i32 9, i32 23, i32 10, i32 25, i32 21, i32 42, i32 7, i32 19, i32 20, i32 39, i32 21, i32 42, i32 44, i32 34, i32 9, i32 26, i32 28, i32 41, i32 21, i32 50, i32 49, i32 30, i32 22, i32 43, i32 45, i32 33, i32 40, i32 32, i32 31, i32 13, i32 4, i32 9, i32 12, i32 22, i32 9, i32 26, i32 22, i32 43, i32 11, i32 20, i32 27, i32 47, i32 28, i32 41, i32 45, i32 33, i32 11, i32 28, i32 27, i32 45, i32 20, i32 41, i32 47, i32 33, i32 27, i32 45, i32 51, i32 35, i32 45, i32 36, i32 35, i32 14, i32 7, i32 23, i32 22, i32 48, i32 21, i32 50, i32 40, i32 32, i32 24, i32 39, i32 47, i32 29, i32 49, i32 30, i32 31, i32 13, i32 20, i32 41, i32 45, i32 36, i32 44, i32 38, i32 31, i32 15, i32 47, i32 33, i32 35, i32 14, i32 31, i32 15, i32 16, i32 5, i32 3, i32 10, i32 9, i32 21, i32 8, i32 25, i32 23, i32 42, i32 9, i32 21, i32 28, i32 49, i32 26, i32 50, i32 41, i32 30, i32 7, i32 21, i32 20, i32 44, i32 19, i32 42, i32 39, i32 34, i32 22, i32 40, i32 45, i32 31, i32 43, i32 32, i32 33, i32 13, i32 8, i32 25, i32 26, i32 50, i32 25, i32 46, i32 50, i32 37, i32 23, i32 42, i32 41, i32 30, i32 50, i32 37, i32 38, i32 17, i32 23, i32 50, i32 41, i32 38, i32 42, i32 37, i32 30, i32 17, i32 48, i32 32, i32 36, i32 15, i32 32, i32 18, i32 15, i32 6, i32 7, i32 21, i32 22, i32 40, i32 23, i32 50, i32 48, i32 32, i32 20, i32 44, i32 45, i32 31, i32 41, i32 38, i32 36, i32 15, i32 24, i32 49, i32 47, i32 31, i32 39, i32 30, i32 29, i32 13, i32 47, i32 31, i32 35, i32 16, i32 33, i32 15, i32 14, i32 5, i32 19, i32 42, i32 43, i32 32, i32 42, i32 37, i32 32, i32 18, i32 39, i32 34, i32 33, i32 13, i32 30, i32 17, i32 15, i32 6, i32 39, i32 30, i32 33, i32 15, i32 34, i32 17, i32 13, i32 6, i32 29, i32 13, i32 14, i32 5, i32 13, i32 6, i32 5, i32 2, i32 -1]
define i32 @aapatternnum(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
store i32 -1, i32* %3
%4 = load i32, i32* %2
%5 = icmp slt i32 %4, 0
br i1 %5, label %9, label %6
6: ; preds = %1
%7 = load i32, i32* %2
%8 = icmp sgt i32 %7, 511
br i1 %8, label %9, label %10
9: ; preds = %6, %1
br label %23
10: ; preds = %6
%11 = load i32, i32* %2
%12 = sdiv i32 %11, 2
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds [257 x i32], [257 x i32]* @aapatternnum.patternnum, i64 0, i64 %13
%15 = load i32, i32* %14
store i32 %15, i32* %3
%16 = load i32, i32* %3
%17 = icmp slt i32 %16, 1
br i1 %17, label %21, label %18
18: ; preds = %10
%19 = load i32, i32* %3
%20 = icmp sgt i32 %19, 51
br i1 %20, label %21, label %22
21: ; preds = %18, %10
store i32 -1, i32* %3
br label %22
22: ; preds = %21, %18
br label %23
23: ; preds = %22, %9
%24 = load i32, i32* %3
ret i32 %24
}

LLVM IR (-O2):
@aapatternnum.patternnum = internal unnamed_addr constant [257 x i32] [i32 1, i32 3, i32 4, i32 7, i32 3, i32 8, i32 7, i32 19, i32 4, i32 7, i32 11, i32 24, i32 9, i32 23, i32 20, i32 39, i32 4, i32 9, i32 11, i32 20, i32 7, i32 23, i32 24, i32 39, i32 12, i32 22, i32 27, i32 47, i32 22, i32 48, i32 47, i32 29, i32 3, i32 8, i32 9, i32 23, i32 10, i32 25, i32 21, i32 42, i32 7, i32 19, i32 20, i32 39, i32 21, i32 42, i32 44, i32 34, i32 9, i32 26, i32 28, i32 41, i32 21, i32 50, i32 49, i32 30, i32 22, i32 43, i32 45, i32 33, i32 40, i32 32, i32 31, i32 13, i32 4, i32 9, i32 12, i32 22, i32 9, i32 26, i32 22, i32 43, i32 11, i32 20, i32 27, i32 47, i32 28, i32 41, i32 45, i32 33, i32 11, i32 28, i32 27, i32 45, i32 20, i32 41, i32 47, i32 33, i32 27, i32 45, i32 51, i32 35, i32 45, i32 36, i32 35, i32 14, i32 7, i32 23, i32 22, i32 48, i32 21, i32 50, i32 40, i32 32, i32 24, i32 39, i32 47, i32 29, i32 49, i32 30, i32 31, i32 13, i32 20, i32 41, i32 45, i32 36, i32 44, i32 38, i32 31, i32 15, i32 47, i32 33, i32 35, i32 14, i32 31, i32 15, i32 16, i32 5, i32 3, i32 10, i32 9, i32 21, i32 8, i32 25, i32 23, i32 42, i32 9, i32 21, i32 28, i32 49, i32 26, i32 50, i32 41, i32 30, i32 7, i32 21, i32 20, i32 44, i32 19, i32 42, i32 39, i32 34, i32 22, i32 40, i32 45, i32 31, i32 43, i32 32, i32 33, i32 13, i32 8, i32 25, i32 26, i32 50, i32 25, i32 46, i32 50, i32 37, i32 23, i32 42, i32 41, i32 30, i32 50, i32 37, i32 38, i32 17, i32 23, i32 50, i32 41, i32 38, i32 42, i32 37, i32 30, i32 17, i32 48, i32 32, i32 36, i32 15, i32 32, i32 18, i32 15, i32 6, i32 7, i32 21, i32 22, i32 40, i32 23, i32 50, i32 48, i32 32, i32 20, i32 44, i32 45, i32 31, i32 41, i32 38, i32 36, i32 15, i32 24, i32 49, i32 47, i32 31, i32 39, i32 30, i32 29, i32 13, i32 47, i32 31, i32 35, i32 16, i32 33, i32 15, i32 14, i32 5, i32 19, i32 42, i32 43, i32 32, i32 42, i32 37, i32 32, i32 18, i32 39, i32 34, i32 33, i32 13, i32 30, i32 17, i32 15, i32 6, i32 39, i32 30, i32 33, i32 15, i32 34, i32 17, i32 13, i32 6, i32 29, i32 13, i32 14, i32 5, i32 13, i32 6, i32 5, i32 2, i32 -1]
define i32 @aapatternnum(i32 %0) {
%2 = icmp ugt i32 %0, 511
br i1 %2, label %8, label %3
3: ; preds = %1
%4 = lshr i32 %0, 1
%5 = zext i32 %4 to i64
%6 = getelementptr inbounds [257 x i32], [257 x i32]* @aapatternnum.patternnum, i64 0, i64 %5
%7 = load i32, i32* %6
br label %8
8: ; preds = %3, %1
%9 = phi i32 [ -1, %1 ], [ %7, %3 ]
ret i32 %9
}