#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 iphdr {
  int /*<<< orphan*/ saddr;
};
struct in_addr {
  int /*<<< orphan*/ s_addr;
};
struct ethhdr {
  int *h_dest;
  int /*<<< orphan*/ h_proto;
};

/* Variables and functions */
int /*<<< orphan*/ AF_PACKET;
int /*<<< orphan*/ ETH_P_ALL;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int, char *,
                           char *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
char *inet_ntoa(struct in_addr);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int main(void) {
  int fd;
  struct ethhdr *eh;
  struct iphdr *iph;
  char buf[1024] = {};
  struct in_addr addr;
  if ((fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) == -1)
    do {
      fprintf(stderr, "[%s][%d]:%s %s\n",
              "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
              "2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/"
              "master/apue/day13/packet.c",
              28, "socket", strerror(errno));
      exit(EXIT_FAILURE);
    } while (0);
  while (1) {
    read(fd, buf, 1023);
    eh = (struct ethhdr *)buf;
    printf("%02X:%02X:%02X:%02X:%02X:%02X\n", eh->h_dest[0], eh->h_dest[1],
           eh->h_dest[2], eh->h_dest[3], eh->h_dest[4], eh->h_dest[5]);
    if (ntohs(eh->h_proto) == 0X0800) {
      iph = (struct iphdr *)(buf + 14);
      addr.s_addr = iph->saddr;
      printf("\t%s\n", inet_ntoa(addr));
    } else if (ntohs(eh->h_proto) == 0X0806) {
      printf("ARP请求!\n");
    } else if (ntohs(eh->h_proto) == 0x0835) {
      printf("ARP应答!\n");
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x430(%rbp)
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x42c(%rbp)
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    -0x430(%rbp),%edi
mov    -0x42c(%rbp),%esi
mov    %eax,%edx
call   10c0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    1268 <main+0xc8>
jmp    1215 <main+0x75>
mov    0x2dac(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x434(%rbp)
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x434(%rbp),%edi
mov    %rax,%r9
lea    0xdbe(%rip),%rsi
lea    0xdc7(%rip),%rdx
mov    $0x1c,%ecx
lea    0xe50(%rip),%r8
call   1080 <fprintf@plt>
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    126d <main+0xcd>
mov    -0x8(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x3ff,%edx
call   1070 <read@plt>
lea    -0x420(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x4(%rax),%edx
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x8(%rax),%ecx
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0xc(%rax),%r8d
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x10(%rax),%r9d
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x14(%rax),%eax
lea    0xddc(%rip),%rdi
mov    %eax,(%rsp)
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%edi
call   1090 <ntohs@plt>
cmp    $0x800,%eax
jne    132d <main+0x18d>
lea    -0x420(%rbp),%rax
add    $0xe,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x428(%rbp)
mov    -0x428(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,%rsi
lea    0xdaa(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1386 <main+0x1e6>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%edi
call   1090 <ntohs@plt>
cmp    $0x806,%eax
jne    1357 <main+0x1b7>
lea    0xd85(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1381 <main+0x1e1>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%edi
call   1090 <ntohs@plt>
cmp    $0x835,%eax
jne    137c <main+0x1dc>
lea    0xd67(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1381 <main+0x1e1>
jmp    1386 <main+0x1e6>
jmp    126d <main+0xcd>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
lea    0x10(%rsp),%rdi
mov    $0x400,%edx
xor    %esi,%esi
call   1070 <memset@plt>
mov    0x2df7(%rip),%rax
mov    (%rax),%ebx
mov    0x2e06(%rip),%rax
mov    (%rax),%ebp
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
call   1050 <htons@plt>
mov    %ebx,%edi
mov    %ebp,%esi
mov    %eax,%edx
call   10d0 <socket@plt>
cmp    $0xffffffff,%eax
je     12cf <main+0x11f>
mov    %eax,%ebx
lea    0x10(%rsp),%r14
lea    0xe98(%rip),%r15
lea    0xeb0(%rip),%r12
lea    0xeb9(%rip),%r13
jmp    1246 <main+0x96>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x1e(%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %r12,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x3ff,%edx
call   1080 <read@plt>
mov    0x10(%rsp),%rax
mov    (%rax),%esi
mov    0x4(%rax),%edx
mov    0x8(%rax),%ecx
mov    0xc(%rax),%r8d
mov    0x10(%rax),%r9d
mov    0x14(%rax),%eax
mov    %eax,(%rsp)
mov    %r15,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x18(%rsp),%ebp
mov    %ebp,%edi
call   10a0 <ntohs@plt>
cmp    $0x800,%eax
je     1230 <main+0x80>
mov    %ebp,%edi
call   10a0 <ntohs@plt>
cmp    $0x806,%eax
jne    12b0 <main+0x100>
mov    %r13,%rdi
call   1030 <puts@plt>
jmp    1246 <main+0x96>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebp,%edi
call   10a0 <ntohs@plt>
cmp    $0x835,%eax
jne    1246 <main+0x96>
lea    0xe0b(%rip),%rdi
call   1030 <puts@plt>
jmp    1246 <main+0x96>
mov    0x2cf2(%rip),%rax
mov    (%rax),%ebx
mov    0x2ce1(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd13(%rip),%rsi
lea    0xd1c(%rip),%rdx
lea    0xdaa(%rip),%r8
mov    %ebx,%edi
mov    $0x1c,%ecx
mov    %rax,%r9
call   1090 <fprintf@plt>
mov    0x2ca7(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.ethhdr = type { i32*, i32 }
%struct.iphdr = type { i32 }
%struct.in_addr = type { i32 }
@AF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_ALL = global i32 0
@stderr = global i32 0
@.str = [16 x i8] c"[%s][%d]:%s %s\0A\00"
@.str.1 = [149 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/master/apue/day13/packet.c\00"
@.str.2 = [7 x i8] c"socket\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.3 = [31 x i8] c"%02X:%02X:%02X:%02X:%02X:%02X\0A\00"
@.str.4 = [5 x i8] c"\09%s\0A\00"
@.str.5 = [12 x i8] c"ARP\E8\AF\B7\E6\B1\82!\0A\00"
@.str.6 = [12 x i8] c"ARP\E5\BA\94\E7\AD\94!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.ethhdr*
%4 = alloca %struct.iphdr*
%5 = alloca [1024 x i8]
%6 = alloca %struct.in_addr
store i32 0, i32* %1
%7 = bitcast [1024 x i8]* %5 to i8*
%8 = load i32, i32* @AF_PACKET
%9 = load i32, i32* @SOCK_RAW
%10 = load i32, i32* @ETH_P_ALL
%11 = call i32 @htons(i32 %10)
%12 = call i32 @socket(i32 %8, i32 %9, i32 %11)
store i32 %12, i32* %2
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %23
14: ; preds = %0
br label %15
15: ; preds = %14
%16 = load i32, i32* @stderr
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([149 x i8], [149 x i8]* @.str.1, i64 0, i64 0), i32 28, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %18)
%20 = load i32, i32* @EXIT_FAILURE
%21 = call i32 @exit(i32 %20)
unreachable
22: ; No predecessors!
br label %23
23: ; preds = %22, %0
br label %24
24: ; preds = %23, %96
%25 = load i32, i32* %2
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%27 = call i32 @read(i32 %25, i8* %26, i32 1023)
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%29 = bitcast i8* %28 to %struct.ethhdr*
store %struct.ethhdr* %29, %struct.ethhdr** %3
%30 = load %struct.ethhdr*, %struct.ethhdr** %3
%31 = getelementptr inbounds %struct.ethhdr, %struct.ethhdr* %30, i32 0, i32 0
%32 = load i32*, i32** %31
%33 = getelementptr inbounds i32, i32* %32, i64 0
%34 = load i32, i32* %33
%35 = load %struct.ethhdr*, %struct.ethhdr** %3
%36 = getelementptr inbounds %struct.ethhdr, %struct.ethhdr* %35, i32 0, i32 0
%37 = load i32*, i32** %36
%38 = getelementptr inbounds i32, i32* %37, i64 1
%39 = load i32, i32* %38
%40 = load %struct.ethhdr*, %struct.ethhdr** %3
%41 = getelementptr inbounds %struct.ethhdr, %struct.ethhdr* %40, i32 0, i32 0
%42 = load i32*, i32** %41
%43 = getelementptr inbounds i32, i32* %42, i64 2
%44 = load i32, i32* %43
%45 = load %struct.ethhdr*, %struct.ethhdr** %3
%46 = getelementptr inbounds %struct.ethhdr, %struct.ethhdr* %45, i32 0, i32 0
%47 = load i32*, i32** %46
%48 = getelementptr inbounds i32, i32* %47, i64 3
%49 = load i32, i32* %48
%50 = load %struct.ethhdr*, %struct.ethhdr** %3
%51 = getelementptr inbounds %struct.ethhdr, %struct.ethhdr* %50, i32 0, i32 0
%52 = load i32*, i32** %51
%53 = getelementptr inbounds i32, i32* %52, i64 4
%54 = load i32, i32* %53
%55 = load %struct.ethhdr*, %struct.ethhdr** %3
%56 = getelementptr inbounds %struct.ethhdr, %struct.ethhdr* %55, i32 0, i32 0
%57 = load i32*, i32** %56
%58 = getelementptr inbounds i32, i32* %57, i64 5
%59 = load i32, i32* %58
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %34, i32 %39, i32 %44, i32 %49, i32 %54, i32 %59)
%61 = load %struct.ethhdr*, %struct.ethhdr** %3
%62 = getelementptr inbounds %struct.ethhdr, %struct.ethhdr* %61, i32 0, i32 1
%63 = load i32, i32* %62
%64 = call i32 @ntohs(i32 %63)
%65 = icmp eq i32 %64, 2048
br i1 %65, label %66, label %78
66: ; preds = %24
%67 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%68 = getelementptr inbounds i8, i8* %67, i64 14
%69 = bitcast i8* %68 to %struct.iphdr*
store %struct.iphdr* %69, %struct.iphdr** %4
%70 = load %struct.iphdr*, %struct.iphdr** %4
%71 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %70, i32 0, i32 0
%72 = load i32, i32* %71
%73 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %6, i32 0, i32 0
store i32 %72, i32* %73
%74 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %6, i32 0, i32 0
%75 = load i32, i32* %74
%76 = call i8* @inet_ntoa(i32 %75)
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* %76)
br label %96
78: ; preds = %24
%79 = load %struct.ethhdr*, %struct.ethhdr** %3
%80 = getelementptr inbounds %struct.ethhdr, %struct.ethhdr* %79, i32 0, i32 1
%81 = load i32, i32* %80
%82 = call i32 @ntohs(i32 %81)
%83 = icmp eq i32 %82, 2054
br i1 %83, label %84, label %86
84: ; preds = %78
%85 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
br label %95
86: ; preds = %78
%87 = load %struct.ethhdr*, %struct.ethhdr** %3
%88 = getelementptr inbounds %struct.ethhdr, %struct.ethhdr* %87, i32 0, i32 1
%89 = load i32, i32* %88
%90 = call i32 @ntohs(i32 %89)
%91 = icmp eq i32 %90, 2101
br i1 %91, label %92, label %94
92: ; preds = %86
%93 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
br label %94
94: ; preds = %92, %86
br label %95
95: ; preds = %94, %84
br label %96
96: ; preds = %95, %66
br label %24
}

LLVM IR (-O2):
@AF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_ALL = global i32 0
@stderr = global i32 0
@.str = [16 x i8] c"[%s][%d]:%s %s\0A\00"
@.str.1 = [149 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/master/apue/day13/packet.c\00"
@.str.2 = [7 x i8] c"socket\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.3 = [31 x i8] c"%02X:%02X:%02X:%02X:%02X:%02X\0A\00"
@.str.4 = [5 x i8] c"\09%s\0A\00"
@str = [11 x i8] c"ARP\E5\BA\94\E7\AD\94!\00"
@str.7 = [11 x i8] c"ARP\E8\AF\B7\E6\B1\82!\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* @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 %15, label %9
9: ; preds = %0
%10 = bitcast [1024 x i8]* %1 to i32**
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 8
%12 = bitcast i8* %11 to i32*
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 14
%14 = bitcast i8* %13 to i32*
br label %22
15: ; preds = %0
%16 = load i32, i32* @stderr
%17 = load i32, i32* @errno
%18 = tail call i8* @strerror(i32 %17)
%19 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([149 x i8], [149 x i8]* @.str.1, i64 0, i64 0), i32 28, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %18)
%20 = load i32, i32* @EXIT_FAILURE
%21 = tail call i32 @exit(i32 %20)
unreachable
22: ; preds = %49, %9
%23 = call i32 @read(i32 %7, i8* %2, i32 1023)
%24 = load i32*, i32** %10
%25 = load i32, i32* %24
%26 = getelementptr inbounds i32, i32* %24, i64 1
%27 = load i32, i32* %26
%28 = getelementptr inbounds i32, i32* %24, i64 2
%29 = load i32, i32* %28
%30 = getelementptr inbounds i32, i32* %24, i64 3
%31 = load i32, i32* %30
%32 = getelementptr inbounds i32, i32* %24, i64 4
%33 = load i32, i32* %32
%34 = getelementptr inbounds i32, i32* %24, i64 5
%35 = load i32, i32* %34
%36 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %25, i32 %27, i32 %29, i32 %31, i32 %33, i32 %35)
%37 = load i32, i32* %12
%38 = tail call i32 @ntohs(i32 %37)
%39 = icmp eq i32 %38, 2048
br i1 %39, label %40, label %44
40: ; preds = %22
%41 = load i32, i32* %14
%42 = tail call i8* @inet_ntoa(i32 %41)
%43 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* %42)
br label %49
44: ; preds = %22
%45 = tail call i32 @ntohs(i32 %37)
%46 = icmp eq i32 %45, 2054
br i1 %46, label %47, label %50
47: ; preds = %44
%48 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.7, i64 0, i64 0))
br label %49
49: ; preds = %47, %53, %50, %40
br label %22
50: ; preds = %44
%51 = tail call i32 @ntohs(i32 %37)
%52 = icmp eq i32 %51, 2101
br i1 %52, label %53, label %49
53: ; preds = %50
%54 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
br label %49
}