#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 */
unsigned int ctoi(unsigned char);
char itoc(unsigned int);
int ustrlen(unsigned char *);
char isbn13_check(unsigned char source[]) {
  unsigned int i, weight, sum, check, h;
  sum = 0;
  weight = 1;
  h = ustrlen(source) - 1;
  for (i = 0; i < h; i++) {
    sum += ctoi(source[i]) * weight;
    if (weight == 1)
      weight = 3;
    else
      weight = 1;
  }
  check = sum % 10;
  check = 10 - check;
  return itoc(check);
}


x86 assembly (clang, -O0):
<isbn13_check>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x1,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <ustrlen@plt>
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jae    11b6 <isbn13_check+0x86>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
movzbl (%rax,%rcx,1),%edi
call   1030 <ctoi@plt>
imul   -0x10(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x10(%rbp)
jne    119c <isbn13_check+0x6c>
movl   $0x3,-0x10(%rbp)
jmp    11a3 <isbn13_check+0x73>
movl   $0x1,-0x10(%rbp)
jmp    11a8 <isbn13_check+0x78>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1160 <isbn13_check+0x30>
mov    -0x14(%rbp),%eax
mov    $0xa,%ecx
xor    %edx,%edx
div    %ecx
mov    %edx,-0x18(%rbp)
mov    $0xa,%eax
sub    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
call   1050 <itoc@plt>
movsbl %al,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<isbn13_check>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r14
call   1040 <ustrlen@plt>
add    $0xffffffff,%eax
je     11a4 <isbn13_check+0x74>
mov    %eax,%r15d
xor    %ebx,%ebx
mov    $0x1,%ebp
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movzbl (%r14,%rbx,1),%edi
call   1030 <ctoi@plt>
imul   %ebp,%eax
add    %eax,%r12d
xor    %eax,%eax
cmp    $0x1,%ebp
sete   %al
lea    (%rax,%rax,1),%ebp
add    $0x1,%ebp
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1160 <isbn13_check+0x30>
mov    %r12d,%eax
mov    $0xcccccccd,%ecx
imul   %rax,%rcx
shr    $0x23,%rcx
add    %ecx,%ecx
lea    (%rcx,%rcx,4),%edi
sub    %r12d,%edi
add    $0xa,%edi
jmp    11a9 <isbn13_check+0x79>
mov    $0xa,%edi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <itoc@plt>

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

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