#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 */
typedef struct TYPE_4__ TYPE_2__;
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
struct TYPE_3__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_family;
  TYPE_1__ sin_addr;
};
struct TYPE_4__ {
  int /*<<< orphan*/ sa_data;
};
struct arpreq {
  TYPE_2__ arp_ha;
  int /*<<< orphan*/ arp_dev;
  int /*<<< orphan*/ arp_pa;
};
typedef int /*<<< orphan*/ caddr_t;
typedef int /*<<< orphan*/ areq;
typedef int /*<<< orphan*/ __be32;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ ARPHRD_ETHER;
int /*<<< orphan*/ ETH_ALEN;
int /*<<< orphan*/ SIOCGARP;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(unsigned char *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int);
int _find_macaddr_by_ip(char *devname, __be32 ipaddr, unsigned char *mac) {
  int sock;
  struct arpreq areq;
  struct sockaddr_in *sin;
  if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
    perror("socket");
    return -1;
  }
  memset((void *)&areq, 0, sizeof(areq));
  sin = (struct sockaddr_in *)&areq.arp_pa;
  sin->sin_family = AF_INET;
  sin->sin_addr.s_addr = ipaddr;
  sin = (struct sockaddr_in *)&areq.arp_ha;
  sin->sin_family = ARPHRD_ETHER;
  strncpy(areq.arp_dev, devname, 15);
  if (ioctl(sock, SIOCGARP, (caddr_t)&areq) == -1) {
    close(sock);
    perror("-- Error: unable to make ARP request, error");
    return -1;
  }
  close(sock);
  memcpy(mac, areq.arp_ha.sa_data, ETH_ALEN);
  if ((mac[0] | mac[1] | mac[2] | mac[3] | mac[4] | mac[5]) == 0) {
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<_find_macaddr_by_ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    11c0 <_find_macaddr_by_ip+0x50>
lea    0xe51(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12cf <_find_macaddr_by_ip+0x15f>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
lea    -0x30(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x38(%rbp)
mov    0x2e05(%rip),%rax
mov    (%rax),%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x14(%rbp),%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,0x4(%rax)
lea    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2dcc(%rip),%rax
mov    (%rax),%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x2c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0xf,%edx
call   1030 <strncpy@plt>
mov    -0x24(%rbp),%edi
mov    0x2d99(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rax
mov    %eax,%edx
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    125d <_find_macaddr_by_ip+0xed>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
lea    0xdbb(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12cf <_find_macaddr_by_ip+0x15f>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%esi
mov    0x2d6d(%rip),%rax
mov    (%rax),%edx
call   1070 <memcpy@plt>
mov    -0x20(%rbp),%rax
movzbl (%rax),%eax
mov    -0x20(%rbp),%rcx
movzbl 0x1(%rcx),%ecx
or     %ecx,%eax
mov    -0x20(%rbp),%rcx
movzbl 0x2(%rcx),%ecx
or     %ecx,%eax
mov    -0x20(%rbp),%rcx
movzbl 0x3(%rcx),%ecx
or     %ecx,%eax
mov    -0x20(%rbp),%rcx
movzbl 0x4(%rcx),%ecx
or     %ecx,%eax
mov    -0x20(%rbp),%rcx
movzbl 0x5(%rcx),%ecx
or     %ecx,%eax
cmp    $0x0,%eax
jne    12c8 <_find_macaddr_by_ip+0x158>
movl   $0xffffffff,-0x4(%rbp)
jmp    12cf <_find_macaddr_by_ip+0x15f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_find_macaddr_by_ip>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r13
mov    %esi,%r15d
mov    %rdi,%r14
mov    0x2e5a(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     1260 <_find_macaddr_by_ip+0xf0>
mov    %eax,%ebp
lea    0x8(%rsp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    (%rbx),%eax
mov    %eax,0x10(%rsp)
mov    %r15d,0x14(%rsp)
mov    0x2dff(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0xc(%rsp),%edi
mov    %r14,%rsi
mov    $0xf,%edx
call   1030 <strncpy@plt>
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r12d,%edx
call   1050 <ioctl@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1060 <close@plt>
cmp    $0xffffffff,%ebx
je     1269 <_find_macaddr_by_ip+0xf9>
mov    0x8(%rsp),%esi
mov    0x2dcc(%rip),%rax
mov    (%rax),%edx
mov    %r13,%rdi
call   1070 <memcpy@plt>
movd   0x0(%r13),%xmm0
movdqa %xmm0,%xmm1
psrld  $0x10,%xmm1
por    %xmm0,%xmm1
movdqa %xmm1,%xmm0
psrlw  $0x8,%xmm0
por    %xmm1,%xmm0
movd   %xmm0,%ecx
or     0x4(%r13),%cl
xor    %eax,%eax
or     0x5(%r13),%cl
sete   %al
neg    %eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd99(%rip),%rdi
jmp    1270 <_find_macaddr_by_ip+0x100>
lea    0xd97(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%eax
jmp    1251 <_find_macaddr_by_ip+0xe1>

LLVM IR (-O0):
%struct.arpreq = type { %struct.TYPE_4__, i32, i32 }
%struct.TYPE_4__ = type { i32 }
%struct.sockaddr_in = type { i32, %struct.TYPE_3__ }
%struct.TYPE_3__ = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [7 x i8] c"socket\00"
@ARPHRD_ETHER = global i32 0
@SIOCGARP = global i32 0
@.str.1 = [44 x i8] c"-- Error: unable to make ARP request, error\00"
@ETH_ALEN = global i32 0
define i32 @_find_macaddr_by_ip(i8* %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca %struct.arpreq
%10 = alloca %struct.sockaddr_in*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_DGRAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %8
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %3
%16 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %88
17: ; preds = %3
%18 = bitcast %struct.arpreq* %9 to i8*
%19 = call i32 @memset(i8* %18, i32 0, i32 12)
%20 = getelementptr inbounds %struct.arpreq, %struct.arpreq* %9, i32 0, i32 2
%21 = bitcast i32* %20 to %struct.sockaddr_in*
store %struct.sockaddr_in* %21, %struct.sockaddr_in** %10
%22 = load i32, i32* @AF_INET
%23 = load %struct.sockaddr_in*, %struct.sockaddr_in** %10
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %23, i32 0, i32 0
store i32 %22, i32* %24
%25 = load i32, i32* %6
%26 = load %struct.sockaddr_in*, %struct.sockaddr_in** %10
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %26, i32 0, i32 1
%28 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %27, i32 0, i32 0
store i32 %25, i32* %28
%29 = getelementptr inbounds %struct.arpreq, %struct.arpreq* %9, i32 0, i32 0
%30 = bitcast %struct.TYPE_4__* %29 to %struct.sockaddr_in*
store %struct.sockaddr_in* %30, %struct.sockaddr_in** %10
%31 = load i32, i32* @ARPHRD_ETHER
%32 = load %struct.sockaddr_in*, %struct.sockaddr_in** %10
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %32, i32 0, i32 0
store i32 %31, i32* %33
%34 = getelementptr inbounds %struct.arpreq, %struct.arpreq* %9, i32 0, i32 1
%35 = load i32, i32* %34
%36 = load i8*, i8** %5
%37 = call i32 @strncpy(i32 %35, i8* %36, i32 15)
%38 = load i32, i32* %8
%39 = load i32, i32* @SIOCGARP
%40 = ptrtoint %struct.arpreq* %9 to i32
%41 = call i32 @ioctl(i32 %38, i32 %39, i32 %40)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %47
43: ; preds = %17
%44 = load i32, i32* %8
%45 = call i32 @close(i32 %44)
%46 = call i32 @perror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %88
47: ; preds = %17
%48 = load i32, i32* %8
%49 = call i32 @close(i32 %48)
%50 = load i8*, i8** %7
%51 = getelementptr inbounds %struct.arpreq, %struct.arpreq* %9, i32 0, i32 0
%52 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %51, i32 0, i32 0
%53 = load i32, i32* %52
%54 = load i32, i32* @ETH_ALEN
%55 = call i32 @memcpy(i8* %50, i32 %53, i32 %54)
%56 = load i8*, i8** %7
%57 = getelementptr inbounds i8, i8* %56, i64 0
%58 = load i8, i8* %57
%59 = zext i8 %58 to i32
%60 = load i8*, i8** %7
%61 = getelementptr inbounds i8, i8* %60, i64 1
%62 = load i8, i8* %61
%63 = zext i8 %62 to i32
%64 = or i32 %59, %63
%65 = load i8*, i8** %7
%66 = getelementptr inbounds i8, i8* %65, i64 2
%67 = load i8, i8* %66
%68 = zext i8 %67 to i32
%69 = or i32 %64, %68
%70 = load i8*, i8** %7
%71 = getelementptr inbounds i8, i8* %70, i64 3
%72 = load i8, i8* %71
%73 = zext i8 %72 to i32
%74 = or i32 %69, %73
%75 = load i8*, i8** %7
%76 = getelementptr inbounds i8, i8* %75, i64 4
%77 = load i8, i8* %76
%78 = zext i8 %77 to i32
%79 = or i32 %74, %78
%80 = load i8*, i8** %7
%81 = getelementptr inbounds i8, i8* %80, i64 5
%82 = load i8, i8* %81
%83 = zext i8 %82 to i32
%84 = or i32 %79, %83
%85 = icmp eq i32 %84, 0
br i1 %85, label %86, label %87
86: ; preds = %47
store i32 -1, i32* %4
br label %88
87: ; preds = %47
store i32 0, i32* %4
br label %88
88: ; preds = %87, %86, %43, %15
%89 = load i32, i32* %4
ret i32 %89
}

LLVM IR (-O2):
%struct.arpreq = type { %struct.TYPE_4__, i32, i32 }
%struct.TYPE_4__ = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [7 x i8] c"socket\00"
@ARPHRD_ETHER = global i32 0
@SIOCGARP = global i32 0
@.str.1 = [44 x i8] c"-- Error: unable to make ARP request, error\00"
@ETH_ALEN = global i32 0
define i32 @_find_macaddr_by_ip(i8* %0, i32 %1, i8* %2) {
%4 = alloca %struct.arpreq
%5 = bitcast %struct.arpreq* %4 to i8*
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_DGRAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %3
%11 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %45
12: ; preds = %3
%13 = call i32 @memset(i8* %5, i32 0, i32 12)
%14 = getelementptr inbounds %struct.arpreq, %struct.arpreq* %4, i64 0, i32 2
%15 = load i32, i32* @AF_INET
store i32 %15, i32* %14
%16 = getelementptr inbounds i32, i32* %14, i64 1
store i32 %1, i32* %16
%17 = load i32, i32* @ARPHRD_ETHER
%18 = getelementptr inbounds %struct.arpreq, %struct.arpreq* %4, i64 0, i32 0, i32 0
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.arpreq, %struct.arpreq* %4, i64 0, i32 1
%20 = load i32, i32* %19
%21 = call i32 @strncpy(i32 %20, i8* %0, i32 15)
%22 = load i32, i32* @SIOCGARP
%23 = ptrtoint %struct.arpreq* %4 to i64
%24 = trunc i64 %23 to i32
%25 = call i32 @ioctl(i32 %8, i32 %22, i32 %24)
%26 = icmp eq i32 %25, -1
%27 = call i32 @close(i32 %8)
br i1 %26, label %28, label %30
28: ; preds = %12
%29 = call i32 @perror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0))
br label %45
30: ; preds = %12
%31 = load i32, i32* %18
%32 = load i32, i32* @ETH_ALEN
%33 = call i32 @memcpy(i8* %2, i32 %31, i32 %32)
%34 = bitcast i8* %2 to <4 x i8>*
%35 = load <4 x i8>, <4 x i8>* %34
%36 = getelementptr inbounds i8, i8* %2, i64 4
%37 = load i8, i8* %36
%38 = getelementptr inbounds i8, i8* %2, i64 5
%39 = load i8, i8* %38
%40 = call i8 @llvm.vector.reduce.or.v4i8(<4 x i8> %35)
%41 = or i8 %40, %37
%42 = or i8 %41, %39
%43 = icmp eq i8 %42, 0
%44 = sext i1 %43 to i32
br label %45
45: ; preds = %30, %28, %10
%46 = phi i32 [ -1, %10 ], [ -1, %28 ], [ %44, %30 ]
ret i32 %46
}