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

/* Variables and functions */
void *htons(scalar_t__);
scalar_t__ ip_cksum(struct ip *, int, int /*<<< orphan*/);
void sock_ipv4_encapsulate(uint8_t *buf, uint8_t proto, uint16_t len,
                           uint32_t saddr, uint32_t daddr) {
  struct ip *iph = (struct ip *)buf;
  iph->ip_hl = 5;
  iph->ip_v = 4;
  iph->ip_tos = 0;
  iph->ip_id = htons(0);
  iph->ip_off = 0;
  iph->ip_ttl = 64;
  iph->ip_p = proto;
  iph->ip_len = htons(len + sizeof(struct ip));
  iph->ip_src.s_addr = saddr;
  iph->ip_dst.s_addr = daddr;
  iph->ip_sum = 0;
  iph->ip_sum = ip_cksum(iph, iph->ip_hl * 4, 0);
}


x86 assembly (clang, -O0):
<sock_ipv4_encapsulate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
movl   $0x5,(%rax)
mov    -0x30(%rbp),%rax
movl   $0x4,0x4(%rax)
mov    -0x30(%rbp),%rax
movq   $0x0,0x48(%rax)
xor    %eax,%eax
mov    %eax,%edi
call   1040 <htons@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,0x40(%rax)
mov    -0x30(%rbp),%rax
movq   $0x0,0x38(%rax)
mov    -0x30(%rbp),%rax
movl   $0x40,0x8(%rax)
mov    -0xc(%rbp),%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,0x30(%rax)
mov    -0x18(%rbp),%rdi
add    $0x50,%rdi
call   1040 <htons@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,0x28(%rax)
mov    -0x20(%rbp),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,0x20(%rax)
mov    -0x28(%rbp),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,0x18(%rax)
mov    -0x30(%rbp),%rax
movq   $0x0,0x10(%rax)
mov    -0x30(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    (%rax),%esi
shl    $0x2,%esi
xor    %edx,%edx
call   1030 <ip_cksum@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,0x10(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_ipv4_encapsulate>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %r8,%r14
mov    %rcx,%r15
mov    %rdx,%r12
mov    %esi,%ebp
mov    %rdi,%rbx
movabs $0x400000005,%rax
mov    %rax,(%rdi)
movq   $0x0,0x48(%rdi)
xor    %edi,%edi
call   1040 <htons@plt>
mov    %rax,0x40(%rbx)
movq   $0x0,0x38(%rbx)
movl   $0x40,0x8(%rbx)
mov    %ebp,0x30(%rbx)
add    $0x50,%r12
mov    %r12,%rdi
call   1040 <htons@plt>
mov    %rax,0x28(%rbx)
mov    %r15,0x20(%rbx)
mov    %r14,0x18(%rbx)
movq   $0x0,0x10(%rbx)
mov    (%rbx),%esi
shl    $0x2,%esi
mov    %rbx,%rdi
xor    %edx,%edx
call   1030 <ip_cksum@plt>
mov    %rax,0x10(%rbx)
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.ip = type { i32, i32, i32, i64, %struct.TYPE_4__, %struct.TYPE_3__, i8*, i32, i64, i8*, i64 }
%struct.TYPE_4__ = type { i8* }
%struct.TYPE_3__ = type { i8* }
define void @sock_ipv4_encapsulate(i32* %0, i32 %1, i64 %2, i8* %3, i8* %4) {
%6 = bitcast i32* %0 to %struct.ip*
store i32 5, i32* %0
%7 = getelementptr inbounds i32, i32* %0, i64 1
store i32 4, i32* %7
%8 = getelementptr inbounds i32, i32* %0, i64 18
%9 = bitcast i32* %8 to i64*
store i64 0, i64* %9
%10 = tail call i8* @htons(i64 0)
%11 = getelementptr inbounds i32, i32* %0, i64 16
%12 = bitcast i32* %11 to i8**
store i8* %10, i8** %12
%13 = getelementptr inbounds i32, i32* %0, i64 14
%14 = bitcast i32* %13 to i64*
store i64 0, i64* %14
%15 = getelementptr inbounds i32, i32* %0, i64 2
store i32 64, i32* %15
%16 = getelementptr inbounds i32, i32* %0, i64 12
store i32 %1, i32* %16
%17 = add i64 %2, 80
%18 = tail call i8* @htons(i64 %17)
%19 = getelementptr inbounds i32, i32* %0, i64 10
%20 = bitcast i32* %19 to i8**
store i8* %18, i8** %20
%21 = getelementptr inbounds i32, i32* %0, i64 8
%22 = bitcast i32* %21 to i8**
store i8* %3, i8** %22
%23 = getelementptr inbounds i32, i32* %0, i64 6
%24 = bitcast i32* %23 to i8**
store i8* %4, i8** %24
%25 = getelementptr inbounds i32, i32* %0, i64 4
%26 = bitcast i32* %25 to i64*
store i64 0, i64* %26
%27 = load i32, i32* %0
%28 = shl nsw i32 %27, 2
%29 = tail call i64 @ip_cksum(%struct.ip* %6, i32 %28, i32 0)
store i64 %29, i64* %26
ret void
}