#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*/ sscanf(char *, char *, unsigned char *);
int valid_checksum(char *str) {
  char *ptr = str;
  unsigned char c_cksum = 0;
  unsigned char r_cksum;
  if (str[0] != '$')
    return 0;
  str++;
  for (ptr = str; *ptr && (*ptr != '*'); ptr++)
    c_cksum ^= *ptr;
  sscanf(ptr, "*%02hhx", &r_cksum);
  return c_cksum == r_cksum;
}


x86 assembly (clang, -O0):
<valid_checksum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
movb   $0x0,-0x19(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x24,%eax
je     1144 <valid_checksum+0x34>
movl   $0x0,-0x4(%rbp)
jmp    11d7 <valid_checksum+0xc7>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x1b(%rbp)
je     117d <valid_checksum+0x6d>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2a,%eax
setne  %al
mov    %al,-0x1b(%rbp)
mov    -0x1b(%rbp),%al
test   $0x1,%al
jne    118d <valid_checksum+0x7d>
jmp    11ae <valid_checksum+0x9e>
mov    -0x18(%rbp),%rax
movsbl (%rax),%ecx
movzbl -0x19(%rbp),%eax
xor    %ecx,%eax
mov    %al,-0x19(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1158 <valid_checksum+0x48>
mov    -0x18(%rbp),%rdi
lea    0xe47(%rip),%rsi
lea    -0x1a(%rbp),%rdx
call   1030 <sscanf@plt>
movzbl -0x19(%rbp),%eax
movzbl -0x1a(%rbp),%ecx
cmp    %ecx,%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<valid_checksum>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
cmpb   $0x24,(%rdi)
jne    1160 <valid_checksum+0x50>
add    $0x1,%rdi
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movzbl (%rdi),%eax
test   %eax,%eax
je     1143 <valid_checksum+0x33>
cmp    $0x2a,%al
je     1143 <valid_checksum+0x33>
xor    %eax,%ebx
add    $0x1,%rdi
jmp    1130 <valid_checksum+0x20>
lea    0xeb6(%rip),%rsi
lea    0xf(%rsp),%rdx
call   1030 <sscanf@plt>
movzbl 0xf(%rsp),%ecx
xor    %eax,%eax
cmp    %ecx,%ebx
sete   %al
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [8 x i8] c"*%02hhx\00"
define i32 @valid_checksum(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8
%6 = alloca i8
store i8* %0, i8** %3
%7 = load i8*, i8** %3
store i8* %7, i8** %4
store i8 0, i8* %5
%8 = load i8*, i8** %3
%9 = getelementptr inbounds i8, i8* %8, i64 0
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
%12 = icmp ne i32 %11, 36
br i1 %12, label %13, label %14
13: ; preds = %1
store i32 0, i32* %2
br label %50
14: ; preds = %1
%15 = load i8*, i8** %3
%16 = getelementptr inbounds i8, i8* %15, i32 1
store i8* %16, i8** %3
%17 = load i8*, i8** %3
store i8* %17, i8** %4
br label %18
18: ; preds = %38, %14
%19 = load i8*, i8** %4
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %18
%24 = load i8*, i8** %4
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = icmp ne i32 %26, 42
br label %28
28: ; preds = %23, %18
%29 = phi i1 [ false, %18 ], [ %27, %23 ]
br i1 %29, label %30, label %41
30: ; preds = %28
%31 = load i8*, i8** %4
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = load i8, i8* %5
%35 = zext i8 %34 to i32
%36 = xor i32 %35, %33
%37 = trunc i32 %36 to i8
store i8 %37, i8* %5
br label %38
38: ; preds = %30
%39 = load i8*, i8** %4
%40 = getelementptr inbounds i8, i8* %39, i32 1
store i8* %40, i8** %4
br label %18
41: ; preds = %28
%42 = load i8*, i8** %4
%43 = call i32 @sscanf(i8* %42, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %6)
%44 = load i8, i8* %5
%45 = zext i8 %44 to i32
%46 = load i8, i8* %6
%47 = zext i8 %46 to i32
%48 = icmp eq i32 %45, %47
%49 = zext i1 %48 to i32
store i32 %49, i32* %2
br label %50
50: ; preds = %41, %13
%51 = load i32, i32* %2
ret i32 %51
}

LLVM IR (-O2):
@.str = [8 x i8] c"*%02hhx\00"
define i32 @valid_checksum(i8* nocapture %0) {
%2 = alloca i8
%3 = load i8, i8* %0
%4 = icmp eq i8 %3, 36
br i1 %4, label %5, label %19
5: ; preds = %1, %10
%6 = phi i8* [ %8, %10 ], [ %0, %1 ]
%7 = phi i32 [ %12, %10 ], [ 0, %1 ]
%8 = getelementptr inbounds i8, i8* %6, i64 1
%9 = load i8, i8* %8
switch i8 %9, label %10 [
i8 0, label %13
i8 42, label %13
]
10: ; preds = %5
%11 = zext i8 %9 to i32
%12 = xor i32 %7, %11
br label %5
13: ; preds = %5, %5
%14 = call i32 @sscanf(i8* %8, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %2)
%15 = load i8, i8* %2
%16 = zext i8 %15 to i32
%17 = icmp eq i32 %7, %16
%18 = zext i1 %17 to i32
br label %19
19: ; preds = %1, %13
%20 = phi i32 [ %18, %13 ], [ 0, %1 ]
ret i32 %20
}