#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 xdp_md {
  scalar_t__ data_end;
  scalar_t__ data;
};
struct iphdr {
  int dummy;
};
struct ethhdr {
  int h_proto;
};
typedef int /*<<< orphan*/ dst_ports;
typedef scalar_t__ __u8;
typedef scalar_t__ __u64;
typedef int __u16;

/* Variables and functions */
int ETH_P_IP;
int XDP_PASS;
int get_dst_port(void *, void *, scalar_t__, scalar_t__);
scalar_t__ get_ip_protocol(void *, void *, scalar_t__);
int /*<<< orphan*/ set_tos(void *, void *, scalar_t__, int);
__attribute__((section("prog"), used)) int xdp_set_tos(struct xdp_md *ctx) {
  int n;
  __u8 protocol;
  __u16 h_proto;
  __u16 dst_port;
  __u16 dst_ports[] = {5004, 5005, 60000, 60001};
  __u64 offset = 0;
  void *pkt = (void *)(long)ctx->data;
  void *end = (void *)(long)ctx->data_end;
  struct ethhdr *eth = pkt;
  if (pkt + sizeof(*eth) > end)
    return XDP_PASS;
  h_proto = eth->h_proto;
  if (h_proto != (((ETH_P_IP & 0x00FF) << 8) | ((ETH_P_IP & 0xFF00) >> 8)))
    return XDP_PASS;
  offset += sizeof(*eth);
  protocol = get_ip_protocol(pkt, end, offset);
  if (protocol == 0)
    return XDP_PASS;
  offset += sizeof(struct iphdr);
  dst_port = get_dst_port(pkt, end, offset, protocol);
  if (dst_port == 0)
    return XDP_PASS;
  for (n = 0; n < (sizeof(dst_ports) / sizeof(dst_ports[0])); n++) {
    if (dst_port == dst_ports[n]) {
      set_tos(pkt, end, offset - sizeof(struct iphdr), 0xFF);
      return XDP_PASS;
    }
  }
  return XDP_PASS;
}


x86 assembly (clang, -O0):
<xdp_set_tos>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    0xebd(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xeba(%rip),%rax
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x58(%rbp)
mov    -0x50(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x50(%rbp),%rax
add    $0x4,%rax
cmp    -0x58(%rbp),%rax
jbe    119c <xdp_set_tos+0x6c>
mov    0x2e56(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12d3 <xdp_set_tos+0x1a3>
mov    -0x60(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    0x2e29(%rip),%rcx
mov    (%rcx),%ecx
and    $0xff,%ecx
shl    $0x8,%ecx
mov    0x2e17(%rip),%rdx
mov    (%rdx),%edx
and    $0xff00,%edx
sar    $0x8,%edx
or     %edx,%ecx
cmp    %ecx,%eax
je     11e7 <xdp_set_tos+0xb7>
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12d3 <xdp_set_tos+0x1a3>
mov    -0x48(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x48(%rbp)
mov    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rsi
mov    -0x48(%rbp),%rdx
call   1050 <get_ip_protocol@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1224 <xdp_set_tos+0xf4>
mov    0x2dce(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12d3 <xdp_set_tos+0x1a3>
mov    -0x48(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x48(%rbp)
mov    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rsi
mov    -0x48(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1040 <get_dst_port@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    1263 <xdp_set_tos+0x133>
mov    0x2d8f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12d3 <xdp_set_tos+0x1a3>
movl   $0x0,-0x14(%rbp)
movslq -0x14(%rbp),%rax
cmp    $0x4,%rax
jae    12c7 <xdp_set_tos+0x197>
mov    -0x28(%rbp),%eax
movslq -0x14(%rbp),%rcx
cmp    -0x40(%rbp,%rcx,4),%eax
jne    12b4 <xdp_set_tos+0x184>
mov    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rsi
mov    -0x48(%rbp),%rdx
sub    $0x4,%rdx
mov    $0xff,%ecx
call   1030 <set_tos@plt>
mov    0x2d3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12d3 <xdp_set_tos+0x1a3>
jmp    12b9 <xdp_set_tos+0x189>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    126a <xdp_set_tos+0x13a>
mov    0x2d1a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xdp_set_tos>:
push   %r14
push   %rbx
push   %rax
mov    (%rdi),%r14
mov    0x8(%rdi),%rbx
lea    0x4(%rbx),%rax
cmp    %r14,%rax
ja     11ab <xdp_set_tos+0x7b>
mov    0x2e8d(%rip),%rax
movzwl (%rax),%eax
rol    $0x8,%ax
movzwl %ax,%eax
cmp    %eax,(%rbx)
jne    11ab <xdp_set_tos+0x7b>
mov    $0x4,%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <get_ip_protocol@plt>
test   %rax,%rax
je     11ab <xdp_set_tos+0x7b>
mov    $0x8,%edx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %rax,%rcx
call   1040 <get_dst_port@plt>
lea    -0xea60(%rax),%ecx
cmp    $0x2,%ecx
jb     1196 <xdp_set_tos+0x66>
add    $0xffffec74,%eax
cmp    $0x1,%eax
ja     11ab <xdp_set_tos+0x7b>
mov    $0x4,%edx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0xff,%ecx
call   1030 <set_tos@plt>
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.xdp_md = type { i64, i64 }
%struct.ethhdr = type { i32 }
@__const.xdp_set_tos.dst_ports = [4 x i32] [i32 5004, i32 5005, i32 60000, i32 60001]
@XDP_PASS = global i32 0
@ETH_P_IP = global i32 0
@llvm.compiler.used = appending global [1 x i8*] [i8* bitcast (i32 (%struct.xdp_md*)* @xdp_set_tos to i8*)], section "llvm.metadata"
define i32 @xdp_set_tos(%struct.xdp_md* %0) section "prog" {
%2 = alloca i32
%3 = alloca %struct.xdp_md*
%4 = alloca i32
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca [4 x i32]
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca %struct.ethhdr*
store %struct.xdp_md* %0, %struct.xdp_md** %3
%13 = bitcast [4 x i32]* %8 to i8*
store i64 0, i64* %9
%14 = load %struct.xdp_md*, %struct.xdp_md** %3
%15 = getelementptr inbounds %struct.xdp_md, %struct.xdp_md* %14, i32 0, i32 1
%16 = load i64, i64* %15
%17 = inttoptr i64 %16 to i8*
store i8* %17, i8** %10
%18 = load %struct.xdp_md*, %struct.xdp_md** %3
%19 = getelementptr inbounds %struct.xdp_md, %struct.xdp_md* %18, i32 0, i32 0
%20 = load i64, i64* %19
%21 = inttoptr i64 %20 to i8*
store i8* %21, i8** %11
%22 = load i8*, i8** %10
%23 = bitcast i8* %22 to %struct.ethhdr*
store %struct.ethhdr* %23, %struct.ethhdr** %12
%24 = load i8*, i8** %10
%25 = getelementptr i8, i8* %24, i64 4
%26 = load i8*, i8** %11
%27 = icmp ugt i8* %25, %26
br i1 %27, label %28, label %30
28: ; preds = %1
%29 = load i32, i32* @XDP_PASS
store i32 %29, i32* %2
br label %93
30: ; preds = %1
%31 = load %struct.ethhdr*, %struct.ethhdr** %12
%32 = getelementptr inbounds %struct.ethhdr, %struct.ethhdr* %31, i32 0, i32 0
%33 = load i32, i32* %32
store i32 %33, i32* %6
%34 = load i32, i32* %6
%35 = load i32, i32* @ETH_P_IP
%36 = and i32 %35, 255
%37 = shl i32 %36, 8
%38 = load i32, i32* @ETH_P_IP
%39 = and i32 %38, 65280
%40 = ashr i32 %39, 8
%41 = or i32 %37, %40
%42 = icmp ne i32 %34, %41
br i1 %42, label %43, label %45
43: ; preds = %30
%44 = load i32, i32* @XDP_PASS
store i32 %44, i32* %2
br label %93
45: ; preds = %30
%46 = load i64, i64* %9
%47 = add i64 %46, 4
store i64 %47, i64* %9
%48 = load i8*, i8** %10
%49 = load i8*, i8** %11
%50 = load i64, i64* %9
%51 = call i64 @get_ip_protocol(i8* %48, i8* %49, i64 %50)
store i64 %51, i64* %5
%52 = load i64, i64* %5
%53 = icmp eq i64 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %45
%55 = load i32, i32* @XDP_PASS
store i32 %55, i32* %2
br label %93
56: ; preds = %45
%57 = load i64, i64* %9
%58 = add i64 %57, 4
store i64 %58, i64* %9
%59 = load i8*, i8** %10
%60 = load i8*, i8** %11
%61 = load i64, i64* %9
%62 = load i64, i64* %5
%63 = call i32 @get_dst_port(i8* %59, i8* %60, i64 %61, i64 %62)
store i32 %63, i32* %7
%64 = load i32, i32* %7
%65 = icmp eq i32 %64, 0
br i1 %65, label %66, label %68
66: ; preds = %56
%67 = load i32, i32* @XDP_PASS
store i32 %67, i32* %2
br label %93
68: ; preds = %56
store i32 0, i32* %4
br label %69
69: ; preds = %88, %68
%70 = load i32, i32* %4
%71 = sext i32 %70 to i64
%72 = icmp ult i64 %71, 4
br i1 %72, label %73, label %91
73: ; preds = %69
%74 = load i32, i32* %7
%75 = load i32, i32* %4
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds [4 x i32], [4 x i32]* %8, i64 0, i64 %76
%78 = load i32, i32* %77
%79 = icmp eq i32 %74, %78
br i1 %79, label %80, label %87
80: ; preds = %73
%81 = load i8*, i8** %10
%82 = load i8*, i8** %11
%83 = load i64, i64* %9
%84 = sub i64 %83, 4
%85 = call i32 @set_tos(i8* %81, i8* %82, i64 %84, i32 255)
%86 = load i32, i32* @XDP_PASS
store i32 %86, i32* %2
br label %93
87: ; preds = %73
br label %88
88: ; preds = %87
%89 = load i32, i32* %4
%90 = add nsw i32 %89, 1
store i32 %90, i32* %4
br label %69
91: ; preds = %69
%92 = load i32, i32* @XDP_PASS
store i32 %92, i32* %2
br label %93
93: ; preds = %91, %80, %66, %54, %43, %28
%94 = load i32, i32* %2
ret i32 %94
}

LLVM IR (-O2):
%struct.xdp_md = type { i64, i64 }
%struct.ethhdr = type { i32 }
@XDP_PASS = global i32 0
@ETH_P_IP = global i32 0
@llvm.compiler.used = appending global [1 x i8*] [i8* bitcast (i32 (%struct.xdp_md*)* @xdp_set_tos to i8*)], section "llvm.metadata"
define i32 @xdp_set_tos(%struct.xdp_md* nocapture %0) section "prog" {
%2 = getelementptr inbounds %struct.xdp_md, %struct.xdp_md* %0, i64 0, i32 1
%3 = load i64, i64* %2
%4 = inttoptr i64 %3 to i8*
%5 = getelementptr inbounds %struct.xdp_md, %struct.xdp_md* %0, i64 0, i32 0
%6 = load i64, i64* %5
%7 = inttoptr i64 %6 to i8*
%8 = getelementptr i8, i8* %4, i64 4
%9 = icmp ugt i8* %8, %7
br i1 %9, label %26, label %10
10: ; preds = %1
%11 = inttoptr i64 %3 to %struct.ethhdr*
%12 = getelementptr inbounds %struct.ethhdr, %struct.ethhdr* %11, i64 0, i32 0
%13 = load i32, i32* %12
%14 = load i32, i32* @ETH_P_IP
%15 = trunc i32 %14 to i16
%16 = tail call i16 @llvm.bswap.i16(i16 %15)
%17 = zext i16 %16 to i32
%18 = icmp eq i32 %13, %17
br i1 %18, label %19, label %26
19: ; preds = %10
%20 = tail call i64 @get_ip_protocol(i8* %4, i8* %7, i64 4)
%21 = icmp eq i64 %20, 0
br i1 %21, label %26, label %22
22: ; preds = %19
%23 = tail call i32 @get_dst_port(i8* %4, i8* %7, i64 8, i64 %20)
switch i32 %23, label %26 [
i32 60001, label %24
i32 5004, label %24
i32 5005, label %24
i32 60000, label %24
]
24: ; preds = %22, %22, %22, %22
%25 = tail call i32 @set_tos(i8* %4, i8* %7, i64 4, i32 255)
br label %26
26: ; preds = %22, %19, %10, %1, %24
%27 = load i32, i32* @XDP_PASS
ret i32 %27
}