#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_4__ {
  scalar_t__ sa_data;
};
struct TYPE_3__ {
  int /*<<< orphan*/ sa_family;
};
struct ifreq {
  TYPE_2__ ifr_hwaddr;
  int /*<<< orphan*/ ifr_name;
  TYPE_1__ ifr_addr;
};
typedef int /*<<< orphan*/ ifr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFHWADDR;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned char, unsigned char,
                            unsigned char, unsigned char, unsigned char,
                            unsigned char);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, scalar_t__);
int get_mac(char *name, char *str_mac) {
  int fd;
  struct ifreq ifr;
  unsigned char *mac = NULL;
  memset(&ifr, 0, sizeof(ifr));
  fd = socket(AF_INET, SOCK_DGRAM, 0);
  ifr.ifr_addr.sa_family = AF_INET;
  strncpy(ifr.ifr_name, name, IFNAMSIZ - 1);
  if (0 == ioctl(fd, SIOCGIFHWADDR, &ifr)) {
    mac = (unsigned char *)ifr.ifr_hwaddr.sa_data;
    snprintf(str_mac, 17, "%.2X%.2X%.2X%.2X%.2X%.2X", mac[0], mac[1], mac[2],
             mac[3], mac[4], mac[5]);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<get_mac>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x30(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    0x2e0a(%rip),%rax
mov    (%rax),%rdx
sub    $0x1,%rdx
call   1030 <strncpy@plt>
mov    -0x14(%rbp),%edi
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    1258 <get_mac+0xf8>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    (%rax),%cl
mov    -0x30(%rbp),%rax
mov    0x1(%rax),%r8b
mov    -0x30(%rbp),%rax
mov    0x2(%rax),%r9b
mov    -0x30(%rbp),%rax
mov    0x3(%rax),%r11b
mov    -0x30(%rbp),%rax
mov    0x4(%rax),%r10b
mov    -0x30(%rbp),%rax
mov    $0x11,%esi
lea    0xdd1(%rip),%rdx
movzbl %cl,%ecx
movzbl %r8b,%r8d
movzbl %r9b,%r9d
movzbl %r11b,%r11d
mov    %r11d,(%rsp)
movzbl %r10b,%r10d
mov    %r10d,0x8(%rsp)
movzbl 0x5(%rax),%eax
mov    %eax,0x10(%rsp)
call   1040 <snprintf@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_mac>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%r12
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2e5d(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebp
mov    (%rbx),%eax
mov    %eax,0xc(%rsp)
mov    0x8(%rsp),%edi
mov    0x2e18(%rip),%rax
mov    (%rax),%rdx
add    $0xffffffffffffffff,%rdx
mov    %r12,%rsi
call   1030 <strncpy@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r15,%rdx
call   1060 <ioctl@plt>
test   %rax,%rax
jne    1215 <get_mac+0xb5>
mov    (%rsp),%rax
movzbl (%rax),%ecx
movzbl 0x1(%rax),%r8d
movzbl 0x2(%rax),%r9d
movzbl 0x3(%rax),%r10d
movzbl 0x4(%rax),%ebx
movzbl 0x5(%rax),%eax
sub    $0x8,%rsp
lea    0xe00(%rip),%rdx
mov    %r14,%rdi
mov    $0x11,%esi
push   %rax
push   %rbx
push   %r10
call   1040 <snprintf@plt>
add    $0x20,%rsp
mov    %ebp,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { %struct.TYPE_4__, i32, %struct.TYPE_3__ }
%struct.TYPE_4__ = type { i64 }
%struct.TYPE_3__ = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IFNAMSIZ = global i64 0
@SIOCGIFHWADDR = global i32 0
@.str = [25 x i8] c"%.2X%.2X%.2X%.2X%.2X%.2X\00"
define i32 @get_mac(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.ifreq
%7 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
store i8* null, i8** %7
%8 = call i32 @memset(%struct.ifreq* %6, i32 0, i32 16)
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_DGRAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %5
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 2
%14 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %13, i32 0, i32 0
store i32 %12, i32* %14
%15 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%16 = load i32, i32* %15
%17 = load i8*, i8** %3
%18 = load i64, i64* @IFNAMSIZ
%19 = sub nsw i64 %18, 1
%20 = call i32 @strncpy(i32 %16, i8* %17, i64 %19)
%21 = load i32, i32* %5
%22 = load i32, i32* @SIOCGIFHWADDR
%23 = call i64 @ioctl(i32 %21, i32 %22, %struct.ifreq* %6)
%24 = icmp eq i64 0, %23
br i1 %24, label %25, label %50
25: ; preds = %2
%26 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%27 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %26, i32 0, i32 0
%28 = load i64, i64* %27
%29 = inttoptr i64 %28 to i8*
store i8* %29, i8** %7
%30 = load i8*, i8** %4
%31 = load i8*, i8** %7
%32 = getelementptr inbounds i8, i8* %31, i64 0
%33 = load i8, i8* %32
%34 = load i8*, i8** %7
%35 = getelementptr inbounds i8, i8* %34, i64 1
%36 = load i8, i8* %35
%37 = load i8*, i8** %7
%38 = getelementptr inbounds i8, i8* %37, i64 2
%39 = load i8, i8* %38
%40 = load i8*, i8** %7
%41 = getelementptr inbounds i8, i8* %40, i64 3
%42 = load i8, i8* %41
%43 = load i8*, i8** %7
%44 = getelementptr inbounds i8, i8* %43, i64 4
%45 = load i8, i8* %44
%46 = load i8*, i8** %7
%47 = getelementptr inbounds i8, i8* %46, i64 5
%48 = load i8, i8* %47
%49 = call i32 @snprintf(i8* %30, i32 17, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8 zeroext %33, i8 zeroext %36, i8 zeroext %39, i8 zeroext %42, i8 zeroext %45, i8 zeroext %48)
br label %50
50: ; preds = %25, %2
%51 = load i32, i32* %5
%52 = call i32 @close(i32 %51)
ret i32 0
}

LLVM IR (-O2):
%struct.ifreq = type { %struct.TYPE_4__, i32, %struct.TYPE_3__ }
%struct.TYPE_4__ = type { i64 }
%struct.TYPE_3__ = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IFNAMSIZ = global i64 0
@SIOCGIFHWADDR = global i32 0
@.str = [25 x i8] c"%.2X%.2X%.2X%.2X%.2X%.2X\00"
define i32 @get_mac(i8* %0, i8* %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = call i32 @memset(%struct.ifreq* %3, i32 0, i32 16)
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_DGRAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 2, i32 0
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%12 = load i32, i32* %11
%13 = load i64, i64* @IFNAMSIZ
%14 = add nsw i64 %13, -1
%15 = call i32 @strncpy(i32 %12, i8* %0, i64 %14)
%16 = load i32, i32* @SIOCGIFHWADDR
%17 = call i64 @ioctl(i32 %8, i32 %16, %struct.ifreq* %3)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %35
19: ; preds = %2
%20 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0, i32 0
%21 = load i64, i64* %20
%22 = inttoptr i64 %21 to i8*
%23 = load i8, i8* %22
%24 = getelementptr inbounds i8, i8* %22, i64 1
%25 = load i8, i8* %24
%26 = getelementptr inbounds i8, i8* %22, i64 2
%27 = load i8, i8* %26
%28 = getelementptr inbounds i8, i8* %22, i64 3
%29 = load i8, i8* %28
%30 = getelementptr inbounds i8, i8* %22, i64 4
%31 = load i8, i8* %30
%32 = getelementptr inbounds i8, i8* %22, i64 5
%33 = load i8, i8* %32
%34 = call i32 @snprintf(i8* %1, i32 17, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8 zeroext %23, i8 zeroext %25, i8 zeroext %27, i8 zeroext %29, i8 zeroext %31, i8 zeroext %33)
br label %35
35: ; preds = %19, %2
%36 = call i32 @close(i32 %8)
ret i32 0
}