#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 const);
char itoc(int);
char msi_check_digit_mod11(const unsigned char source[], const int length,
                           const int wrap) {
  int i, x = 0, weight = 2;
  for (i = length - 1; i >= 0; i--) {
    x += weight * ctoi(source[i]);
    weight++;
    if (weight > wrap) {
      weight = 2;
    }
  }
  return itoc((11 - x % 11) % 11);
}


x86 assembly (clang, -O0):
<msi_check_digit_mod11>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x2,-0x1c(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     11a7 <msi_check_digit_mod11+0x87>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movzbl (%rax,%rcx,1),%edi
call   1030 <ctoi@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
imul   %ecx,%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x10(%rbp),%eax
jle    1194 <msi_check_digit_mod11+0x74>
movl   $0x2,-0x1c(%rbp)
jmp    1199 <msi_check_digit_mod11+0x79>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1149 <msi_check_digit_mod11+0x29>
mov    -0x18(%rbp),%eax
mov    $0xb,%ecx
cltd
idiv   %ecx
mov    $0xb,%eax
sub    %edx,%eax
mov    $0xb,%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_mod11>:
test   %esi,%esi
jle    11d1 <msi_check_digit_mod11+0xb1>
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rdi,%r15
mov    %esi,%ebp
add    $0x1,%rbp
xor    %r13d,%r13d
mov    $0x2,%r12d
mov    $0x2,%ebx
nopl   (%rax)
lea    -0x2(%rbp),%eax
movzbl (%r15,%rax,1),%edi
call   1030 <ctoi@plt>
imul   %ebx,%eax
add    %eax,%r13d
cmp    %r14d,%ebx
lea    0x1(%rbx),%eax
cmovge %r12d,%eax
add    $0xffffffffffffffff,%rbp
mov    %eax,%ebx
cmp    $0x1,%rbp
ja     1150 <msi_check_digit_mod11+0x30>
movslq %r13d,%rax
imul   $0x2e8ba2e9,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
shr    $0x21,%rcx
add    %edx,%ecx
lea    (%rcx,%rcx,4),%edx
lea    (%rcx,%rdx,2),%ecx
sub    %ecx,%eax
mov    $0xb,%cl
sub    %al,%cl
movzbl %cl,%eax
imul   $0x75,%eax,%ecx
shr    $0x8,%ecx
mov    %eax,%edx
sub    %cl,%dl
shr    %dl
add    %cl,%dl
shr    $0x3,%dl
movzbl %dl,%ecx
lea    (%rcx,%rcx,4),%edx
lea    (%rcx,%rdx,2),%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):
define signext i8 @msi_check_digit_mod11(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i32 0, i32* %8
store i32 2, i32* %9
%10 = load i32, i32* %5
%11 = sub nsw i32 %10, 1
store i32 %11, i32* %7
br label %12
12: ; preds = %33, %3
%13 = load i32, i32* %7
%14 = icmp sge i32 %13, 0
br i1 %14, label %15, label %36
15: ; preds = %12
%16 = load i32, i32* %9
%17 = load i8*, i8** %4
%18 = load i32, i32* %7
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = load i8, i8* %20
%22 = call i32 @ctoi(i8 zeroext %21)
%23 = mul nsw i32 %16, %22
%24 = load i32, i32* %8
%25 = add nsw i32 %24, %23
store i32 %25, i32* %8
%26 = load i32, i32* %9
%27 = add nsw i32 %26, 1
store i32 %27, i32* %9
%28 = load i32, i32* %9
%29 = load i32, i32* %6
%30 = icmp sgt i32 %28, %29
br i1 %30, label %31, label %32
31: ; preds = %15
store i32 2, i32* %9
br label %32
32: ; preds = %31, %15
br label %33
33: ; preds = %32
%34 = load i32, i32* %7
%35 = add nsw i32 %34, -1
store i32 %35, i32* %7
br label %12
36: ; preds = %12
%37 = load i32, i32* %8
%38 = srem i32 %37, 11
%39 = sub nsw i32 11, %38
%40 = srem i32 %39, 11
%41 = call signext i8 @itoc(i32 %40)
ret i8 %41
}

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