int grid_get_num(int *num, int cell) {
  int i;
  static int num_flags = 0;
  if (num_flags == 0)
    for (i = 0; i <= 9; i++)
      num_flags |= ((i) + 1);
  cell &= num_flags;
  for (i = 0; i <= 9; i++)
    if (cell == ((i) + 1)) {
      *num = i;
      return (0);
    }
  *num = 0;
  return (0);
}


x86 assembly (clang, -O0):
<grid_get_num>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x0,0x2f12(%rip)
jne    114e <grid_get_num+0x4e>
movl   $0x0,-0x18(%rbp)
cmpl   $0x9,-0x18(%rbp)
jg     1149 <grid_get_num+0x49>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
or     0x2eef(%rip),%eax
mov    %eax,0x2ee9(%rip)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    111f <grid_get_num+0x1f>
jmp    114e <grid_get_num+0x4e>
mov    0x2ed0(%rip),%eax
and    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x9,-0x18(%rbp)
jg     11a4 <grid_get_num+0xa4>
mov    -0x14(%rbp),%eax
mov    -0x18(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jne    1191 <grid_get_num+0x91>
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11b5 <grid_get_num+0xb5>
jmp    1196 <grid_get_num+0x96>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1161 <grid_get_num+0x61>
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<grid_get_num>:
xor    %eax,%eax
cmpb   $0x0,0x2f1b(%rip)
mov    $0xf,%edx
mov    $0x0,%ecx
cmovne %edx,%ecx
jne    1124 <grid_get_num+0x24>
movb   $0x1,0x2f05(%rip)
mov    $0xf,%ecx
and    %esi,%ecx
add    $0xffffffff,%ecx
cmp    $0xa,%ecx
mov    $0xa,%edx
cmovb  %ecx,%edx
cmovae %eax,%edx
mov    %edx,(%rdi)
xor    %eax,%eax
ret

LLVM IR (-O0):
@grid_get_num.num_flags = internal global i32 0
define i32 @grid_get_num(i32* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
%7 = load i32, i32* @grid_get_num.num_flags
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %22
9: ; preds = %2
store i32 0, i32* %6
br label %10
10: ; preds = %18, %9
%11 = load i32, i32* %6
%12 = icmp sle i32 %11, 9
br i1 %12, label %13, label %21
13: ; preds = %10
%14 = load i32, i32* %6
%15 = add nsw i32 %14, 1
%16 = load i32, i32* @grid_get_num.num_flags
%17 = or i32 %16, %15
store i32 %17, i32* @grid_get_num.num_flags
br label %18
18: ; preds = %13
%19 = load i32, i32* %6
%20 = add nsw i32 %19, 1
store i32 %20, i32* %6
br label %10
21: ; preds = %10
br label %22
22: ; preds = %21, %2
%23 = load i32, i32* @grid_get_num.num_flags
%24 = load i32, i32* %5
%25 = and i32 %24, %23
store i32 %25, i32* %5
store i32 0, i32* %6
br label %26
26: ; preds = %38, %22
%27 = load i32, i32* %6
%28 = icmp sle i32 %27, 9
br i1 %28, label %29, label %41
29: ; preds = %26
%30 = load i32, i32* %5
%31 = load i32, i32* %6
%32 = add nsw i32 %31, 1
%33 = icmp eq i32 %30, %32
br i1 %33, label %34, label %37
34: ; preds = %29
%35 = load i32, i32* %6
%36 = load i32*, i32** %4
store i32 %35, i32* %36
store i32 0, i32* %3
br label %43
37: ; preds = %29
br label %38
38: ; preds = %37
%39 = load i32, i32* %6
%40 = add nsw i32 %39, 1
store i32 %40, i32* %6
br label %26
41: ; preds = %26
%42 = load i32*, i32** %4
store i32 0, i32* %42
store i32 0, i32* %3
br label %43
43: ; preds = %41, %34
%44 = load i32, i32* %3
ret i32 %44
}

LLVM IR (-O2):
@grid_get_num.num_flags = internal unnamed_addr global i1 false
define i32 @grid_get_num(i32* nocapture writeonly %0, i32 %1) {
%3 = load i1, i1* @grid_get_num.num_flags
%4 = select i1 %3, i32 15, i32 0
br i1 %3, label %6, label %5
5: ; preds = %2
store i1 true, i1* @grid_get_num.num_flags
br label %6
6: ; preds = %5, %2
%7 = phi i32 [ 15, %5 ], [ %4, %2 ]
%8 = and i32 %7, %1
%9 = add nsw i32 %8, -1
%10 = call i32 @llvm.umin.i32(i32 %9, i32 10)
%11 = add nsw i32 %8, -1
%12 = icmp ult i32 %11, 10
%13 = select i1 %12, i32 %10, i32 0
store i32 %13, i32* %0
ret i32 0
}