#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, unsigned int);
int gethexbyte(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int chk_cksum(FILE *fp, unsigned int cksum) {
  int n;
  if ((n = gethexbyte(fp)) < 0)
    return -1;
  cksum = ~cksum & 0xff;
  if (cksum != n) {
    fprintf(stderr, "Bad cksum[%02x]\n", cksum);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<chk_cksum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <gethexbyte@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    1150 <chk_cksum+0x30>
movl   $0xffffffff,-0x4(%rbp)
jmp    1195 <chk_cksum+0x75>
mov    -0x14(%rbp),%eax
xor    $0xffffffff,%eax
and    $0xff,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
je     118e <chk_cksum+0x6e>
mov    0x2e6f(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xe83(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1195 <chk_cksum+0x75>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chk_cksum>:
push   %rbx
mov    %esi,%ebx
call   1040 <gethexbyte@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
test   %ecx,%ecx
js     1158 <chk_cksum+0x38>
not    %ebx
movzbl %bl,%edx
xor    %eax,%eax
cmp    %ecx,%edx
je     1158 <chk_cksum+0x38>
mov    0x2e9b(%rip),%rax
mov    (%rax),%edi
lea    0xeb2(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0xffffffff,%eax
pop    %rbx
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [17 x i8] c"Bad cksum[%02x]\0A\00"
define i32 @chk_cksum(i32* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
%7 = load i32*, i32** %4
%8 = call i32 @gethexbyte(i32* %7)
store i32 %8, i32* %6
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %2
store i32 -1, i32* %3
br label %23
11: ; preds = %2
%12 = load i32, i32* %5
%13 = xor i32 %12, -1
%14 = and i32 %13, 255
store i32 %14, i32* %5
%15 = load i32, i32* %5
%16 = load i32, i32* %6
%17 = icmp ne i32 %15, %16
br i1 %17, label %18, label %22
18: ; preds = %11
%19 = load i32, i32* @stderr
%20 = load i32, i32* %5
%21 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %20)
store i32 -1, i32* %3
br label %23
22: ; preds = %11
store i32 0, i32* %3
br label %23
23: ; preds = %22, %18, %10
%24 = load i32, i32* %3
ret i32 %24
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [17 x i8] c"Bad cksum[%02x]\0A\00"
define i32 @chk_cksum(i32* %0, i32 %1) {
%3 = tail call i32 @gethexbyte(i32* %0)
%4 = icmp slt i32 %3, 0
br i1 %4, label %12, label %5
5: ; preds = %2
%6 = and i32 %1, 255
%7 = xor i32 %6, 255
%8 = icmp eq i32 %7, %3
br i1 %8, label %12, label %9
9: ; preds = %5
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %7)
br label %12
12: ; preds = %5, %2, %9
%13 = phi i32 [ -1, %9 ], [ -1, %2 ], [ 0, %5 ]
ret i32 %13
}