#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 */
size_t ctoi(unsigned char const);
char itoc(int);
char msi_check_digit_mod10(const unsigned char source[], const int length) {
  static const int vals[2][10] = {
      {0, 2, 4, 6, 8, 1, 3, 5, 7, 9},
      {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
  };
  int i, x = 0, undoubled = 0;
  for (i = length - 1; i >= 0; i--) {
    x += vals[undoubled][ctoi(source[i])];
    undoubled = !undoubled;
  }
  return itoc((10 - x % 10) % 10);
}


x86 assembly (clang, -O0):
<msi_check_digit_mod10>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jl     11a6 <msi_check_digit_mod10+0x86>
movslq -0x18(%rbp),%rcx
lea    0xea5(%rip),%rax
imul   $0x28,%rcx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movzbl (%rax,%rcx,1),%edi
call   1030 <ctoi@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    (%rax,%rcx,4),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x18(%rbp)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1146 <msi_check_digit_mod10+0x26>
mov    -0x14(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    $0xa,%eax
sub    %edx,%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %edx,%edi
call   1040 <itoc@plt>
movsbl %al,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<msi_check_digit_mod10>:
test   %esi,%esi
jle    11cc <msi_check_digit_mod10+0xac>
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    %esi,%ebx
add    $0x1,%rbx
xor    %ebp,%ebp
lea    0xebb(%rip),%r15
xor    %r12d,%r12d
nopl   0x0(%rax,%rax,1)
lea    -0x2(%rbx),%eax
mov    %ebp,%r13d
movzbl (%r14,%rax,1),%edi
call   1030 <ctoi@plt>
lea    0x0(,%r13,4),%rcx
add    %r13,%rcx
lea    (%r15,%rcx,8),%rcx
add    (%rcx,%rax,4),%r12d
xor    $0x1,%ebp
add    $0xffffffffffffffff,%rbx
cmp    $0x1,%rbx
ja     1150 <msi_check_digit_mod10+0x30>
movslq %r12d,%rax
imul   $0x66666667,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
shr    $0x22,%rcx
add    %edx,%ecx
add    %ecx,%ecx
lea    (%rcx,%rcx,4),%ecx
sub    %ecx,%eax
mov    $0xa,%cl
sub    %al,%cl
movzbl %cl,%eax
imul   $0xcd,%eax,%ecx
shr    $0xa,%ecx
and    $0xfffffffe,%ecx
lea    (%rcx,%rcx,4),%ecx
sub    %cl,%al
movzbl %al,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <itoc@plt>
xor    %edi,%edi
jmp    1040 <itoc@plt>

LLVM IR (-O0):
@msi_check_digit_mod10.vals = internal constant [2 x [10 x i32]] [[10 x i32] [i32 0, i32 2, i32 4, i32 6, i32 8, i32 1, i32 3, i32 5, i32 7, i32 9], [10 x i32] [i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9]]
define signext i8 @msi_check_digit_mod10(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 0, i32* %6
store i32 0, i32* %7
%8 = load i32, i32* %4
%9 = sub nsw i32 %8, 1
store i32 %9, i32* %5
br label %10
10: ; preds = %31, %2
%11 = load i32, i32* %5
%12 = icmp sge i32 %11, 0
br i1 %12, label %13, label %34
13: ; preds = %10
%14 = load i32, i32* %7
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds [2 x [10 x i32]], [2 x [10 x i32]]* @msi_check_digit_mod10.vals, i64 0, i64 %15
%17 = load i8*, i8** %3
%18 = load i32, i32* %5
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = load i8, i8* %20
%22 = call i64 @ctoi(i8 zeroext %21)
%23 = getelementptr inbounds [10 x i32], [10 x i32]* %16, i64 0, i64 %22
%24 = load i32, i32* %23
%25 = load i32, i32* %6
%26 = add nsw i32 %25, %24
store i32 %26, i32* %6
%27 = load i32, i32* %7
%28 = icmp ne i32 %27, 0
%29 = xor i1 %28, true
%30 = zext i1 %29 to i32
store i32 %30, i32* %7
br label %31
31: ; preds = %13
%32 = load i32, i32* %5
%33 = add nsw i32 %32, -1
store i32 %33, i32* %5
br label %10
34: ; preds = %10
%35 = load i32, i32* %6
%36 = srem i32 %35, 10
%37 = sub nsw i32 10, %36
%38 = srem i32 %37, 10
%39 = call signext i8 @itoc(i32 %38)
ret i8 %39
}

LLVM IR (-O2):
@msi_check_digit_mod10.vals = internal unnamed_addr constant [2 x [10 x i32]] [[10 x i32] [i32 0, i32 2, i32 4, i32 6, i32 8, i32 1, i32 3, i32 5, i32 7, i32 9], [10 x i32] [i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9]]
define signext i8 @msi_check_digit_mod10(i8* nocapture %0, i32 %1) {
%3 = icmp sgt i32 %1, 0
br i1 %3, label %4, label %27
4: ; preds = %2
%5 = zext i32 %1 to i64
br label %6
6: ; preds = %4, %6
%7 = phi i64 [ %5, %4 ], [ %10, %6 ]
%8 = phi i32 [ 0, %4 ], [ %19, %6 ]
%9 = phi i32 [ 0, %4 ], [ %18, %6 ]
%10 = add nsw i64 %7, -1
%11 = zext i32 %8 to i64
%12 = and i64 %10, 4294967295
%13 = getelementptr inbounds i8, i8* %0, i64 %12
%14 = load i8, i8* %13
%15 = tail call i64 @ctoi(i8 zeroext %14)
%16 = getelementptr inbounds [2 x [10 x i32]], [2 x [10 x i32]]* @msi_check_digit_mod10.vals, i64 0, i64 %11, i64 %15
%17 = load i32, i32* %16
%18 = add nsw i32 %17, %9
%19 = xor i32 %8, 1
%20 = icmp ugt i64 %7, 1
br i1 %20, label %6, label %21
21: ; preds = %6
%22 = srem i32 %18, 10
%23 = trunc i32 %22 to i8
%24 = sub nsw i8 10, %23
%25 = urem i8 %24, 10
%26 = zext i8 %25 to i32
br label %27
27: ; preds = %21, %2
%28 = phi i32 [ 0, %2 ], [ %26, %21 ]
%29 = tail call signext i8 @itoc(i32 %28)
ret i8 %29
}