#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 {
  scalar_t__ check;
  void *len;
  void *source;
  void *dest;
};
struct iphdr {
  int ttl;
  int ihl;
  int version;
  scalar_t__ check;
  void *tot_len;
  int /*<<< orphan*/ protocol;
  void *daddr;
  void *saddr;
};
struct ethhdr {
  void *h_proto;
};

/* Variables and functions */
size_t ETH_P_IP;
scalar_t__ INADDR_LOOPBACK;
int /*<<< orphan*/ IPPROTO_UDP;
void *htonl(scalar_t__);
void *htons(size_t);
int /*<<< orphan*/ memset(void *, char, size_t);
void build_packet(void *buffer, size_t blen, char payload_char) {
  struct udphdr *udph;
  struct ethhdr *eth;
  struct iphdr *iph;
  size_t off = 0;
  memset(buffer, 0, blen);
  eth = buffer;
  eth->h_proto = htons(ETH_P_IP);
  off += sizeof(*eth);
  iph = buffer + off;
  iph->ttl = 8;
  iph->ihl = 5;
  iph->version = 4;
  iph->saddr = htonl(INADDR_LOOPBACK);
  iph->daddr = htonl(INADDR_LOOPBACK + 1);
  iph->protocol = IPPROTO_UDP;
  iph->tot_len = htons(blen - off);
  iph->check = 0;
  off += sizeof(*iph);
  udph = buffer + off;
  udph->dest = htons(8000);
  udph->source = htons(8001);
  udph->len = htons(blen - off);
  udph->check = 0;
  off += sizeof(*udph);
  memset(buffer + off, payload_char, blen - off);
}


x86 assembly (clang, -O0):
<build_packet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %dl,%al
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rdx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%rdi
call   1030 <htons@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
movl   $0x8,(%rax)
mov    -0x30(%rbp),%rax
movl   $0x5,0x4(%rax)
mov    -0x30(%rbp),%rax
movl   $0x4,0x8(%rax)
mov    0x2e2c(%rip),%rax
mov    (%rax),%rdi
call   1040 <htonl@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,0x30(%rax)
mov    0x2e12(%rip),%rax
mov    (%rax),%rdi
add    $0x1,%rdi
call   1040 <htonl@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,0x28(%rax)
mov    0x2dd4(%rip),%rax
mov    (%rax),%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,0x20(%rax)
mov    -0x10(%rbp),%rdi
sub    -0x38(%rbp),%rdi
call   1030 <htons@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,0x18(%rax)
mov    -0x30(%rbp),%rax
movq   $0x0,0x10(%rax)
mov    -0x38(%rbp),%rax
add    $0x38,%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    $0x1f40,%edi
call   1030 <htons@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,0x18(%rax)
mov    $0x1f41,%edi
call   1030 <htons@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,0x10(%rax)
mov    -0x10(%rbp),%rdi
sub    -0x38(%rbp),%rdi
call   1030 <htons@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x38(%rbp),%rax
add    $0x20,%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
add    -0x38(%rbp),%rdi
mov    -0x11(%rbp),%al
mov    -0x10(%rbp),%rdx
sub    -0x38(%rbp),%rdx
movsbl %al,%esi
call   1050 <memset@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<build_packet>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %rdi,%rbp
xor    %esi,%esi
mov    %rbx,%rdx
call   1050 <memset@plt>
mov    0x2e87(%rip),%rax
mov    (%rax),%rdi
call   1030 <htons@plt>
mov    %rax,0x0(%rbp)
movabs $0x500000008,%rax
mov    %rax,0x8(%rbp)
movl   $0x4,0x10(%rbp)
mov    0x2e6f(%rip),%r15
mov    (%r15),%rdi
call   1040 <htonl@plt>
mov    %rax,0x38(%rbp)
mov    (%r15),%rdi
add    $0x1,%rdi
call   1040 <htonl@plt>
mov    %rax,0x30(%rbp)
mov    0x2e2c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rbp)
lea    -0x8(%rbx),%rdi
call   1030 <htons@plt>
mov    %rax,0x20(%rbp)
movq   $0x0,0x18(%rbp)
mov    $0x1f40,%edi
call   1030 <htons@plt>
mov    %rax,0x58(%rbp)
mov    $0x1f41,%edi
call   1030 <htons@plt>
mov    %rax,0x50(%rbp)
lea    -0x40(%rbx),%rdi
call   1030 <htons@plt>
mov    %rax,0x48(%rbp)
movq   $0x0,0x40(%rbp)
add    $0x60,%rbp
add    $0xffffffffffffffa0,%rbx
mov    %rbp,%rdi
mov    %r14d,%esi
mov    %rbx,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <memset@plt>

LLVM IR (-O0):
%struct.udphdr = type { i64, i8*, i8*, i8* }
%struct.ethhdr = type { i8* }
%struct.iphdr = type { i32, i32, i32, i64, i8*, i32, i8*, i8* }
@ETH_P_IP = global i64 0
@INADDR_LOOPBACK = global i64 0
@IPPROTO_UDP = global i32 0
define void @build_packet(i8* %0, i64 %1, i8 signext %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8
%7 = alloca %struct.udphdr*
%8 = alloca %struct.ethhdr*
%9 = alloca %struct.iphdr*
%10 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
store i8 %2, i8* %6
store i64 0, i64* %10
%11 = load i8*, i8** %4
%12 = load i64, i64* %5
%13 = call i32 @memset(i8* %11, i8 signext 0, i64 %12)
%14 = load i8*, i8** %4
%15 = bitcast i8* %14 to %struct.ethhdr*
store %struct.ethhdr* %15, %struct.ethhdr** %8
%16 = load i64, i64* @ETH_P_IP
%17 = call i8* @htons(i64 %16)
%18 = load %struct.ethhdr*, %struct.ethhdr** %8
%19 = getelementptr inbounds %struct.ethhdr, %struct.ethhdr* %18, i32 0, i32 0
store i8* %17, i8** %19
%20 = load i64, i64* %10
%21 = add i64 %20, 8
store i64 %21, i64* %10
%22 = load i8*, i8** %4
%23 = load i64, i64* %10
%24 = getelementptr i8, i8* %22, i64 %23
%25 = bitcast i8* %24 to %struct.iphdr*
store %struct.iphdr* %25, %struct.iphdr** %9
%26 = load %struct.iphdr*, %struct.iphdr** %9
%27 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %26, i32 0, i32 0
store i32 8, i32* %27
%28 = load %struct.iphdr*, %struct.iphdr** %9
%29 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %28, i32 0, i32 1
store i32 5, i32* %29
%30 = load %struct.iphdr*, %struct.iphdr** %9
%31 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %30, i32 0, i32 2
store i32 4, i32* %31
%32 = load i64, i64* @INADDR_LOOPBACK
%33 = call i8* @htonl(i64 %32)
%34 = load %struct.iphdr*, %struct.iphdr** %9
%35 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %34, i32 0, i32 7
store i8* %33, i8** %35
%36 = load i64, i64* @INADDR_LOOPBACK
%37 = add nsw i64 %36, 1
%38 = call i8* @htonl(i64 %37)
%39 = load %struct.iphdr*, %struct.iphdr** %9
%40 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %39, i32 0, i32 6
store i8* %38, i8** %40
%41 = load i32, i32* @IPPROTO_UDP
%42 = load %struct.iphdr*, %struct.iphdr** %9
%43 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %42, i32 0, i32 5
store i32 %41, i32* %43
%44 = load i64, i64* %5
%45 = load i64, i64* %10
%46 = sub i64 %44, %45
%47 = call i8* @htons(i64 %46)
%48 = load %struct.iphdr*, %struct.iphdr** %9
%49 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %48, i32 0, i32 4
store i8* %47, i8** %49
%50 = load %struct.iphdr*, %struct.iphdr** %9
%51 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %50, i32 0, i32 3
store i64 0, i64* %51
%52 = load i64, i64* %10
%53 = add i64 %52, 56
store i64 %53, i64* %10
%54 = load i8*, i8** %4
%55 = load i64, i64* %10
%56 = getelementptr i8, i8* %54, i64 %55
%57 = bitcast i8* %56 to %struct.udphdr*
store %struct.udphdr* %57, %struct.udphdr** %7
%58 = call i8* @htons(i64 8000)
%59 = load %struct.udphdr*, %struct.udphdr** %7
%60 = getelementptr inbounds %struct.udphdr, %struct.udphdr* %59, i32 0, i32 3
store i8* %58, i8** %60
%61 = call i8* @htons(i64 8001)
%62 = load %struct.udphdr*, %struct.udphdr** %7
%63 = getelementptr inbounds %struct.udphdr, %struct.udphdr* %62, i32 0, i32 2
store i8* %61, i8** %63
%64 = load i64, i64* %5
%65 = load i64, i64* %10
%66 = sub i64 %64, %65
%67 = call i8* @htons(i64 %66)
%68 = load %struct.udphdr*, %struct.udphdr** %7
%69 = getelementptr inbounds %struct.udphdr, %struct.udphdr* %68, i32 0, i32 1
store i8* %67, i8** %69
%70 = load %struct.udphdr*, %struct.udphdr** %7
%71 = getelementptr inbounds %struct.udphdr, %struct.udphdr* %70, i32 0, i32 0
store i64 0, i64* %71
%72 = load i64, i64* %10
%73 = add i64 %72, 32
store i64 %73, i64* %10
%74 = load i8*, i8** %4
%75 = load i64, i64* %10
%76 = getelementptr i8, i8* %74, i64 %75
%77 = load i8, i8* %6
%78 = load i64, i64* %5
%79 = load i64, i64* %10
%80 = sub i64 %78, %79
%81 = call i32 @memset(i8* %76, i8 signext %77, i64 %80)
ret void
}

LLVM IR (-O2):
@ETH_P_IP = global i64 0
@INADDR_LOOPBACK = global i64 0
@IPPROTO_UDP = global i32 0
define void @build_packet(i8* %0, i64 %1, i8 signext %2) {
%4 = tail call i32 @memset(i8* %0, i8 signext 0, i64 %1)
%5 = load i64, i64* @ETH_P_IP
%6 = tail call i8* @htons(i64 %5)
%7 = bitcast i8* %0 to i8**
store i8* %6, i8** %7
%8 = getelementptr i8, i8* %0, i64 8
%9 = bitcast i8* %8 to i32*
store i32 8, i32* %9
%10 = getelementptr i8, i8* %0, i64 12
%11 = bitcast i8* %10 to i32*
store i32 5, i32* %11
%12 = getelementptr i8, i8* %0, i64 16
%13 = bitcast i8* %12 to i32*
store i32 4, i32* %13
%14 = load i64, i64* @INADDR_LOOPBACK
%15 = tail call i8* @htonl(i64 %14)
%16 = getelementptr i8, i8* %0, i64 56
%17 = bitcast i8* %16 to i8**
store i8* %15, i8** %17
%18 = load i64, i64* @INADDR_LOOPBACK
%19 = add nsw i64 %18, 1
%20 = tail call i8* @htonl(i64 %19)
%21 = getelementptr i8, i8* %0, i64 48
%22 = bitcast i8* %21 to i8**
store i8* %20, i8** %22
%23 = load i32, i32* @IPPROTO_UDP
%24 = getelementptr i8, i8* %0, i64 40
%25 = bitcast i8* %24 to i32*
store i32 %23, i32* %25
%26 = add i64 %1, -8
%27 = tail call i8* @htons(i64 %26)
%28 = getelementptr i8, i8* %0, i64 32
%29 = bitcast i8* %28 to i8**
store i8* %27, i8** %29
%30 = getelementptr i8, i8* %0, i64 24
%31 = bitcast i8* %30 to i64*
store i64 0, i64* %31
%32 = getelementptr i8, i8* %0, i64 64
%33 = tail call i8* @htons(i64 8000)
%34 = getelementptr i8, i8* %0, i64 88
%35 = bitcast i8* %34 to i8**
store i8* %33, i8** %35
%36 = tail call i8* @htons(i64 8001)
%37 = getelementptr i8, i8* %0, i64 80
%38 = bitcast i8* %37 to i8**
store i8* %36, i8** %38
%39 = add i64 %1, -64
%40 = tail call i8* @htons(i64 %39)
%41 = getelementptr i8, i8* %0, i64 72
%42 = bitcast i8* %41 to i8**
store i8* %40, i8** %42
%43 = bitcast i8* %32 to i64*
store i64 0, i64* %43
%44 = getelementptr i8, i8* %0, i64 96
%45 = add i64 %1, -96
%46 = tail call i32 @memset(i8* %44, i8 signext %2, i64 %45)
ret void
}