#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 uint32_t;

/* Variables and functions */
int ETH_HLEN;
int calculate_icmpv6_checksum(unsigned char *, int);
int verify_and_fix_icmpv6_checksum(unsigned char *buf, int totallen) {
  unsigned long old_checksum =
      buf[ETH_HLEN + 8 + 32 + 2] * 256 + buf[ETH_HLEN + 8 + 32 + 3];
  uint32_t new_checksum = calculate_icmpv6_checksum(buf, totallen);
  buf[ETH_HLEN + 8 + 32 + 2] = new_checksum >> 8;
  buf[ETH_HLEN + 8 + 32 + 3] = new_checksum & 0xFF;
  return (new_checksum == old_checksum) ? 0 : -1;
}


x86 assembly (clang, -O0):
<verify_and_fix_icmpv6_checksum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    0x2eb6(%rip),%rcx
mov    (%rcx),%ecx
add    $0x8,%ecx
add    $0x20,%ecx
add    $0x2,%ecx
movslq %ecx,%rcx
movzbl (%rax,%rcx,1),%eax
shl    $0x8,%eax
mov    -0x8(%rbp),%rcx
mov    0x2e96(%rip),%rdx
mov    (%rdx),%edx
add    $0x8,%edx
add    $0x20,%edx
add    $0x3,%edx
movslq %edx,%rdx
movzbl (%rcx,%rdx,1),%ecx
add    %ecx,%eax
cltq
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1030 <calculate_icmpv6_checksum@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
sar    $0x8,%eax
mov    %al,%dl
mov    -0x8(%rbp),%rax
mov    0x2e5a(%rip),%rcx
mov    (%rcx),%ecx
add    $0x8,%ecx
add    $0x20,%ecx
add    $0x2,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
and    $0xff,%eax
mov    %al,%dl
mov    -0x8(%rbp),%rax
mov    0x2e34(%rip),%rcx
mov    (%rcx),%ecx
add    $0x8,%ecx
add    $0x20,%ecx
add    $0x3,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
movslq -0x1c(%rbp),%rdx
mov    -0x18(%rbp),%rsi
mov    $0xffffffff,%eax
xor    %ecx,%ecx
cmp    %rsi,%rdx
cmove  %ecx,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<verify_and_fix_icmpv6_checksum>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbp
mov    0x2ec2(%rip),%r14
movslq (%r14),%rax
movzwl 0x2a(%rax,%rdi,1),%ebx
shl    $0x30,%rbx
bswap  %rbx
call   1030 <calculate_icmpv6_checksum@plt>
movslq (%r14),%rcx
mov    %ah,0x2a(%rcx,%rbp,1)
movslq (%r14),%rcx
mov    %al,0x2b(%rcx,%rbp,1)
movslq %eax,%rcx
xor    %eax,%eax
cmp    %rcx,%rbx
setne  %al
neg    %eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@ETH_HLEN = global i32 0
define i32 @verify_and_fix_icmpv6_checksum(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i64
%6 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = load i8*, i8** %3
%8 = load i32, i32* @ETH_HLEN
%9 = add nsw i32 %8, 8
%10 = add nsw i32 %9, 32
%11 = add nsw i32 %10, 2
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %7, i64 %12
%14 = load i8, i8* %13
%15 = zext i8 %14 to i32
%16 = mul nsw i32 %15, 256
%17 = load i8*, i8** %3
%18 = load i32, i32* @ETH_HLEN
%19 = add nsw i32 %18, 8
%20 = add nsw i32 %19, 32
%21 = add nsw i32 %20, 3
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %17, i64 %22
%24 = load i8, i8* %23
%25 = zext i8 %24 to i32
%26 = add nsw i32 %16, %25
%27 = sext i32 %26 to i64
store i64 %27, i64* %5
%28 = load i8*, i8** %3
%29 = load i32, i32* %4
%30 = call i32 @calculate_icmpv6_checksum(i8* %28, i32 %29)
store i32 %30, i32* %6
%31 = load i32, i32* %6
%32 = ashr i32 %31, 8
%33 = trunc i32 %32 to i8
%34 = load i8*, i8** %3
%35 = load i32, i32* @ETH_HLEN
%36 = add nsw i32 %35, 8
%37 = add nsw i32 %36, 32
%38 = add nsw i32 %37, 2
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %34, i64 %39
store i8 %33, i8* %40
%41 = load i32, i32* %6
%42 = and i32 %41, 255
%43 = trunc i32 %42 to i8
%44 = load i8*, i8** %3
%45 = load i32, i32* @ETH_HLEN
%46 = add nsw i32 %45, 8
%47 = add nsw i32 %46, 32
%48 = add nsw i32 %47, 3
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %44, i64 %49
store i8 %43, i8* %50
%51 = load i32, i32* %6
%52 = sext i32 %51 to i64
%53 = load i64, i64* %5
%54 = icmp eq i64 %52, %53
%55 = zext i1 %54 to i64
%56 = select i1 %54, i32 0, i32 -1
ret i32 %56
}

LLVM IR (-O2):
@ETH_HLEN = global i32 0
define i32 @verify_and_fix_icmpv6_checksum(i8* %0, i32 %1) {
%3 = load i32, i32* @ETH_HLEN
%4 = add nsw i32 %3, 42
%5 = sext i32 %4 to i64
%6 = getelementptr inbounds i8, i8* %0, i64 %5
%7 = load i8, i8* %6
%8 = zext i8 %7 to i64
%9 = shl nuw nsw i64 %8, 8
%10 = add nsw i32 %3, 43
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %0, i64 %11
%13 = load i8, i8* %12
%14 = zext i8 %13 to i64
%15 = or i64 %9, %14
%16 = tail call i32 @calculate_icmpv6_checksum(i8* %0, i32 %1)
%17 = lshr i32 %16, 8
%18 = trunc i32 %17 to i8
%19 = load i32, i32* @ETH_HLEN
%20 = add nsw i32 %19, 42
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %0, i64 %21
store i8 %18, i8* %22
%23 = trunc i32 %16 to i8
%24 = load i32, i32* @ETH_HLEN
%25 = add nsw i32 %24, 43
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %0, i64 %26
store i8 %23, i8* %27
%28 = sext i32 %16 to i64
%29 = icmp ne i64 %15, %28
%30 = sext i1 %29 to i32
ret i32 %30
}