#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 iphdr {
  int version;
  int ihl;
  int id;
  int ttl;
  int protocol;
  int /*<<< orphan*/ check;
  void *daddr;
  void *saddr;
  scalar_t__ frag_off;
  int /*<<< orphan*/ tot_len;
  scalar_t__ tos;
};

/* Variables and functions */
int /*<<< orphan*/ htons(int);
void *inet_addr(char *);
int /*<<< orphan*/ ip_fast_csum(unsigned char *, int);
int CompileIPhdr(struct iphdr *ip, char *sAddr, char *dAddr, int protocol,
                 int ID) {
  ip->version = 4;
  ip->ihl = 5;
  ip->tos = 0;
  ip->tot_len = htons(40);
  ip->id = ID;
  ip->frag_off = 0;
  ip->ttl = 255;
  ip->protocol = protocol;
  ip->saddr = inet_addr(sAddr);
  ip->daddr = inet_addr(dAddr);
  ip->check = ip_fast_csum((unsigned char *)&ip, ip->ihl);
  return 1;
}


x86 assembly (clang, -O0):
<CompileIPhdr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
mov    -0x8(%rbp),%rax
movl   $0x4,(%rax)
mov    -0x8(%rbp),%rax
movl   $0x5,0x4(%rax)
mov    -0x8(%rbp),%rax
movq   $0x0,0x38(%rax)
mov    $0x28,%edi
call   1030 <htons@plt>
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,0x30(%rax)
mov    -0x20(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x8(%rbp),%rax
movq   $0x0,0x28(%rax)
mov    -0x8(%rbp),%rax
movl   $0xff,0xc(%rax)
mov    -0x1c(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,0x10(%rax)
mov    -0x10(%rbp),%rdi
call   1050 <inet_addr@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,0x20(%rax)
mov    -0x18(%rbp),%rdi
call   1050 <inet_addr@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,0x18(%rax)
lea    -0x8(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%esi
call   1040 <ip_fast_csum@plt>
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,0x14(%rax)
mov    $0x1,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<CompileIPhdr>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %r8d,%ebx
mov    %ecx,%r12d
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbp
mov    %rdi,0x8(%rsp)
movabs $0x500000004,%rax
mov    %rax,(%rdi)
movq   $0x0,0x38(%rdi)
mov    $0x28,%edi
call   1030 <htons@plt>
mov    %eax,0x30(%rbp)
mov    %ebx,0x8(%rbp)
movq   $0x0,0x28(%rbp)
movl   $0xff,0xc(%rbp)
mov    %r12d,0x10(%rbp)
mov    %r15,%rdi
call   1050 <inet_addr@plt>
mov    %rax,0x20(%rbp)
mov    %r14,%rdi
call   1050 <inet_addr@plt>
mov    %rax,0x18(%rbp)
mov    0x4(%rbp),%esi
lea    0x8(%rsp),%rdi
call   1040 <ip_fast_csum@plt>
mov    0x8(%rsp),%rcx
mov    %eax,0x14(%rcx)
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.iphdr = type { i32, i32, i32, i32, i32, i32, i8*, i8*, i64, i32, i64 }
define i32 @CompileIPhdr(%struct.iphdr* %0, i8* %1, i8* %2, i32 %3, i32 %4) {
%6 = alloca %struct.iphdr*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store %struct.iphdr* %0, %struct.iphdr** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
store i32 %4, i32* %10
%11 = load %struct.iphdr*, %struct.iphdr** %6
%12 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %11, i32 0, i32 0
store i32 4, i32* %12
%13 = load %struct.iphdr*, %struct.iphdr** %6
%14 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %13, i32 0, i32 1
store i32 5, i32* %14
%15 = load %struct.iphdr*, %struct.iphdr** %6
%16 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %15, i32 0, i32 10
store i64 0, i64* %16
%17 = call i32 @htons(i32 40)
%18 = load %struct.iphdr*, %struct.iphdr** %6
%19 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %18, i32 0, i32 9
store i32 %17, i32* %19
%20 = load i32, i32* %10
%21 = load %struct.iphdr*, %struct.iphdr** %6
%22 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %21, i32 0, i32 2
store i32 %20, i32* %22
%23 = load %struct.iphdr*, %struct.iphdr** %6
%24 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %23, i32 0, i32 8
store i64 0, i64* %24
%25 = load %struct.iphdr*, %struct.iphdr** %6
%26 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %25, i32 0, i32 3
store i32 255, i32* %26
%27 = load i32, i32* %9
%28 = load %struct.iphdr*, %struct.iphdr** %6
%29 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %28, i32 0, i32 4
store i32 %27, i32* %29
%30 = load i8*, i8** %7
%31 = call i8* @inet_addr(i8* %30)
%32 = load %struct.iphdr*, %struct.iphdr** %6
%33 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %32, i32 0, i32 7
store i8* %31, i8** %33
%34 = load i8*, i8** %8
%35 = call i8* @inet_addr(i8* %34)
%36 = load %struct.iphdr*, %struct.iphdr** %6
%37 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %36, i32 0, i32 6
store i8* %35, i8** %37
%38 = bitcast %struct.iphdr** %6 to i8*
%39 = load %struct.iphdr*, %struct.iphdr** %6
%40 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %39, i32 0, i32 1
%41 = load i32, i32* %40
%42 = call i32 @ip_fast_csum(i8* %38, i32 %41)
%43 = load %struct.iphdr*, %struct.iphdr** %6
%44 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %43, i32 0, i32 5
store i32 %42, i32* %44
ret i32 1
}

LLVM IR (-O2):
%struct.iphdr = type { i32, i32, i32, i32, i32, i32, i8*, i8*, i64, i32, i64 }
define i32 @CompileIPhdr(%struct.iphdr* %0, i8* %1, i8* %2, i32 %3, i32 %4) {
%6 = alloca %struct.iphdr*
store %struct.iphdr* %0, %struct.iphdr** %6
%7 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %0, i64 0, i32 0
store i32 4, i32* %7
%8 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %0, i64 0, i32 1
store i32 5, i32* %8
%9 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %0, i64 0, i32 10
store i64 0, i64* %9
%10 = tail call i32 @htons(i32 40)
%11 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %0, i64 0, i32 9
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %0, i64 0, i32 2
store i32 %4, i32* %12
%13 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %0, i64 0, i32 8
store i64 0, i64* %13
%14 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %0, i64 0, i32 3
store i32 255, i32* %14
%15 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %0, i64 0, i32 4
store i32 %3, i32* %15
%16 = tail call i8* @inet_addr(i8* %1)
%17 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %0, i64 0, i32 7
store i8* %16, i8** %17
%18 = tail call i8* @inet_addr(i8* %2)
%19 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %0, i64 0, i32 6
store i8* %18, i8** %19
%20 = bitcast %struct.iphdr** %6 to i8*
%21 = load i32, i32* %8
%22 = call i32 @ip_fast_csum(i8* %20, i32 %21)
%23 = load %struct.iphdr*, %struct.iphdr** %6
%24 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %23, i64 0, i32 5
store i32 %22, i32* %24
ret i32 1
}