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

/* Variables and functions */
int /*<<< orphan*/ IPPROTO_ICMP;
void *htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memset(struct iphdr *, int /*<<< orphan*/, int);
int rand();
void init_iphdr(struct iphdr *ip, char *dest, int len) {
  memset(ip, 0, sizeof(struct iphdr));
  ip->version = 4;
  ip->ihl = 5;
  ip->tos = 0;
  ip->tot_len = htons(len);
  ip->id = htons(rand() % 255);
  ip->ttl = 64;
  ip->check = 0;
  ip->protocol = IPPROTO_ICMP;
  ip->daddr = inet_addr(dest);
}


x86 assembly (clang, -O0):
<init_iphdr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
mov    $0x38,%edx
call   1040 <memset@plt>
mov    -0x8(%rbp),%rax
movl   $0x4,(%rax)
mov    -0x8(%rbp),%rax
movl   $0x5,0x4(%rax)
mov    -0x8(%rbp),%rax
movq   $0x0,0x30(%rax)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,0x28(%rax)
mov    $0x0,%al
call   1060 <rand@plt>
mov    $0xff,%ecx
cltd
idiv   %ecx
mov    %edx,%edi
call   1030 <htons@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,0x20(%rax)
mov    -0x8(%rbp),%rax
movl   $0x40,0x8(%rax)
mov    -0x8(%rbp),%rax
movq   $0x0,0x18(%rax)
mov    0x2e12(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,0x10(%rax)
mov    -0x10(%rbp),%rdi
call   1050 <inet_addr@plt>
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,0xc(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_iphdr>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %esi,%esi
mov    $0x38,%edx
call   1040 <memset@plt>
movabs $0x500000004,%rax
mov    %rax,(%rbx)
movq   $0x0,0x30(%rbx)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %rax,0x28(%rbx)
xor    %eax,%eax
call   1060 <rand@plt>
cltq
imul   $0xffffffff80808081,%rax,%rdi
shr    $0x20,%rdi
add    %eax,%edi
mov    %edi,%ecx
shr    $0x1f,%ecx
sar    $0x7,%edi
add    %ecx,%edi
mov    %edi,%ecx
shl    $0x8,%ecx
sub    %ecx,%edi
add    %eax,%edi
call   1030 <htons@plt>
mov    %rax,0x20(%rbx)
movl   $0x40,0x8(%rbx)
movq   $0x0,0x18(%rbx)
mov    0x2e28(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rbx)
mov    %r14,%rdi
call   1050 <inet_addr@plt>
mov    %eax,0xc(%rbx)
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.iphdr = type { i32, i32, i32, i32, i32, i64, i8*, i8*, i64 }
@IPPROTO_ICMP = global i32 0
define void @init_iphdr(%struct.iphdr* %0, i8* %1, i32 %2) {
%4 = alloca %struct.iphdr*
%5 = alloca i8*
%6 = alloca i32
store %struct.iphdr* %0, %struct.iphdr** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%7 = load %struct.iphdr*, %struct.iphdr** %4
%8 = call i32 @memset(%struct.iphdr* %7, i32 0, i32 56)
%9 = load %struct.iphdr*, %struct.iphdr** %4
%10 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %9, i32 0, i32 0
store i32 4, i32* %10
%11 = load %struct.iphdr*, %struct.iphdr** %4
%12 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %11, i32 0, i32 1
store i32 5, i32* %12
%13 = load %struct.iphdr*, %struct.iphdr** %4
%14 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %13, i32 0, i32 8
store i64 0, i64* %14
%15 = load i32, i32* %6
%16 = call i8* @htons(i32 %15)
%17 = load %struct.iphdr*, %struct.iphdr** %4
%18 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %17, i32 0, i32 7
store i8* %16, i8** %18
%19 = call i32 (...) @rand()
%20 = srem i32 %19, 255
%21 = call i8* @htons(i32 %20)
%22 = load %struct.iphdr*, %struct.iphdr** %4
%23 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %22, i32 0, i32 6
store i8* %21, i8** %23
%24 = load %struct.iphdr*, %struct.iphdr** %4
%25 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %24, i32 0, i32 2
store i32 64, i32* %25
%26 = load %struct.iphdr*, %struct.iphdr** %4
%27 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %26, i32 0, i32 5
store i64 0, i64* %27
%28 = load i32, i32* @IPPROTO_ICMP
%29 = load %struct.iphdr*, %struct.iphdr** %4
%30 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %29, i32 0, i32 4
store i32 %28, i32* %30
%31 = load i8*, i8** %5
%32 = call i32 @inet_addr(i8* %31)
%33 = load %struct.iphdr*, %struct.iphdr** %4
%34 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %33, i32 0, i32 3
store i32 %32, i32* %34
ret void
}

LLVM IR (-O2):
%struct.iphdr = type { i32, i32, i32, i32, i32, i64, i8*, i8*, i64 }
@IPPROTO_ICMP = global i32 0
define void @init_iphdr(%struct.iphdr* %0, i8* %1, i32 %2) {
%4 = tail call i32 @memset(%struct.iphdr* %0, i32 0, i32 56)
%5 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %0, i64 0, i32 0
store i32 4, i32* %5
%6 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %0, i64 0, i32 1
store i32 5, i32* %6
%7 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %0, i64 0, i32 8
store i64 0, i64* %7
%8 = tail call i8* @htons(i32 %2)
%9 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %0, i64 0, i32 7
store i8* %8, i8** %9
%10 = tail call i32 (...) @rand()
%11 = srem i32 %10, 255
%12 = tail call i8* @htons(i32 %11)
%13 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %0, i64 0, i32 6
store i8* %12, i8** %13
%14 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %0, i64 0, i32 2
store i32 64, i32* %14
%15 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %0, i64 0, i32 5
store i64 0, i64* %15
%16 = load i32, i32* @IPPROTO_ICMP
%17 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %0, i64 0, i32 4
store i32 %16, i32* %17
%18 = tail call i32 @inet_addr(i8* %1)
%19 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %0, i64 0, i32 3
store i32 %18, i32* %19
ret void
}