#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;
  if (check == 10)
    check = 0;
  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)
cmpl   $0xa,-0x18(%rbp)
jne    11e1 <isbn13_check+0xb1>
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):
<isbn13_check>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r14
call   1040 <ustrlen@plt>
add    $0xffffffff,%eax
je     11ab <isbn13_check+0x7b>
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),%eax
sub    %eax,%r12d
je     11ab <isbn13_check+0x7b>
mov    $0xa,%edi
sub    %r12d,%edi
jmp    11ad <isbn13_check+0x7d>
xor    %edi,%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 = icmp eq i32 %39, 10
br i1 %40, label %41, label %42
41: ; preds = %34
store i32 0, i32* %6
br label %42
42: ; preds = %41, %34
%43 = load i32, i32* %6
%44 = call signext i8 @itoc(i32 %43)
ret i8 %44
}

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 %24, 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
%23 = icmp eq i32 %21, 0
br i1 %23, label %24, label %25
24: ; preds = %1, %20
br label %25
25: ; preds = %20, %24
%26 = phi i32 [ 0, %24 ], [ %22, %20 ]
%27 = tail call signext i8 @itoc(i32 %26)
ret i8 %27
}