#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 /*<<< orphan*/ source;
  int /*<<< orphan*/ dest;
};
struct iphdr {
  int ihl;
  scalar_t__ protocol;
  int /*<<< orphan*/ saddr;
};

/* Variables and functions */
scalar_t__ IPPROTO_UDP;
scalar_t__ isSSDPsamsung(unsigned char *);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ replySSDP(int /*<<< orphan*/, int /*<<< orphan*/);
void ProcessUDPPacket(unsigned char *buffer, int size) {
  struct iphdr *iph = (struct iphdr *)buffer;
  unsigned int ipHdrLen = iph->ihl * 4;
  if (iph->protocol == IPPROTO_UDP) {
    struct udphdr *udph = (struct udphdr *)(buffer + ipHdrLen);
    unsigned int udpHdrLen = sizeof(struct udphdr);
    if (ntohs(udph->dest) == 1900 &&
        isSSDPsamsung(buffer + ipHdrLen + udpHdrLen)) {
      printf("Received Samsung RCR multicast request\n");
      replySSDP(iph->saddr, udph->source);
      printf("\n");
    }
  }
}


x86 assembly (clang, -O0):
<ProcessUDPPacket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
shl    $0x2,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2e66(%rip),%rcx
cmp    (%rcx),%rax
jne    11f5 <ProcessUDPPacket+0xb5>
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
movl   $0x8,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%edi
call   1060 <ntohs@plt>
cmp    $0x76c,%eax
jne    11f0 <ProcessUDPPacket+0xb0>
mov    -0x8(%rbp),%rdi
mov    -0x1c(%rbp),%eax
add    %rax,%rdi
mov    -0x2c(%rbp),%eax
add    %rax,%rdi
call   1040 <isSSDPsamsung@plt>
cmp    $0x0,%rax
je     11f0 <ProcessUDPPacket+0xb0>
lea    0xe33(%rip),%rdi
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x28(%rbp),%rax
mov    (%rax),%esi
call   1050 <replySSDP@plt>
lea    0xe3b(%rip),%rdi
call   1030 <printf@plt>
jmp    11f5 <ProcessUDPPacket+0xb5>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ProcessUDPPacket>:
push   %r14
push   %rbx
push   %rax
mov    0x8(%rdi),%rax
mov    0x2e79(%rip),%rcx
cmp    (%rcx),%rax
jne    11b8 <ProcessUDPPacket+0x68>
mov    %rdi,%rbx
mov    (%rdi),%r14d
shl    $0x2,%r14d
mov    0x4(%rdi,%r14,1),%edi
call   1070 <ntohs@plt>
cmp    $0x76c,%eax
jne    11b8 <ProcessUDPPacket+0x68>
add    %rbx,%r14
lea    0x8(%r14),%rdi
call   1050 <isSSDPsamsung@plt>
test   %rax,%rax
je     11b8 <ProcessUDPPacket+0x68>
lea    0xe69(%rip),%rdi
call   1040 <puts@plt>
mov    0x10(%rbx),%edi
mov    (%r14),%esi
call   1060 <replySSDP@plt>
mov    $0xa,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <putchar@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.iphdr = type { i32, i64, i32 }
%struct.udphdr = type { i32, i32 }
@IPPROTO_UDP = global i64 0
@.str = [40 x i8] c"Received Samsung RCR multicast request\0A\00"
@.str.1 = [2 x i8] c"\0A\00"
define void @ProcessUDPPacket(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.iphdr*
%6 = alloca i32
%7 = alloca %struct.udphdr*
%8 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%9 = load i8*, i8** %3
%10 = bitcast i8* %9 to %struct.iphdr*
store %struct.iphdr* %10, %struct.iphdr** %5
%11 = load %struct.iphdr*, %struct.iphdr** %5
%12 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %11, i32 0, i32 0
%13 = load i32, i32* %12
%14 = mul nsw i32 %13, 4
store i32 %14, i32* %6
%15 = load %struct.iphdr*, %struct.iphdr** %5
%16 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %15, i32 0, i32 1
%17 = load i64, i64* %16
%18 = load i64, i64* @IPPROTO_UDP
%19 = icmp eq i64 %17, %18
br i1 %19, label %20, label %52
20: ; preds = %2
%21 = load i8*, i8** %3
%22 = load i32, i32* %6
%23 = zext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %21, i64 %23
%25 = bitcast i8* %24 to %struct.udphdr*
store %struct.udphdr* %25, %struct.udphdr** %7
store i32 8, i32* %8
%26 = load %struct.udphdr*, %struct.udphdr** %7
%27 = getelementptr inbounds %struct.udphdr, %struct.udphdr* %26, i32 0, i32 1
%28 = load i32, i32* %27
%29 = call i32 @ntohs(i32 %28)
%30 = icmp eq i32 %29, 1900
br i1 %30, label %31, label %51
31: ; preds = %20
%32 = load i8*, i8** %3
%33 = load i32, i32* %6
%34 = zext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
%36 = load i32, i32* %8
%37 = zext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %35, i64 %37
%39 = call i64 @isSSDPsamsung(i8* %38)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %51
41: ; preds = %31
%42 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
%43 = load %struct.iphdr*, %struct.iphdr** %5
%44 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %43, i32 0, i32 2
%45 = load i32, i32* %44
%46 = load %struct.udphdr*, %struct.udphdr** %7
%47 = getelementptr inbounds %struct.udphdr, %struct.udphdr* %46, i32 0, i32 0
%48 = load i32, i32* %47
%49 = call i32 @replySSDP(i32 %45, i32 %48)
%50 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %51
51: ; preds = %41, %31, %20
br label %52
52: ; preds = %51, %2
ret void
}

LLVM IR (-O2):
@IPPROTO_UDP = global i64 0
@str = [39 x i8] c"Received Samsung RCR multicast request\00"
define void @ProcessUDPPacket(i8* %0, i32 %1) {
%3 = getelementptr inbounds i8, i8* %0, i64 8
%4 = bitcast i8* %3 to i64*
%5 = load i64, i64* %4
%6 = load i64, i64* @IPPROTO_UDP
%7 = icmp eq i64 %5, %6
br i1 %7, label %8, label %32
8: ; preds = %2
%9 = bitcast i8* %0 to i32*
%10 = load i32, i32* %9
%11 = shl nsw i32 %10, 2
%12 = zext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %0, i64 %12
%14 = getelementptr inbounds i8, i8* %13, i64 4
%15 = bitcast i8* %14 to i32*
%16 = load i32, i32* %15
%17 = tail call i32 @ntohs(i32 %16)
%18 = icmp eq i32 %17, 1900
br i1 %18, label %19, label %32
19: ; preds = %8
%20 = getelementptr inbounds i8, i8* %13, i64 8
%21 = tail call i64 @isSSDPsamsung(i8* %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %32, label %23
23: ; preds = %19
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str, i64 0, i64 0))
%25 = getelementptr inbounds i8, i8* %0, i64 16
%26 = bitcast i8* %25 to i32*
%27 = load i32, i32* %26
%28 = bitcast i8* %13 to i32*
%29 = load i32, i32* %28
%30 = tail call i32 @replySSDP(i32 %27, i32 %29)
%31 = tail call i32 @putchar(i32 10)
br label %32
32: ; preds = %8, %19, %23, %2
ret void
}