#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 void *uint32_t;
typedef scalar_t__ uint16_t;
struct udphdr {
  scalar_t__ uh_dport;
};
struct tcphdr {
  unsigned char syn;
  scalar_t__ ack;
  scalar_t__ th_dport;
};
struct TYPE_4__ {
  void *s_addr;
};
struct TYPE_3__ {
  void *s_addr;
};
struct ip {
  TYPE_2__ ip_dst;
  TYPE_1__ ip_src;
  scalar_t__ ip_p;
};

/* Variables and functions */
scalar_t__ IPPROTO_TCP;
scalar_t__ IPPROTO_UDP;
void make_iphdr(void *iphdr, uint32_t saddr, uint32_t daddr, uint16_t prot,
                uint16_t dport, unsigned char flags) {
  struct ip *ip_hdr = (struct ip *)iphdr;
  ip_hdr->ip_p = prot;
  ip_hdr->ip_src.s_addr = saddr;
  ip_hdr->ip_dst.s_addr = daddr;
  if (prot == IPPROTO_TCP) {
    struct tcphdr *th = (struct tcphdr *)(ip_hdr + 1);
    th->th_dport = dport;
    th->syn = flags & 0x02;
    th->ack = 0;
  } else if (prot == IPPROTO_UDP) {
    struct udphdr *uh = (struct udphdr *)(ip_hdr + 1);
    uh->uh_dport = dport;
  }
}


x86 assembly (clang, -O0):
<make_iphdr>:
push   %rbp
mov    %rsp,%rbp
mov    %r9b,%al
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %al,-0x29(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rcx
mov    -0x38(%rbp),%rax
mov    %rcx,0x10(%rax)
mov    -0x10(%rbp),%rcx
mov    -0x38(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x18(%rbp),%rcx
mov    -0x38(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
mov    0x2e94(%rip),%rcx
cmp    (%rcx),%rax
jne    1195 <make_iphdr+0x95>
mov    -0x38(%rbp),%rax
add    $0x18,%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rcx
mov    -0x40(%rbp),%rax
mov    %rcx,0x10(%rax)
movzbl -0x29(%rbp),%eax
and    $0x2,%eax
mov    %al,%cl
mov    -0x40(%rbp),%rax
mov    %cl,(%rax)
mov    -0x40(%rbp),%rax
movq   $0x0,0x8(%rax)
jmp    11c5 <make_iphdr+0xc5>
mov    -0x20(%rbp),%rax
mov    0x2e30(%rip),%rcx
cmp    (%rcx),%rax
jne    11c0 <make_iphdr+0xc0>
mov    -0x38(%rbp),%rax
add    $0x18,%rax
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%rcx
mov    -0x48(%rbp),%rax
mov    %rcx,(%rax)
jmp    11c5 <make_iphdr+0xc5>
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_iphdr>:
mov    %rcx,0x10(%rdi)
mov    %rsi,0x8(%rdi)
mov    %rdx,(%rdi)
mov    0x2ed6(%rip),%rax
cmp    %rcx,(%rax)
jne    112d <make_iphdr+0x2d>
mov    %r8,0x28(%rdi)
and    $0x2,%r9b
mov    %r9b,0x18(%rdi)
mov    $0x20,%eax
xor    %r8d,%r8d
jmp    113e <make_iphdr+0x3e>
mov    0x2e9c(%rip),%rdx
mov    $0x18,%eax
cmp    %rcx,(%rdx)
jne    1142 <make_iphdr+0x42>
mov    %r8,(%rdi,%rax,1)
ret

LLVM IR (-O0):
%struct.ip = type { %struct.TYPE_4__, %struct.TYPE_3__, i64 }
%struct.TYPE_4__ = type { i8* }
%struct.TYPE_3__ = type { i8* }
%struct.tcphdr = type { i8, i64, i64 }
%struct.udphdr = type { i64 }
@IPPROTO_TCP = global i64 0
@IPPROTO_UDP = global i64 0
define void @make_iphdr(i8* %0, i8* %1, i8* %2, i64 %3, i64 %4, i8 zeroext %5) {
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
%12 = alloca i8
%13 = alloca %struct.ip*
%14 = alloca %struct.tcphdr*
%15 = alloca %struct.udphdr*
store i8* %0, i8** %7
store i8* %1, i8** %8
store i8* %2, i8** %9
store i64 %3, i64* %10
store i64 %4, i64* %11
store i8 %5, i8* %12
%16 = load i8*, i8** %7
%17 = bitcast i8* %16 to %struct.ip*
store %struct.ip* %17, %struct.ip** %13
%18 = load i64, i64* %10
%19 = load %struct.ip*, %struct.ip** %13
%20 = getelementptr inbounds %struct.ip, %struct.ip* %19, i32 0, i32 2
store i64 %18, i64* %20
%21 = load i8*, i8** %8
%22 = load %struct.ip*, %struct.ip** %13
%23 = getelementptr inbounds %struct.ip, %struct.ip* %22, i32 0, i32 1
%24 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %23, i32 0, i32 0
store i8* %21, i8** %24
%25 = load i8*, i8** %9
%26 = load %struct.ip*, %struct.ip** %13
%27 = getelementptr inbounds %struct.ip, %struct.ip* %26, i32 0, i32 0
%28 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %27, i32 0, i32 0
store i8* %25, i8** %28
%29 = load i64, i64* %10
%30 = load i64, i64* @IPPROTO_TCP
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %47
32: ; preds = %6
%33 = load %struct.ip*, %struct.ip** %13
%34 = getelementptr inbounds %struct.ip, %struct.ip* %33, i64 1
%35 = bitcast %struct.ip* %34 to %struct.tcphdr*
store %struct.tcphdr* %35, %struct.tcphdr** %14
%36 = load i64, i64* %11
%37 = load %struct.tcphdr*, %struct.tcphdr** %14
%38 = getelementptr inbounds %struct.tcphdr, %struct.tcphdr* %37, i32 0, i32 2
store i64 %36, i64* %38
%39 = load i8, i8* %12
%40 = zext i8 %39 to i32
%41 = and i32 %40, 2
%42 = trunc i32 %41 to i8
%43 = load %struct.tcphdr*, %struct.tcphdr** %14
%44 = getelementptr inbounds %struct.tcphdr, %struct.tcphdr* %43, i32 0, i32 0
store i8 %42, i8* %44
%45 = load %struct.tcphdr*, %struct.tcphdr** %14
%46 = getelementptr inbounds %struct.tcphdr, %struct.tcphdr* %45, i32 0, i32 1
store i64 0, i64* %46
br label %59
47: ; preds = %6
%48 = load i64, i64* %10
%49 = load i64, i64* @IPPROTO_UDP
%50 = icmp eq i64 %48, %49
br i1 %50, label %51, label %58
51: ; preds = %47
%52 = load %struct.ip*, %struct.ip** %13
%53 = getelementptr inbounds %struct.ip, %struct.ip* %52, i64 1
%54 = bitcast %struct.ip* %53 to %struct.udphdr*
store %struct.udphdr* %54, %struct.udphdr** %15
%55 = load i64, i64* %11
%56 = load %struct.udphdr*, %struct.udphdr** %15
%57 = getelementptr inbounds %struct.udphdr, %struct.udphdr* %56, i32 0, i32 0
store i64 %55, i64* %57
br label %58
58: ; preds = %51, %47
br label %59
59: ; preds = %58, %32
ret void
}

LLVM IR (-O2):
@IPPROTO_TCP = global i64 0
@IPPROTO_UDP = global i64 0
define void @make_iphdr(i8* nocapture writeonly %0, i8* %1, i8* %2, i64 %3, i64 %4, i8 zeroext %5) {
%7 = getelementptr inbounds i8, i8* %0, i64 16
%8 = bitcast i8* %7 to i64*
store i64 %3, i64* %8
%9 = getelementptr inbounds i8, i8* %0, i64 8
%10 = bitcast i8* %9 to i8**
store i8* %1, i8** %10
%11 = bitcast i8* %0 to i8**
store i8* %2, i8** %11
%12 = load i64, i64* @IPPROTO_TCP
%13 = icmp eq i64 %12, %3
br i1 %13, label %14, label %19
14: ; preds = %6
%15 = getelementptr inbounds i8, i8* %0, i64 24
%16 = getelementptr inbounds i8, i8* %0, i64 40
%17 = bitcast i8* %16 to i64*
store i64 %4, i64* %17
%18 = and i8 %5, 2
store i8 %18, i8* %15
br label %22
19: ; preds = %6
%20 = load i64, i64* @IPPROTO_UDP
%21 = icmp eq i64 %20, %3
br i1 %21, label %22, label %27
22: ; preds = %19, %14
%23 = phi i64 [ 32, %14 ], [ 24, %19 ]
%24 = phi i64 [ 0, %14 ], [ %4, %19 ]
%25 = getelementptr inbounds i8, i8* %0, i64 %23
%26 = bitcast i8* %25 to i64*
store i64 %24, i64* %26
br label %27
27: ; preds = %22, %19
ret void
}