#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 sockaddr {
  int dummy;
};
struct ifreq {
  int dummy;
};
struct hostent {
  int dummy;
};
typedef int /*<<< orphan*/ saddr;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ ETH_P_ALL;
int /*<<< orphan*/ PANIC(char *);
int /*<<< orphan*/ SOCK_PACKET;
int /*<<< orphan*/ clear_promiscuous_mode(char *);
int /*<<< orphan*/ display(unsigned char *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int recvfrom(int, unsigned char *, int, int /*<<< orphan*/, struct sockaddr *,
             int *);
int /*<<< orphan*/ set_promiscuous_mode(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  struct hostent *hname;
  struct sockaddr saddr;
  struct ifreq f;
  int sd, bytes, len = sizeof(saddr);
  unsigned char buf[2048];
  int count;
  sd = socket(AF_INET, SOCK_PACKET, htons(ETH_P_ALL));
  if (sd < 0)
    PANIC("socket");
  set_promiscuous_mode("enp2s0");
  for (count = 0;; count++) {
    len = sizeof(saddr);
    bytes = recvfrom(sd, buf, sizeof(buf), 0, (struct sockaddr *)&saddr, &len);
    if (bytes > 0)
      display(buf);
  }
  clear_promiscuous_mode("enp2s0");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x850,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x4,-0x34(%rbp)
mov    0x2e59(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x84c(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x848(%rbp)
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    -0x84c(%rbp),%edi
mov    -0x848(%rbp),%esi
mov    %eax,%edx
call   1080 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11d8 <main+0x78>
lea    0xe2d(%rip),%rdi
call   1060 <PANIC@plt>
lea    0xe28(%rip),%rdi
call   1050 <set_promiscuous_mode@plt>
movl   $0x0,-0x844(%rbp)
movl   $0x4,-0x34(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x840(%rbp),%rsi
mov    $0x800,%edx
xor    %ecx,%ecx
lea    -0x20(%rbp),%r8
lea    -0x34(%rbp),%r9
call   1070 <recvfrom@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jle    122c <main+0xcc>
lea    -0x840(%rbp),%rdi
call   1030 <display@plt>
jmp    1231 <main+0xd1>
mov    -0x844(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x844(%rbp)
jmp    11ee <main+0x8e>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x818,%rsp
mov    0x2e6c(%rip),%rax
mov    (%rax),%ebx
mov    0x2e6b(%rip),%rax
mov    (%rax),%ebp
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    %ebx,%edi
mov    %ebp,%esi
mov    %eax,%edx
call   1080 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11aa <main+0x4a>
lea    0xe5b(%rip),%rdi
call   1060 <PANIC@plt>
lea    0xe56(%rip),%rdi
call   1050 <set_promiscuous_mode@plt>
lea    0x10(%rsp),%rbx
lea    0x8(%rsp),%r14
lea    0x4(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nop
movl   $0x4,0x4(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x800,%edx
xor    %ecx,%ecx
mov    %r14,%r8
mov    %r15,%r9
call   1070 <recvfrom@plt>
test   %eax,%eax
jle    11d0 <main+0x70>
mov    %rbx,%rdi
call   1030 <display@plt>
jmp    11d0 <main+0x70>

LLVM IR (-O0):
%struct.hostent = type { i32 }
%struct.sockaddr = type { i32 }
%struct.ifreq = type { i32 }
@AF_INET = global i32 0
@SOCK_PACKET = global i32 0
@ETH_P_ALL = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [7 x i8] c"enp2s0\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.hostent*
%7 = alloca %struct.sockaddr
%8 = alloca %struct.ifreq
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca [2048 x i8]
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 4, i32* %11
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_PACKET
%16 = load i32, i32* @ETH_P_ALL
%17 = call i32 @htons(i32 %16)
%18 = call i32 @socket(i32 %14, i32 %15, i32 %17)
store i32 %18, i32* %9
%19 = load i32, i32* %9
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %2
%22 = call i32 @PANIC(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %21, %2
%24 = call i32 @set_promiscuous_mode(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %13
br label %25
25: ; preds = %35, %23
store i32 4, i32* %11
%26 = load i32, i32* %9
%27 = getelementptr inbounds [2048 x i8], [2048 x i8]* %12, i64 0, i64 0
%28 = call i32 @recvfrom(i32 %26, i8* %27, i32 2048, i32 0, %struct.sockaddr* %7, i32* %11)
store i32 %28, i32* %10
%29 = load i32, i32* %10
%30 = icmp sgt i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %25
%32 = getelementptr inbounds [2048 x i8], [2048 x i8]* %12, i64 0, i64 0
%33 = call i32 @display(i8* %32)
br label %34
34: ; preds = %31, %25
br label %35
35: ; preds = %34
%36 = load i32, i32* %13
%37 = add nsw i32 %36, 1
store i32 %37, i32* %13
br label %25
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
%struct.hostent = type { i32 }
@AF_INET = global i32 0
@SOCK_PACKET = global i32 0
@ETH_P_ALL = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [7 x i8] c"enp2s0\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr
%4 = alloca i32
%5 = alloca [2048 x i8]
%6 = bitcast %struct.sockaddr* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_PACKET
%11 = load i32, i32* @ETH_P_ALL
%12 = tail call i32 @htons(i32 %11)
%13 = tail call i32 @socket(i32 %9, i32 %10, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = tail call i32 @PANIC(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %2
%18 = tail call i32 @set_promiscuous_mode(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %22, %17
store i32 4, i32* %4
%20 = call i32 @recvfrom(i32 %13, i8* %8, i32 2048, i32 0, %struct.sockaddr* %3, i32* %4)
%21 = icmp sgt i32 %20, 0
br i1 %21, label %23, label %22
22: ; preds = %19, %23
br label %19
23: ; preds = %19
%24 = call i32 @display(i8* %8)
br label %22
}