#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_protocol;
  int /*<<< orphan*/ sll_ifindex;
  int /*<<< orphan*/ sll_family;
};
struct ifreq {
  int /*<<< orphan*/ ifr_ifindex;
  int /*<<< orphan*/ ifr_name;
};
typedef int /*<<< orphan*/ peer_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_PACKET;
int /*<<< orphan*/ ETH_P_ARP;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SIOCGIFINDEX;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ bzero(struct ifreq *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct sockaddr_ll *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int recv_arp(int, struct sockaddr_ll *);
int send_arp(int, struct sockaddr_ll *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
char *strerror(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int sockfd;
  int rtval = -1;
  struct sockaddr_ll peer_addr;
  sockfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ARP));
  if (sockfd < 0) {
    fprintf(stderr, "socket error: %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  memset(&peer_addr, 0, sizeof(peer_addr));
  peer_addr.sll_family = AF_PACKET;
  struct ifreq req;
  bzero(&req, sizeof(struct ifreq));
  strcpy(req.ifr_name, "eth0");
  if (ioctl(sockfd, SIOCGIFINDEX, &req) != 0)
    perror("ioctl()");
  peer_addr.sll_ifindex = req.ifr_ifindex;
  peer_addr.sll_protocol = htons(ETH_P_ARP);
  while (1) {
    rtval = send_arp(sockfd, &peer_addr);
    if (-1 == rtval) {
      fprintf(stderr, "Send arp socket failed: %s\n", strerror(errno));
    }
    rtval = recv_arp(sockfd, &peer_addr);
    if (rtval == 0) {
      printf("Get packet from peer and IP conflicts!\n");
    } else if (rtval == -1) {
      fprintf(stderr, "Recv arp IP not conflicts: %s\n", strerror(errno));
    } else {
      fprintf(stderr, "Recv arp socket failed: %s\n", strerror(errno));
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x18(%rbp)
mov    0x2de4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2de8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    -0x38(%rbp),%edi
mov    -0x34(%rbp),%esi
mov    %eax,%edx
call   10f0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1267 <main+0x97>
mov    0x2d9c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    %rax,%rdx
lea    0xdac(%rip),%rsi
call   1080 <fprintf@plt>
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2d5a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
lea    -0x30(%rbp),%rdi
mov    $0x8,%esi
call   10c0 <bzero@plt>
mov    -0x2c(%rbp),%edi
lea    0xd77(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x14(%rbp),%edi
mov    0x2cfe(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1070 <ioctl@plt>
cmp    $0x0,%rax
je     12cb <main+0xfb>
lea    0xd51(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2ce0(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1090 <send_arp@plt>
mov    %eax,-0x18(%rbp)
mov    $0xffffffff,%eax
cmp    -0x18(%rbp),%eax
jne    132b <main+0x15b>
mov    0x2cca(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2cb6(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x40(%rbp),%edi
mov    %rax,%rdx
lea    0xcf9(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   10a0 <recv_arp@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    1355 <main+0x185>
lea    0xcf0(%rip),%rdi
call   1050 <printf@plt>
jmp    13c1 <main+0x1f1>
cmpl   $0xffffffff,-0x18(%rbp)
jne    1390 <main+0x1c0>
mov    0x2c6a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    0x2c56(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    %rax,%rdx
lea    0xcdd(%rip),%rsi
call   1080 <fprintf@plt>
jmp    13bc <main+0x1ec>
mov    0x2c39(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    0x2c25(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x48(%rbp),%edi
mov    %rax,%rdx
lea    0xccb(%rip),%rsi
call   1080 <fprintf@plt>
jmp    13c1 <main+0x1f1>
jmp    12e2 <main+0x112>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2df3(%rip),%r15
mov    (%r15),%ebx
mov    0x2df9(%rip),%rax
mov    (%rax),%ebp
mov    0x2dc0(%rip),%r14
mov    (%r14),%edi
call   1050 <htons@plt>
mov    %ebx,%edi
mov    %ebp,%esi
mov    %eax,%edx
call   10f0 <socket@plt>
test   %eax,%eax
js     1333 <main+0x163>
mov    %eax,%ebx
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x8(%rsp)
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x8,%esi
call   10c0 <bzero@plt>
mov    0x14(%rsp),%edi
lea    0xdca(%rip),%rsi
call   1030 <strcpy@plt>
mov    0x2d54(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %rbp,%rdx
call   1070 <ioctl@plt>
test   %rax,%rax
jne    1322 <main+0x152>
mov    0x10(%rsp),%eax
mov    %eax,0x4(%rsp)
mov    (%r14),%edi
call   1050 <htons@plt>
mov    %eax,(%rsp)
mov    %rsp,%r14
mov    0x2d4a(%rip),%r13
mov    0x2d3b(%rip),%r15
jmp    129c <main+0xcc>
nop
lea    0xddf(%rip),%rdi
call   1040 <puts@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1090 <send_arp@plt>
cmp    $0xffffffff,%eax
jne    12c9 <main+0xf9>
mov    0x0(%r13),%r12d
mov    (%r15),%edi
call   10e0 <strerror@plt>
mov    %r12d,%edi
lea    0xd5e(%rip),%rsi
mov    %rax,%rdx
call   1080 <fprintf@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   10a0 <recv_arp@plt>
cmp    $0xffffffff,%eax
je     1300 <main+0x130>
test   %eax,%eax
je     1290 <main+0xc0>
mov    0x0(%r13),%ebp
mov    (%r15),%edi
call   10e0 <strerror@plt>
mov    %ebp,%edi
lea    0xd69(%rip),%rsi
jmp    1315 <main+0x145>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x0(%r13),%ebp
mov    (%r15),%edi
call   10e0 <strerror@plt>
mov    %ebp,%edi
lea    0xd26(%rip),%rsi
mov    %rax,%rdx
call   1080 <fprintf@plt>
jmp    129c <main+0xcc>
lea    0xcee(%rip),%rdi
call   10b0 <perror@plt>
jmp    1269 <main+0x99>
mov    0x2c96(%rip),%rax
mov    (%rax),%ebx
mov    0x2c85(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xcaf(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1080 <fprintf@plt>
mov    0x2c5e(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_ll = type { i32, i32, i32 }
%struct.ifreq = type { i32, i32 }
@AF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_ARP = global i32 0
@stderr = global i32 0
@.str = [18 x i8] c"socket error: %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [5 x i8] c"eth0\00"
@SIOCGIFINDEX = global i32 0
@.str.2 = [8 x i8] c"ioctl()\00"
@.str.3 = [28 x i8] c"Send arp socket failed: %s\0A\00"
@.str.4 = [40 x i8] c"Get packet from peer and IP conflicts!\0A\00"
@.str.5 = [31 x i8] c"Recv arp IP not conflicts: %s\0A\00"
@.str.6 = [28 x i8] c"Recv arp socket failed: %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 %struct.sockaddr_ll
%9 = alloca %struct.ifreq
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %7
%10 = load i32, i32* @AF_PACKET
%11 = load i32, i32* @SOCK_RAW
%12 = load i32, i32* @ETH_P_ARP
%13 = call i32 @htons(i32 %12)
%14 = call i32 @socket(i32 %10, i32 %11, i32 %13)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %24
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = load i32, i32* @errno
%20 = call i8* @strerror(i32 %19)
%21 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %2
%25 = call i32 @memset(%struct.sockaddr_ll* %8, i32 0, i32 12)
%26 = load i32, i32* @AF_PACKET
%27 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %8, i32 0, i32 2
store i32 %26, i32* %27
%28 = call i32 @bzero(%struct.ifreq* %9, i32 8)
%29 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %9, i32 0, i32 1
%30 = load i32, i32* %29
%31 = call i32 @strcpy(i32 %30, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%32 = load i32, i32* %6
%33 = load i32, i32* @SIOCGIFINDEX
%34 = call i64 @ioctl(i32 %32, i32 %33, %struct.ifreq* %9)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %24
%37 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %38
38: ; preds = %36, %24
%39 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %9, i32 0, i32 0
%40 = load i32, i32* %39
%41 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %8, i32 0, i32 1
store i32 %40, i32* %41
%42 = load i32, i32* @ETH_P_ARP
%43 = call i32 @htons(i32 %42)
%44 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %8, i32 0, i32 0
store i32 %43, i32* %44
br label %45
45: ; preds = %38, %76
%46 = load i32, i32* %6
%47 = call i32 @send_arp(i32 %46, %struct.sockaddr_ll* %8)
store i32 %47, i32* %7
%48 = load i32, i32* %7
%49 = icmp eq i32 -1, %48
br i1 %49, label %50, label %55
50: ; preds = %45
%51 = load i32, i32* @stderr
%52 = load i32, i32* @errno
%53 = call i8* @strerror(i32 %52)
%54 = call i32 @fprintf(i32 %51, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %53)
br label %55
55: ; preds = %50, %45
%56 = load i32, i32* %6
%57 = call i32 @recv_arp(i32 %56, %struct.sockaddr_ll* %8)
store i32 %57, i32* %7
%58 = load i32, i32* %7
%59 = icmp eq i32 %58, 0
br i1 %59, label %60, label %62
60: ; preds = %55
%61 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0))
br label %76
62: ; preds = %55
%63 = load i32, i32* %7
%64 = icmp eq i32 %63, -1
br i1 %64, label %65, label %70
65: ; preds = %62
%66 = load i32, i32* @stderr
%67 = load i32, i32* @errno
%68 = call i8* @strerror(i32 %67)
%69 = call i32 @fprintf(i32 %66, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i8* %68)
br label %75
70: ; preds = %62
%71 = load i32, i32* @stderr
%72 = load i32, i32* @errno
%73 = call i8* @strerror(i32 %72)
%74 = call i32 @fprintf(i32 %71, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0), i8* %73)
br label %75
75: ; preds = %70, %65
br label %76
76: ; preds = %75, %60
br label %45
}

LLVM IR (-O2):
%struct.sockaddr_ll = type { i32, i32, i32 }
%struct.ifreq = type { i32, i32 }
@AF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_ARP = global i32 0
@stderr = global i32 0
@.str = [18 x i8] c"socket error: %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [5 x i8] c"eth0\00"
@SIOCGIFINDEX = global i32 0
@.str.2 = [8 x i8] c"ioctl()\00"
@.str.3 = [28 x i8] c"Send arp socket failed: %s\0A\00"
@.str.5 = [31 x i8] c"Recv arp IP not conflicts: %s\0A\00"
@.str.6 = [28 x i8] c"Recv arp socket failed: %s\0A\00"
@str = [39 x i8] c"Get packet from peer and IP conflicts!\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_ll
%4 = alloca %struct.ifreq
%5 = bitcast %struct.sockaddr_ll* %3 to i8*
%6 = load i32, i32* @AF_PACKET
%7 = load i32, i32* @SOCK_RAW
%8 = load i32, i32* @ETH_P_ARP
%9 = tail call i32 @htons(i32 %8)
%10 = tail call i32 @socket(i32 %6, i32 %7, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %19
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i32, i32* @errno
%15 = tail call i8* @strerror(i32 %14)
%16 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = load i32, i32* @EXIT_FAILURE
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %2
%20 = call i32 @memset(%struct.sockaddr_ll* %3, i32 0, i32 12)
%21 = load i32, i32* @AF_PACKET
%22 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %3, i64 0, i32 2
store i32 %21, i32* %22
%23 = bitcast %struct.ifreq* %4 to i8*
%24 = call i32 @bzero(%struct.ifreq* %4, i32 8)
%25 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 1
%26 = load i32, i32* %25
%27 = call i32 @strcpy(i32 %26, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%28 = load i32, i32* @SIOCGIFINDEX
%29 = call i64 @ioctl(i32 %10, i32 %28, %struct.ifreq* %4)
%30 = icmp eq i64 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %19
%32 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %33
33: ; preds = %31, %19
%34 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 0
%35 = load i32, i32* %34
%36 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %3, i64 0, i32 1
store i32 %35, i32* %36
%37 = load i32, i32* @ETH_P_ARP
%38 = call i32 @htons(i32 %37)
%39 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %3, i64 0, i32 0
store i32 %38, i32* %39
br label %40
40: ; preds = %57, %33
%41 = call i32 @send_arp(i32 %10, %struct.sockaddr_ll* %3)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %48
43: ; preds = %40
%44 = load i32, i32* @stderr
%45 = load i32, i32* @errno
%46 = call i8* @strerror(i32 %45)
%47 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %46)
br label %48
48: ; preds = %43, %40
%49 = call i32 @recv_arp(i32 %10, %struct.sockaddr_ll* %3)
switch i32 %49, label %58 [
i32 0, label %50
i32 -1, label %52
]
50: ; preds = %48
%51 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str, i64 0, i64 0))
br label %57
52: ; preds = %48
%53 = load i32, i32* @stderr
%54 = load i32, i32* @errno
%55 = call i8* @strerror(i32 %54)
%56 = call i32 @fprintf(i32 %53, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i8* %55)
br label %57
57: ; preds = %52, %58, %50
br label %40
58: ; preds = %48
%59 = load i32, i32* @stderr
%60 = load i32, i32* @errno
%61 = call i8* @strerror(i32 %60)
%62 = call i32 @fprintf(i32 %59, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0), i8* %61)
br label %57
}