#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_custom_packet_listening(void *arg) {
  int sd = socket(PF_INET, SOCK_RAW, 145);
  if (sd < 0) {
    printf("socket() error\n");
    return NULL;
  }
  char buffer[65535] = {0};
  char *position_name;
  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("[CUSTOM]");
    processIpHeader(iphdr);
  }
  return NULL;
}


x86 assembly (clang, -O0):
<start_custom_packet_listening>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10040,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
mov    $0x91,%edx
call   1090 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11c1 <start_custom_packet_listening+0x51>
lea    0xe51(%rip),%rdi
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1291 <start_custom_packet_listening+0x121>
lea    -0x10020(%rbp),%rdi
xor    %esi,%esi
mov    $0xffff,%edx
call   1050 <memset@plt>
movl   $0x0,-0x1002c(%rbp)
mov    0x2ddb(%rip),%rax
mov    (%rax),%eax
or     -0x10030(%rbp),%eax
mov    %eax,-0x10030(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
lea    -0x10030(%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,-0x1002c(%rbp)
cmp    $0x0,%eax
jge    1249 <start_custom_packet_listening+0xd9>
lea    0xdd9(%rip),%rdi
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1291 <start_custom_packet_listening+0x121>
movslq -0x1002c(%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,-0x10038(%rbp)
lea    0xda8(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10038(%rbp),%rdi
call   1080 <processIpHeader@plt>
jmp    120b <start_custom_packet_listening+0x9b>
mov    -0x8(%rbp),%rax
add    $0x10040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<start_custom_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    $0x91,%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     124d <start_custom_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_custom_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_custom_packet_listening+0x90>
lea    0xdbe(%rip),%rdi
jmp    1254 <start_custom_packet_listening+0xd4>
lea    0xdc7(%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 = [9 x i8] c"[CUSTOM]\00"
define i8* @start_custom_packet_listening(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [65535 x i8]
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.ifreq
%9 = alloca %struct.iphdr*
store i8* %0, i8** %3
%10 = load i32, i32* @PF_INET
%11 = load i32, i32* @SOCK_RAW
%12 = call i32 @socket(i32 %10, i32 %11, i32 145)
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %1
%16 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %2
br label %44
17: ; preds = %1
%18 = bitcast [65535 x i8]* %5 to i8*
store i32 0, i32* %7
%19 = load i32, i32* @IFF_PROMISC
%20 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 0
%21 = load i32, i32* %20
%22 = or i32 %21, %19
store i32 %22, i32* %20
%23 = load i32, i32* %4
%24 = load i32, i32* @SIOCSIFFLAGS
%25 = call i32 @ioctl(i32 %23, i32 %24, %struct.ifreq* %8)
br label %26
26: ; preds = %17, %33
%27 = load i32, i32* %4
%28 = getelementptr inbounds [65535 x i8], [65535 x i8]* %5, i64 0, i64 0
%29 = call i32 @recv(i32 %27, i8* %28, i32 65535, i32 0)
store i32 %29, i32* %7
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %26
%32 = 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 %44
33: ; preds = %26
%34 = load i32, i32* %7
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds [65535 x i8], [65535 x i8]* %5, i64 0, i64 %35
store i8 0, i8* %36
%37 = load i32, i32* @stdout
%38 = call i32 @fflush(i32 %37)
%39 = getelementptr inbounds [65535 x i8], [65535 x i8]* %5, i64 0, i64 0
%40 = bitcast i8* %39 to %struct.iphdr*
store %struct.iphdr* %40, %struct.iphdr** %9
%41 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%42 = load %struct.iphdr*, %struct.iphdr** %9
%43 = call i32 @processIpHeader(%struct.iphdr* %42)
br label %26
44: ; preds = %31, %15
%45 = load i8*, i8** %2
ret i8* %45
}

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 = [9 x i8] c"[CUSTOM]\00"
@str = [18 x i8] c"recvfrom() failed\00"
@str.3 = [15 x i8] c"socket() error\00"
define noalias i8* @start_custom_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 145)
%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 ([9 x i8], [9 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
}