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

/* Variables and functions */
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ build_ip_csum(void *, int, int /*<<< orphan*/);
void *htonl(int);
void *htons(int);
int build_ipv4_header(void *header, int payload_len) {
  struct iphdr *iph = header;
  iph->ihl = 5;
  iph->version = 4;
  iph->ttl = 8;
  iph->tot_len = htons(sizeof(*iph) + sizeof(struct udphdr) + payload_len);
  iph->id = htons(1337);
  iph->protocol = IPPROTO_UDP;
  iph->saddr = htonl((172 << 24) | (17 << 16) | 2);
  iph->daddr = htonl((172 << 24) | (17 << 16) | 1);
  iph->check = build_ip_csum((void *)iph, iph->ihl << 1, 0);
  return iph->ihl << 2;
}


x86 assembly (clang, -O0):
<build_ipv4_header>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movl   $0x5,(%rax)
mov    -0x18(%rbp),%rax
movl   $0x4,0x4(%rax)
mov    -0x18(%rbp),%rax
movl   $0x8,0x8(%rax)
movslq -0xc(%rbp),%rax
add    $0x3c,%rax
mov    %eax,%edi
call   1040 <htons@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,0x30(%rax)
mov    $0x539,%edi
call   1040 <htons@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,0x28(%rax)
mov    0x2e3b(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x20(%rax)
mov    $0xac110002,%edi
call   1050 <htonl@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,0x18(%rax)
mov    $0xac110001,%edi
call   1050 <htonl@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,0x10(%rax)
mov    -0x18(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
shl    $0x1,%esi
xor    %edx,%edx
call   1030 <build_ip_csum@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0xc(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
shl    $0x2,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<build_ipv4_header>:
push   %rbx
mov    %rdi,%rbx
movabs $0x400000005,%rax
mov    %rax,(%rdi)
movl   $0x8,0x8(%rdi)
lea    0x3c(%rsi),%edi
call   1040 <htons@plt>
mov    %rax,0x30(%rbx)
mov    $0x539,%edi
call   1040 <htons@plt>
mov    %rax,0x28(%rbx)
mov    0x2e6f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rbx)
mov    $0xac110002,%edi
call   1050 <htonl@plt>
mov    %rax,0x18(%rbx)
mov    $0xac110001,%edi
call   1050 <htonl@plt>
mov    %rax,0x10(%rbx)
mov    (%rbx),%esi
add    %esi,%esi
mov    %rbx,%rdi
xor    %edx,%edx
call   1030 <build_ip_csum@plt>
mov    %eax,0xc(%rbx)
mov    (%rbx),%eax
shl    $0x2,%eax
pop    %rbx
ret

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

LLVM IR (-O2):
@IPPROTO_UDP = global i32 0
define i32 @build_ipv4_header(i8* %0, i32 %1) {
%3 = bitcast i8* %0 to i32*
store i32 5, i32* %3
%4 = getelementptr inbounds i8, i8* %0, i64 4
%5 = bitcast i8* %4 to i32*
store i32 4, i32* %5
%6 = getelementptr inbounds i8, i8* %0, i64 8
%7 = bitcast i8* %6 to i32*
store i32 8, i32* %7
%8 = add i32 %1, 60
%9 = tail call i8* @htons(i32 %8)
%10 = getelementptr inbounds i8, i8* %0, i64 48
%11 = bitcast i8* %10 to i8**
store i8* %9, i8** %11
%12 = tail call i8* @htons(i32 1337)
%13 = getelementptr inbounds i8, i8* %0, i64 40
%14 = bitcast i8* %13 to i8**
store i8* %12, i8** %14
%15 = load i32, i32* @IPPROTO_UDP
%16 = getelementptr inbounds i8, i8* %0, i64 32
%17 = bitcast i8* %16 to i32*
store i32 %15, i32* %17
%18 = tail call i8* @htonl(i32 -1408172030)
%19 = getelementptr inbounds i8, i8* %0, i64 24
%20 = bitcast i8* %19 to i8**
store i8* %18, i8** %20
%21 = tail call i8* @htonl(i32 -1408172031)
%22 = getelementptr inbounds i8, i8* %0, i64 16
%23 = bitcast i8* %22 to i8**
store i8* %21, i8** %23
%24 = load i32, i32* %3
%25 = shl i32 %24, 1
%26 = tail call i32 @build_ip_csum(i8* %0, i32 %25, i32 0)
%27 = getelementptr inbounds i8, i8* %0, i64 12
%28 = bitcast i8* %27 to i32*
store i32 %26, i32* %28
%29 = load i32, i32* %3
%30 = shl i32 %29, 2
ret i32 %30
}