#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*/ uint8_t;
struct TYPE_2__ {
  int /*<<< orphan*/ sa_data;
};
typedef struct ifreq {
  int ifc_len;
  char *ifc_buf;
  int ifr_flags;
  TYPE_1__ ifr_hwaddr;
  int /*<<< orphan*/ ifr_name;
  struct ifreq const *ifc_req;
} const ifreq;
typedef struct ifconf {
  int ifc_len;
  char *ifc_buf;
  int ifr_flags;
  TYPE_1__ ifr_hwaddr;
  int /*<<< orphan*/ ifr_name;
  struct ifconf const *ifc_req;
} const ifconf;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int IFF_LOOPBACK;
int /*<<< orphan*/ IPPROTO_IP;
int /*<<< orphan*/ SIOCGIFCONF;
int /*<<< orphan*/ SIOCGIFFLAGS;
int /*<<< orphan*/ SIOCGIFHWADDR;
int /*<<< orphan*/ SOCK_DGRAM;
int ioctl(int, int /*<<< orphan*/, struct ifreq const *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ strcmp(int /*<<< orphan*/, char *);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, int /*<<< orphan*/);
int netutil_get_mac_addr(char *iface, uint8_t *mac_address) {
  struct ifreq ifr;
  struct ifconf ifc;
  char buf[1024];
  int ret = -1;
  int iface_flag = 0;
  int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
  if (sock == -1) {
    return -1;
  }
  ifc.ifc_len = sizeof(buf);
  ifc.ifc_buf = buf;
  if (ioctl(sock, SIOCGIFCONF, &ifc) == -1) {
    return -1;
  }
  struct ifreq *it = ifc.ifc_req;
  const struct ifreq *const end = it + (ifc.ifc_len / sizeof(struct ifreq));
  for (; it != end; ++it) {
    strcpy(ifr.ifr_name, it->ifr_name);
    if ((iface != NULL) && (0 == strcmp(ifr.ifr_name, iface))) {
      iface_flag = 1;
      if (ioctl(sock, SIOCGIFFLAGS, &ifr) == 0) {
        if (ifr.ifr_flags & IFF_LOOPBACK) {
          continue;
        }
        if (ioctl(sock, SIOCGIFHWADDR, &ifr) == 0) {
          ret = 0;
          memcpy(mac_address, ifr.ifr_hwaddr.sa_data, 6);
        }
      }
      break;
    }
  }
  if ((ret == -1) && (iface_flag == 0)) {
    it = ifc.ifc_req;
    for (; it != end; ++it) {
      strcpy(ifr.ifr_name, it->ifr_name);
      if (ioctl(sock, SIOCGIFFLAGS, &ifr) == 0) {
        if (ifr.ifr_flags & IFF_LOOPBACK) {
          continue;
        }
        if (ioctl(sock, SIOCGIFHWADDR, &ifr) == 0) {
          ret = 1;
          memcpy(mac_address, ifr.ifr_hwaddr.sa_data, 6);
          break;
        }
      }
    }
  }
  return ret;
}


x86 assembly (clang, -O0):
<netutil_get_mac_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4a0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0xffffffff,-0x474(%rbp)
ff ff ff
movl   $0x0,-0x478(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
mov    0x2e48(%rip),%rax
mov    (%rax),%edx
call   1070 <socket@plt>
mov    %eax,-0x47c(%rbp)
cmpl   $0xffffffff,-0x47c(%rbp)
jne    11b6 <netutil_get_mac_addr+0x66>
movl   $0xffffffff,-0x4(%rbp)
jmp    141c <netutil_get_mac_addr+0x2cc>
movl   $0x400,-0x68(%rbp)
lea    -0x470(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x47c(%rbp),%edi
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
lea    -0x68(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    11f5 <netutil_get_mac_addr+0xa5>
movl   $0xffffffff,-0x4(%rbp)
jmp    141c <netutil_get_mac_addr+0x2cc>
mov    -0x48(%rbp),%rax
mov    %rax,-0x488(%rbp)
mov    -0x488(%rbp),%rax
mov    %rax,-0x498(%rbp)
movslq -0x68(%rbp),%rax
mov    $0x28,%ecx
xor    %edx,%edx
div    %rcx
mov    %rax,%rcx
mov    -0x498(%rbp),%rax
imul   $0x28,%rcx,%rcx
add    %rcx,%rax
mov    %rax,-0x490(%rbp)
mov    -0x488(%rbp),%rax
cmp    -0x490(%rbp),%rax
je     1326 <netutil_get_mac_addr+0x1d6>
mov    -0x28(%rbp),%edi
mov    -0x488(%rbp),%rax
mov    0x18(%rax),%esi
call   1030 <strcpy@plt>
cmpq   $0x0,-0x10(%rbp)
je     130a <netutil_get_mac_addr+0x1ba>
mov    -0x28(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1050 <strcmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    130a <netutil_get_mac_addr+0x1ba>
movl   $0x1,-0x478(%rbp)
mov    -0x47c(%rbp),%edi
mov    0x2d32(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%eax
jne    1305 <netutil_get_mac_addr+0x1b5>
mov    -0x30(%rbp),%eax
mov    0x2d44(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12c4 <netutil_get_mac_addr+0x174>
jmp    130f <netutil_get_mac_addr+0x1bf>
mov    -0x47c(%rbp),%edi
mov    0x2ce7(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%eax
jne    1300 <netutil_get_mac_addr+0x1b0>
movl   $0x0,-0x474(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    $0x6,%edx
call   1060 <memcpy@plt>
jmp    1305 <netutil_get_mac_addr+0x1b5>
jmp    1326 <netutil_get_mac_addr+0x1d6>
jmp    130f <netutil_get_mac_addr+0x1bf>
mov    -0x488(%rbp),%rax
add    $0x28,%rax
mov    %rax,-0x488(%rbp)
jmp    1234 <netutil_get_mac_addr+0xe4>
cmpl   $0xffffffff,-0x474(%rbp)
jne    1413 <netutil_get_mac_addr+0x2c3>
cmpl   $0x0,-0x478(%rbp)
jne    1413 <netutil_get_mac_addr+0x2c3>
mov    -0x48(%rbp),%rax
mov    %rax,-0x488(%rbp)
mov    -0x488(%rbp),%rax
cmp    -0x490(%rbp),%rax
je     140e <netutil_get_mac_addr+0x2be>
mov    -0x28(%rbp),%edi
mov    -0x488(%rbp),%rax
mov    0x18(%rax),%esi
call   1030 <strcpy@plt>
mov    -0x47c(%rbp),%edi
mov    0x2c4a(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%eax
jne    13f2 <netutil_get_mac_addr+0x2a2>
mov    -0x30(%rbp),%eax
mov    0x2c5c(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     13ac <netutil_get_mac_addr+0x25c>
jmp    13f7 <netutil_get_mac_addr+0x2a7>
mov    -0x47c(%rbp),%edi
mov    0x2bff(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%eax
jne    13ed <netutil_get_mac_addr+0x29d>
movl   $0x1,-0x474(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    $0x6,%edx
call   1060 <memcpy@plt>
jmp    140e <netutil_get_mac_addr+0x2be>
jmp    13f2 <netutil_get_mac_addr+0x2a2>
jmp    13f7 <netutil_get_mac_addr+0x2a7>
mov    -0x488(%rbp),%rax
add    $0x28,%rax
mov    %rax,-0x488(%rbp)
jmp    134b <netutil_get_mac_addr+0x1fb>
jmp    1413 <netutil_get_mac_addr+0x2c3>
mov    -0x474(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x4a0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<netutil_get_mac_addr>:
sub    $0x438,%rsp
mov    0x2eba(%rip),%rax
mov    (%rax),%edi
mov    0x2ea1(%rip),%rax
mov    (%rax),%esi
mov    0x2ea0(%rip),%rax
mov    (%rax),%edx
call   1040 <socket@plt>
cmp    $0xffffffff,%eax
je     1173 <netutil_get_mac_addr+0x53>
movl   $0x400,0x8(%rsp)
lea    0x30(%rsp),%rcx
mov    %rcx,0x10(%rsp)
mov    0x2e63(%rip),%rcx
mov    (%rcx),%esi
lea    0x8(%rsp),%rdx
mov    %eax,%edi
call   1030 <ioctl@plt>
mov    $0xffffffff,%eax
add    $0x438,%rsp
ret

LLVM IR (-O0):
%struct.ifreq = type { i32, i8*, i32, %struct.TYPE_2__, i32, %struct.ifreq* }
%struct.TYPE_2__ = type { i32 }
%struct.ifconf = type { i32, i8*, i32, %struct.TYPE_2__, i32, %struct.ifconf* }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_IP = global i32 0
@SIOCGIFCONF = global i32 0
@SIOCGIFFLAGS = global i32 0
@IFF_LOOPBACK = global i32 0
@SIOCGIFHWADDR = global i32 0
define i32 @netutil_get_mac_addr(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca %struct.ifreq
%7 = alloca %struct.ifconf
%8 = alloca [1024 x i8]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.ifreq*
%13 = alloca %struct.ifreq*
store i8* %0, i8** %4
store i32* %1, i32** %5
store i32 -1, i32* %9
store i32 0, i32* %10
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_DGRAM
%16 = load i32, i32* @IPPROTO_IP
%17 = call i32 @socket(i32 %14, i32 %15, i32 %16)
store i32 %17, i32* %11
%18 = load i32, i32* %11
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %21
20: ; preds = %2
store i32 -1, i32* %3
br label %140
21: ; preds = %2
%22 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %7, i32 0, i32 0
store i32 1024, i32* %22
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%24 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %7, i32 0, i32 1
store i8* %23, i8** %24
%25 = load i32, i32* %11
%26 = load i32, i32* @SIOCGIFCONF
%27 = bitcast %struct.ifconf* %7 to %struct.ifreq*
%28 = call i32 @ioctl(i32 %25, i32 %26, %struct.ifreq* %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %31
30: ; preds = %21
store i32 -1, i32* %3
br label %140
31: ; preds = %21
%32 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %7, i32 0, i32 5
%33 = load %struct.ifconf*, %struct.ifconf** %32
%34 = bitcast %struct.ifconf* %33 to %struct.ifreq*
store %struct.ifreq* %34, %struct.ifreq** %12
%35 = load %struct.ifreq*, %struct.ifreq** %12
%36 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %7, i32 0, i32 0
%37 = load i32, i32* %36
%38 = sext i32 %37 to i64
%39 = udiv i64 %38, 40
%40 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %35, i64 %39
store %struct.ifreq* %40, %struct.ifreq** %13
br label %41
41: ; preds = %86, %31
%42 = load %struct.ifreq*, %struct.ifreq** %12
%43 = load %struct.ifreq*, %struct.ifreq** %13
%44 = icmp ne %struct.ifreq* %42, %43
br i1 %44, label %45, label %89
45: ; preds = %41
%46 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 4
%47 = load i32, i32* %46
%48 = load %struct.ifreq*, %struct.ifreq** %12
%49 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %48, i32 0, i32 4
%50 = load i32, i32* %49
%51 = call i32 @strcpy(i32 %47, i32 %50)
%52 = load i8*, i8** %4
%53 = icmp ne i8* %52, null
br i1 %53, label %54, label %85
54: ; preds = %45
%55 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 4
%56 = load i32, i32* %55
%57 = load i8*, i8** %4
%58 = call i64 @strcmp(i32 %56, i8* %57)
%59 = icmp eq i64 0, %58
br i1 %59, label %60, label %85
60: ; preds = %54
store i32 1, i32* %10
%61 = load i32, i32* %11
%62 = load i32, i32* @SIOCGIFFLAGS
%63 = call i32 @ioctl(i32 %61, i32 %62, %struct.ifreq* %6)
%64 = icmp eq i32 %63, 0
br i1 %64, label %65, label %84
65: ; preds = %60
%66 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 2
%67 = load i32, i32* %66
%68 = load i32, i32* @IFF_LOOPBACK
%69 = and i32 %67, %68
%70 = icmp ne i32 %69, 0
br i1 %70, label %71, label %72
71: ; preds = %65
br label %86
72: ; preds = %65
%73 = load i32, i32* %11
%74 = load i32, i32* @SIOCGIFHWADDR
%75 = call i32 @ioctl(i32 %73, i32 %74, %struct.ifreq* %6)
%76 = icmp eq i32 %75, 0
br i1 %76, label %77, label %83
77: ; preds = %72
store i32 0, i32* %9
%78 = load i32*, i32** %5
%79 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 3
%80 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %79, i32 0, i32 0
%81 = load i32, i32* %80
%82 = call i32 @memcpy(i32* %78, i32 %81, i32 6)
br label %83
83: ; preds = %77, %72
br label %84
84: ; preds = %83, %60
br label %89
85: ; preds = %54, %45
br label %86
86: ; preds = %85, %71
%87 = load %struct.ifreq*, %struct.ifreq** %12
%88 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %87, i32 1
store %struct.ifreq* %88, %struct.ifreq** %12
br label %41
89: ; preds = %84, %41
%90 = load i32, i32* %9
%91 = icmp eq i32 %90, -1
br i1 %91, label %92, label %138
92: ; preds = %89
%93 = load i32, i32* %10
%94 = icmp eq i32 %93, 0
br i1 %94, label %95, label %138
95: ; preds = %92
%96 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %7, i32 0, i32 5
%97 = load %struct.ifconf*, %struct.ifconf** %96
%98 = bitcast %struct.ifconf* %97 to %struct.ifreq*
store %struct.ifreq* %98, %struct.ifreq** %12
br label %99
99: ; preds = %134, %95
%100 = load %struct.ifreq*, %struct.ifreq** %12
%101 = load %struct.ifreq*, %struct.ifreq** %13
%102 = icmp ne %struct.ifreq* %100, %101
br i1 %102, label %103, label %137
103: ; preds = %99
%104 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 4
%105 = load i32, i32* %104
%106 = load %struct.ifreq*, %struct.ifreq** %12
%107 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %106, i32 0, i32 4
%108 = load i32, i32* %107
%109 = call i32 @strcpy(i32 %105, i32 %108)
%110 = load i32, i32* %11
%111 = load i32, i32* @SIOCGIFFLAGS
%112 = call i32 @ioctl(i32 %110, i32 %111, %struct.ifreq* %6)
%113 = icmp eq i32 %112, 0
br i1 %113, label %114, label %133
114: ; preds = %103
%115 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 2
%116 = load i32, i32* %115
%117 = load i32, i32* @IFF_LOOPBACK
%118 = and i32 %116, %117
%119 = icmp ne i32 %118, 0
br i1 %119, label %120, label %121
120: ; preds = %114
br label %134
121: ; preds = %114
%122 = load i32, i32* %11
%123 = load i32, i32* @SIOCGIFHWADDR
%124 = call i32 @ioctl(i32 %122, i32 %123, %struct.ifreq* %6)
%125 = icmp eq i32 %124, 0
br i1 %125, label %126, label %132
126: ; preds = %121
store i32 1, i32* %9
%127 = load i32*, i32** %5
%128 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 3
%129 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %128, i32 0, i32 0
%130 = load i32, i32* %129
%131 = call i32 @memcpy(i32* %127, i32 %130, i32 6)
br label %137
132: ; preds = %121
br label %133
133: ; preds = %132, %103
br label %134
134: ; preds = %133, %120
%135 = load %struct.ifreq*, %struct.ifreq** %12
%136 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %135, i32 1
store %struct.ifreq* %136, %struct.ifreq** %12
br label %99
137: ; preds = %126, %99
br label %138
138: ; preds = %137, %92, %89
%139 = load i32, i32* %9
store i32 %139, i32* %3
br label %140
140: ; preds = %138, %30, %20
%141 = load i32, i32* %3
ret i32 %141
}

LLVM IR (-O2):
%struct.ifconf = type { i32, i8*, i32, %struct.TYPE_2__, i32, %struct.ifconf* }
%struct.TYPE_2__ = type { i32 }
%struct.ifreq = type { i32, i8*, i32, %struct.TYPE_2__, i32, %struct.ifreq* }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_IP = global i32 0
@SIOCGIFCONF = global i32 0
@SIOCGIFFLAGS = global i32 0
@IFF_LOOPBACK = global i32 0
@SIOCGIFHWADDR = global i32 0
define i32 @netutil_get_mac_addr(i8* %0, i32* %1) {
%3 = alloca %struct.ifconf
%4 = alloca [1024 x i8]
%5 = bitcast %struct.ifconf* %3 to i8*
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_DGRAM
%9 = load i32, i32* @IPPROTO_IP
%10 = tail call i32 @socket(i32 %7, i32 %8, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %22, label %12
12: ; preds = %2
%13 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %3, i64 0, i32 0
store i32 1024, i32* %13
%14 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %3, i64 0, i32 1
store i8* %6, i8** %14
%15 = load i32, i32* @SIOCGIFCONF
%16 = bitcast %struct.ifconf* %3 to %struct.ifreq*
%17 = call i32 @ioctl(i32 %10, i32 %15, %struct.ifreq* %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %22, label %19
19: ; preds = %12
%20 = load i32, i32* %13
%21 = icmp ult i32 %20, 40
br label %22
22: ; preds = %19, %12, %2
ret i32 -1
}