#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 /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ sscanf(char *, char *, unsigned int *);
int checksum(char asciiHex[], int from, int to, char *checksumAscii) {
  int i, sum;
  unsigned int b;
  sum = 0;
  for (i = from; i < to; i += 2) {
    sscanf(asciiHex + i, "%2x%*s", &b);
    sum += b;
  }
  sum = 0x100 - sum;
  sum &= 0xFF;
  sprintf(checksumAscii, "%2X", sum);
  return sum;
}


x86 assembly (clang, -O0):
<checksum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    1181 <checksum+0x61>
mov    -0x8(%rbp),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
lea    0xe9f(%rip),%rsi
lea    -0x24(%rbp),%rdx
call   1030 <sscanf@plt>
mov    -0x24(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x1c(%rbp)
jmp    1143 <checksum+0x23>
mov    $0x100,%eax
sub    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
and    $0xff,%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%edx
lea    0xe62(%rip),%rsi
call   1040 <sprintf@plt>
mov    -0x20(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checksum>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,0x10(%rsp)
xor    %ebp,%ebp
cmp    %edx,%esi
jge    116c <checksum+0x4c>
mov    %rdi,%r13
movslq %esi,%rbx
movslq %edx,%r14
xor    %ebp,%ebp
lea    0xeb5(%rip),%r15
lea    0xc(%rsp),%r12
lea    (%rbx,%r13,1),%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1030 <sscanf@plt>
add    0xc(%rsp),%ebp
add    $0x2,%rbx
cmp    %r14,%rbx
jl     1150 <checksum+0x30>
neg    %ebp
movzbl %bpl,%ebx
lea    0xe8e(%rip),%rsi
mov    0x10(%rsp),%rdi
mov    %ebx,%edx
call   1040 <sprintf@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"%2x%*s\00"
@.str.1 = [4 x i8] c"%2X\00"
define i32 @checksum(i8* %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i8* %3, i8** %8
store i32 0, i32* %10
%12 = load i32, i32* %6
store i32 %12, i32* %9
br label %13
13: ; preds = %26, %4
%14 = load i32, i32* %9
%15 = load i32, i32* %7
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %29
17: ; preds = %13
%18 = load i8*, i8** %5
%19 = load i32, i32* %9
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
%22 = call i32 @sscanf(i8* %21, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32* %11)
%23 = load i32, i32* %11
%24 = load i32, i32* %10
%25 = add i32 %24, %23
store i32 %25, i32* %10
br label %26
26: ; preds = %17
%27 = load i32, i32* %9
%28 = add nsw i32 %27, 2
store i32 %28, i32* %9
br label %13
29: ; preds = %13
%30 = load i32, i32* %10
%31 = sub nsw i32 256, %30
store i32 %31, i32* %10
%32 = load i32, i32* %10
%33 = and i32 %32, 255
store i32 %33, i32* %10
%34 = load i8*, i8** %8
%35 = load i32, i32* %10
%36 = call i32 @sprintf(i8* %34, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %35)
%37 = load i32, i32* %10
ret i32 %37
}

LLVM IR (-O2):
@.str = [7 x i8] c"%2x%*s\00"
@.str.1 = [4 x i8] c"%2X\00"
define i32 @checksum(i8* nocapture %0, i32 %1, i32 %2, i8* nocapture writeonly %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
%7 = icmp slt i32 %1, %2
br i1 %7, label %8, label %20
8: ; preds = %4
%9 = sext i32 %1 to i64
%10 = sext i32 %2 to i64
br label %11
11: ; preds = %8, %11
%12 = phi i64 [ %9, %8 ], [ %18, %11 ]
%13 = phi i32 [ 0, %8 ], [ %17, %11 ]
%14 = getelementptr inbounds i8, i8* %0, i64 %12
%15 = call i32 @sscanf(i8* %14, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32* %5)
%16 = load i32, i32* %5
%17 = add i32 %16, %13
%18 = add i64 %12, 2
%19 = icmp slt i64 %18, %10
br i1 %19, label %11, label %20
20: ; preds = %11, %4
%21 = phi i32 [ 0, %4 ], [ %17, %11 ]
%22 = sub i32 0, %21
%23 = and i32 %22, 255
%24 = call i32 @sprintf(i8* dereferenceable(1) %3, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %23)
ret i32 %23
}