#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(char);
char itoc(unsigned int);
unsigned int strlen(char *);
char ean_check(char source[]) {
  int i;
  unsigned int h, count, check_digit;
  count = 0;
  h = strlen(source);
  for (i = h - 1; i >= 0; i--) {
    count += ctoi(source[i]);
    if (i & 1) {
      count += 2 * ctoi(source[i]);
    }
  }
  check_digit = 10 - (count % 10);
  if (check_digit == 10) {
    check_digit = 0;
  }
  return itoc(check_digit);
}


x86 assembly (clang, -O0):
<ean_check>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     11b5 <ean_check+0x85>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <ctoi@plt>
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     11a2 <ean_check+0x72>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <ctoi@plt>
shl    $0x1,%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    11a7 <ean_check+0x77>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    1158 <ean_check+0x28>
mov    -0x14(%rbp),%eax
mov    $0xa,%ecx
xor    %edx,%edx
div    %ecx
mov    $0xa,%eax
sub    %edx,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0xa,-0x18(%rbp)
jne    11dc <ean_check+0xac>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%edi
call   1050 <itoc@plt>
movsbl %al,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ean_check>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r14
call   1040 <strlen@plt>
mov    %eax,%ebp
add    $0xffffffff,%ebp
js     1182 <ean_check+0x52>
xor    %r12d,%r12d
jmp    115a <ean_check+0x2a>
nopl   0x0(%rax)
add    $0xffffffff,%ebx
mov    %ebx,%ebp
mov    %r15d,%r12d
js     1185 <ean_check+0x55>
mov    %ebp,%ebx
movsbl (%r14,%rbx,1),%edi
call   1030 <ctoi@plt>
mov    %eax,%r15d
add    %r12d,%r15d
test   $0x1,%bpl
je     1150 <ean_check+0x20>
movsbl (%r14,%rbx,1),%edi
call   1030 <ctoi@plt>
lea    (%r15,%rax,2),%r15d
jmp    1150 <ean_check+0x20>
xor    %r15d,%r15d
mov    %r15d,%eax
mov    $0xcccccccd,%ecx
imul   %rax,%rcx
shr    $0x23,%rcx
add    %ecx,%ecx
lea    (%rcx,%rcx,4),%eax
sub    %eax,%r15d
mov    $0xa,%edi
sub    %r15d,%edi
test   %r15d,%r15d
cmove  %r15d,%edi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <itoc@plt>

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

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