#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*/ buf;

/* Variables and functions */
int /*<<< orphan*/ ETH_P_ALL;
int /*<<< orphan*/ PF_PACKET;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ recvfrom(int, unsigned char *, int, int /*<<< orphan*/,
                            int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(unsigned char *, char *, unsigned char,
                           unsigned char, unsigned char, unsigned char,
                           unsigned char, unsigned char);
int main(int argc, char *argv[]) {
  int i = 0;
  unsigned char buf[1024] = "";
  int sock_raw_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  while (1) {
    unsigned char src_mac[18] = "";
    unsigned char dst_mac[18] = "";
    recvfrom(sock_raw_fd, buf, sizeof(buf), 0, NULL, NULL);
    sprintf(dst_mac, "%02x:%02x:%02x:%02x:%02x:%02x", buf[0], buf[1], buf[2],
            buf[3], buf[4], buf[5]);
    sprintf(src_mac, "%02x:%02x:%02x:%02x:%02x:%02x", buf[6], buf[7], buf[8],
            buf[9], buf[10], buf[11]);
    if (buf[12] == 0x08 && buf[13] == 0x00) {
      printf("______________IP数据报_______________\n");
      printf("MAC:%s >> %s\n", src_mac, dst_mac);
    } else if (buf[12] == 0x08 && buf[13] == 0x06) {
      printf("______________ARP数据报_______________\n");
      printf("MAC:%s >> %s\n", src_mac, dst_mac);
    } else if (buf[12] == 0x80 && buf[13] == 0x35) {
      printf("______________RARP数据报_______________\n");
      printf("MAC:%s>>%s\n", src_mac, dst_mac);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x480,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    0x2e56(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x468(%rbp)
mov    0x2e37(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x464(%rbp)
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    -0x468(%rbp),%edi
mov    -0x464(%rbp),%esi
mov    %eax,%edx
call   1080 <socket@plt>
mov    %eax,-0x424(%rbp)
lea    -0x440(%rbp),%rdi
xor    %esi,%esi
mov    $0x12,%edx
call   1050 <memset@plt>
lea    -0x460(%rbp),%rdi
xor    %esi,%esi
mov    $0x12,%edx
call   1050 <memset@plt>
mov    -0x424(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
xor    %eax,%eax
mov    %eax,%r9d
mov    %r9,%r8
call   1060 <recvfrom@plt>
lea    -0x460(%rbp),%rdi
mov    -0x420(%rbp),%dl
mov    -0x41f(%rbp),%cl
mov    -0x41e(%rbp),%r8b
mov    -0x41d(%rbp),%r9b
mov    -0x41c(%rbp),%al
lea    0xdb3(%rip),%rsi
movzbl %dl,%edx
movzbl %cl,%ecx
movzbl %r8b,%r8d
movzbl %r9b,%r9d
movzbl %al,%eax
mov    %eax,(%rsp)
movzbl -0x41b(%rbp),%eax
mov    %eax,0x8(%rsp)
call   1070 <sprintf@plt>
lea    -0x440(%rbp),%rdi
mov    -0x41a(%rbp),%dl
mov    -0x419(%rbp),%cl
mov    -0x418(%rbp),%r8b
mov    -0x417(%rbp),%r9b
mov    -0x416(%rbp),%al
lea    0xd61(%rip),%rsi
movzbl %dl,%edx
movzbl %cl,%ecx
movzbl %r8b,%r8d
movzbl %r9b,%r9d
movzbl %al,%eax
mov    %eax,(%rsp)
movzbl -0x415(%rbp),%eax
mov    %eax,0x8(%rsp)
call   1070 <sprintf@plt>
movzbl -0x414(%rbp),%eax
cmp    $0x8,%eax
jne    1312 <main+0x1b2>
movzbl -0x413(%rbp),%eax
cmp    $0x0,%eax
jne    1312 <main+0x1b2>
lea    0xd34(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x440(%rbp),%rsi
lea    -0x460(%rbp),%rdx
lea    0xd42(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    13b7 <main+0x257>
movzbl -0x414(%rbp),%eax
cmp    $0x8,%eax
jne    1361 <main+0x201>
movzbl -0x413(%rbp),%eax
cmp    $0x6,%eax
jne    1361 <main+0x201>
lea    0xd1d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x440(%rbp),%rsi
lea    -0x460(%rbp),%rdx
lea    0xcf3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    13b2 <main+0x252>
movzbl -0x414(%rbp),%eax
cmp    $0x80,%eax
jne    13ad <main+0x24d>
movzbl -0x413(%rbp),%eax
cmp    $0x35,%eax
jne    13ad <main+0x24d>
lea    0xcf7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x440(%rbp),%rsi
lea    -0x460(%rbp),%rdx
lea    0xd07(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    13b2 <main+0x252>
jmp    13b7 <main+0x257>
jmp    11d8 <main+0x78>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x448,%rsp
lea    0x40(%rsp),%rdi
mov    $0x400,%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    0x2e57(%rip),%rax
mov    (%rax),%ebx
mov    0x2e3e(%rip),%rax
mov    (%rax),%ebp
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    %ebx,%edi
mov    %ebp,%esi
mov    %eax,%edx
call   1090 <socket@plt>
mov    %eax,%r13d
lea    0xe39(%rip),%rbx
mov    %rsp,%r15
lea    0x20(%rsp),%r12
lea    0xe48(%rip),%r14
jmp    11f5 <main+0x85>
nopl   0x0(%rax,%rax,1)
call   1030 <puts@plt>
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,0x20(%rsp)
movw   $0x0,0x30(%rsp)
movaps %xmm0,(%rsp)
movw   $0x0,0x10(%rsp)
mov    %r13d,%edi
lea    0x40(%rsp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1070 <recvfrom@plt>
movzbl 0x40(%rsp),%edx
movzbl 0x41(%rsp),%ecx
movzbl 0x42(%rsp),%r8d
movzbl 0x43(%rsp),%r9d
movzbl 0x44(%rsp),%eax
movzbl 0x45(%rsp),%ebp
mov    %r15,%rdi
mov    %rbx,%rsi
push   %rbp
push   %rax
call   1080 <sprintf@plt>
add    $0x10,%rsp
movzbl 0x46(%rsp),%edx
movzbl 0x47(%rsp),%ecx
movzbl 0x48(%rsp),%r8d
movzbl 0x49(%rsp),%r9d
movzbl 0x4a(%rsp),%eax
movzbl 0x4b(%rsp),%ebp
mov    %r12,%rdi
mov    %rbx,%rsi
push   %rbp
push   %rax
call   1080 <sprintf@plt>
add    $0x10,%rsp
movzbl 0x4c(%rsp),%ecx
movzbl 0x4d(%rsp),%eax
cmp    $0x8,%cl
jne    12ac <main+0x13c>
lea    0xdec(%rip),%rdi
mov    %r14,%rbp
test   %al,%al
je     11e0 <main+0x70>
cmp    $0x8,%cl
jne    12c3 <main+0x153>
lea    0xdab(%rip),%rdi
mov    %r14,%rbp
cmp    $0x6,%al
je     11e0 <main+0x70>
cmp    $0x80,%cl
jne    11f5 <main+0x85>
lea    0xd65(%rip),%rdi
lea    0xd52(%rip),%rbp
cmp    $0x35,%al
je     11e0 <main+0x70>
jmp    11f5 <main+0x85>

LLVM IR (-O0):
@PF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_ALL = global i32 0
@.str = [30 x i8] c"%02x:%02x:%02x:%02x:%02x:%02x\00"
@.str.1 = [42 x i8] c"______________IP\E6\95\B0\E6\8D\AE\E6\8A\A5_______________\0A\00"
@.str.2 = [14 x i8] c"MAC:%s >> %s\0A\00"
@.str.3 = [43 x i8] c"______________ARP\E6\95\B0\E6\8D\AE\E6\8A\A5_______________\0A\00"
@.str.4 = [44 x i8] c"______________RARP\E6\95\B0\E6\8D\AE\E6\8A\A5_______________\0A\00"
@.str.5 = [12 x i8] c"MAC:%s>>%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [1024 x i8]
%8 = alloca i32
%9 = alloca [18 x i8]
%10 = alloca [18 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
%11 = bitcast [1024 x i8]* %7 to i8*
%12 = load i32, i32* @PF_PACKET
%13 = load i32, i32* @SOCK_RAW
%14 = load i32, i32* @ETH_P_ALL
%15 = call i32 @htons(i32 %14)
%16 = call i32 @socket(i32 %12, i32 %13, i32 %15)
store i32 %16, i32* %8
br label %17
17: ; preds = %2, %97
%18 = bitcast [18 x i8]* %9 to i8*
%19 = bitcast [18 x i8]* %10 to i8*
%20 = load i32, i32* %8
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%22 = call i32 @recvfrom(i32 %20, i8* %21, i32 1024, i32 0, i32* null, i32* null)
%23 = getelementptr inbounds [18 x i8], [18 x i8]* %10, i64 0, i64 0
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%25 = load i8, i8* %24
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 1
%27 = load i8, i8* %26
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 2
%29 = load i8, i8* %28
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 3
%31 = load i8, i8* %30
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 4
%33 = load i8, i8* %32
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 5
%35 = load i8, i8* %34
%36 = call i32 @sprintf(i8* %23, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8 zeroext %25, i8 zeroext %27, i8 zeroext %29, i8 zeroext %31, i8 zeroext %33, i8 zeroext %35)
%37 = getelementptr inbounds [18 x i8], [18 x i8]* %9, i64 0, i64 0
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 6
%39 = load i8, i8* %38
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 7
%41 = load i8, i8* %40
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 8
%43 = load i8, i8* %42
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 9
%45 = load i8, i8* %44
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 10
%47 = load i8, i8* %46
%48 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 11
%49 = load i8, i8* %48
%50 = call i32 @sprintf(i8* %37, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8 zeroext %39, i8 zeroext %41, i8 zeroext %43, i8 zeroext %45, i8 zeroext %47, i8 zeroext %49)
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 12
%52 = load i8, i8* %51
%53 = zext i8 %52 to i32
%54 = icmp eq i32 %53, 8
br i1 %54, label %55, label %65
55: ; preds = %17
%56 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 13
%57 = load i8, i8* %56
%58 = zext i8 %57 to i32
%59 = icmp eq i32 %58, 0
br i1 %59, label %60, label %65
60: ; preds = %55
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0))
%62 = getelementptr inbounds [18 x i8], [18 x i8]* %9, i64 0, i64 0
%63 = getelementptr inbounds [18 x i8], [18 x i8]* %10, i64 0, i64 0
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %62, i8* %63)
br label %97
65: ; preds = %55, %17
%66 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 12
%67 = load i8, i8* %66
%68 = zext i8 %67 to i32
%69 = icmp eq i32 %68, 8
br i1 %69, label %70, label %80
70: ; preds = %65
%71 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 13
%72 = load i8, i8* %71
%73 = zext i8 %72 to i32
%74 = icmp eq i32 %73, 6
br i1 %74, label %75, label %80
75: ; preds = %70
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0))
%77 = getelementptr inbounds [18 x i8], [18 x i8]* %9, i64 0, i64 0
%78 = getelementptr inbounds [18 x i8], [18 x i8]* %10, i64 0, i64 0
%79 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %77, i8* %78)
br label %96
80: ; preds = %70, %65
%81 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 12
%82 = load i8, i8* %81
%83 = zext i8 %82 to i32
%84 = icmp eq i32 %83, 128
br i1 %84, label %85, label %95
85: ; preds = %80
%86 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 13
%87 = load i8, i8* %86
%88 = zext i8 %87 to i32
%89 = icmp eq i32 %88, 53
br i1 %89, label %90, label %95
90: ; preds = %85
%91 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0))
%92 = getelementptr inbounds [18 x i8], [18 x i8]* %9, i64 0, i64 0
%93 = getelementptr inbounds [18 x i8], [18 x i8]* %10, i64 0, i64 0
%94 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i8* %92, i8* %93)
br label %95
95: ; preds = %90, %85, %80
br label %96
96: ; preds = %95, %75
br label %97
97: ; preds = %96, %60
br label %17
}

LLVM IR (-O2):
@PF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_ALL = global i32 0
@.str = [30 x i8] c"%02x:%02x:%02x:%02x:%02x:%02x\00"
@.str.2 = [14 x i8] c"MAC:%s >> %s\0A\00"
@.str.5 = [12 x i8] c"MAC:%s>>%s\0A\00"
@str = [43 x i8] c"______________RARP\E6\95\B0\E6\8D\AE\E6\8A\A5_______________\00"
@str.6 = [42 x i8] c"______________ARP\E6\95\B0\E6\8D\AE\E6\8A\A5_______________\00"
@str.7 = [41 x i8] c"______________IP\E6\95\B0\E6\8D\AE\E6\8A\A5_______________\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [1024 x i8]
%4 = alloca [18 x i8]
%5 = alloca [18 x i8]
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%7 = load i32, i32* @PF_PACKET
%8 = load i32, i32* @SOCK_RAW
%9 = load i32, i32* @ETH_P_ALL
%10 = tail call i32 @htons(i32 %9)
%11 = tail call i32 @socket(i32 %7, i32 %8, i32 %10)
%12 = getelementptr inbounds [18 x i8], [18 x i8]* %4, i64 0, i64 0
%13 = getelementptr inbounds [18 x i8], [18 x i8]* %5, i64 0, i64 0
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 1
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 2
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 3
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 4
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 5
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 6
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 7
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 8
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 9
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 10
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 11
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 12
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 13
br label %27
27: ; preds = %60, %2
%28 = call i32 @recvfrom(i32 %11, i8* %6, i32 1024, i32 0, i32* null, i32* null)
%29 = load i8, i8* %6
%30 = load i8, i8* %14
%31 = load i8, i8* %15
%32 = load i8, i8* %16
%33 = load i8, i8* %17
%34 = load i8, i8* %18
%35 = call i32 @sprintf(i8* %13, i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8 zeroext %29, i8 zeroext %30, i8 zeroext %31, i8 zeroext %32, i8 zeroext %33, i8 zeroext %34)
%36 = load i8, i8* %19
%37 = load i8, i8* %20
%38 = load i8, i8* %21
%39 = load i8, i8* %22
%40 = load i8, i8* %23
%41 = load i8, i8* %24
%42 = call i32 @sprintf(i8* %12, i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8 zeroext %36, i8 zeroext %37, i8 zeroext %38, i8 zeroext %39, i8 zeroext %40, i8 zeroext %41)
%43 = load i8, i8* %25
%44 = icmp eq i8 %43, 8
%45 = load i8, i8* %26
%46 = icmp eq i8 %45, 0
%47 = select i1 %44, i1 %46, i1 false
br i1 %47, label %55, label %48
48: ; preds = %27
%49 = icmp eq i8 %45, 6
%50 = select i1 %44, i1 %49, i1 false
br i1 %50, label %55, label %51
51: ; preds = %48
%52 = icmp eq i8 %43, -128
%53 = icmp eq i8 %45, 53
%54 = select i1 %52, i1 %53, i1 false
br i1 %54, label %55, label %60
55: ; preds = %51, %48, %27
%56 = phi i8* [ getelementptr inbounds ([41 x i8], [41 x i8]* @str.7, i64 0, i64 0), %27 ], [ getelementptr inbounds ([42 x i8], [42 x i8]* @str.6, i64 0, i64 0), %48 ], [ getelementptr inbounds ([43 x i8], [43 x i8]* @str, i64 0, i64 0), %51 ]
%57 = phi i8* [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), %27 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), %48 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), %51 ]
%58 = call i32 @puts(i8* dereferenceable(1) %56)
%59 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) %57, i8* %12, i8* %13)
br label %60
60: ; preds = %55, %51
br label %27
}