#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 ip {
  int ip_hl;
  int /*<<< orphan*/ ip_dst;
  int /*<<< orphan*/ ip_src;
};
struct in_addr {
  int dummy;
};
struct ether_header {
  int *ether_dhost;
  int *ether_shost;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fopen(char *, char *);
char *inet_ntoa(struct in_addr);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int recvfrom(int, char *, int, int /*<<< orphan*/, int /*<<< orphan*/ *,
             int /*<<< orphan*/ *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char **argv) {
  int sock, n;
  char buffer[2048];
  struct ether_header *eth;
  struct ip *iph;
  FILE *file;
  if (0 > (sock = socket(AF_INET, SOCK_RAW, 0))) {
    perror("socket");
    exit(1);
  }
  file = fopen("log.txt", "w");
  while (1) {
    printf("=====================================\n");
    n = recvfrom(sock, buffer, 2048, 0, NULL, NULL);
    printf("%d bytes read\n", n);
    eth = (struct ether_header *)buffer;
    printf("Dest MAC addr:%02x:%02x:%02x:%02x:%02x:%02x\n", eth->ether_dhost[0],
           eth->ether_dhost[1], eth->ether_dhost[2], eth->ether_dhost[3],
           eth->ether_dhost[4], eth->ether_dhost[5]);
    printf("Source MAC addr:%02x:%02x:%02x:%02x:%02x:%02x\n",
           eth->ether_shost[0], eth->ether_shost[1], eth->ether_shost[2],
           eth->ether_shost[3], eth->ether_shost[4], eth->ether_shost[5]);
    iph = (struct ip *)(buffer + sizeof(struct ether_header));
    if (iph->ip_hl == 4) {
      printf("Source host:%s\n", inet_ntoa(*(struct in_addr *)&iph->ip_src));
      printf("Dest host:%s\n", inet_ntoa(*(struct in_addr *)&iph->ip_dst));
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ecx
mov    %ecx,-0x14(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
jle    11c7 <main+0x57>
lea    0xe48(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xe39(%rip),%rdi
lea    0xe3a(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x838(%rbp)
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x820(%rbp),%rsi
mov    $0x800,%edx
xor    %ecx,%ecx
xor    %eax,%eax
mov    %eax,%r9d
mov    %r9,%r8
call   1050 <recvfrom@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x820(%rbp),%rax
mov    %rax,-0x828(%rbp)
mov    -0x828(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%esi
mov    -0x828(%rbp),%rax
mov    (%rax),%rax
mov    0x4(%rax),%edx
mov    -0x828(%rbp),%rax
mov    (%rax),%rax
mov    0x8(%rax),%ecx
mov    -0x828(%rbp),%rax
mov    (%rax),%rax
mov    0xc(%rax),%r8d
mov    -0x828(%rbp),%rax
mov    (%rax),%rax
mov    0x10(%rax),%r9d
mov    -0x828(%rbp),%rax
mov    (%rax),%rax
mov    0x14(%rax),%eax
lea    0xdc2(%rip),%rdi
mov    %eax,(%rsp)
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x828(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%esi
mov    -0x828(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x4(%rax),%edx
mov    -0x828(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x8(%rax),%ecx
mov    -0x828(%rbp),%rax
mov    0x8(%rax),%rax
mov    0xc(%rax),%r8d
mov    -0x828(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x10(%rax),%r9d
mov    -0x828(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x14(%rax),%eax
lea    0xd89(%rip),%rdi
mov    %eax,(%rsp)
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x820(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x830(%rbp)
mov    -0x830(%rbp),%rax
cmpl   $0x4,(%rax)
jne    1357 <main+0x1e7>
mov    -0x830(%rbp),%rax
mov    0x8(%rax),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,%rsi
lea    0xd73(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x830(%rbp),%rax
mov    0x4(%rax),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,%rsi
lea    0xd63(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11e1 <main+0x71>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x818,%rsp
mov    0x2e50(%rip),%rax
mov    (%rax),%edi
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     12a5 <main+0x125>
mov    %eax,%ebx
lea    0xe4c(%rip),%rdi
lea    0xe4d(%rip),%rsi
call   1070 <fopen@plt>
lea    0xecc(%rip),%r14
lea    0x10(%rsp),%r15
lea    0xe37(%rip),%r12
lea    0xe3f(%rip),%r13
lea    0xe65(%rip),%rbp
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x800,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1060 <recvfrom@plt>
mov    %r12,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@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    %r13,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x18(%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    %rbp,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
cmpl   $0x4,0x20(%rsp)
jne    11f0 <main+0x70>
mov    0x28(%rsp),%edi
call   1040 <inet_ntoa@plt>
lea    0xe00(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x24(%rsp),%edi
call   1040 <inet_ntoa@plt>
lea    0xdf6(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11f0 <main+0x70>
lea    0xd54(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.ether_header = type { i32*, i32* }
%struct.ip = type { i32, i32, i32 }
%struct.in_addr = type { i32 }
@AF_INET = global i32 0
@SOCK_RAW = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [8 x i8] c"log.txt\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [39 x i8] c"=====================================\0A\00"
@.str.4 = [15 x i8] c"%d bytes read\0A\00"
@.str.5 = [45 x i8] c"Dest MAC addr:%02x:%02x:%02x:%02x:%02x:%02x\0A\00"
@.str.6 = [47 x i8] c"Source MAC addr:%02x:%02x:%02x:%02x:%02x:%02x\0A\00"
@.str.7 = [16 x i8] c"Source host:%s\0A\00"
@.str.8 = [14 x i8] c"Dest host:%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [2048 x i8]
%9 = alloca %struct.ether_header*
%10 = alloca %struct.ip*
%11 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_RAW
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %6
%15 = icmp sgt i32 0, %14
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %20, i32** %11
br label %21
21: ; preds = %19, %114
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
%23 = load i32, i32* %6
%24 = getelementptr inbounds [2048 x i8], [2048 x i8]* %8, i64 0, i64 0
%25 = call i32 @recvfrom(i32 %23, i8* %24, i32 2048, i32 0, i32* null, i32* null)
store i32 %25, i32* %7
%26 = load i32, i32* %7
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %26)
%28 = getelementptr inbounds [2048 x i8], [2048 x i8]* %8, i64 0, i64 0
%29 = bitcast i8* %28 to %struct.ether_header*
store %struct.ether_header* %29, %struct.ether_header** %9
%30 = load %struct.ether_header*, %struct.ether_header** %9
%31 = getelementptr inbounds %struct.ether_header, %struct.ether_header* %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.ether_header*, %struct.ether_header** %9
%36 = getelementptr inbounds %struct.ether_header, %struct.ether_header* %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.ether_header*, %struct.ether_header** %9
%41 = getelementptr inbounds %struct.ether_header, %struct.ether_header* %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.ether_header*, %struct.ether_header** %9
%46 = getelementptr inbounds %struct.ether_header, %struct.ether_header* %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.ether_header*, %struct.ether_header** %9
%51 = getelementptr inbounds %struct.ether_header, %struct.ether_header* %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.ether_header*, %struct.ether_header** %9
%56 = getelementptr inbounds %struct.ether_header, %struct.ether_header* %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 ([45 x i8], [45 x i8]* @.str.5, i64 0, i64 0), i32 %34, i32 %39, i32 %44, i32 %49, i32 %54, i32 %59)
%61 = load %struct.ether_header*, %struct.ether_header** %9
%62 = getelementptr inbounds %struct.ether_header, %struct.ether_header* %61, i32 0, i32 1
%63 = load i32*, i32** %62
%64 = getelementptr inbounds i32, i32* %63, i64 0
%65 = load i32, i32* %64
%66 = load %struct.ether_header*, %struct.ether_header** %9
%67 = getelementptr inbounds %struct.ether_header, %struct.ether_header* %66, i32 0, i32 1
%68 = load i32*, i32** %67
%69 = getelementptr inbounds i32, i32* %68, i64 1
%70 = load i32, i32* %69
%71 = load %struct.ether_header*, %struct.ether_header** %9
%72 = getelementptr inbounds %struct.ether_header, %struct.ether_header* %71, i32 0, i32 1
%73 = load i32*, i32** %72
%74 = getelementptr inbounds i32, i32* %73, i64 2
%75 = load i32, i32* %74
%76 = load %struct.ether_header*, %struct.ether_header** %9
%77 = getelementptr inbounds %struct.ether_header, %struct.ether_header* %76, i32 0, i32 1
%78 = load i32*, i32** %77
%79 = getelementptr inbounds i32, i32* %78, i64 3
%80 = load i32, i32* %79
%81 = load %struct.ether_header*, %struct.ether_header** %9
%82 = getelementptr inbounds %struct.ether_header, %struct.ether_header* %81, i32 0, i32 1
%83 = load i32*, i32** %82
%84 = getelementptr inbounds i32, i32* %83, i64 4
%85 = load i32, i32* %84
%86 = load %struct.ether_header*, %struct.ether_header** %9
%87 = getelementptr inbounds %struct.ether_header, %struct.ether_header* %86, i32 0, i32 1
%88 = load i32*, i32** %87
%89 = getelementptr inbounds i32, i32* %88, i64 5
%90 = load i32, i32* %89
%91 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.6, i64 0, i64 0), i32 %65, i32 %70, i32 %75, i32 %80, i32 %85, i32 %90)
%92 = getelementptr inbounds [2048 x i8], [2048 x i8]* %8, i64 0, i64 0
%93 = getelementptr inbounds i8, i8* %92, i64 16
%94 = bitcast i8* %93 to %struct.ip*
store %struct.ip* %94, %struct.ip** %10
%95 = load %struct.ip*, %struct.ip** %10
%96 = getelementptr inbounds %struct.ip, %struct.ip* %95, i32 0, i32 0
%97 = load i32, i32* %96
%98 = icmp eq i32 %97, 4
br i1 %98, label %99, label %114
99: ; preds = %21
%100 = load %struct.ip*, %struct.ip** %10
%101 = getelementptr inbounds %struct.ip, %struct.ip* %100, i32 0, i32 2
%102 = bitcast i32* %101 to %struct.in_addr*
%103 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %102, i32 0, i32 0
%104 = load i32, i32* %103
%105 = call i8* @inet_ntoa(i32 %104)
%106 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0), i8* %105)
%107 = load %struct.ip*, %struct.ip** %10
%108 = getelementptr inbounds %struct.ip, %struct.ip* %107, i32 0, i32 1
%109 = bitcast i32* %108 to %struct.in_addr*
%110 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %109, i32 0, i32 0
%111 = load i32, i32* %110
%112 = call i8* @inet_ntoa(i32 %111)
%113 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.8, i64 0, i64 0), i8* %112)
br label %114
114: ; preds = %99, %21
br label %21
}

LLVM IR (-O2):
@AF_INET = global i32 0
@SOCK_RAW = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [8 x i8] c"log.txt\00"
@.str.2 = [2 x i8] c"w\00"
@.str.4 = [15 x i8] c"%d bytes read\0A\00"
@.str.5 = [45 x i8] c"Dest MAC addr:%02x:%02x:%02x:%02x:%02x:%02x\0A\00"
@.str.6 = [47 x i8] c"Source MAC addr:%02x:%02x:%02x:%02x:%02x:%02x\0A\00"
@.str.7 = [16 x i8] c"Source host:%s\0A\00"
@.str.8 = [14 x i8] c"Dest host:%s\0A\00"
@str = [38 x i8] c"=====================================\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [2048 x i8]
%4 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_RAW
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = tail call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%14 = bitcast [2048 x i8]* %3 to i32**
%15 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 8
%16 = bitcast i8* %15 to i32**
%17 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 16
%18 = bitcast i8* %17 to i32*
%19 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 24
%20 = bitcast i8* %19 to i32*
%21 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 20
%22 = bitcast i8* %21 to i32*
br label %23
23: ; preds = %62, %12
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str, i64 0, i64 0))
%25 = call i32 @recvfrom(i32 %7, i8* %4, i32 2048, i32 0, i32* null, i32* null)
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %25)
%27 = load i32*, i32** %14
%28 = load i32, i32* %27
%29 = getelementptr inbounds i32, i32* %27, i64 1
%30 = load i32, i32* %29
%31 = getelementptr inbounds i32, i32* %27, i64 2
%32 = load i32, i32* %31
%33 = getelementptr inbounds i32, i32* %27, i64 3
%34 = load i32, i32* %33
%35 = getelementptr inbounds i32, i32* %27, i64 4
%36 = load i32, i32* %35
%37 = getelementptr inbounds i32, i32* %27, i64 5
%38 = load i32, i32* %37
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.5, i64 0, i64 0), i32 %28, i32 %30, i32 %32, i32 %34, i32 %36, i32 %38)
%40 = load i32*, i32** %16
%41 = load i32, i32* %40
%42 = getelementptr inbounds i32, i32* %40, i64 1
%43 = load i32, i32* %42
%44 = getelementptr inbounds i32, i32* %40, i64 2
%45 = load i32, i32* %44
%46 = getelementptr inbounds i32, i32* %40, i64 3
%47 = load i32, i32* %46
%48 = getelementptr inbounds i32, i32* %40, i64 4
%49 = load i32, i32* %48
%50 = getelementptr inbounds i32, i32* %40, i64 5
%51 = load i32, i32* %50
%52 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.6, i64 0, i64 0), i32 %41, i32 %43, i32 %45, i32 %47, i32 %49, i32 %51)
%53 = load i32, i32* %18
%54 = icmp eq i32 %53, 4
br i1 %54, label %55, label %62
55: ; preds = %23
%56 = load i32, i32* %20
%57 = call i8* @inet_ntoa(i32 %56)
%58 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0), i8* %57)
%59 = load i32, i32* %22
%60 = call i8* @inet_ntoa(i32 %59)
%61 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.8, i64 0, i64 0), i8* %60)
br label %62
62: ; preds = %55, %23
br label %23
}