#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 */
struct udphdr {
  scalar_t__ check;
};
struct tcphdr {
  scalar_t__ check;
};
struct iphdr {
  scalar_t__ check;
  scalar_t__ protocol;
  int ihl;
};

/* Variables and functions */
scalar_t__ IPPROTO_TCP;
scalar_t__ IPPROTO_UDP;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, scalar_t__, int);
int ip_checksum(unsigned char *);
int /*<<< orphan*/ printf(char *, unsigned short, int);
int /*<<< orphan*/ stderr;
int tcp_checksum(unsigned char *);
int udp_checksum(unsigned char *);
void show_checksum(unsigned char *data, int len, int transport) {
  struct iphdr *iph = (struct iphdr *)data;
  printf(" IP checksum = %x\t%x\n", (unsigned short)iph->check,
         ip_checksum(data));
  if (iph->check != ip_checksum(data)) {
    fprintf(stderr, "IP checksum error (%x,%x)\n", iph->check,
            ip_checksum(data));
    exit(1);
  }
  if (transport && iph->protocol == IPPROTO_TCP) {
    struct tcphdr *tcph = (struct tcphdr *)(((char *)iph) + iph->ihl * 4);
    printf("TCP checksum = %x\t%x\n", (unsigned short)tcph->check,
           tcp_checksum(data));
    if (tcph->check != tcp_checksum(data)) {
      fprintf(stderr, "TCP checksum error (%x %x)\n", tcph->check,
              tcp_checksum(data));
      exit(1);
    }
  } else if (transport && iph->protocol == IPPROTO_UDP) {
    struct udphdr *udph = (struct udphdr *)(((char *)iph) + iph->ihl * 4);
    printf("UDP checksum = %x\t%x\n", (unsigned short)udph->check,
           udp_checksum(data));
    if (udph->check != udp_checksum(data)) {
      fprintf(stderr, "UDP checksum error (%x %x)\n", udph->check,
              udp_checksum(data));
      exit(1);
    }
  }
}


x86 assembly (clang, -O0):
<show_checksum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %ax,-0x32(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <ip_checksum@plt>
mov    %eax,%edx
mov    -0x32(%rbp),%ax
lea    0xe62(%rip),%rdi
movzwl %ax,%esi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <ip_checksum@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
cmp    %rcx,%rax
je     120b <show_checksum+0xab>
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <ip_checksum@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %eax,%ecx
lea    0xe1a(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpl   $0x0,-0x10(%rbp)
je     12d8 <show_checksum+0x178>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2dc4(%rip),%rcx
cmp    (%rcx),%rax
jne    12d8 <show_checksum+0x178>
mov    -0x18(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    0x10(%rcx),%ecx
shl    $0x2,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %ax,-0x52(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <tcp_checksum@plt>
mov    %eax,%edx
mov    -0x52(%rbp),%ax
lea    0xdcb(%rip),%rdi
movzwl %ax,%esi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x50(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <tcp_checksum@plt>
mov    %eax,%ecx
mov    -0x50(%rbp),%rax
movslq %ecx,%rcx
cmp    %rcx,%rax
je     12d3 <show_checksum+0x173>
mov    0x2d3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x60(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <tcp_checksum@plt>
mov    -0x64(%rbp),%edi
mov    -0x60(%rbp),%rdx
mov    %eax,%ecx
lea    0xd83(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    13b0 <show_checksum+0x250>
cmpl   $0x0,-0x10(%rbp)
je     13ab <show_checksum+0x24b>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2cd7(%rip),%rcx
cmp    (%rcx),%rax
jne    13ab <show_checksum+0x24b>
mov    -0x18(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    0x10(%rcx),%ecx
shl    $0x2,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    %ax,-0x72(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <udp_checksum@plt>
mov    %eax,%edx
mov    -0x72(%rbp),%ax
lea    0xd30(%rip),%rdi
movzwl %ax,%esi
call   1040 <printf@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x70(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <udp_checksum@plt>
mov    %eax,%ecx
mov    -0x70(%rbp),%rax
movslq %ecx,%rcx
cmp    %rcx,%rax
je     13a6 <show_checksum+0x246>
mov    0x2c70(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x84(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x80(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <udp_checksum@plt>
mov    -0x84(%rbp),%edi
mov    -0x80(%rbp),%rdx
mov    %eax,%ecx
lea    0xce2(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    13ab <show_checksum+0x24b>
jmp    13b0 <show_checksum+0x250>
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<show_checksum>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %rdi,%rbx
movzwl (%rdi),%ebp
call   1060 <ip_checksum@plt>
lea    0xe87(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1040 <printf@plt>
mov    (%rbx),%rbp
mov    %rbx,%rdi
call   1060 <ip_checksum@plt>
cltq
cmp    %rax,%rbp
jne    1248 <show_checksum+0xe8>
test   %r14d,%r14d
je     1243 <show_checksum+0xe3>
mov    0x8(%rbx),%rax
mov    0x2e3c(%rip),%rcx
cmp    (%rcx),%rax
jne    1203 <show_checksum+0xa3>
movslq 0x10(%rbx),%r14
movzwl (%rbx,%r14,4),%ebp
mov    %rbx,%rdi
call   1070 <tcp_checksum@plt>
lea    0xe68(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1040 <printf@plt>
mov    (%rbx,%r14,4),%rbp
mov    %rbx,%rdi
call   1070 <tcp_checksum@plt>
cltq
cmp    %rax,%rbp
je     1243 <show_checksum+0xe3>
mov    0x2dec(%rip),%rax
mov    (%rax),%ebp
mov    (%rbx,%r14,4),%r14
mov    %rbx,%rdi
call   1070 <tcp_checksum@plt>
lea    0xe46(%rip),%rsi
jmp    1263 <show_checksum+0x103>
mov    0x2dbe(%rip),%rcx
cmp    (%rcx),%rax
jne    1243 <show_checksum+0xe3>
movslq 0x10(%rbx),%r14
movzwl (%rbx,%r14,4),%ebp
mov    %rbx,%rdi
call   1030 <udp_checksum@plt>
lea    0xe3c(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1040 <printf@plt>
mov    (%rbx,%r14,4),%rbp
mov    %rbx,%rdi
call   1030 <udp_checksum@plt>
cltq
cmp    %rax,%rbp
jne    1279 <show_checksum+0x119>
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d89(%rip),%rax
mov    (%rax),%ebp
mov    (%rbx),%r14
mov    %rbx,%rdi
call   1060 <ip_checksum@plt>
lea    0xdb3(%rip),%rsi
mov    %ebp,%edi
mov    %r14,%rdx
mov    %eax,%ecx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2d58(%rip),%rax
mov    (%rax),%ebp
mov    (%rbx,%r14,4),%r14
mov    %rbx,%rdi
call   1030 <udp_checksum@plt>
lea    0xde4(%rip),%rsi
jmp    1263 <show_checksum+0x103>

LLVM IR (-O0):
%struct.iphdr = type { i64, i64, i32 }
%struct.tcphdr = type { i64 }
%struct.udphdr = type { i64 }
@.str = [22 x i8] c" IP checksum = %x\09%x\0A\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"IP checksum error (%x,%x)\0A\00"
@IPPROTO_TCP = global i64 0
@.str.2 = [22 x i8] c"TCP checksum = %x\09%x\0A\00"
@.str.3 = [28 x i8] c"TCP checksum error (%x %x)\0A\00"
@IPPROTO_UDP = global i64 0
@.str.4 = [22 x i8] c"UDP checksum = %x\09%x\0A\00"
@.str.5 = [28 x i8] c"UDP checksum error (%x %x)\0A\00"
define void @show_checksum(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.iphdr*
%8 = alloca %struct.tcphdr*
%9 = alloca %struct.udphdr*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%10 = load i8*, i8** %4
%11 = bitcast i8* %10 to %struct.iphdr*
store %struct.iphdr* %11, %struct.iphdr** %7
%12 = load %struct.iphdr*, %struct.iphdr** %7
%13 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %12, i32 0, i32 0
%14 = load i64, i64* %13
%15 = trunc i64 %14 to i16
%16 = load i8*, i8** %4
%17 = call i32 @ip_checksum(i8* %16)
%18 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i16 zeroext %15, i32 %17)
%19 = load %struct.iphdr*, %struct.iphdr** %7
%20 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %19, i32 0, i32 0
%21 = load i64, i64* %20
%22 = load i8*, i8** %4
%23 = call i32 @ip_checksum(i8* %22)
%24 = sext i32 %23 to i64
%25 = icmp ne i64 %21, %24
br i1 %25, label %26, label %35
26: ; preds = %3
%27 = load i32, i32* @stderr
%28 = load %struct.iphdr*, %struct.iphdr** %7
%29 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %28, i32 0, i32 0
%30 = load i64, i64* %29
%31 = load i8*, i8** %4
%32 = call i32 @ip_checksum(i8* %31)
%33 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i64 %30, i32 %32)
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %3
%36 = load i32, i32* %6
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %78
38: ; preds = %35
%39 = load %struct.iphdr*, %struct.iphdr** %7
%40 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %39, i32 0, i32 1
%41 = load i64, i64* %40
%42 = load i64, i64* @IPPROTO_TCP
%43 = icmp eq i64 %41, %42
br i1 %43, label %44, label %78
44: ; preds = %38
%45 = load %struct.iphdr*, %struct.iphdr** %7
%46 = bitcast %struct.iphdr* %45 to i8*
%47 = load %struct.iphdr*, %struct.iphdr** %7
%48 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %47, i32 0, i32 2
%49 = load i32, i32* %48
%50 = mul nsw i32 %49, 4
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %46, i64 %51
%53 = bitcast i8* %52 to %struct.tcphdr*
store %struct.tcphdr* %53, %struct.tcphdr** %8
%54 = load %struct.tcphdr*, %struct.tcphdr** %8
%55 = getelementptr inbounds %struct.tcphdr, %struct.tcphdr* %54, i32 0, i32 0
%56 = load i64, i64* %55
%57 = trunc i64 %56 to i16
%58 = load i8*, i8** %4
%59 = call i32 @tcp_checksum(i8* %58)
%60 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i16 zeroext %57, i32 %59)
%61 = load %struct.tcphdr*, %struct.tcphdr** %8
%62 = getelementptr inbounds %struct.tcphdr, %struct.tcphdr* %61, i32 0, i32 0
%63 = load i64, i64* %62
%64 = load i8*, i8** %4
%65 = call i32 @tcp_checksum(i8* %64)
%66 = sext i32 %65 to i64
%67 = icmp ne i64 %63, %66
br i1 %67, label %68, label %77
68: ; preds = %44
%69 = load i32, i32* @stderr
%70 = load %struct.tcphdr*, %struct.tcphdr** %8
%71 = getelementptr inbounds %struct.tcphdr, %struct.tcphdr* %70, i32 0, i32 0
%72 = load i64, i64* %71
%73 = load i8*, i8** %4
%74 = call i32 @tcp_checksum(i8* %73)
%75 = call i32 @fprintf(i32 %69, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i64 %72, i32 %74)
%76 = call i32 @exit(i32 1)
unreachable
77: ; preds = %44
br label %122
78: ; preds = %38, %35
%79 = load i32, i32* %6
%80 = icmp ne i32 %79, 0
br i1 %80, label %81, label %121
81: ; preds = %78
%82 = load %struct.iphdr*, %struct.iphdr** %7
%83 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %82, i32 0, i32 1
%84 = load i64, i64* %83
%85 = load i64, i64* @IPPROTO_UDP
%86 = icmp eq i64 %84, %85
br i1 %86, label %87, label %121
87: ; preds = %81
%88 = load %struct.iphdr*, %struct.iphdr** %7
%89 = bitcast %struct.iphdr* %88 to i8*
%90 = load %struct.iphdr*, %struct.iphdr** %7
%91 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %90, i32 0, i32 2
%92 = load i32, i32* %91
%93 = mul nsw i32 %92, 4
%94 = sext i32 %93 to i64
%95 = getelementptr inbounds i8, i8* %89, i64 %94
%96 = bitcast i8* %95 to %struct.udphdr*
store %struct.udphdr* %96, %struct.udphdr** %9
%97 = load %struct.udphdr*, %struct.udphdr** %9
%98 = getelementptr inbounds %struct.udphdr, %struct.udphdr* %97, i32 0, i32 0
%99 = load i64, i64* %98
%100 = trunc i64 %99 to i16
%101 = load i8*, i8** %4
%102 = call i32 @udp_checksum(i8* %101)
%103 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i16 zeroext %100, i32 %102)
%104 = load %struct.udphdr*, %struct.udphdr** %9
%105 = getelementptr inbounds %struct.udphdr, %struct.udphdr* %104, i32 0, i32 0
%106 = load i64, i64* %105
%107 = load i8*, i8** %4
%108 = call i32 @udp_checksum(i8* %107)
%109 = sext i32 %108 to i64
%110 = icmp ne i64 %106, %109
br i1 %110, label %111, label %120
111: ; preds = %87
%112 = load i32, i32* @stderr
%113 = load %struct.udphdr*, %struct.udphdr** %9
%114 = getelementptr inbounds %struct.udphdr, %struct.udphdr* %113, i32 0, i32 0
%115 = load i64, i64* %114
%116 = load i8*, i8** %4
%117 = call i32 @udp_checksum(i8* %116)
%118 = call i32 @fprintf(i32 %112, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i64 %115, i32 %117)
%119 = call i32 @exit(i32 1)
unreachable
120: ; preds = %87
br label %121
121: ; preds = %120, %81, %78
br label %122
122: ; preds = %121, %77
ret void
}

LLVM IR (-O2):
@.str = [22 x i8] c" IP checksum = %x\09%x\0A\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"IP checksum error (%x,%x)\0A\00"
@IPPROTO_TCP = global i64 0
@.str.2 = [22 x i8] c"TCP checksum = %x\09%x\0A\00"
@.str.3 = [28 x i8] c"TCP checksum error (%x %x)\0A\00"
@IPPROTO_UDP = global i64 0
@.str.4 = [22 x i8] c"UDP checksum = %x\09%x\0A\00"
@.str.5 = [28 x i8] c"UDP checksum error (%x %x)\0A\00"
define void @show_checksum(i8* %0, i32 %1, i32 %2) {
%4 = bitcast i8* %0 to i64*
%5 = load i64, i64* %4
%6 = trunc i64 %5 to i16
%7 = tail call i32 @ip_checksum(i8* %0)
%8 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i16 zeroext %6, i32 %7)
%9 = load i64, i64* %4
%10 = tail call i32 @ip_checksum(i8* %0)
%11 = sext i32 %10 to i64
%12 = icmp eq i64 %9, %11
br i1 %12, label %19, label %13
13: ; preds = %3
%14 = load i32, i32* @stderr
%15 = load i64, i64* %4
%16 = tail call i32 @ip_checksum(i8* %0)
%17 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i64 %15, i32 %16)
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %3
%20 = icmp eq i32 %2, 0
br i1 %20, label %74, label %21
21: ; preds = %19
%22 = getelementptr inbounds i8, i8* %0, i64 8
%23 = bitcast i8* %22 to i64*
%24 = load i64, i64* %23
%25 = load i64, i64* @IPPROTO_TCP
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %49
27: ; preds = %21
%28 = getelementptr inbounds i8, i8* %0, i64 16
%29 = bitcast i8* %28 to i32*
%30 = load i32, i32* %29
%31 = shl nsw i32 %30, 2
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %0, i64 %32
%34 = bitcast i8* %33 to i64*
%35 = load i64, i64* %34
%36 = trunc i64 %35 to i16
%37 = tail call i32 @tcp_checksum(i8* %0)
%38 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i16 zeroext %36, i32 %37)
%39 = load i64, i64* %34
%40 = tail call i32 @tcp_checksum(i8* %0)
%41 = sext i32 %40 to i64
%42 = icmp eq i64 %39, %41
br i1 %42, label %74, label %43
43: ; preds = %27
%44 = load i32, i32* @stderr
%45 = load i64, i64* %34
%46 = tail call i32 @tcp_checksum(i8* %0)
%47 = tail call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i64 %45, i32 %46)
%48 = tail call i32 @exit(i32 1)
unreachable
49: ; preds = %21
%50 = load i64, i64* @IPPROTO_UDP
%51 = icmp eq i64 %24, %50
br i1 %51, label %52, label %74
52: ; preds = %49
%53 = getelementptr inbounds i8, i8* %0, i64 16
%54 = bitcast i8* %53 to i32*
%55 = load i32, i32* %54
%56 = shl nsw i32 %55, 2
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %0, i64 %57
%59 = bitcast i8* %58 to i64*
%60 = load i64, i64* %59
%61 = trunc i64 %60 to i16
%62 = tail call i32 @udp_checksum(i8* %0)
%63 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i16 zeroext %61, i32 %62)
%64 = load i64, i64* %59
%65 = tail call i32 @udp_checksum(i8* %0)
%66 = sext i32 %65 to i64
%67 = icmp eq i64 %64, %66
br i1 %67, label %74, label %68
68: ; preds = %52
%69 = load i32, i32* @stderr
%70 = load i64, i64* %59
%71 = tail call i32 @udp_checksum(i8* %0)
%72 = tail call i32 @fprintf(i32 %69, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i64 %70, i32 %71)
%73 = tail call i32 @exit(i32 1)
unreachable
74: ; preds = %19, %52, %27, %49
ret void
}