#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 TYPE_4__ {
  void *s_addr;
};
struct TYPE_3__ {
  void *s_addr;
};
struct ip {
  int ip_hl;
  int ip_v;
  int ip_tos;
  int ip_ttl;
  int ip_p;
  int ip_sum;
  TYPE_2__ ip_dst;
  TYPE_1__ ip_src;
  scalar_t__ ip_off;
  scalar_t__ ip_id;
  int /*<<< orphan*/ ip_len;
};

/* Variables and functions */
int /*<<< orphan*/ htons(int);
int in_cksum(unsigned short *, int);
void *inet_addr(char *);
int write_ipv4_header(unsigned char *buffer, char *source_ip, char *dest_ip,
                      int ip_data_len) {
  struct ip *ip_header = (struct ip *)buffer;
  ip_header->ip_hl = 0x05;
  ip_header->ip_v = 4;
  ip_header->ip_tos = 0xc0;
  ip_header->ip_len = htons(ip_data_len + sizeof(struct ip));
  ip_header->ip_id = 0;
  ip_header->ip_off = 0;
  ip_header->ip_ttl = 1;
  ip_header->ip_p = 89;
  ip_header->ip_sum = 0x0000;
  ip_header->ip_src.s_addr = inet_addr(source_ip);
  ip_header->ip_dst.s_addr = inet_addr(dest_ip);
  ip_header->ip_sum = in_cksum((unsigned short *)ip_header, sizeof(struct ip));
  return sizeof(struct ip);
}


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

x86 assembly (clang, -O2):
<write_ipv4_header>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
movabs $0x400000005,%rax
mov    %rax,(%rdi)
movl   $0xc0,0x8(%rdi)
lea    0x40(%rcx),%edi
call   1040 <htons@plt>
mov    %eax,0x38(%rbx)
xorps  %xmm0,%xmm0
movups %xmm0,0x28(%rbx)
movabs $0x5900000001,%rax
mov    %rax,0xc(%rbx)
movl   $0x0,0x14(%rbx)
mov    %r15,%rdi
call   1050 <inet_addr@plt>
mov    %rax,0x20(%rbx)
mov    %r14,%rdi
call   1050 <inet_addr@plt>
mov    %rax,0x18(%rbx)
mov    %rbx,%rdi
mov    $0x40,%esi
call   1030 <in_cksum@plt>
mov    %eax,0x14(%rbx)
mov    $0x40,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.ip = type { i32, i32, i32, i32, i32, i32, %struct.TYPE_4__, %struct.TYPE_3__, i64, i64, i32 }
%struct.TYPE_4__ = type { i8* }
%struct.TYPE_3__ = type { i8* }
define i32 @write_ipv4_header(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca %struct.ip*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%10 = load i8*, i8** %5
%11 = bitcast i8* %10 to %struct.ip*
store %struct.ip* %11, %struct.ip** %9
%12 = load %struct.ip*, %struct.ip** %9
%13 = getelementptr inbounds %struct.ip, %struct.ip* %12, i32 0, i32 0
store i32 5, i32* %13
%14 = load %struct.ip*, %struct.ip** %9
%15 = getelementptr inbounds %struct.ip, %struct.ip* %14, i32 0, i32 1
store i32 4, i32* %15
%16 = load %struct.ip*, %struct.ip** %9
%17 = getelementptr inbounds %struct.ip, %struct.ip* %16, i32 0, i32 2
store i32 192, i32* %17
%18 = load i32, i32* %8
%19 = sext i32 %18 to i64
%20 = add i64 %19, 64
%21 = trunc i64 %20 to i32
%22 = call i32 @htons(i32 %21)
%23 = load %struct.ip*, %struct.ip** %9
%24 = getelementptr inbounds %struct.ip, %struct.ip* %23, i32 0, i32 10
store i32 %22, i32* %24
%25 = load %struct.ip*, %struct.ip** %9
%26 = getelementptr inbounds %struct.ip, %struct.ip* %25, i32 0, i32 9
store i64 0, i64* %26
%27 = load %struct.ip*, %struct.ip** %9
%28 = getelementptr inbounds %struct.ip, %struct.ip* %27, i32 0, i32 8
store i64 0, i64* %28
%29 = load %struct.ip*, %struct.ip** %9
%30 = getelementptr inbounds %struct.ip, %struct.ip* %29, i32 0, i32 3
store i32 1, i32* %30
%31 = load %struct.ip*, %struct.ip** %9
%32 = getelementptr inbounds %struct.ip, %struct.ip* %31, i32 0, i32 4
store i32 89, i32* %32
%33 = load %struct.ip*, %struct.ip** %9
%34 = getelementptr inbounds %struct.ip, %struct.ip* %33, i32 0, i32 5
store i32 0, i32* %34
%35 = load i8*, i8** %6
%36 = call i8* @inet_addr(i8* %35)
%37 = load %struct.ip*, %struct.ip** %9
%38 = getelementptr inbounds %struct.ip, %struct.ip* %37, i32 0, i32 7
%39 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %38, i32 0, i32 0
store i8* %36, i8** %39
%40 = load i8*, i8** %7
%41 = call i8* @inet_addr(i8* %40)
%42 = load %struct.ip*, %struct.ip** %9
%43 = getelementptr inbounds %struct.ip, %struct.ip* %42, i32 0, i32 6
%44 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %43, i32 0, i32 0
store i8* %41, i8** %44
%45 = load %struct.ip*, %struct.ip** %9
%46 = bitcast %struct.ip* %45 to i16*
%47 = call i32 @in_cksum(i16* %46, i32 64)
%48 = load %struct.ip*, %struct.ip** %9
%49 = getelementptr inbounds %struct.ip, %struct.ip* %48, i32 0, i32 5
store i32 %47, i32* %49
ret i32 64
}

LLVM IR (-O2):
define i32 @write_ipv4_header(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = bitcast i8* %0 to i32*
store i32 5, i32* %5
%6 = getelementptr inbounds i8, i8* %0, i64 4
%7 = bitcast i8* %6 to i32*
store i32 4, i32* %7
%8 = getelementptr inbounds i8, i8* %0, i64 8
%9 = bitcast i8* %8 to i32*
store i32 192, i32* %9
%10 = add i32 %3, 64
%11 = tail call i32 @htons(i32 %10)
%12 = getelementptr inbounds i8, i8* %0, i64 56
%13 = bitcast i8* %12 to i32*
store i32 %11, i32* %13
%14 = getelementptr inbounds i8, i8* %0, i64 40
%15 = getelementptr inbounds i8, i8* %0, i64 12
%16 = bitcast i8* %15 to i32*
store i32 1, i32* %16
%17 = getelementptr inbounds i8, i8* %0, i64 16
%18 = bitcast i8* %17 to i32*
store i32 89, i32* %18
%19 = getelementptr inbounds i8, i8* %0, i64 20
%20 = bitcast i8* %19 to i32*
store i32 0, i32* %20
%21 = tail call i8* @inet_addr(i8* %1)
%22 = getelementptr inbounds i8, i8* %0, i64 32
%23 = bitcast i8* %22 to i8**
store i8* %21, i8** %23
%24 = tail call i8* @inet_addr(i8* %2)
%25 = getelementptr inbounds i8, i8* %0, i64 24
%26 = bitcast i8* %25 to i8**
store i8* %24, i8** %26
%27 = bitcast i8* %0 to i16*
%28 = tail call i32 @in_cksum(i16* %27, i32 64)
store i32 %28, i32* %20
ret i32 64
}