#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*/ uint8_t;
typedef int /*<<< orphan*/ uint16_t;
struct icmp {
  int /*<<< orphan*/ icmp_seq;
  int /*<<< orphan*/ icmp_id;
  int /*<<< orphan*/ icmp_code;
  int /*<<< orphan*/ icmp_type;
};

/* Variables and functions */
int IP_MAXPACKET;
int /*<<< orphan*/ checksum(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ memcpy(char *, int /*<<< orphan*/ *, int);
uint16_t icmp_checksum(struct icmp *icmphdr, uint8_t *payload,
                       int payload_len) {
  char buf[IP_MAXPACKET];
  char *ptr;
  int check_len = 0;
  int i;
  ptr = &buf[0];
  memcpy(ptr, &icmphdr->icmp_type, sizeof(icmphdr->icmp_type));
  ptr += sizeof(icmphdr->icmp_type);
  check_len += sizeof(icmphdr->icmp_type);
  memcpy(ptr, &icmphdr->icmp_code, sizeof(icmphdr->icmp_code));
  ptr += sizeof(icmphdr->icmp_code);
  check_len += sizeof(icmphdr->icmp_code);
  *ptr = 0;
  ptr++;
  *ptr = 0;
  ptr++;
  check_len += 2;
  memcpy(ptr, &icmphdr->icmp_id, sizeof(icmphdr->icmp_id));
  ptr += sizeof(icmphdr->icmp_id);
  check_len += sizeof(icmphdr->icmp_id);
  memcpy(ptr, &icmphdr->icmp_seq, sizeof(icmphdr->icmp_seq));
  ptr += sizeof(icmphdr->icmp_seq);
  check_len += sizeof(icmphdr->icmp_seq);
  memcpy(ptr, payload, payload_len);
  ptr += payload_len;
  check_len += payload_len;
  for (i = 0; i < payload_len % 2; i++, ptr++) {
    *ptr = 0;
    ptr++;
    check_len++;
  }
  return checksum((uint16_t *)buf, check_len);
}


x86 assembly (clang, -O0):
<icmp_checksum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    0x2ebe(%rip),%rax
mov    (%rax),%eax
mov    %eax,%ecx
mov    %rsp,%rax
mov    %rax,-0x20(%rbp)
mov    %rcx,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rax
sub    %rdx,%rax
mov    %rax,-0x40(%rbp)
mov    %rax,%rsp
mov    %rcx,-0x28(%rbp)
movl   $0x0,-0x34(%rbp)
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rsi
add    $0xc,%rsi
mov    $0x4,%edx
call   1040 <memcpy@plt>
mov    -0x30(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x30(%rbp)
movslq -0x34(%rbp),%rax
add    $0x4,%rax
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rsi
add    $0x8,%rsi
mov    $0x4,%edx
call   1040 <memcpy@plt>
mov    -0x30(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x30(%rbp)
movslq -0x34(%rbp),%rax
add    $0x4,%rax
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x34(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rsi
add    $0x4,%rsi
mov    $0x4,%edx
call   1040 <memcpy@plt>
mov    -0x30(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x30(%rbp)
movslq -0x34(%rbp),%rax
add    $0x4,%rax
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1040 <memcpy@plt>
mov    -0x30(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x30(%rbp)
movslq -0x34(%rbp),%rax
add    $0x4,%rax
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x14(%rbp),%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x14(%rbp),%eax
add    -0x34(%rbp),%eax
mov    %eax,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    -0x44(%rbp),%eax
cmp    %edx,%eax
jge    12ce <icmp_checksum+0x1ae>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    127c <icmp_checksum+0x15c>
mov    -0x40(%rbp),%rdi
mov    -0x34(%rbp),%esi
call   1030 <checksum@plt>
mov    -0x20(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<icmp_checksum>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,-0x30(%rbp)
mov    %rdi,%r12
mov    0x2eb9(%rip),%rax
mov    (%rax),%ebx
mov    %rsp,%r15
add    $0xf,%rbx
and    $0xfffffffffffffff0,%rbx
mov    %r15,%r13
sub    %rbx,%r13
mov    %r13,%rsp
lea    0xc(%rdi),%rsi
mov    %r13,%rdi
mov    $0x4,%edx
call   1040 <memcpy@plt>
lea    0x4(%r13),%rdi
lea    0x8(%r12),%rsi
mov    $0x4,%edx
call   1040 <memcpy@plt>
neg    %rbx
movw   $0x0,0x8(%r15,%rbx,1)
lea    0xa(%r13),%rdi
lea    0x4(%r12),%rsi
mov    $0x4,%edx
call   1040 <memcpy@plt>
lea    0xe(%r13),%rdi
mov    %r12,%rsi
mov    $0x4,%edx
call   1040 <memcpy@plt>
lea    0x12(%r13),%r12
mov    %r12,%rdi
mov    -0x30(%rbp),%rsi
mov    %r14d,%edx
call   1040 <memcpy@plt>
mov    %r14d,%eax
and    $0x80000001,%eax
cmp    $0x1,%eax
jne    11d6 <icmp_checksum+0xb6>
mov    %r14d,%eax
movb   $0x0,(%r12,%rax,1)
add    $0x13,%r14d
jmp    11da <icmp_checksum+0xba>
add    $0x12,%r14d
mov    %r13,%rdi
mov    %r14d,%esi
call   1030 <checksum@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.icmp = type { i32, i32, i32, i32 }
@IP_MAXPACKET = global i32 0
define i32 @icmp_checksum(%struct.icmp* %0, i32* %1, i32 %2) {
%4 = alloca %struct.icmp*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
store %struct.icmp* %0, %struct.icmp** %4
store i32* %1, i32** %5
store i32 %2, i32* %6
%12 = load i32, i32* @IP_MAXPACKET
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %7
%15 = alloca i8, i64 %13
store i64 %13, i64* %8
store i32 0, i32* %10
%16 = getelementptr inbounds i8, i8* %15, i64 0
store i8* %16, i8** %9
%17 = load i8*, i8** %9
%18 = load %struct.icmp*, %struct.icmp** %4
%19 = getelementptr inbounds %struct.icmp, %struct.icmp* %18, i32 0, i32 3
%20 = call i32 @memcpy(i8* %17, i32* %19, i32 4)
%21 = load i8*, i8** %9
%22 = getelementptr inbounds i8, i8* %21, i64 4
store i8* %22, i8** %9
%23 = load i32, i32* %10
%24 = sext i32 %23 to i64
%25 = add i64 %24, 4
%26 = trunc i64 %25 to i32
store i32 %26, i32* %10
%27 = load i8*, i8** %9
%28 = load %struct.icmp*, %struct.icmp** %4
%29 = getelementptr inbounds %struct.icmp, %struct.icmp* %28, i32 0, i32 2
%30 = call i32 @memcpy(i8* %27, i32* %29, i32 4)
%31 = load i8*, i8** %9
%32 = getelementptr inbounds i8, i8* %31, i64 4
store i8* %32, i8** %9
%33 = load i32, i32* %10
%34 = sext i32 %33 to i64
%35 = add i64 %34, 4
%36 = trunc i64 %35 to i32
store i32 %36, i32* %10
%37 = load i8*, i8** %9
store i8 0, i8* %37
%38 = load i8*, i8** %9
%39 = getelementptr inbounds i8, i8* %38, i32 1
store i8* %39, i8** %9
%40 = load i8*, i8** %9
store i8 0, i8* %40
%41 = load i8*, i8** %9
%42 = getelementptr inbounds i8, i8* %41, i32 1
store i8* %42, i8** %9
%43 = load i32, i32* %10
%44 = add nsw i32 %43, 2
store i32 %44, i32* %10
%45 = load i8*, i8** %9
%46 = load %struct.icmp*, %struct.icmp** %4
%47 = getelementptr inbounds %struct.icmp, %struct.icmp* %46, i32 0, i32 1
%48 = call i32 @memcpy(i8* %45, i32* %47, i32 4)
%49 = load i8*, i8** %9
%50 = getelementptr inbounds i8, i8* %49, i64 4
store i8* %50, i8** %9
%51 = load i32, i32* %10
%52 = sext i32 %51 to i64
%53 = add i64 %52, 4
%54 = trunc i64 %53 to i32
store i32 %54, i32* %10
%55 = load i8*, i8** %9
%56 = load %struct.icmp*, %struct.icmp** %4
%57 = getelementptr inbounds %struct.icmp, %struct.icmp* %56, i32 0, i32 0
%58 = call i32 @memcpy(i8* %55, i32* %57, i32 4)
%59 = load i8*, i8** %9
%60 = getelementptr inbounds i8, i8* %59, i64 4
store i8* %60, i8** %9
%61 = load i32, i32* %10
%62 = sext i32 %61 to i64
%63 = add i64 %62, 4
%64 = trunc i64 %63 to i32
store i32 %64, i32* %10
%65 = load i8*, i8** %9
%66 = load i32*, i32** %5
%67 = load i32, i32* %6
%68 = call i32 @memcpy(i8* %65, i32* %66, i32 %67)
%69 = load i32, i32* %6
%70 = load i8*, i8** %9
%71 = sext i32 %69 to i64
%72 = getelementptr inbounds i8, i8* %70, i64 %71
store i8* %72, i8** %9
%73 = load i32, i32* %6
%74 = load i32, i32* %10
%75 = add nsw i32 %74, %73
store i32 %75, i32* %10
store i32 0, i32* %11
br label %76
76: ; preds = %87, %3
%77 = load i32, i32* %11
%78 = load i32, i32* %6
%79 = srem i32 %78, 2
%80 = icmp slt i32 %77, %79
br i1 %80, label %81, label %92
81: ; preds = %76
%82 = load i8*, i8** %9
store i8 0, i8* %82
%83 = load i8*, i8** %9
%84 = getelementptr inbounds i8, i8* %83, i32 1
store i8* %84, i8** %9
%85 = load i32, i32* %10
%86 = add nsw i32 %85, 1
store i32 %86, i32* %10
br label %87
87: ; preds = %81
%88 = load i32, i32* %11
%89 = add nsw i32 %88, 1
store i32 %89, i32* %11
%90 = load i8*, i8** %9
%91 = getelementptr inbounds i8, i8* %90, i32 1
store i8* %91, i8** %9
br label %76
92: ; preds = %76
%93 = bitcast i8* %15 to i32*
%94 = load i32, i32* %10
%95 = call i32 @checksum(i32* %93, i32 %94)
%96 = load i8*, i8** %7
ret i32 %95
}

LLVM IR (-O2):
%struct.icmp = type { i32, i32, i32, i32 }
@IP_MAXPACKET = global i32 0
define i32 @icmp_checksum(%struct.icmp* %0, i32* %1, i32 %2) {
%4 = load i32, i32* @IP_MAXPACKET
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = getelementptr inbounds %struct.icmp, %struct.icmp* %0, i64 0, i32 3
%8 = call i32 @memcpy(i8* %6, i32* %7, i32 4)
%9 = getelementptr inbounds i8, i8* %6, i64 4
%10 = getelementptr inbounds %struct.icmp, %struct.icmp* %0, i64 0, i32 2
%11 = call i32 @memcpy(i8* %9, i32* %10, i32 4)
%12 = getelementptr inbounds i8, i8* %6, i64 8
store i8 0, i8* %12
%13 = getelementptr inbounds i8, i8* %6, i64 9
store i8 0, i8* %13
%14 = getelementptr inbounds i8, i8* %6, i64 10
%15 = getelementptr inbounds %struct.icmp, %struct.icmp* %0, i64 0, i32 1
%16 = call i32 @memcpy(i8* %14, i32* %15, i32 4)
%17 = getelementptr inbounds i8, i8* %6, i64 14
%18 = getelementptr inbounds %struct.icmp, %struct.icmp* %0, i64 0, i32 0
%19 = call i32 @memcpy(i8* %17, i32* %18, i32 4)
%20 = getelementptr inbounds i8, i8* %6, i64 18
%21 = call i32 @memcpy(i8* %20, i32* %1, i32 %2)
%22 = add nsw i32 %2, 18
%23 = and i32 %2, -2147483647
%24 = icmp eq i32 %23, 1
br i1 %24, label %25, label %29
25: ; preds = %3
%26 = zext i32 %2 to i64
%27 = getelementptr inbounds i8, i8* %20, i64 %26
store i8 0, i8* %27
%28 = add nuw i32 %2, 19
br label %29
29: ; preds = %25, %3
%30 = phi i32 [ %22, %3 ], [ %28, %25 ]
%31 = bitcast i8* %6 to i32*
%32 = call i32 @checksum(i32* %31, i32 %30)
ret i32 %32
}