#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);
__attribute__((visibility("hidden"))) char
gs1_check_digit(const unsigned char source[], const int length) {
  int i;
  int count = 0;
  int factor = length & 1 ? 3 : 1;
  for (i = 0; i < length; i++) {
    count += factor * ctoi(source[i]);
    factor = factor == 1 ? 3 : 1;
  }
  return itoc((10 - (count % 10)) % 10);
}


x86 assembly (clang, -O0):
<gs1_check_digit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0xc(%rbp),%edx
and    $0x1,%edx
mov    $0x1,%eax
mov    $0x3,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11ab <gs1_check_digit+0x8b>
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movzbl (%rax,%rcx,1),%edi
call   1030 <ctoi@plt>
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
imul   %ecx,%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edx
mov    $0x1,%eax
mov    $0x3,%ecx
cmp    $0x1,%edx
cmove  %ecx,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1156 <gs1_check_digit+0x36>
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):
<gs1_check_digit>:
test   %esi,%esi
jle    11c0 <gs1_check_digit+0xa0>
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r14
mov    %esi,%r15d
and    $0x1,%esi
lea    (%rsi,%rsi,1),%r12d
add    $0x1,%r12d
xor    %ebx,%ebx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
movzbl (%r14,%rbx,1),%edi
call   1030 <ctoi@plt>
imul   %r12d,%eax
add    %eax,%ebp
xor    %eax,%eax
cmp    $0x1,%r12d
sete   %al
lea    (%rax,%rax,1),%r12d
add    $0x1,%r12d
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1150 <gs1_check_digit+0x30>
movslq %ebp,%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
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <itoc@plt>
xor    %edi,%edi
jmp    1040 <itoc@plt>

LLVM IR (-O0):
define hidden signext i8 @gs1_check_digit(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
%8 = load i32, i32* %4
%9 = and i32 %8, 1
%10 = icmp ne i32 %9, 0
%11 = zext i1 %10 to i64
%12 = select i1 %10, i32 3, i32 1
store i32 %12, i32* %7
store i32 0, i32* %5
br label %13
13: ; preds = %32, %2
%14 = load i32, i32* %5
%15 = load i32, i32* %4
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %35
17: ; preds = %13
%18 = load i32, i32* %7
%19 = load i8*, i8** %3
%20 = load i32, i32* %5
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %19, i64 %21
%23 = load i8, i8* %22
%24 = call i32 @ctoi(i8 zeroext %23)
%25 = mul nsw i32 %18, %24
%26 = load i32, i32* %6
%27 = add nsw i32 %26, %25
store i32 %27, i32* %6
%28 = load i32, i32* %7
%29 = icmp eq i32 %28, 1
%30 = zext i1 %29 to i64
%31 = select i1 %29, i32 3, i32 1
store i32 %31, i32* %7
br label %32
32: ; preds = %17
%33 = load i32, i32* %5
%34 = add nsw i32 %33, 1
store i32 %34, i32* %5
br label %13
35: ; preds = %13
%36 = load i32, i32* %6
%37 = srem i32 %36, 10
%38 = sub nsw i32 10, %37
%39 = srem i32 %38, 10
%40 = call signext i8 @itoc(i32 %39)
ret i8 %40
}

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