#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 */
typedef int /*<<< orphan*/ data;

/* Variables and functions */
int /*<<< orphan*/ DumpPacket(char *, int);
int /*<<< orphan*/ ETH_P_ALL;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_PACKET;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int recvfrom(int, char *, int, int /*<<< orphan*/, int /*<<< orphan*/,
             int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  int sd, bytes_read;
  char data[1024];
  sd = socket(PF_INET, SOCK_PACKET, htons(ETH_P_ALL));
  if (sd < 0) {
    perror("Snooper socket");
    exit(0);
  };
  do {
    bytes_read = recvfrom(sd, data, sizeof(data), 0, 0, 0);
    if (bytes_read > 0)
      DumpPacket(data, bytes_read);
  } while (bytes_read > 0);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e57(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x418(%rbp)
mov    0x2e60(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x414(%rbp)
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    -0x418(%rbp),%edi
mov    -0x414(%rbp),%esi
mov    %eax,%edx
call   1080 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11d1 <main+0x71>
lea    0xe3b(%rip),%rdi
call   1060 <perror@plt>
xor    %edi,%edi
call   1070 <exit@plt>
jmp    11d6 <main+0x76>
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
xor    %r9d,%r9d
mov    %r9d,%ecx
mov    %r9d,%r8d
call   1050 <recvfrom@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    120f <main+0xaf>
lea    -0x410(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <DumpPacket@plt>
jmp    1214 <main+0xb4>
cmpl   $0x0,-0xc(%rbp)
jg     11d6 <main+0x76>
xor    %eax,%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x408,%rsp
mov    0x2e60(%rip),%rax
mov    (%rax),%ebx
mov    0x2e6f(%rip),%rax
mov    (%rax),%ebp
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    %ebx,%edi
mov    %ebp,%esi
mov    %eax,%edx
call   1080 <socket@plt>
test   %eax,%eax
js     11f1 <main+0x91>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x400,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1050 <recvfrom@plt>
test   %eax,%eax
jle    11e5 <main+0x85>
mov    %rsp,%rbx
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %eax,%esi
call   1040 <DumpPacket@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1050 <recvfrom@plt>
test   %eax,%eax
jg     11c0 <main+0x60>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe08(%rip),%rdi
call   1060 <perror@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@PF_INET = global i32 0
@SOCK_PACKET = global i32 0
@ETH_P_ALL = global i32 0
@.str = [15 x i8] c"Snooper socket\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1024 x i8]
store i32 0, i32* %1
%5 = load i32, i32* @PF_INET
%6 = load i32, i32* @SOCK_PACKET
%7 = load i32, i32* @ETH_P_ALL
%8 = call i32 @htons(i32 %7)
%9 = call i32 @socket(i32 %5, i32 %6, i32 %8)
store i32 %9, i32* %2
%10 = load i32, i32* %2
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 0)
unreachable
15: ; preds = %0
br label %16
16: ; preds = %27, %15
%17 = load i32, i32* %2
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%19 = call i32 @recvfrom(i32 %17, i8* %18, i32 1024, i32 0, i32 0, i32 0)
store i32 %19, i32* %3
%20 = load i32, i32* %3
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %16
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%24 = load i32, i32* %3
%25 = call i32 @DumpPacket(i8* %23, i32 %24)
br label %26
26: ; preds = %22, %16
br label %27
27: ; preds = %26
%28 = load i32, i32* %3
%29 = icmp sgt i32 %28, 0
br i1 %29, label %16, label %30
30: ; preds = %27
ret i32 0
}

LLVM IR (-O2):
@PF_INET = global i32 0
@SOCK_PACKET = global i32 0
@ETH_P_ALL = global i32 0
@.str = [15 x i8] c"Snooper socket\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @PF_INET
%4 = load i32, i32* @SOCK_PACKET
%5 = load i32, i32* @ETH_P_ALL
%6 = tail call i32 @htons(i32 %5)
%7 = tail call i32 @socket(i32 %3, i32 %4, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %12, label %9
9: ; preds = %0
%10 = call i32 @recvfrom(i32 %7, i8* %2, i32 1024, i32 0, i32 0, i32 0)
%11 = icmp sgt i32 %10, 0
br i1 %11, label %15, label %20
12: ; preds = %0
%13 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%14 = tail call i32 @exit(i32 0)
unreachable
15: ; preds = %9, %15
%16 = phi i32 [ %18, %15 ], [ %10, %9 ]
%17 = call i32 @DumpPacket(i8* %2, i32 %16)
%18 = call i32 @recvfrom(i32 %7, i8* %2, i32 1024, i32 0, i32 0, i32 0)
%19 = icmp sgt i32 %18, 0
br i1 %19, label %15, label %20
20: ; preds = %15, %9
ret i32 0
}