#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 */
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 /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int ioctl(int, int /*<<< orphan*/, struct ifreq const *);
int /*<<< orphan*/ memcpy(unsigned char *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, int /*<<< orphan*/);
void hwaddr_mac(unsigned char *mac, size_t len) {
  struct ifreq ifr;
  struct ifconf ifc;
  char buf[1024];
  int success = 0;
  int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
  if (sock == -1) {
    fprintf(stderr, "Could not open socket gather hwaddr info\n");
    return;
  };
  ifc.ifc_len = sizeof(buf);
  ifc.ifc_buf = buf;
  if (ioctl(sock, SIOCGIFCONF, &ifc) == -1) {
    fprintf(stderr,
            "[IOCTL error] : Could not open socket gather hwaddr info\n");
    return;
  }
  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 (ioctl(sock, SIOCGIFFLAGS, &ifr) == 0) {
      if (!(ifr.ifr_flags & IFF_LOOPBACK)) {
        if (ioctl(sock, SIOCGIFHWADDR, &ifr) == 0) {
          success = 1;
          break;
        }
      }
    } else {
      fprintf(stderr, "Mac adress is not gathered\n");
      return;
    }
  }
  if (success)
    memcpy(mac, ifr.ifr_hwaddr.sa_data, 6);
}


x86 assembly (clang, -O0):
<hwaddr_mac>:
push   %rbp
mov    %rsp,%rbp
sub    $0x480,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x464(%rbp)
mov    0x2e6c(%rip),%rax
mov    (%rax),%edi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
mov    0x2e52(%rip),%rax
mov    (%rax),%edx
call   1070 <socket@plt>
mov    %eax,-0x468(%rbp)
cmpl   $0xffffffff,-0x468(%rbp)
jne    11ba <hwaddr_mac+0x6a>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
lea    0xe60(%rip),%rsi
call   1050 <fprintf@plt>
jmp    1330 <hwaddr_mac+0x1e0>
movl   $0x400,-0x60(%rbp)
lea    -0x460(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x468(%rbp),%edi
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
lea    -0x60(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    1207 <hwaddr_mac+0xb7>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
lea    0xe03(%rip),%rsi
call   1050 <fprintf@plt>
jmp    1330 <hwaddr_mac+0x1e0>
mov    -0x40(%rbp),%rax
mov    %rax,-0x470(%rbp)
mov    -0x470(%rbp),%rax
mov    %rax,-0x480(%rbp)
movslq -0x60(%rbp),%rax
mov    $0x28,%ecx
xor    %edx,%edx
div    %rcx
mov    %rax,%rcx
mov    -0x480(%rbp),%rax
imul   $0x28,%rcx,%rcx
add    %rcx,%rax
mov    %rax,-0x478(%rbp)
mov    -0x470(%rbp),%rax
cmp    -0x478(%rbp),%rax
je     1312 <hwaddr_mac+0x1c2>
mov    -0x20(%rbp),%edi
mov    -0x470(%rbp),%rax
mov    0x18(%rax),%esi
call   1030 <strcpy@plt>
mov    -0x468(%rbp),%edi
mov    0x2d4f(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%eax
jne    12dc <hwaddr_mac+0x18c>
mov    -0x28(%rbp),%eax
mov    0x2d61(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    12d7 <hwaddr_mac+0x187>
mov    -0x468(%rbp),%edi
mov    0x2d09(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%eax
jne    12d2 <hwaddr_mac+0x182>
movl   $0x1,-0x464(%rbp)
jmp    1312 <hwaddr_mac+0x1c2>
jmp    12d7 <hwaddr_mac+0x187>
jmp    12f6 <hwaddr_mac+0x1a6>
mov    0x2ccd(%rip),%rax
mov    (%rax),%edi
lea    0xd4e(%rip),%rsi
call   1050 <fprintf@plt>
jmp    1330 <hwaddr_mac+0x1e0>
jmp    12fb <hwaddr_mac+0x1ab>
mov    -0x470(%rbp),%rax
add    $0x28,%rax
mov    %rax,-0x470(%rbp)
jmp    1246 <hwaddr_mac+0xf6>
cmpl   $0x0,-0x464(%rbp)
je     1330 <hwaddr_mac+0x1e0>
mov    -0x8(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    $0x6,%edx
call   1060 <memcpy@plt>
add    $0x480,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hwaddr_mac>:
sub    $0x438,%rsp
mov    0x2eaa(%rip),%rax
mov    (%rax),%edi
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
mov    0x2e90(%rip),%rax
mov    (%rax),%edx
call   1050 <socket@plt>
cmp    $0xffffffff,%eax
je     1190 <hwaddr_mac+0x60>
movl   $0x400,0x8(%rsp)
lea    0x30(%rsp),%rcx
mov    %rcx,0x10(%rsp)
mov    0x2e4b(%rip),%rcx
mov    (%rcx),%esi
lea    0x8(%rsp),%rdx
mov    %eax,%edi
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
je     11ac <hwaddr_mac+0x7c>
add    $0x438,%rsp
ret
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
lea    0xe70(%rip),%rsi
add    $0x438,%rsp
jmp    1040 <fprintf@plt>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
lea    0xe44(%rip),%rsi
call   1040 <fprintf@plt>
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
@stderr = global i32 0
@.str = [42 x i8] c"Could not open socket gather hwaddr info\0A\00"
@SIOCGIFCONF = global i32 0
@.str.1 = [58 x i8] c"[IOCTL error] : Could not open socket gather hwaddr info\0A\00"
@SIOCGIFFLAGS = global i32 0
@IFF_LOOPBACK = global i32 0
@SIOCGIFHWADDR = global i32 0
@.str.2 = [28 x i8] c"Mac adress is not gathered\0A\00"
define void @hwaddr_mac(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca %struct.ifreq
%6 = alloca %struct.ifconf
%7 = alloca [1024 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.ifreq*
%11 = alloca %struct.ifreq*
store i8* %0, i8** %3
store i64 %1, i64* %4
store i32 0, i32* %8
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_DGRAM
%14 = load i32, i32* @IPPROTO_IP
%15 = call i32 @socket(i32 %12, i32 %13, i32 %14)
store i32 %15, i32* %9
%16 = load i32, i32* %9
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %2
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
br label %88
21: ; preds = %2
%22 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %6, i32 0, i32 0
store i32 1024, i32* %22
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%24 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %6, i32 0, i32 1
store i8* %23, i8** %24
%25 = load i32, i32* %9
%26 = load i32, i32* @SIOCGIFCONF
%27 = bitcast %struct.ifconf* %6 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 %33
30: ; preds = %21
%31 = load i32, i32* @stderr
%32 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.1, i64 0, i64 0))
br label %88
33: ; preds = %21
%34 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %6, i32 0, i32 5
%35 = load %struct.ifconf*, %struct.ifconf** %34
%36 = bitcast %struct.ifconf* %35 to %struct.ifreq*
store %struct.ifreq* %36, %struct.ifreq** %10
%37 = load %struct.ifreq*, %struct.ifreq** %10
%38 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %6, i32 0, i32 0
%39 = load i32, i32* %38
%40 = sext i32 %39 to i64
%41 = udiv i64 %40, 40
%42 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %37, i64 %41
store %struct.ifreq* %42, %struct.ifreq** %11
br label %43
43: ; preds = %76, %33
%44 = load %struct.ifreq*, %struct.ifreq** %10
%45 = load %struct.ifreq*, %struct.ifreq** %11
%46 = icmp ne %struct.ifreq* %44, %45
br i1 %46, label %47, label %79
47: ; preds = %43
%48 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 4
%49 = load i32, i32* %48
%50 = load %struct.ifreq*, %struct.ifreq** %10
%51 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %50, i32 0, i32 4
%52 = load i32, i32* %51
%53 = call i32 @strcpy(i32 %49, i32 %52)
%54 = load i32, i32* %9
%55 = load i32, i32* @SIOCGIFFLAGS
%56 = call i32 @ioctl(i32 %54, i32 %55, %struct.ifreq* %5)
%57 = icmp eq i32 %56, 0
br i1 %57, label %58, label %72
58: ; preds = %47
%59 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 2
%60 = load i32, i32* %59
%61 = load i32, i32* @IFF_LOOPBACK
%62 = and i32 %60, %61
%63 = icmp ne i32 %62, 0
br i1 %63, label %71, label %64
64: ; preds = %58
%65 = load i32, i32* %9
%66 = load i32, i32* @SIOCGIFHWADDR
%67 = call i32 @ioctl(i32 %65, i32 %66, %struct.ifreq* %5)
%68 = icmp eq i32 %67, 0
br i1 %68, label %69, label %70
69: ; preds = %64
store i32 1, i32* %8
br label %79
70: ; preds = %64
br label %71
71: ; preds = %70, %58
br label %75
72: ; preds = %47
%73 = load i32, i32* @stderr
%74 = call i32 @fprintf(i32 %73, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %88
75: ; preds = %71
br label %76
76: ; preds = %75
%77 = load %struct.ifreq*, %struct.ifreq** %10
%78 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %77, i32 1
store %struct.ifreq* %78, %struct.ifreq** %10
br label %43
79: ; preds = %69, %43
%80 = load i32, i32* %8
%81 = icmp ne i32 %80, 0
br i1 %81, label %82, label %88
82: ; preds = %79
%83 = load i8*, i8** %3
%84 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 3
%85 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %84, i32 0, i32 0
%86 = load i32, i32* %85
%87 = call i32 @memcpy(i8* %83, i32 %86, i32 6)
br label %88
88: ; preds = %18, %30, %72, %82, %79
ret void
}

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
@stderr = global i32 0
@.str = [42 x i8] c"Could not open socket gather hwaddr info\0A\00"
@SIOCGIFCONF = global i32 0
@.str.1 = [58 x i8] c"[IOCTL error] : Could not open socket gather hwaddr info\0A\00"
@SIOCGIFFLAGS = global i32 0
@IFF_LOOPBACK = global i32 0
@SIOCGIFHWADDR = global i32 0
define void @hwaddr_mac(i8* %0, i64 %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 %12, label %15
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
br label %28
15: ; preds = %2
%16 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %3, i64 0, i32 0
store i32 1024, i32* %16
%17 = getelementptr inbounds %struct.ifconf, %struct.ifconf* %3, i64 0, i32 1
store i8* %6, i8** %17
%18 = load i32, i32* @SIOCGIFCONF
%19 = bitcast %struct.ifconf* %3 to %struct.ifreq*
%20 = call i32 @ioctl(i32 %10, i32 %18, %struct.ifreq* %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %15
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.1, i64 0, i64 0))
br label %28
25: ; preds = %15
%26 = load i32, i32* %16
%27 = icmp ult i32 %26, 40
br label %28
28: ; preds = %25, %22, %12
ret void
}