#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 ip {
  int dummy;
};
typedef struct ip uint8_t;
typedef int /*<<< orphan*/ uint16_t;

/* Variables and functions */
int IP_MAXPACKET;
int /*<<< orphan*/ checksum(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ memcpy(char *, struct ip *, int);
uint16_t ip4_checksum(struct ip iphdr, uint8_t *options, int opt_len) {
  char buf[IP_MAXPACKET];
  char *ptr;
  int chksumlen = 0;
  ptr = &buf[0];
  memcpy(ptr, &iphdr, 20);
  ptr += 20;
  chksumlen += 20;
  memcpy(ptr, options, opt_len);
  ptr += opt_len;
  chksumlen += opt_len;
  return checksum((uint16_t *)buf, chksumlen);
}


x86 assembly (clang, -O0):
<ip4_checksum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    0x2ebf(%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
lea    -0x8(%rbp),%rsi
mov    $0x14,%edx
call   1040 <memcpy@plt>
mov    -0x30(%rbp),%rax
add    $0x14,%rax
mov    %rax,-0x30(%rbp)
mov    -0x34(%rbp),%eax
add    $0x14,%eax
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x40(%rbp),%rdi
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)
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):
<ip4_checksum>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r14
mov    %edi,-0x30(%rbp)
mov    0x2eba(%rip),%rax
mov    (%rax),%ebx
mov    %rsp,%r13
add    $0xf,%rbx
and    $0xfffffffffffffff0,%rbx
mov    %r13,%r12
sub    %rbx,%r12
mov    %r12,%rsp
lea    -0x30(%rbp),%rsi
mov    %r12,%rdi
mov    $0x14,%edx
call   1040 <memcpy@plt>
neg    %rbx
lea    (%rbx,%r13,1),%rdi
add    $0x14,%rdi
mov    %r14,%rsi
mov    %r15d,%edx
call   1040 <memcpy@plt>
add    $0x14,%r15d
mov    %r12,%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 { i32 }
@IP_MAXPACKET = global i32 0
define i32 @ip4_checksum(i32 %0, %struct.ip* %1, i32 %2) {
%4 = alloca %struct.ip
%5 = alloca %struct.ip*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i32
%11 = getelementptr inbounds %struct.ip, %struct.ip* %4, i32 0, i32 0
store i32 %0, i32* %11
store %struct.ip* %1, %struct.ip** %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 = call i32 @memcpy(i8* %17, %struct.ip* %4, i32 20)
%19 = load i8*, i8** %9
%20 = getelementptr inbounds i8, i8* %19, i64 20
store i8* %20, i8** %9
%21 = load i32, i32* %10
%22 = add nsw i32 %21, 20
store i32 %22, i32* %10
%23 = load i8*, i8** %9
%24 = load %struct.ip*, %struct.ip** %5
%25 = load i32, i32* %6
%26 = call i32 @memcpy(i8* %23, %struct.ip* %24, i32 %25)
%27 = load i32, i32* %6
%28 = load i8*, i8** %9
%29 = sext i32 %27 to i64
%30 = getelementptr inbounds i8, i8* %28, i64 %29
store i8* %30, i8** %9
%31 = load i32, i32* %6
%32 = load i32, i32* %10
%33 = add nsw i32 %32, %31
store i32 %33, i32* %10
%34 = bitcast i8* %15 to i32*
%35 = load i32, i32* %10
%36 = call i32 @checksum(i32* %34, i32 %35)
%37 = load i8*, i8** %7
ret i32 %36
}

LLVM IR (-O2):
%struct.ip = type { i32 }
@IP_MAXPACKET = global i32 0
define i32 @ip4_checksum(i32 %0, %struct.ip* %1, i32 %2) {
%4 = alloca %struct.ip
%5 = getelementptr inbounds %struct.ip, %struct.ip* %4, i64 0, i32 0
store i32 %0, i32* %5
%6 = load i32, i32* @IP_MAXPACKET
%7 = zext i32 %6 to i64
%8 = alloca i8, i64 %7
%9 = call i32 @memcpy(i8* %8, %struct.ip* %4, i32 20)
%10 = getelementptr inbounds i8, i8* %8, i64 20
%11 = call i32 @memcpy(i8* %10, %struct.ip* %1, i32 %2)
%12 = add nsw i32 %2, 20
%13 = bitcast i8* %8 to i32*
%14 = call i32 @checksum(i32* %13, i32 %12)
ret i32 %14
}