#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 sockaddr_ll {
  int /*<<< orphan*/ sll_ifindex;
  int /*<<< orphan*/ sll_protocol;
  int /*<<< orphan*/ sll_family;
};
struct sockaddr {
  int dummy;
};
struct ifreq {
  int /*<<< orphan*/ ifr_ifindex;
  int /*<<< orphan*/ ifr_name;
};
typedef int /*<<< orphan*/ saddr;

/* Variables and functions */
int /*<<< orphan*/ AF_PACKET;
int /*<<< orphan*/ ETH_P_ALL;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ PF_PACKET;
int /*<<< orphan*/ SIOCGIFINDEX;
int /*<<< orphan*/ SOCK_RAW;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ print_hex(char *, int);
int /*<<< orphan*/ printf(char *, int);
int recvfrom(int, char *, int, int /*<<< orphan*/, int /*<<< orphan*/ *,
             int /*<<< orphan*/ *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  int sock, n;
  char buffer[2048];
  char sendbuf[2048];
  struct ifreq ethreq;
  struct sockaddr_ll saddr;
  int packet_num = 1;
  int i;
  for (i = 0; i < 2048; i++) {
    buffer[i] = 0;
    sendbuf[i] = 0;
  }
  if ((sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0) {
    perror("socket");
    exit(1);
  }
  strncpy(ethreq.ifr_name, "nf0", IFNAMSIZ);
  if (ioctl(sock, SIOCGIFINDEX, &ethreq) == -1) {
    perror("ioctl");
    close(sock);
    exit(1);
  }
  saddr.sll_family = AF_PACKET;
  saddr.sll_protocol = htons(ETH_P_ALL);
  saddr.sll_ifindex = ethreq.ifr_ifindex;
  if (bind(sock, (struct sockaddr *)(&(saddr)), sizeof(saddr)) < 0) {
    perror("Bind");
    close(sock);
    exit(1);
  }
  sendbuf[0] = 0xfe;
  sendbuf[1] = 0xca;
  sendbuf[2] = 0xae;
  sendbuf[7] = 0x03;
  sendbuf[32] = 0x0a;
  sendbuf[33] = 0x0b;
  sendbuf[34] = 0x0c;
  sendbuf[35] = 0x0d;
  sendbuf[36] = 0x1a;
  sendbuf[37] = 0x1b;
  sendbuf[38] = 0x1c;
  sendbuf[39] = 0x1d;
  while (1) {
    n = recvfrom(sock, buffer, 2048, 0, NULL, NULL);
    printf("Packet%d -----------------------------------------------------\n",
           packet_num);
    print_hex(buffer, n);
    packet_num++;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1050,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x103c(%rbp)
movl   $0x0,-0x1040(%rbp)
cmpl   $0x800,-0x1040(%rbp)
jge    121f <main+0x6f>
movslq -0x1040(%rbp),%rax
movb   $0x0,-0x820(%rbp,%rax,1)
movslq -0x1040(%rbp),%rax
movb   $0x0,-0x1020(%rbp,%rax,1)
mov    -0x1040(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1040(%rbp)
jmp    11dd <main+0x2d>
mov    0x2dca(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1048(%rbp)
mov    0x2dab(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1044(%rbp)
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    -0x1048(%rbp),%edi
mov    -0x1044(%rbp),%esi
mov    %eax,%edx
call   10d0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1280 <main+0xd0>
lea    0xd8f(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x1024(%rbp),%edi
mov    0x2d33(%rip),%rax
mov    (%rax),%edx
lea    0xd71(%rip),%rsi
call   1030 <strncpy@plt>
mov    -0x14(%rbp),%edi
mov    0x2d0b(%rip),%rax
mov    (%rax),%esi
lea    -0x1028(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    12da <main+0x12a>
lea    0xd48(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2ce7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1030(%rbp)
mov    0x2ce0(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    %eax,-0x1034(%rbp)
mov    -0x1028(%rbp),%eax
mov    %eax,-0x1038(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x1038(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jge    1345 <main+0x195>
lea    0xce3(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
movb   $0xfe,-0x1020(%rbp)
movb   $0xca,-0x101f(%rbp)
movb   $0xae,-0x101e(%rbp)
movb   $0x3,-0x1019(%rbp)
movb   $0xa,-0x1000(%rbp)
movb   $0xb,-0xfff(%rbp)
movb   $0xc,-0xffe(%rbp)
movb   $0xd,-0xffd(%rbp)
movb   $0x1a,-0xffc(%rbp)
movb   $0x1b,-0xffb(%rbp)
movb   $0x1c,-0xffa(%rbp)
movb   $0x1d,-0xff9(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x820(%rbp),%rsi
mov    $0x800,%edx
xor    %ecx,%ecx
xor    %eax,%eax
mov    %eax,%r9d
mov    %r9,%r8
call   1090 <recvfrom@plt>
mov    %eax,-0x18(%rbp)
mov    -0x103c(%rbp),%esi
lea    0xc4f(%rip),%rdi
call   1050 <printf@plt>
lea    -0x820(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1080 <print_hex@plt>
mov    -0x103c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x104c(%rbp)
mov    %eax,-0x103c(%rbp)
jmp    1399 <main+0x1e9>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x820,%rsp
lea    0x20(%rsp),%rdi
mov    $0x800,%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    0x2e09(%rip),%rax
mov    (%rax),%ebx
mov    0x2df0(%rip),%rax
mov    (%rax),%ebp
mov    0x2dd7(%rip),%r15
mov    (%r15),%edi
call   1040 <htons@plt>
mov    %ebx,%edi
mov    %ebp,%esi
mov    %eax,%edx
call   10e0 <socket@plt>
test   %eax,%eax
js     12d3 <main+0x113>
mov    %eax,%r14d
mov    0x2da2(%rip),%rax
mov    (%rax),%edx
lea    0xde0(%rip),%rsi
call   1030 <strncpy@plt>
mov    0x2d7d(%rip),%rax
mov    (%rax),%esi
lea    0x18(%rsp),%rdx
mov    %r14d,%edi
call   1070 <ioctl@plt>
cmp    $0xffffffff,%eax
je     12e9 <main+0x129>
mov    0x2d76(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    (%r15),%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x18(%rsp),%eax
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %r14d,%edi
mov    $0xc,%edx
call   10b0 <bind@plt>
test   %rax,%rax
js     12f2 <main+0x132>
mov    $0x1,%ebp
lea    0x20(%rsp),%r12
lea    0xd82(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r14d,%edi
mov    %r12,%rsi
mov    $0x800,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   10a0 <recvfrom@plt>
mov    %eax,%ebx
mov    %r15,%rdi
mov    %ebp,%esi
call   1050 <printf@plt>
mov    %r12,%rdi
mov    %ebx,%esi
call   1090 <print_hex@plt>
add    $0x1,%ebp
jmp    12a0 <main+0xe0>
lea    0xd26(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xd1b(%rip),%rdi
jmp    12f9 <main+0x139>
lea    0xd18(%rip),%rdi
call   10c0 <perror@plt>
mov    %r14d,%edi
call   1080 <close@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
%struct.sockaddr_ll = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_ALL = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [4 x i8] c"nf0\00"
@IFNAMSIZ = global i32 0
@SIOCGIFINDEX = global i32 0
@.str.2 = [6 x i8] c"ioctl\00"
@AF_PACKET = global i32 0
@.str.3 = [5 x i8] c"Bind\00"
@.str.4 = [64 x i8] c"Packet%d -----------------------------------------------------\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 [2048 x i8]
%10 = alloca %struct.ifreq
%11 = alloca %struct.sockaddr_ll
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %12
store i32 0, i32* %13
br label %14
14: ; preds = %24, %2
%15 = load i32, i32* %13
%16 = icmp slt i32 %15, 2048
br i1 %16, label %17, label %27
17: ; preds = %14
%18 = load i32, i32* %13
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [2048 x i8], [2048 x i8]* %8, i64 0, i64 %19
store i8 0, i8* %20
%21 = load i32, i32* %13
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [2048 x i8], [2048 x i8]* %9, i64 0, i64 %22
store i8 0, i8* %23
br label %24
24: ; preds = %17
%25 = load i32, i32* %13
%26 = add nsw i32 %25, 1
store i32 %26, i32* %13
br label %14
27: ; preds = %14
%28 = load i32, i32* @PF_PACKET
%29 = load i32, i32* @SOCK_RAW
%30 = load i32, i32* @ETH_P_ALL
%31 = call i32 @htons(i32 %30)
%32 = call i32 @socket(i32 %28, i32 %29, i32 %31)
store i32 %32, i32* %6
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %27
%35 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %27
%38 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %10, i32 0, i32 1
%39 = load i32, i32* %38
%40 = load i32, i32* @IFNAMSIZ
%41 = call i32 @strncpy(i32 %39, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %40)
%42 = load i32, i32* %6
%43 = load i32, i32* @SIOCGIFINDEX
%44 = call i32 @ioctl(i32 %42, i32 %43, %struct.ifreq* %10)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %51
46: ; preds = %37
%47 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%48 = load i32, i32* %6
%49 = call i32 @close(i32 %48)
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %37
%52 = load i32, i32* @AF_PACKET
%53 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %11, i32 0, i32 2
store i32 %52, i32* %53
%54 = load i32, i32* @ETH_P_ALL
%55 = call i32 @htons(i32 %54)
%56 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %11, i32 0, i32 1
store i32 %55, i32* %56
%57 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %10, i32 0, i32 0
%58 = load i32, i32* %57
%59 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %11, i32 0, i32 0
store i32 %58, i32* %59
%60 = load i32, i32* %6
%61 = bitcast %struct.sockaddr_ll* %11 to %struct.sockaddr*
%62 = call i64 @bind(i32 %60, %struct.sockaddr* %61, i32 12)
%63 = icmp slt i64 %62, 0
br i1 %63, label %64, label %69
64: ; preds = %51
%65 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%66 = load i32, i32* %6
%67 = call i32 @close(i32 %66)
%68 = call i32 @exit(i32 1)
unreachable
69: ; preds = %51
%70 = getelementptr inbounds [2048 x i8], [2048 x i8]* %9, i64 0, i64 0
store i8 -2, i8* %70
%71 = getelementptr inbounds [2048 x i8], [2048 x i8]* %9, i64 0, i64 1
store i8 -54, i8* %71
%72 = getelementptr inbounds [2048 x i8], [2048 x i8]* %9, i64 0, i64 2
store i8 -82, i8* %72
%73 = getelementptr inbounds [2048 x i8], [2048 x i8]* %9, i64 0, i64 7
store i8 3, i8* %73
%74 = getelementptr inbounds [2048 x i8], [2048 x i8]* %9, i64 0, i64 32
store i8 10, i8* %74
%75 = getelementptr inbounds [2048 x i8], [2048 x i8]* %9, i64 0, i64 33
store i8 11, i8* %75
%76 = getelementptr inbounds [2048 x i8], [2048 x i8]* %9, i64 0, i64 34
store i8 12, i8* %76
%77 = getelementptr inbounds [2048 x i8], [2048 x i8]* %9, i64 0, i64 35
store i8 13, i8* %77
%78 = getelementptr inbounds [2048 x i8], [2048 x i8]* %9, i64 0, i64 36
store i8 26, i8* %78
%79 = getelementptr inbounds [2048 x i8], [2048 x i8]* %9, i64 0, i64 37
store i8 27, i8* %79
%80 = getelementptr inbounds [2048 x i8], [2048 x i8]* %9, i64 0, i64 38
store i8 28, i8* %80
%81 = getelementptr inbounds [2048 x i8], [2048 x i8]* %9, i64 0, i64 39
store i8 29, i8* %81
br label %82
82: ; preds = %69, %82
%83 = load i32, i32* %6
%84 = getelementptr inbounds [2048 x i8], [2048 x i8]* %8, i64 0, i64 0
%85 = call i32 @recvfrom(i32 %83, i8* %84, i32 2048, i32 0, i32* null, i32* null)
store i32 %85, i32* %7
%86 = load i32, i32* %12
%87 = call i32 @printf(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.4, i64 0, i64 0), i32 %86)
%88 = getelementptr inbounds [2048 x i8], [2048 x i8]* %8, i64 0, i64 0
%89 = load i32, i32* %7
%90 = call i32 @print_hex(i8* %88, i32 %89)
%91 = load i32, i32* %12
%92 = add nsw i32 %91, 1
store i32 %92, i32* %12
br label %82
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
%struct.sockaddr_ll = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_ALL = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [4 x i8] c"nf0\00"
@IFNAMSIZ = global i32 0
@SIOCGIFINDEX = global i32 0
@.str.2 = [6 x i8] c"ioctl\00"
@AF_PACKET = global i32 0
@.str.3 = [5 x i8] c"Bind\00"
@.str.4 = [64 x i8] c"Packet%d -----------------------------------------------------\0A\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 = alloca %struct.ifreq
%6 = alloca %struct.sockaddr_ll
%7 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 0
%8 = bitcast %struct.ifreq* %5 to i8*
%9 = bitcast %struct.sockaddr_ll* %6 to i8*
%10 = load i32, i32* @PF_PACKET
%11 = load i32, i32* @SOCK_RAW
%12 = load i32, i32* @ETH_P_ALL
%13 = tail call i32 @htons(i32 %12)
%14 = tail call i32 @socket(i32 %10, i32 %11, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = load i32, i32* @IFNAMSIZ
%21 = tail call i32 @strncpy(i32 undef, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %20)
%22 = load i32, i32* @SIOCGIFINDEX
%23 = call i32 @ioctl(i32 %14, i32 %22, %struct.ifreq* %5)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %29
25: ; preds = %19
%26 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%27 = call i32 @close(i32 %14)
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %19
%30 = load i32, i32* @AF_PACKET
%31 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %6, i64 0, i32 2
store i32 %30, i32* %31
%32 = load i32, i32* @ETH_P_ALL
%33 = call i32 @htons(i32 %32)
%34 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %6, i64 0, i32 1
store i32 %33, i32* %34
%35 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i64 0, i32 0
%36 = load i32, i32* %35
%37 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %6, i64 0, i32 0
store i32 %36, i32* %37
%38 = bitcast %struct.sockaddr_ll* %6 to %struct.sockaddr*
%39 = call i64 @bind(i32 %14, %struct.sockaddr* %38, i32 12)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %29
%42 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%43 = call i32 @close(i32 %14)
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %29, %45
%46 = phi i32 [ %50, %45 ], [ 1, %29 ]
%47 = call i32 @recvfrom(i32 %14, i8* %7, i32 2048, i32 0, i32* null, i32* null)
%48 = call i32 @printf(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.4, i64 0, i64 0), i32 %46)
%49 = call i32 @print_hex(i8* %7, i32 %47)
%50 = add nuw nsw i32 %46, 1
br label %45
}