#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 */
typedef struct TYPE_4__ TYPE_2__;
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
struct udphdr {
  unsigned char len;
  unsigned char source;
  unsigned char dest;
};
struct TYPE_4__ {
  unsigned char s_addr;
};
struct TYPE_3__ {
  unsigned char s_addr;
};
struct ip {
  unsigned char ip_p;
  TYPE_2__ ip_dst;
  TYPE_1__ ip_src;
};

/* Variables and functions */
int IP_MAXPACKET;
unsigned short checksum(unsigned short *, int);
int /*<<< orphan*/ memcpy(char *, unsigned char *, int);
unsigned short int udp4_checksum(struct ip iphdr, struct udphdr udphdr,
                                 unsigned char *payload, int payloadlen) {
  char buf[IP_MAXPACKET];
  char *ptr;
  int chksumlen = 0;
  int i;
  ptr = &buf[0];
  memcpy(ptr, &iphdr.ip_src.s_addr, sizeof(iphdr.ip_src.s_addr));
  ptr += sizeof(iphdr.ip_src.s_addr);
  chksumlen += sizeof(iphdr.ip_src.s_addr);
  memcpy(ptr, &iphdr.ip_dst.s_addr, sizeof(iphdr.ip_dst.s_addr));
  ptr += sizeof(iphdr.ip_dst.s_addr);
  chksumlen += sizeof(iphdr.ip_dst.s_addr);
  *ptr = 0;
  ptr++;
  chksumlen += 1;
  memcpy(ptr, &iphdr.ip_p, sizeof(iphdr.ip_p));
  ptr += sizeof(iphdr.ip_p);
  chksumlen += sizeof(iphdr.ip_p);
  memcpy(ptr, &udphdr.len, sizeof(udphdr.len));
  ptr += sizeof(udphdr.len);
  chksumlen += sizeof(udphdr.len);
  memcpy(ptr, &udphdr.source, sizeof(udphdr.source));
  ptr += sizeof(udphdr.source);
  chksumlen += sizeof(udphdr.source);
  memcpy(ptr, &udphdr.dest, sizeof(udphdr.dest));
  ptr += sizeof(udphdr.dest);
  chksumlen += sizeof(udphdr.dest);
  memcpy(ptr, &udphdr.len, sizeof(udphdr.len));
  ptr += sizeof(udphdr.len);
  chksumlen += sizeof(udphdr.len);
  *ptr = 0;
  ptr++;
  *ptr = 0;
  ptr++;
  chksumlen += 2;
  memcpy(ptr, payload, payloadlen);
  ptr += payloadlen;
  chksumlen += payloadlen;
  for (i = 0; i < payloadlen % 2; i++, ptr++) {
    *ptr = 0;
    ptr++;
    chksumlen++;
  }
  return checksum((unsigned short int *)buf, chksumlen);
}


x86 assembly (clang, -O0):
<udp4_checksum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %di,%ax
mov    %ax,-0xc(%rbp)
shr    $0x10,%edi
mov    %dil,%al
mov    %al,-0xa(%rbp)
mov    -0xa(%rbp),%al
mov    %al,-0x6(%rbp)
mov    -0xc(%rbp),%ax
mov    %ax,-0x8(%rbp)
mov    %esi,%eax
shr    $0x10,%eax
mov    %al,-0x12(%rbp)
mov    %si,%ax
mov    %ax,-0x14(%rbp)
mov    -0x12(%rbp),%al
mov    %al,-0xe(%rbp)
mov    -0x14(%rbp),%ax
mov    %ax,-0x10(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    0x2e87(%rip),%rax
mov    (%rax),%eax
mov    %eax,%ecx
mov    %rsp,%rax
mov    %rax,-0x30(%rbp)
mov    %rcx,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rax
sub    %rdx,%rax
mov    %rax,-0x50(%rbp)
mov    %rax,%rsp
mov    %rcx,-0x38(%rbp)
movl   $0x0,-0x44(%rbp)
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
lea    -0x8(%rbp),%rsi
add    $0x2,%rsi
mov    $0x1,%edx
call   1040 <memcpy@plt>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
movslq -0x44(%rbp),%rax
add    $0x1,%rax
mov    %eax,-0x44(%rbp)
mov    -0x40(%rbp),%rdi
lea    -0x8(%rbp),%rsi
add    $0x1,%rsi
mov    $0x1,%edx
call   1040 <memcpy@plt>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
movslq -0x44(%rbp),%rax
add    $0x1,%rax
mov    %eax,-0x44(%rbp)
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
mov    -0x40(%rbp),%rdi
lea    -0x8(%rbp),%rsi
mov    $0x1,%edx
call   1040 <memcpy@plt>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
movslq -0x44(%rbp),%rax
add    $0x1,%rax
mov    %eax,-0x44(%rbp)
mov    -0x40(%rbp),%rdi
lea    -0x10(%rbp),%rsi
mov    $0x1,%edx
call   1040 <memcpy@plt>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
movslq -0x44(%rbp),%rax
add    $0x1,%rax
mov    %eax,-0x44(%rbp)
mov    -0x40(%rbp),%rdi
lea    -0x10(%rbp),%rsi
add    $0x1,%rsi
mov    $0x1,%edx
call   1040 <memcpy@plt>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
movslq -0x44(%rbp),%rax
add    $0x1,%rax
mov    %eax,-0x44(%rbp)
mov    -0x40(%rbp),%rdi
lea    -0x10(%rbp),%rsi
add    $0x2,%rsi
mov    $0x1,%edx
call   1040 <memcpy@plt>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
movslq -0x44(%rbp),%rax
add    $0x1,%rax
mov    %eax,-0x44(%rbp)
mov    -0x40(%rbp),%rdi
lea    -0x10(%rbp),%rsi
mov    $0x1,%edx
call   1040 <memcpy@plt>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
movslq -0x44(%rbp),%rax
add    $0x1,%rax
mov    %eax,-0x44(%rbp)
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
mov    -0x44(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x44(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x24(%rbp),%ecx
mov    -0x40(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x24(%rbp),%eax
add    -0x44(%rbp),%eax
mov    %eax,-0x44(%rbp)
movl   $0x0,-0x48(%rbp)
mov    -0x48(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x24(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    -0x54(%rbp),%eax
cmp    %edx,%eax
jge    13a0 <udp4_checksum+0x280>
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
jmp    134e <udp4_checksum+0x22e>
mov    -0x50(%rbp),%rdi
mov    -0x44(%rbp),%esi
call   1030 <checksum@plt>
mov    -0x30(%rbp),%rcx
mov    %rcx,%rsp
movzwl %ax,%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udp4_checksum>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r15d
mov    %rdx,-0x40(%rbp)
mov    %di,-0x38(%rbp)
shr    $0x10,%edi
mov    %dil,-0x36(%rbp)
mov    %si,-0x30(%rbp)
shr    $0x10,%esi
mov    %sil,-0x2e(%rbp)
mov    0x2ea3(%rip),%rax
mov    (%rax),%r13d
mov    %rsp,%r14
add    $0xf,%r13
and    $0xfffffffffffffff0,%r13
mov    %r14,%rbx
sub    %r13,%rbx
mov    %rbx,%rsp
lea    -0x36(%rbp),%rsi
mov    %rbx,%rdi
mov    $0x1,%edx
call   1040 <memcpy@plt>
lea    0x1(%rbx),%rdi
lea    -0x37(%rbp),%rsi
mov    $0x1,%edx
call   1040 <memcpy@plt>
movb   $0x0,0x2(%rbx)
lea    0x3(%rbx),%rdi
lea    -0x38(%rbp),%rsi
mov    $0x1,%edx
call   1040 <memcpy@plt>
lea    0x4(%rbx),%rdi
lea    -0x30(%rbp),%r12
mov    %r12,%rsi
mov    $0x1,%edx
call   1040 <memcpy@plt>
lea    0x5(%rbx),%rdi
lea    -0x2f(%rbp),%rsi
mov    $0x1,%edx
call   1040 <memcpy@plt>
lea    0x6(%rbx),%rdi
lea    -0x2e(%rbp),%rsi
mov    $0x1,%edx
call   1040 <memcpy@plt>
lea    0x7(%rbx),%rdi
mov    %r12,%rsi
mov    $0x1,%edx
call   1040 <memcpy@plt>
neg    %r13
movw   $0x0,0x8(%r14,%r13,1)
lea    0xa(%rbx),%r12
mov    %r12,%rdi
mov    -0x40(%rbp),%rsi
mov    %r15d,%edx
call   1040 <memcpy@plt>
mov    %r15d,%eax
and    $0x80000001,%eax
cmp    $0x1,%eax
jne    1228 <udp4_checksum+0x108>
mov    %r15d,%eax
movb   $0x0,(%r12,%rax,1)
add    $0xb,%r15d
jmp    122c <udp4_checksum+0x10c>
add    $0xa,%r15d
mov    %rbx,%rdi
mov    %r15d,%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.ip = type { i8, %struct.TYPE_4__, %struct.TYPE_3__ }
%struct.TYPE_4__ = type { i8 }
%struct.TYPE_3__ = type { i8 }
%struct.udphdr = type { i8, i8, i8 }
@IP_MAXPACKET = global i32 0
define zeroext i16 @udp4_checksum(i24 %0, i24 %1, i8* %2, i32 %3) {
%5 = alloca %struct.ip
%6 = alloca i24
%7 = alloca %struct.udphdr
%8 = alloca i24
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i8*
%14 = alloca i32
%15 = alloca i32
store i24 %0, i24* %6
%16 = bitcast %struct.ip* %5 to i8*
%17 = bitcast i24* %6 to i8*
store i24 %1, i24* %8
%18 = bitcast %struct.udphdr* %7 to i8*
%19 = bitcast i24* %8 to i8*
store i8* %2, i8** %9
store i32 %3, i32* %10
%20 = load i32, i32* @IP_MAXPACKET
%21 = zext i32 %20 to i64
%22 = call i8* @llvm.stacksave()
store i8* %22, i8** %11
%23 = alloca i8, i64 %21
store i64 %21, i64* %12
store i32 0, i32* %14
%24 = getelementptr inbounds i8, i8* %23, i64 0
store i8* %24, i8** %13
%25 = load i8*, i8** %13
%26 = getelementptr inbounds %struct.ip, %struct.ip* %5, i32 0, i32 2
%27 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %26, i32 0, i32 0
%28 = call i32 @memcpy(i8* %25, i8* %27, i32 1)
%29 = load i8*, i8** %13
%30 = getelementptr inbounds i8, i8* %29, i64 1
store i8* %30, i8** %13
%31 = load i32, i32* %14
%32 = sext i32 %31 to i64
%33 = add i64 %32, 1
%34 = trunc i64 %33 to i32
store i32 %34, i32* %14
%35 = load i8*, i8** %13
%36 = getelementptr inbounds %struct.ip, %struct.ip* %5, i32 0, i32 1
%37 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %36, i32 0, i32 0
%38 = call i32 @memcpy(i8* %35, i8* %37, i32 1)
%39 = load i8*, i8** %13
%40 = getelementptr inbounds i8, i8* %39, i64 1
store i8* %40, i8** %13
%41 = load i32, i32* %14
%42 = sext i32 %41 to i64
%43 = add i64 %42, 1
%44 = trunc i64 %43 to i32
store i32 %44, i32* %14
%45 = load i8*, i8** %13
store i8 0, i8* %45
%46 = load i8*, i8** %13
%47 = getelementptr inbounds i8, i8* %46, i32 1
store i8* %47, i8** %13
%48 = load i32, i32* %14
%49 = add nsw i32 %48, 1
store i32 %49, i32* %14
%50 = load i8*, i8** %13
%51 = getelementptr inbounds %struct.ip, %struct.ip* %5, i32 0, i32 0
%52 = call i32 @memcpy(i8* %50, i8* %51, i32 1)
%53 = load i8*, i8** %13
%54 = getelementptr inbounds i8, i8* %53, i64 1
store i8* %54, i8** %13
%55 = load i32, i32* %14
%56 = sext i32 %55 to i64
%57 = add i64 %56, 1
%58 = trunc i64 %57 to i32
store i32 %58, i32* %14
%59 = load i8*, i8** %13
%60 = getelementptr inbounds %struct.udphdr, %struct.udphdr* %7, i32 0, i32 0
%61 = call i32 @memcpy(i8* %59, i8* %60, i32 1)
%62 = load i8*, i8** %13
%63 = getelementptr inbounds i8, i8* %62, i64 1
store i8* %63, i8** %13
%64 = load i32, i32* %14
%65 = sext i32 %64 to i64
%66 = add i64 %65, 1
%67 = trunc i64 %66 to i32
store i32 %67, i32* %14
%68 = load i8*, i8** %13
%69 = getelementptr inbounds %struct.udphdr, %struct.udphdr* %7, i32 0, i32 1
%70 = call i32 @memcpy(i8* %68, i8* %69, i32 1)
%71 = load i8*, i8** %13
%72 = getelementptr inbounds i8, i8* %71, i64 1
store i8* %72, i8** %13
%73 = load i32, i32* %14
%74 = sext i32 %73 to i64
%75 = add i64 %74, 1
%76 = trunc i64 %75 to i32
store i32 %76, i32* %14
%77 = load i8*, i8** %13
%78 = getelementptr inbounds %struct.udphdr, %struct.udphdr* %7, i32 0, i32 2
%79 = call i32 @memcpy(i8* %77, i8* %78, i32 1)
%80 = load i8*, i8** %13
%81 = getelementptr inbounds i8, i8* %80, i64 1
store i8* %81, i8** %13
%82 = load i32, i32* %14
%83 = sext i32 %82 to i64
%84 = add i64 %83, 1
%85 = trunc i64 %84 to i32
store i32 %85, i32* %14
%86 = load i8*, i8** %13
%87 = getelementptr inbounds %struct.udphdr, %struct.udphdr* %7, i32 0, i32 0
%88 = call i32 @memcpy(i8* %86, i8* %87, i32 1)
%89 = load i8*, i8** %13
%90 = getelementptr inbounds i8, i8* %89, i64 1
store i8* %90, i8** %13
%91 = load i32, i32* %14
%92 = sext i32 %91 to i64
%93 = add i64 %92, 1
%94 = trunc i64 %93 to i32
store i32 %94, i32* %14
%95 = load i8*, i8** %13
store i8 0, i8* %95
%96 = load i8*, i8** %13
%97 = getelementptr inbounds i8, i8* %96, i32 1
store i8* %97, i8** %13
%98 = load i8*, i8** %13
store i8 0, i8* %98
%99 = load i8*, i8** %13
%100 = getelementptr inbounds i8, i8* %99, i32 1
store i8* %100, i8** %13
%101 = load i32, i32* %14
%102 = add nsw i32 %101, 2
store i32 %102, i32* %14
%103 = load i8*, i8** %13
%104 = load i8*, i8** %9
%105 = load i32, i32* %10
%106 = call i32 @memcpy(i8* %103, i8* %104, i32 %105)
%107 = load i32, i32* %10
%108 = load i8*, i8** %13
%109 = sext i32 %107 to i64
%110 = getelementptr inbounds i8, i8* %108, i64 %109
store i8* %110, i8** %13
%111 = load i32, i32* %10
%112 = load i32, i32* %14
%113 = add nsw i32 %112, %111
store i32 %113, i32* %14
store i32 0, i32* %15
br label %114
114: ; preds = %125, %4
%115 = load i32, i32* %15
%116 = load i32, i32* %10
%117 = srem i32 %116, 2
%118 = icmp slt i32 %115, %117
br i1 %118, label %119, label %130
119: ; preds = %114
%120 = load i8*, i8** %13
store i8 0, i8* %120
%121 = load i8*, i8** %13
%122 = getelementptr inbounds i8, i8* %121, i32 1
store i8* %122, i8** %13
%123 = load i32, i32* %14
%124 = add nsw i32 %123, 1
store i32 %124, i32* %14
br label %125
125: ; preds = %119
%126 = load i32, i32* %15
%127 = add nsw i32 %126, 1
store i32 %127, i32* %15
%128 = load i8*, i8** %13
%129 = getelementptr inbounds i8, i8* %128, i32 1
store i8* %129, i8** %13
br label %114
130: ; preds = %114
%131 = bitcast i8* %23 to i16*
%132 = load i32, i32* %14
%133 = call zeroext i16 @checksum(i16* %131, i32 %132)
%134 = load i8*, i8** %11
ret i16 %133
}

LLVM IR (-O2):
%struct.ip = type { i8, %struct.TYPE_4__, %struct.TYPE_3__ }
%struct.TYPE_4__ = type { i8 }
%struct.TYPE_3__ = type { i8 }
%struct.udphdr = type { i8, i8, i8 }
@IP_MAXPACKET = global i32 0
define zeroext i16 @udp4_checksum(i24 %0, i24 %1, i8* %2, i32 %3) {
%5 = alloca %struct.ip
%6 = alloca %struct.udphdr
%7 = bitcast %struct.ip* %5 to i24*
store i24 %0, i24* %7
%8 = bitcast %struct.udphdr* %6 to i24*
store i24 %1, i24* %8
%9 = load i32, i32* @IP_MAXPACKET
%10 = zext i32 %9 to i64
%11 = alloca i8, i64 %10
%12 = getelementptr inbounds %struct.ip, %struct.ip* %5, i64 0, i32 2, i32 0
%13 = call i32 @memcpy(i8* %11, i8* %12, i32 1)
%14 = getelementptr inbounds i8, i8* %11, i64 1
%15 = getelementptr inbounds %struct.ip, %struct.ip* %5, i64 0, i32 1, i32 0
%16 = call i32 @memcpy(i8* %14, i8* %15, i32 1)
%17 = getelementptr inbounds i8, i8* %11, i64 2
store i8 0, i8* %17
%18 = getelementptr inbounds i8, i8* %11, i64 3
%19 = getelementptr inbounds %struct.ip, %struct.ip* %5, i64 0, i32 0
%20 = call i32 @memcpy(i8* %18, i8* %19, i32 1)
%21 = getelementptr inbounds i8, i8* %11, i64 4
%22 = getelementptr inbounds %struct.udphdr, %struct.udphdr* %6, i64 0, i32 0
%23 = call i32 @memcpy(i8* %21, i8* %22, i32 1)
%24 = getelementptr inbounds i8, i8* %11, i64 5
%25 = getelementptr inbounds %struct.udphdr, %struct.udphdr* %6, i64 0, i32 1
%26 = call i32 @memcpy(i8* %24, i8* %25, i32 1)
%27 = getelementptr inbounds i8, i8* %11, i64 6
%28 = getelementptr inbounds %struct.udphdr, %struct.udphdr* %6, i64 0, i32 2
%29 = call i32 @memcpy(i8* %27, i8* %28, i32 1)
%30 = getelementptr inbounds i8, i8* %11, i64 7
%31 = call i32 @memcpy(i8* %30, i8* %22, i32 1)
%32 = getelementptr inbounds i8, i8* %11, i64 8
store i8 0, i8* %32
%33 = getelementptr inbounds i8, i8* %11, i64 9
store i8 0, i8* %33
%34 = getelementptr inbounds i8, i8* %11, i64 10
%35 = call i32 @memcpy(i8* %34, i8* %2, i32 %3)
%36 = add nsw i32 %3, 10
%37 = and i32 %3, -2147483647
%38 = icmp eq i32 %37, 1
br i1 %38, label %39, label %43
39: ; preds = %4
%40 = zext i32 %3 to i64
%41 = getelementptr inbounds i8, i8* %34, i64 %40
store i8 0, i8* %41
%42 = add nuw i32 %3, 11
br label %43
43: ; preds = %39, %4
%44 = phi i32 [ %36, %4 ], [ %42, %39 ]
%45 = bitcast i8* %11 to i16*
%46 = call zeroext i16 @checksum(i16* %45, i32 %44)
ret i16 %46
}