#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 ethhdr {
  int /*<<< orphan*/ h_proto;
};

/* Variables and functions */
int /*<<< orphan*/ AF_PACKET;
int /*<<< orphan*/ ETH_P_ALL;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  int fd;
  char buf[2000];
  int r;
  struct ethhdr *eh;
  fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  while (1) {
    r = read(fd, buf, 2000);
    eh = (struct ethhdr *)buf;
    printf("-->%x\n", ntohs(eh->h_proto));
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x800,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e67(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x7f8(%rbp)
mov    0x2e70(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x7f4(%rbp)
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    -0x7f8(%rbp),%edi
mov    -0x7f4(%rbp),%esi
mov    %eax,%edx
call   1070 <socket@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x7e0(%rbp),%rsi
mov    $0x7d0,%edx
call   1050 <read@plt>
mov    %eax,-0x7e4(%rbp)
lea    -0x7e0(%rbp),%rax
mov    %rax,-0x7f0(%rbp)
mov    -0x7f0(%rbp),%rax
mov    (%rax),%edi
call   1060 <ntohs@plt>
mov    %eax,%esi
lea    0xe1d(%rip),%rdi
call   1040 <printf@plt>
jmp    11a4 <main+0x54>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x7d8,%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>
mov    %eax,%ebx
mov    %rsp,%r14
lea    0xe6c(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x7d0,%edx
call   1050 <read@plt>
mov    (%rsp),%edi
call   1060 <ntohs@plt>
mov    %r15,%rdi
mov    %eax,%esi
call   1040 <printf@plt>
jmp    11a0 <main+0x50>

LLVM IR (-O0):
%struct.ethhdr = type { i32 }
@AF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_ALL = global i32 0
@.str = [7 x i8] c"-->%x\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [2000 x i8]
%4 = alloca i32
%5 = alloca %struct.ethhdr*
store i32 0, i32* %1
%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* %2
br label %11
11: ; preds = %0, %11
%12 = load i32, i32* %2
%13 = getelementptr inbounds [2000 x i8], [2000 x i8]* %3, i64 0, i64 0
%14 = call i32 @read(i32 %12, i8* %13, i32 2000)
store i32 %14, i32* %4
%15 = getelementptr inbounds [2000 x i8], [2000 x i8]* %3, i64 0, i64 0
%16 = bitcast i8* %15 to %struct.ethhdr*
store %struct.ethhdr* %16, %struct.ethhdr** %5
%17 = load %struct.ethhdr*, %struct.ethhdr** %5
%18 = getelementptr inbounds %struct.ethhdr, %struct.ethhdr* %17, i32 0, i32 0
%19 = load i32, i32* %18
%20 = call i32 @ntohs(i32 %19)
%21 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %20)
br label %11
}

LLVM IR (-O2):
@AF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_ALL = global i32 0
@.str = [7 x i8] c"-->%x\0A\00"
define i32 @main() {
%1 = alloca [2000 x i8]
%2 = getelementptr inbounds [2000 x i8], [2000 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 = bitcast [2000 x i8]* %1 to i32*
br label %9
9: ; preds = %9, %0
%10 = call i32 @read(i32 %7, i8* %2, i32 2000)
%11 = load i32, i32* %8
%12 = tail call i32 @ntohs(i32 %11)
%13 = tail call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %12)
br label %9
}