#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_2__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ uint32_t;
struct TYPE_2__ {
  int /*<<< orphan*/ sa_data;
};
struct ifreq {
  int ifr_ifindex;
  TYPE_1__ ifr_hwaddr;
  int /*<<< orphan*/ ifr_name;
};

/* Variables and functions */
int /*<<< orphan*/ AF_PACKET;
int /*<<< orphan*/ ETH_P_ARP;
int IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFHWADDR;
int /*<<< orphan*/ SIOCGIFINDEX;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ close(int);
int errno;
int get_if_ip4(int, char const *, int /*<<< orphan*/ *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memcpy(char *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
int strlen(char const *);
int get_if_info(const char *ifname, uint32_t *ip, char *mac, int *ifindex) {
  struct ifreq ifr;
  int raw_socket = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ARP));
  if (raw_socket < 0)
    return -1;
  if (strlen(ifname) > (IFNAMSIZ - 1)) {
    close(raw_socket);
    errno = -5;
    return -1;
  }
  strcpy(ifr.ifr_name, ifname);
  if (ioctl(raw_socket, SIOCGIFINDEX, &ifr) == -1) {
    close(raw_socket);
    return -1;
  }
  *ifindex = ifr.ifr_ifindex;
  if (ioctl(raw_socket, SIOCGIFHWADDR, &ifr) == -1) {
    close(raw_socket);
    return -1;
  }
  memcpy(mac, ifr.ifr_hwaddr.sa_data, 6);
  int err = get_if_ip4(raw_socket, ifname, ip);
  if (err)
    close(raw_socket);
  return err;
}


x86 assembly (clang, -O0):
<get_if_info>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
call   1060 <htons@plt>
mov    -0x48(%rbp),%edi
mov    -0x44(%rbp),%esi
mov    %eax,%edx
call   10a0 <socket@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    11e4 <get_if_info+0x64>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d7 <get_if_info+0x157>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    0x2dd4(%rip),%rcx
mov    (%rcx),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jle    1222 <get_if_info+0xa2>
mov    -0x3c(%rbp),%edi
call   1080 <close@plt>
mov    0x2db0(%rip),%rax
movl   $0xfffffffb,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12d7 <get_if_info+0x157>
mov    -0x30(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x3c(%rbp),%edi
mov    0x2d70(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1070 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    1260 <get_if_info+0xe0>
mov    -0x3c(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d7 <get_if_info+0x157>
mov    -0x38(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x3c(%rbp),%edi
mov    0x2d5d(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1070 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    129b <get_if_info+0x11b>
mov    -0x3c(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d7 <get_if_info+0x157>
mov    -0x20(%rbp),%rdi
mov    -0x34(%rbp),%esi
mov    $0x6,%edx
call   1090 <memcpy@plt>
mov    -0x3c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <get_if_ip4@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
je     12d1 <get_if_info+0x151>
mov    -0x3c(%rbp),%edi
call   1080 <close@plt>
mov    -0x40(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_if_info>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r13
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e37(%rip),%rax
mov    (%rax),%ebx
mov    0x2e3e(%rip),%rax
mov    (%rax),%ebp
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
call   1060 <htons@plt>
mov    %ebx,%edi
mov    %ebp,%esi
mov    %eax,%edx
call   10a0 <socket@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     126c <get_if_info+0xec>
mov    %eax,%ebp
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    0x2de5(%rip),%rcx
cmp    (%rcx),%eax
jge    1258 <get_if_info+0xd8>
mov    %r15,%rsi
call   1030 <strcpy@plt>
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1070 <ioctl@plt>
cmp    $0xffffffff,%eax
je     124f <get_if_info+0xcf>
mov    0x8(%rsp),%eax
mov    %eax,0x0(%r13)
mov    0x2db8(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1070 <ioctl@plt>
cmp    $0xffffffff,%eax
je     124f <get_if_info+0xcf>
mov    0xc(%rsp),%esi
mov    %r12,%rdi
mov    $0x6,%edx
call   1090 <memcpy@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1040 <get_if_ip4@plt>
test   %eax,%eax
je     127d <get_if_info+0xfd>
mov    %eax,%ebx
mov    %ebp,%edi
call   1080 <close@plt>
jmp    126c <get_if_info+0xec>
mov    %ebp,%edi
call   1080 <close@plt>
mov    0x2d5a(%rip),%rax
movl   $0xfffffffb,(%rax)
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %ebx,%ebx
jmp    126c <get_if_info+0xec>

LLVM IR (-O0):
%struct.ifreq = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@AF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_ARP = global i32 0
@IFNAMSIZ = global i32 0
@errno = global i32 0
@SIOCGIFINDEX = global i32 0
@SIOCGIFHWADDR = global i32 0
define i32 @get_if_info(i8* %0, i32* %1, i8* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca %struct.ifreq
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %6
store i32* %1, i32** %7
store i8* %2, i8** %8
store i32* %3, i32** %9
%13 = load i32, i32* @AF_PACKET
%14 = load i32, i32* @SOCK_RAW
%15 = load i32, i32* @ETH_P_ARP
%16 = call i32 @htons(i32 %15)
%17 = call i32 @socket(i32 %13, i32 %14, i32 %16)
store i32 %17, i32* %11
%18 = load i32, i32* %11
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %4
store i32 -1, i32* %5
br label %70
21: ; preds = %4
%22 = load i8*, i8** %6
%23 = call i32 @strlen(i8* %22)
%24 = load i32, i32* @IFNAMSIZ
%25 = sub nsw i32 %24, 1
%26 = icmp sgt i32 %23, %25
br i1 %26, label %27, label %30
27: ; preds = %21
%28 = load i32, i32* %11
%29 = call i32 @close(i32 %28)
store i32 -5, i32* @errno
store i32 -1, i32* %5
br label %70
30: ; preds = %21
%31 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %10, i32 0, i32 2
%32 = load i32, i32* %31
%33 = load i8*, i8** %6
%34 = call i32 @strcpy(i32 %32, i8* %33)
%35 = load i32, i32* %11
%36 = load i32, i32* @SIOCGIFINDEX
%37 = call i32 @ioctl(i32 %35, i32 %36, %struct.ifreq* %10)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %30
%40 = load i32, i32* %11
%41 = call i32 @close(i32 %40)
store i32 -1, i32* %5
br label %70
42: ; preds = %30
%43 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %10, i32 0, i32 0
%44 = load i32, i32* %43
%45 = load i32*, i32** %9
store i32 %44, i32* %45
%46 = load i32, i32* %11
%47 = load i32, i32* @SIOCGIFHWADDR
%48 = call i32 @ioctl(i32 %46, i32 %47, %struct.ifreq* %10)
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %53
50: ; preds = %42
%51 = load i32, i32* %11
%52 = call i32 @close(i32 %51)
store i32 -1, i32* %5
br label %70
53: ; preds = %42
%54 = load i8*, i8** %8
%55 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %10, i32 0, i32 1
%56 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %55, i32 0, i32 0
%57 = load i32, i32* %56
%58 = call i32 @memcpy(i8* %54, i32 %57, i32 6)
%59 = load i32, i32* %11
%60 = load i8*, i8** %6
%61 = load i32*, i32** %7
%62 = call i32 @get_if_ip4(i32 %59, i8* %60, i32* %61)
store i32 %62, i32* %12
%63 = load i32, i32* %12
%64 = icmp ne i32 %63, 0
br i1 %64, label %65, label %68
65: ; preds = %53
%66 = load i32, i32* %11
%67 = call i32 @close(i32 %66)
br label %68
68: ; preds = %65, %53
%69 = load i32, i32* %12
store i32 %69, i32* %5
br label %70
70: ; preds = %68, %50, %39, %27, %20
%71 = load i32, i32* %5
ret i32 %71
}

LLVM IR (-O2):
%struct.ifreq = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@AF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_ARP = global i32 0
@IFNAMSIZ = global i32 0
@errno = global i32 0
@SIOCGIFINDEX = global i32 0
@SIOCGIFHWADDR = global i32 0
define i32 @get_if_info(i8* %0, i32* %1, i8* %2, i32* nocapture writeonly %3) {
%5 = alloca %struct.ifreq
%6 = bitcast %struct.ifreq* %5 to i8*
%7 = load i32, i32* @AF_PACKET
%8 = load i32, i32* @SOCK_RAW
%9 = load i32, i32* @ETH_P_ARP
%10 = tail call i32 @htons(i32 %9)
%11 = tail call i32 @socket(i32 %7, i32 %8, i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %42, label %13
13: ; preds = %4
%14 = tail call i32 @strlen(i8* %0)
%15 = load i32, i32* @IFNAMSIZ
%16 = icmp slt i32 %14, %15
br i1 %16, label %19, label %17
17: ; preds = %13
%18 = tail call i32 @close(i32 %11)
store i32 -5, i32* @errno
br label %42
19: ; preds = %13
%20 = tail call i32 @strcpy(i32 undef, i8* %0)
%21 = load i32, i32* @SIOCGIFINDEX
%22 = call i32 @ioctl(i32 %11, i32 %21, %struct.ifreq* %5)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = call i32 @close(i32 %11)
br label %42
26: ; preds = %19
%27 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i64 0, i32 0
%28 = load i32, i32* %27
store i32 %28, i32* %3
%29 = load i32, i32* @SIOCGIFHWADDR
%30 = call i32 @ioctl(i32 %11, i32 %29, %struct.ifreq* %5)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %26
%33 = call i32 @close(i32 %11)
br label %42
34: ; preds = %26
%35 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i64 0, i32 1, i32 0
%36 = load i32, i32* %35
%37 = call i32 @memcpy(i8* %2, i32 %36, i32 6)
%38 = call i32 @get_if_ip4(i32 %11, i8* %0, i32* %1)
%39 = icmp eq i32 %38, 0
br i1 %39, label %42, label %40
40: ; preds = %34
%41 = call i32 @close(i32 %11)
br label %42
42: ; preds = %34, %40, %4, %32, %24, %17
%43 = phi i32 [ -1, %17 ], [ -1, %24 ], [ -1, %32 ], [ -1, %4 ], [ %38, %40 ], [ 0, %34 ]
ret i32 %43
}