#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 */
typedef int /*<<< orphan*/ uint8_t;
typedef int /*<<< orphan*/ uint16_t;
struct iphdr {
  int version;
  int ihl;
  int ttl;
  int /*<<< orphan*/ check;
  int /*<<< orphan*/ daddr;
  int /*<<< orphan*/ saddr;
  int /*<<< orphan*/ protocol;
  void *tot_len;
  void *id;
};
struct in_addr {
  int dummy;
};
typedef int /*<<< orphan*/ ssize_t;

/* Variables and functions */
void *htons(int /*<<< orphan*/);
scalar_t__ inet_aton(char *, struct in_addr *);
int /*<<< orphan*/ ip_checksum(struct iphdr *, int);
int /*<<< orphan*/ printf(char *);
void make_ipv4(struct iphdr *ip, char *src_addr, char *dst_addr, uint8_t proto,
               uint16_t id, ssize_t total_len) {
  ip->version = 4;
  ip->ihl = 5;
  ip->ttl = 64;
  ip->id = htons(id);
  ip->tot_len = htons(total_len);
  ip->protocol = proto;
  if (inet_aton(src_addr, (struct in_addr *)&ip->saddr) == 0) {
    printf("Cannot add IPv4 src address!\n");
  }
  if (inet_aton(dst_addr, (struct in_addr *)&ip->daddr) == 0) {
    printf("Cannot add IPv4 dst address!\n");
  }
  ip->check = ip_checksum(ip, sizeof(struct iphdr));
}


x86 assembly (clang, -O0):
<make_ipv4>:
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    %r8d,-0x20(%rbp)
mov    %r9d,-0x24(%rbp)
mov    -0x8(%rbp),%rax
movl   $0x4,(%rax)
mov    -0x8(%rbp),%rax
movl   $0x5,0x4(%rax)
mov    -0x8(%rbp),%rax
movl   $0x40,0x8(%rax)
mov    -0x20(%rbp),%edi
call   1030 <htons@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,0x28(%rax)
mov    -0x24(%rbp),%edi
call   1030 <htons@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,0x20(%rax)
mov    -0x1c(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,0x18(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
add    $0x14,%rsi
call   1050 <inet_aton@plt>
cmp    $0x0,%rax
jne    11d6 <make_ipv4+0x96>
lea    0xe2f(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
add    $0x10,%rsi
call   1050 <inet_aton@plt>
cmp    $0x0,%rax
jne    11fd <make_ipv4+0xbd>
lea    0xe26(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    $0x30,%esi
call   1060 <ip_checksum@plt>
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,0xc(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_ipv4>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %r9d,%ebp
mov    %ecx,%r15d
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%rbx
movabs $0x500000004,%rax
mov    %rax,(%rdi)
movl   $0x40,0x8(%rdi)
mov    %r8d,%edi
call   1040 <htons@plt>
mov    %rax,0x28(%rbx)
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %rax,0x20(%rbx)
mov    %r15d,0x18(%rbx)
lea    0x14(%rbx),%rsi
mov    %r12,%rdi
call   1050 <inet_aton@plt>
test   %rax,%rax
jne    11a3 <make_ipv4+0x63>
lea    0xe7f(%rip),%rdi
call   1030 <puts@plt>
lea    0x10(%rbx),%rsi
mov    %r14,%rdi
call   1050 <inet_aton@plt>
test   %rax,%rax
jne    11c0 <make_ipv4+0x80>
lea    0xe45(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
mov    $0x30,%esi
call   1060 <ip_checksum@plt>
mov    %eax,0xc(%rbx)
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.iphdr = type { i32, i32, i32, i32, i32, i32, i32, i8*, i8* }
%struct.in_addr = type { i32 }
@.str = [30 x i8] c"Cannot add IPv4 src address!\0A\00"
@.str.1 = [30 x i8] c"Cannot add IPv4 dst address!\0A\00"
define void @make_ipv4(%struct.iphdr* %0, i8* %1, i8* %2, i32 %3, i32 %4, i32 %5) {
%7 = alloca %struct.iphdr*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store %struct.iphdr* %0, %struct.iphdr** %7
store i8* %1, i8** %8
store i8* %2, i8** %9
store i32 %3, i32* %10
store i32 %4, i32* %11
store i32 %5, i32* %12
%13 = load %struct.iphdr*, %struct.iphdr** %7
%14 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %13, i32 0, i32 0
store i32 4, i32* %14
%15 = load %struct.iphdr*, %struct.iphdr** %7
%16 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %15, i32 0, i32 1
store i32 5, i32* %16
%17 = load %struct.iphdr*, %struct.iphdr** %7
%18 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %17, i32 0, i32 2
store i32 64, i32* %18
%19 = load i32, i32* %11
%20 = call i8* @htons(i32 %19)
%21 = load %struct.iphdr*, %struct.iphdr** %7
%22 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %21, i32 0, i32 8
store i8* %20, i8** %22
%23 = load i32, i32* %12
%24 = call i8* @htons(i32 %23)
%25 = load %struct.iphdr*, %struct.iphdr** %7
%26 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %25, i32 0, i32 7
store i8* %24, i8** %26
%27 = load i32, i32* %10
%28 = load %struct.iphdr*, %struct.iphdr** %7
%29 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %28, i32 0, i32 6
store i32 %27, i32* %29
%30 = load i8*, i8** %8
%31 = load %struct.iphdr*, %struct.iphdr** %7
%32 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %31, i32 0, i32 5
%33 = bitcast i32* %32 to %struct.in_addr*
%34 = call i64 @inet_aton(i8* %30, %struct.in_addr* %33)
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %6
%37 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
br label %38
38: ; preds = %36, %6
%39 = load i8*, i8** %9
%40 = load %struct.iphdr*, %struct.iphdr** %7
%41 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %40, i32 0, i32 4
%42 = bitcast i32* %41 to %struct.in_addr*
%43 = call i64 @inet_aton(i8* %39, %struct.in_addr* %42)
%44 = icmp eq i64 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %38
%46 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %47
47: ; preds = %45, %38
%48 = load %struct.iphdr*, %struct.iphdr** %7
%49 = call i32 @ip_checksum(%struct.iphdr* %48, i32 48)
%50 = load %struct.iphdr*, %struct.iphdr** %7
%51 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %50, i32 0, i32 3
store i32 %49, i32* %51
ret void
}

LLVM IR (-O2):
%struct.iphdr = type { i32, i32, i32, i32, i32, i32, i32, i8*, i8* }
%struct.in_addr = type { i32 }
@str = [29 x i8] c"Cannot add IPv4 dst address!\00"
@str.2 = [29 x i8] c"Cannot add IPv4 src address!\00"
define void @make_ipv4(%struct.iphdr* %0, i8* %1, i8* %2, i32 %3, i32 %4, i32 %5) {
%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 2
store i32 64, i32* %9
%10 = tail call i8* @htons(i32 %4)
%11 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %0, i64 0, i32 8
store i8* %10, i8** %11
%12 = tail call i8* @htons(i32 %5)
%13 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %0, i64 0, i32 7
store i8* %12, i8** %13
%14 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %0, i64 0, i32 6
store i32 %3, i32* %14
%15 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %0, i64 0, i32 5
%16 = bitcast i32* %15 to %struct.in_addr*
%17 = tail call i64 @inet_aton(i8* %1, %struct.in_addr* %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %6
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.2, i64 0, i64 0))
br label %21
21: ; preds = %19, %6
%22 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %0, i64 0, i32 4
%23 = bitcast i32* %22 to %struct.in_addr*
%24 = tail call i64 @inet_aton(i8* %2, %struct.in_addr* %23)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
br label %28
28: ; preds = %26, %21
%29 = tail call i32 @ip_checksum(%struct.iphdr* %0, i32 48)
%30 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %0, i64 0, i32 3
store i32 %29, i32* %30
ret void
}