#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 iphdr {
  int dummy;
};
struct ifreq {
  int /*<<< orphan*/ ifr_flags;
};

/* Variables and functions */
int /*<<< orphan*/ IFF_PROMISC;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SIOCSIFFLAGS;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ processIpHeader(struct iphdr *);
int recv(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int);
int /*<<< orphan*/ stdout;
void *start_udp_packet_listening(void *arg) {
  int sd = socket(PF_INET, SOCK_RAW, 17);
  if (sd < 0) {
    printf("socket() error\n");
    return NULL;
  }
  char buffer[65535] = {0};
  int receivedBytes = 0;
  struct ifreq ifopts;
  ifopts.ifr_flags |= IFF_PROMISC;
  ioctl(sd, SIOCSIFFLAGS, &ifopts);
  while (1) {
    if ((receivedBytes = recv(sd, buffer, 65535, 0)) < 0) {
      printf("recvfrom() failed\n");
      return NULL;
    }
    buffer[receivedBytes] = '\0';
    fflush(stdout);
    struct iphdr *iphdr = (struct iphdr *)buffer;
    printf("[UDP]");
    processIpHeader(iphdr);
  }
  return NULL;
}


x86 assembly (clang, -O0):
<start_udp_packet_listening>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10030,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
mov    $0x11,%edx
call   1090 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11c1 <start_udp_packet_listening+0x51>
lea    0xe51(%rip),%rdi
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1291 <start_udp_packet_listening+0x121>
lea    -0x10020(%rbp),%rdi
xor    %esi,%esi
mov    $0xffff,%edx
call   1050 <memset@plt>
movl   $0x0,-0x10024(%rbp)
mov    0x2ddb(%rip),%rax
mov    (%rax),%eax
or     -0x10028(%rbp),%eax
mov    %eax,-0x10028(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
lea    -0x10028(%rbp),%rdx
call   1060 <ioctl@plt>
mov    -0x14(%rbp),%edi
lea    -0x10020(%rbp),%rsi
mov    $0xffff,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x10024(%rbp)
cmp    $0x0,%eax
jge    1249 <start_udp_packet_listening+0xd9>
lea    0xdd9(%rip),%rdi
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1291 <start_udp_packet_listening+0x121>
movslq -0x10024(%rbp),%rax
movb   $0x0,-0x10020(%rbp,%rax,1)
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
lea    -0x10020(%rbp),%rax
mov    %rax,-0x10030(%rbp)
lea    0xda8(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10030(%rbp),%rdi
call   1080 <processIpHeader@plt>
jmp    120b <start_udp_packet_listening+0x9b>
mov    -0x8(%rbp),%rax
add    $0x10030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<start_udp_packet_listening>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x10018,%rsp
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
mov    $0x11,%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     124d <start_udp_packet_listening+0xcd>
mov    %eax,%ebp
lea    0x10(%rsp),%rbx
mov    $0xffff,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
movl   $0xffffffff,0x8(%rsp)
ff
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1070 <ioctl@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xffff,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
js     1244 <start_udp_packet_listening+0xc4>
mov    0x2dc8(%rip),%r15
lea    0xdf9(%rip),%r14
lea    0x10(%rsp),%rbx
nopl   0x0(%rax)
mov    %eax,%eax
movb   $0x0,0x10(%rsp,%rax,1)
mov    (%r15),%edi
call   1080 <fflush@plt>
mov    %r14,%rdi
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1090 <processIpHeader@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xffff,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jns    1210 <start_udp_packet_listening+0x90>
lea    0xdbb(%rip),%rdi
jmp    1254 <start_udp_packet_listening+0xd4>
lea    0xdc4(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x10018,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i32 }
%struct.iphdr = type { i32 }
@PF_INET = global i32 0
@SOCK_RAW = global i32 0
@.str = [16 x i8] c"socket() error\0A\00"
@IFF_PROMISC = global i32 0
@SIOCSIFFLAGS = global i32 0
@.str.1 = [19 x i8] c"recvfrom() failed\0A\00"
@stdout = global i32 0
@.str.2 = [6 x i8] c"[UDP]\00"
define i8* @start_udp_packet_listening(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [65535 x i8]
%6 = alloca i32
%7 = alloca %struct.ifreq
%8 = alloca %struct.iphdr*
store i8* %0, i8** %3
%9 = load i32, i32* @PF_INET
%10 = load i32, i32* @SOCK_RAW
%11 = call i32 @socket(i32 %9, i32 %10, i32 17)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %2
br label %43
16: ; preds = %1
%17 = bitcast [65535 x i8]* %5 to i8*
store i32 0, i32* %6
%18 = load i32, i32* @IFF_PROMISC
%19 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 0
%20 = load i32, i32* %19
%21 = or i32 %20, %18
store i32 %21, i32* %19
%22 = load i32, i32* %4
%23 = load i32, i32* @SIOCSIFFLAGS
%24 = call i32 @ioctl(i32 %22, i32 %23, %struct.ifreq* %7)
br label %25
25: ; preds = %16, %32
%26 = load i32, i32* %4
%27 = getelementptr inbounds [65535 x i8], [65535 x i8]* %5, i64 0, i64 0
%28 = call i32 @recv(i32 %26, i8* %27, i32 65535, i32 0)
store i32 %28, i32* %6
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %2
br label %43
32: ; preds = %25
%33 = load i32, i32* %6
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [65535 x i8], [65535 x i8]* %5, i64 0, i64 %34
store i8 0, i8* %35
%36 = load i32, i32* @stdout
%37 = call i32 @fflush(i32 %36)
%38 = getelementptr inbounds [65535 x i8], [65535 x i8]* %5, i64 0, i64 0
%39 = bitcast i8* %38 to %struct.iphdr*
store %struct.iphdr* %39, %struct.iphdr** %8
%40 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%41 = load %struct.iphdr*, %struct.iphdr** %8
%42 = call i32 @processIpHeader(%struct.iphdr* %41)
br label %25
43: ; preds = %30, %14
%44 = load i8*, i8** %2
ret i8* %44
}

LLVM IR (-O2):
%struct.ifreq = type { i32 }
%struct.iphdr = type { i32 }
@PF_INET = global i32 0
@SOCK_RAW = global i32 0
@IFF_PROMISC = global i32 0
@SIOCSIFFLAGS = global i32 0
@stdout = global i32 0
@.str.2 = [6 x i8] c"[UDP]\00"
@str = [18 x i8] c"recvfrom() failed\00"
@str.3 = [15 x i8] c"socket() error\00"
define noalias i8* @start_udp_packet_listening(i8* nocapture readnone %0) {
%2 = alloca [65535 x i8]
%3 = alloca %struct.ifreq
%4 = load i32, i32* @PF_INET
%5 = load i32, i32* @SOCK_RAW
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 17)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.3, i64 0, i64 0))
br label %32
10: ; preds = %1
%11 = getelementptr inbounds [65535 x i8], [65535 x i8]* %2, i64 0, i64 0
%12 = bitcast %struct.ifreq* %3 to i8*
%13 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
store i32 -1, i32* %13
%14 = load i32, i32* @SIOCSIFFLAGS
%15 = call i32 @ioctl(i32 %6, i32 %14, %struct.ifreq* %3)
%16 = call i32 @recv(i32 %6, i8* %11, i32 65535, i32 0)
%17 = icmp slt i32 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %10
%19 = bitcast [65535 x i8]* %2 to %struct.iphdr*
br label %22
20: ; preds = %22, %10
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
br label %32
22: ; preds = %18, %22
%23 = phi i32 [ %16, %18 ], [ %30, %22 ]
%24 = zext i32 %23 to i64
%25 = getelementptr inbounds [65535 x i8], [65535 x i8]* %2, i64 0, i64 %24
store i8 0, i8* %25
%26 = load i32, i32* @stdout
%27 = call i32 @fflush(i32 %26)
%28 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%29 = call i32 @processIpHeader(%struct.iphdr* %19)
%30 = call i32 @recv(i32 %6, i8* %11, i32 65535, i32 0)
%31 = icmp slt i32 %30, 0
br i1 %31, label %20, label %22
32: ; preds = %20, %8
ret i8* null
}