#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*/ u_char;
struct iphdr {
  int dummy;
};

/* Variables and functions */
unsigned short checksum(int /*<<< orphan*/ *, int);
unsigned short checksum2(int /*<<< orphan*/ *, int, int /*<<< orphan*/ *, int);
int checkIPchecksum(struct iphdr *iphdr, u_char *option, int optionLen) {
  unsigned short sum;
  if (optionLen == 0) {
    sum = checksum((u_char *)iphdr, sizeof(struct iphdr));
    if (sum == 0 || sum == 0xFFFF) {
      return (1);
    } else {
      return (0);
    }
  } else {
    sum = checksum2((u_char *)iphdr, sizeof(struct iphdr), option, optionLen);
    if (sum == 0 || sum == 0xFFFF) {
      return (1);
    } else {
      return (0);
    }
  }
}


x86 assembly (clang, -O0):
<checkIPchecksum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1183 <checkIPchecksum+0x63>
mov    -0x10(%rbp),%rdi
mov    $0x4,%esi
call   1030 <checksum@plt>
mov    %ax,-0x1e(%rbp)
movzwl -0x1e(%rbp),%eax
cmp    $0x0,%eax
je     116b <checkIPchecksum+0x4b>
movzwl -0x1e(%rbp),%eax
cmp    $0xffff,%eax
jne    1177 <checkIPchecksum+0x57>
movl   $0x1,-0x4(%rbp)
jmp    11cb <checkIPchecksum+0xab>
movl   $0x0,-0x4(%rbp)
jmp    11cb <checkIPchecksum+0xab>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
mov    $0x4,%esi
call   1040 <checksum2@plt>
mov    %ax,-0x1e(%rbp)
movzwl -0x1e(%rbp),%eax
cmp    $0x0,%eax
je     11b8 <checkIPchecksum+0x98>
movzwl -0x1e(%rbp),%eax
cmp    $0xffff,%eax
jne    11c4 <checkIPchecksum+0xa4>
movl   $0x1,-0x4(%rbp)
jmp    11cb <checkIPchecksum+0xab>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checkIPchecksum>:
push   %rax
mov    %edx,%ecx
test   %edx,%edx
je     1136 <checkIPchecksum+0x16>
mov    %rsi,%rdx
mov    $0x4,%esi
call   1040 <checksum2@plt>
jmp    1140 <checkIPchecksum+0x20>
mov    $0x4,%esi
call   1030 <checksum@plt>
add    $0x1,%eax
xor    %ecx,%ecx
cmp    $0x2,%ax
setb   %cl
mov    %ecx,%eax
pop    %rcx
ret

LLVM IR (-O0):
%struct.iphdr = type { i32 }
define i32 @checkIPchecksum(%struct.iphdr* %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.iphdr*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i16
store %struct.iphdr* %0, %struct.iphdr** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
%9 = load i32, i32* %7
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %24
11: ; preds = %3
%12 = load %struct.iphdr*, %struct.iphdr** %5
%13 = bitcast %struct.iphdr* %12 to i32*
%14 = call zeroext i16 @checksum(i32* %13, i32 4)
store i16 %14, i16* %8
%15 = load i16, i16* %8
%16 = zext i16 %15 to i32
%17 = icmp eq i32 %16, 0
br i1 %17, label %22, label %18
18: ; preds = %11
%19 = load i16, i16* %8
%20 = zext i16 %19 to i32
%21 = icmp eq i32 %20, 65535
br i1 %21, label %22, label %23
22: ; preds = %18, %11
store i32 1, i32* %4
br label %39
23: ; preds = %18
store i32 0, i32* %4
br label %39
24: ; preds = %3
%25 = load %struct.iphdr*, %struct.iphdr** %5
%26 = bitcast %struct.iphdr* %25 to i32*
%27 = load i32*, i32** %6
%28 = load i32, i32* %7
%29 = call zeroext i16 @checksum2(i32* %26, i32 4, i32* %27, i32 %28)
store i16 %29, i16* %8
%30 = load i16, i16* %8
%31 = zext i16 %30 to i32
%32 = icmp eq i32 %31, 0
br i1 %32, label %37, label %33
33: ; preds = %24
%34 = load i16, i16* %8
%35 = zext i16 %34 to i32
%36 = icmp eq i32 %35, 65535
br i1 %36, label %37, label %38
37: ; preds = %33, %24
store i32 1, i32* %4
br label %39
38: ; preds = %33
store i32 0, i32* %4
br label %39
39: ; preds = %38, %37, %23, %22
%40 = load i32, i32* %4
ret i32 %40
}

LLVM IR (-O2):
%struct.iphdr = type { i32 }
define i32 @checkIPchecksum(%struct.iphdr* %0, i32* %1, i32 %2) {
%4 = icmp eq i32 %2, 0
%5 = getelementptr %struct.iphdr, %struct.iphdr* %0, i64 0, i32 0
br i1 %4, label %6, label %8
6: ; preds = %3
%7 = tail call zeroext i16 @checksum(i32* %5, i32 4)
br label %10
8: ; preds = %3
%9 = tail call zeroext i16 @checksum2(i32* %5, i32 4, i32* %1, i32 %2)
br label %10
10: ; preds = %8, %6
%11 = phi i16 [ %7, %6 ], [ %9, %8 ]
%12 = add i16 %11, 1
%13 = icmp ult i16 %12, 2
%14 = zext i1 %13 to i32
ret i32 %14
}