#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 */
scalar_t__ parse_hex(char *, int);
int strlen(char *);
unsigned char checksum(char *str) {
  unsigned int i;
  unsigned char acum = 0;
  for (i = 0; i < ((strlen(str) - 1) / 2); i++) {
    acum += parse_hex(str + 1 + (i * 2), 2);
  }
  return acum;
}


x86 assembly (clang, -O0):
<checksum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movb   $0x0,-0xd(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x14(%rbp),%eax
cmp    %ecx,%eax
jae    1196 <checksum+0x76>
mov    -0x8(%rbp),%rdi
add    $0x1,%rdi
mov    -0xc(%rbp),%eax
shl    $0x1,%eax
mov    %eax,%eax
add    %rax,%rdi
mov    $0x2,%esi
call   1040 <parse_hex@plt>
mov    %rax,%rcx
movzbl -0xd(%rbp),%eax
add    %rcx,%rax
mov    %al,-0xd(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1137 <checksum+0x17>
movzbl -0xd(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checksum>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r15
call   1030 <strlen@plt>
cmp    $0x3,%eax
jae    113a <checksum+0x1a>
xor    %r12d,%r12d
jmp    1189 <checksum+0x69>
lea    0x1(%r15),%r14
xor    %ebx,%ebx
xor    %ebp,%ebp
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebx,%edi
and    $0xfffffffe,%edi
add    %r14,%rdi
mov    $0x2,%esi
call   1040 <parse_hex@plt>
add    %al,%r12b
add    $0x1,%rbp
mov    %r15,%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
add    $0xffffffff,%ecx
shr    $0x1f,%ecx
add    %ecx,%eax
add    $0xffffffff,%eax
sar    %eax
add    $0x2,%rbx
cmp    %rax,%rbp
jb     1150 <checksum+0x30>
mov    %r12d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define zeroext i8 @checksum(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8
store i8* %0, i8** %2
store i8 0, i8* %4
store i32 0, i32* %3
br label %5
5: ; preds = %24, %1
%6 = load i32, i32* %3
%7 = load i8*, i8** %2
%8 = call i32 @strlen(i8* %7)
%9 = sub nsw i32 %8, 1
%10 = sdiv i32 %9, 2
%11 = icmp ult i32 %6, %10
br i1 %11, label %12, label %27
12: ; preds = %5
%13 = load i8*, i8** %2
%14 = getelementptr inbounds i8, i8* %13, i64 1
%15 = load i32, i32* %3
%16 = mul i32 %15, 2
%17 = zext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %14, i64 %17
%19 = call i64 @parse_hex(i8* %18, i32 2)
%20 = load i8, i8* %4
%21 = zext i8 %20 to i64
%22 = add nsw i64 %21, %19
%23 = trunc i64 %22 to i8
store i8 %23, i8* %4
br label %24
24: ; preds = %12
%25 = load i32, i32* %3
%26 = add i32 %25, 1
store i32 %26, i32* %3
br label %5
27: ; preds = %5
%28 = load i8, i8* %4
ret i8 %28
}

LLVM IR (-O2):
define zeroext i8 @checksum(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = icmp ult i32 %2, 3
br i1 %3, label %21, label %4
4: ; preds = %1
%5 = getelementptr inbounds i8, i8* %0, i64 1
br label %6
6: ; preds = %4, %6
%7 = phi i64 [ 0, %4 ], [ %15, %6 ]
%8 = phi i8 [ 0, %4 ], [ %14, %6 ]
%9 = shl nuw i64 %7, 1
%10 = and i64 %9, 4294967294
%11 = getelementptr inbounds i8, i8* %5, i64 %10
%12 = tail call i64 @parse_hex(i8* %11, i32 2)
%13 = trunc i64 %12 to i8
%14 = add i8 %8, %13
%15 = add nuw nsw i64 %7, 1
%16 = tail call i32 @strlen(i8* %0)
%17 = add nsw i32 %16, -1
%18 = sdiv i32 %17, 2
%19 = zext i32 %18 to i64
%20 = icmp ult i64 %15, %19
br i1 %20, label %6, label %21
21: ; preds = %6, %1
%22 = phi i8 [ 0, %1 ], [ %14, %6 ]
ret i8 %22
}