#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 scalar_t__ ssize_t;
typedef int /*<<< orphan*/ pktbuf;

/* Variables and functions */
int /*<<< orphan*/ AF_PACKET;
int /*<<< orphan*/ ETH_P_ALL;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ print_packet(unsigned char *, scalar_t__);
scalar_t__ read(int, unsigned char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  unsigned char pktbuf[2048];
  int sockfd, res = 0;
  sockfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  if (-1 == sockfd) {
    perror("socket");
    goto error;
  }
  while (1) {
    ssize_t pktlen = read(sockfd, pktbuf, sizeof(pktbuf));
    if (pktlen < 0) {
      perror("read");
      continue;
    }
    if (pktlen > 0) {
      print_packet(pktbuf, pktlen);
    }
  }
  close(sockfd);
out:
  return res;
error:
  res = 1;
  goto out;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x818(%rbp)
mov    0x2e5d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x828(%rbp)
mov    0x2e66(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x824(%rbp)
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    -0x828(%rbp),%edi
mov    -0x824(%rbp),%esi
mov    %eax,%edx
call   1070 <socket@plt>
mov    %eax,-0x814(%rbp)
mov    $0xffffffff,%eax
cmp    -0x814(%rbp),%eax
jne    11d3 <main+0x83>
lea    0xe37(%rip),%rdi
call   1050 <perror@plt>
jmp    124a <main+0xfa>
jmp    11d8 <main+0x88>
mov    -0x814(%rbp),%edi
lea    -0x810(%rbp),%rsi
mov    $0x800,%edx
call   1040 <read@plt>
mov    %rax,-0x820(%rbp)
cmpq   $0x0,-0x820(%rbp)
jge    1215 <main+0xc5>
lea    0xdfc(%rip),%rdi
call   1050 <perror@plt>
jmp    11d8 <main+0x88>
cmpq   $0x0,-0x820(%rbp)
jle    1236 <main+0xe6>
lea    -0x810(%rbp),%rdi
mov    -0x820(%rbp),%rsi
call   1060 <print_packet@plt>
jmp    11d8 <main+0x88>
mov    -0x818(%rbp),%eax
add    $0x830,%rsp
pop    %rbp
ret
movl   $0x1,-0x818(%rbp)
jmp    123b <main+0xeb>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x808,%rsp
mov    0x2e6c(%rip),%rax
mov    (%rax),%ebx
mov    0x2e7b(%rip),%rax
mov    (%rax),%ebp
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    %ebx,%edi
mov    %ebp,%esi
mov    %eax,%edx
call   1070 <socket@plt>
cmp    $0xffffffff,%eax
je     11cb <main+0x7b>
mov    %eax,%ebx
mov    %rsp,%r14
lea    0xe6e(%rip),%r15
jmp    11a8 <main+0x58>
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1050 <perror@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x800,%edx
call   1040 <read@plt>
test   %rax,%rax
js     11a0 <main+0x50>
je     11a8 <main+0x58>
mov    %r14,%rdi
mov    %rax,%rsi
call   1060 <print_packet@plt>
jmp    11a8 <main+0x58>
lea    0xe2e(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%eax
add    $0x808,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@AF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_ALL = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [5 x i8] c"read\00"
define i32 @main() {
%1 = alloca [2048 x i8]
%2 = getelementptr inbounds [2048 x i8], [2048 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @AF_PACKET
%4 = load i32, i32* @SOCK_RAW
%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 eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
ret i32 1
11: ; preds = %0, %18
%12 = call i64 @read(i32 %7, i8* %2, i32 2048)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %18
16: ; preds = %11
%17 = icmp eq i64 %12, 0
br i1 %17, label %18, label %19
18: ; preds = %16, %19, %14
br label %11
19: ; preds = %16
%20 = call i32 @print_packet(i8* %2, i64 %12)
br label %18
}