#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 ctoi(unsigned char);
int calc_check_digit(unsigned char *src) {
  int i, check_digit;
  int count = 0;
  for (i = 0; i < 13; i++) {
    count += (i & 1) ? ctoi(src[i]) : 3 * ctoi(src[i]);
  }
  check_digit = 10 - (count % 10);
  if (check_digit == 10) {
    check_digit = 0;
  }
  return check_digit;
}


x86 assembly (clang, -O0):
<calc_check_digit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0xd,-0xc(%rbp)
jge    118a <calc_check_digit+0x7a>
mov    -0xc(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     115c <calc_check_digit+0x4c>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movzbl (%rax,%rcx,1),%edi
call   1030 <ctoi@plt>
mov    %eax,-0x18(%rbp)
jmp    1173 <calc_check_digit+0x63>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movzbl (%rax,%rcx,1),%edi
call   1030 <ctoi@plt>
imul   $0x3,%eax,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    112a <calc_check_digit+0x1a>
mov    -0x14(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    $0xa,%eax
sub    %edx,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0xa,-0x10(%rbp)
jne    11b0 <calc_check_digit+0xa0>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calc_check_digit>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r15
movzbl (%rdi),%edi
call   1030 <ctoi@plt>
lea    (%rax,%rax,2),%r14d
movzbl 0x1(%r15),%edi
call   1030 <ctoi@plt>
mov    %eax,%ebp
add    %r14d,%ebp
movzbl 0x2(%r15),%edi
call   1030 <ctoi@plt>
lea    (%rax,%rax,2),%ebx
add    %ebp,%ebx
movzbl 0x3(%r15),%edi
call   1030 <ctoi@plt>
mov    %eax,%ebp
add    %ebx,%ebp
movzbl 0x4(%r15),%edi
call   1030 <ctoi@plt>
lea    (%rax,%rax,2),%ebx
add    %ebp,%ebx
movzbl 0x5(%r15),%edi
call   1030 <ctoi@plt>
mov    %eax,%ebp
add    %ebx,%ebp
movzbl 0x6(%r15),%edi
call   1030 <ctoi@plt>
lea    (%rax,%rax,2),%ebx
add    %ebp,%ebx
movzbl 0x7(%r15),%edi
call   1030 <ctoi@plt>
mov    %eax,%ebp
add    %ebx,%ebp
movzbl 0x8(%r15),%edi
call   1030 <ctoi@plt>
lea    (%rax,%rax,2),%ebx
add    %ebp,%ebx
movzbl 0x9(%r15),%edi
call   1030 <ctoi@plt>
mov    %eax,%ebp
add    %ebx,%ebp
movzbl 0xa(%r15),%edi
call   1030 <ctoi@plt>
lea    (%rax,%rax,2),%ebx
add    %ebp,%ebx
movzbl 0xb(%r15),%edi
call   1030 <ctoi@plt>
mov    %eax,%ebp
add    %ebx,%ebp
movzbl 0xc(%r15),%edi
call   1030 <ctoi@plt>
lea    (%rax,%rax,2),%eax
add    %ebp,%eax
movslq %eax,%rcx
imul   $0x66666667,%rcx,%rax
mov    %rax,%rdx
shr    $0x3f,%rdx
sar    $0x22,%rax
add    %edx,%eax
add    %eax,%eax
lea    (%rax,%rax,4),%eax
sub    %eax,%ecx
mov    $0xa,%eax
sub    %ecx,%eax
test   %ecx,%ecx
cmove  %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @calc_check_digit(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %5
store i32 0, i32* %3
br label %6
6: ; preds = %32, %1
%7 = load i32, i32* %3
%8 = icmp slt i32 %7, 13
br i1 %8, label %9, label %35
9: ; preds = %6
%10 = load i32, i32* %3
%11 = and i32 %10, 1
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %20
13: ; preds = %9
%14 = load i8*, i8** %2
%15 = load i32, i32* %3
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
%18 = load i8, i8* %17
%19 = call i32 @ctoi(i8 zeroext %18)
br label %28
20: ; preds = %9
%21 = load i8*, i8** %2
%22 = load i32, i32* %3
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %21, i64 %23
%25 = load i8, i8* %24
%26 = call i32 @ctoi(i8 zeroext %25)
%27 = mul nsw i32 3, %26
br label %28
28: ; preds = %20, %13
%29 = phi i32 [ %19, %13 ], [ %27, %20 ]
%30 = load i32, i32* %5
%31 = add nsw i32 %30, %29
store i32 %31, i32* %5
br label %32
32: ; preds = %28
%33 = load i32, i32* %3
%34 = add nsw i32 %33, 1
store i32 %34, i32* %3
br label %6
35: ; preds = %6
%36 = load i32, i32* %5
%37 = srem i32 %36, 10
%38 = sub nsw i32 10, %37
store i32 %38, i32* %4
%39 = load i32, i32* %4
%40 = icmp eq i32 %39, 10
br i1 %40, label %41, label %42
41: ; preds = %35
store i32 0, i32* %4
br label %42
42: ; preds = %41, %35
%43 = load i32, i32* %4
ret i32 %43
}

LLVM IR (-O2):
define i32 @calc_check_digit(i8* nocapture %0) {
%2 = load i8, i8* %0
%3 = tail call i32 @ctoi(i8 zeroext %2)
%4 = mul nsw i32 %3, 3
%5 = getelementptr inbounds i8, i8* %0, i64 1
%6 = load i8, i8* %5
%7 = tail call i32 @ctoi(i8 zeroext %6)
%8 = add nsw i32 %7, %4
%9 = getelementptr inbounds i8, i8* %0, i64 2
%10 = load i8, i8* %9
%11 = tail call i32 @ctoi(i8 zeroext %10)
%12 = mul nsw i32 %11, 3
%13 = add nsw i32 %12, %8
%14 = getelementptr inbounds i8, i8* %0, i64 3
%15 = load i8, i8* %14
%16 = tail call i32 @ctoi(i8 zeroext %15)
%17 = add nsw i32 %16, %13
%18 = getelementptr inbounds i8, i8* %0, i64 4
%19 = load i8, i8* %18
%20 = tail call i32 @ctoi(i8 zeroext %19)
%21 = mul nsw i32 %20, 3
%22 = add nsw i32 %21, %17
%23 = getelementptr inbounds i8, i8* %0, i64 5
%24 = load i8, i8* %23
%25 = tail call i32 @ctoi(i8 zeroext %24)
%26 = add nsw i32 %25, %22
%27 = getelementptr inbounds i8, i8* %0, i64 6
%28 = load i8, i8* %27
%29 = tail call i32 @ctoi(i8 zeroext %28)
%30 = mul nsw i32 %29, 3
%31 = add nsw i32 %30, %26
%32 = getelementptr inbounds i8, i8* %0, i64 7
%33 = load i8, i8* %32
%34 = tail call i32 @ctoi(i8 zeroext %33)
%35 = add nsw i32 %34, %31
%36 = getelementptr inbounds i8, i8* %0, i64 8
%37 = load i8, i8* %36
%38 = tail call i32 @ctoi(i8 zeroext %37)
%39 = mul nsw i32 %38, 3
%40 = add nsw i32 %39, %35
%41 = getelementptr inbounds i8, i8* %0, i64 9
%42 = load i8, i8* %41
%43 = tail call i32 @ctoi(i8 zeroext %42)
%44 = add nsw i32 %43, %40
%45 = getelementptr inbounds i8, i8* %0, i64 10
%46 = load i8, i8* %45
%47 = tail call i32 @ctoi(i8 zeroext %46)
%48 = mul nsw i32 %47, 3
%49 = add nsw i32 %48, %44
%50 = getelementptr inbounds i8, i8* %0, i64 11
%51 = load i8, i8* %50
%52 = tail call i32 @ctoi(i8 zeroext %51)
%53 = add nsw i32 %52, %49
%54 = getelementptr inbounds i8, i8* %0, i64 12
%55 = load i8, i8* %54
%56 = tail call i32 @ctoi(i8 zeroext %55)
%57 = mul nsw i32 %56, 3
%58 = add nsw i32 %57, %53
%59 = srem i32 %58, 10
%60 = sub nsw i32 10, %59
%61 = icmp eq i32 %59, 0
%62 = select i1 %61, i32 0, i32 %60
ret i32 %62
}