#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 upc_check(char source[]) {
  unsigned int i, count, check_digit;
  count = 0;
  for (i = 0; i < strlen(source); 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):
<upc_check>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%eax
cmp    %ecx,%eax
jae    11b7 <upc_check+0x87>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
movsbl (%rax,%rcx,1),%edi
call   1030 <ctoi@plt>
add    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
jne    11a4 <upc_check+0x74>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
movsbl (%rax,%rcx,1),%edi
call   1030 <ctoi@plt>
shl    $0x1,%eax
add    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    11a9 <upc_check+0x79>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    114a <upc_check+0x1a>
mov    -0x10(%rbp),%eax
mov    $0xa,%ecx
xor    %edx,%edx
div    %ecx
mov    $0xa,%eax
sub    %edx,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0xa,-0x14(%rbp)
jne    11de <upc_check+0xae>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <itoc@plt>
movsbl %al,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<upc_check>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1040 <strlen@plt>
test   %eax,%eax
je     118b <upc_check+0x5b>
xor    %ebx,%ebx
xor    %ebp,%ebp
jmp    1166 <upc_check+0x36>
nopl   0x0(%rax)
add    $0x1,%rbx
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%eax
mov    %r15d,%ebp
cmp    %rax,%rbx
jae    118e <upc_check+0x5e>
movsbl (%r14,%rbx,1),%edi
call   1030 <ctoi@plt>
mov    %eax,%r15d
add    %ebp,%r15d
test   $0x1,%bl
jne    1150 <upc_check+0x20>
movsbl (%r14,%rbx,1),%edi
call   1030 <ctoi@plt>
lea    (%r15,%rax,2),%r15d
jmp    1150 <upc_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
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <itoc@plt>

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

LLVM IR (-O2):
define signext i8 @upc_check(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = icmp eq i32 %2, 0
br i1 %3, label %24, label %4
4: ; preds = %1, %18
%5 = phi i64 [ %20, %18 ], [ 0, %1 ]
%6 = phi i32 [ %19, %18 ], [ 0, %1 ]
%7 = getelementptr inbounds i8, i8* %0, i64 %5
%8 = load i8, i8* %7
%9 = tail call i32 @ctoi(i8 signext %8)
%10 = add i32 %9, %6
%11 = and i64 %5, 1
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %4
%14 = load i8, i8* %7
%15 = tail call i32 @ctoi(i8 signext %14)
%16 = shl nsw i32 %15, 1
%17 = add i32 %16, %10
br label %18
18: ; preds = %4, %13
%19 = phi i32 [ %10, %4 ], [ %17, %13 ]
%20 = add nuw nsw i64 %5, 1
%21 = tail call i32 @strlen(i8* %0)
%22 = zext i32 %21 to i64
%23 = icmp ult i64 %20, %22
br i1 %23, label %4, label %24
24: ; preds = %18, %1
%25 = phi i32 [ 0, %1 ], [ %19, %18 ]
%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
}