#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*/ snprintf(char *, int, char *, unsigned int);
void nmea_add_checksum(char *sentence) {
  char *p = sentence;
  if (*p == '$') {
    unsigned char sum = '\0';
    char c;
    p++;
    while (((c = *p) != '*') && (c != '\0')) {
      sum ^= c;
      p++;
    }
    *p++ = '*';
    (void)snprintf(p, 5, "%02X\r\n", (unsigned int)sum);
  }
}


x86 assembly (clang, -O0):
<nmea_add_checksum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x24,%eax
jne    11c4 <nmea_add_checksum+0xb4>
movb   $0x0,-0x11(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x12(%rbp)
movsbl %al,%ecx
xor    %eax,%eax
cmp    $0x2a,%ecx
mov    %al,-0x13(%rbp)
je     116b <nmea_add_checksum+0x5b>
movsbl -0x12(%rbp),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x13(%rbp)
mov    -0x13(%rbp),%al
test   $0x1,%al
jne    117b <nmea_add_checksum+0x6b>
jmp    1199 <nmea_add_checksum+0x89>
movsbl -0x12(%rbp),%ecx
movzbl -0x11(%rbp),%eax
xor    %ecx,%eax
mov    %al,-0x11(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1144 <nmea_add_checksum+0x34>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x2a,(%rax)
mov    -0x10(%rbp),%rdi
movzbl -0x11(%rbp),%ecx
mov    $0x5,%esi
lea    0xe41(%rip),%rdx
call   1030 <snprintf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nmea_add_checksum>:
cmpb   $0x24,(%rdi)
jne    1134 <nmea_add_checksum+0x24>
add    $0x2,%rdi
xor    %ecx,%ecx
nopl   0x0(%rax,%rax,1)
movzbl -0x1(%rdi),%eax
test   %eax,%eax
je     1135 <nmea_add_checksum+0x25>
cmp    $0x2a,%al
je     1135 <nmea_add_checksum+0x25>
xor    %eax,%ecx
add    $0x1,%rdi
jmp    1120 <nmea_add_checksum+0x10>
ret
movb   $0x2a,-0x1(%rdi)
lea    0xec0(%rip),%rdx
mov    $0x5,%esi
jmp    1030 <snprintf@plt>

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

LLVM IR (-O2):
@.str = [7 x i8] c"%02X\0D\0A\00"
define void @nmea_add_checksum(i8* %0) {
%2 = load i8, i8* %0
%3 = icmp eq i8 %2, 36
br i1 %3, label %4, label %15
4: ; preds = %1, %9
%5 = phi i32 [ %11, %9 ], [ 0, %1 ]
%6 = phi i8* [ %7, %9 ], [ %0, %1 ]
%7 = getelementptr inbounds i8, i8* %6, i64 1
%8 = load i8, i8* %7
switch i8 %8, label %9 [
i8 42, label %12
i8 0, label %12
]
9: ; preds = %4
%10 = zext i8 %8 to i32
%11 = xor i32 %5, %10
br label %4
12: ; preds = %4, %4
%13 = getelementptr inbounds i8, i8* %6, i64 2
store i8 42, i8* %7
%14 = tail call i32 @snprintf(i8* %13, i32 5, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %5)
br label %15
15: ; preds = %12, %1
ret void
}