#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*/ ifrn_name;
};
struct ifreq {
  TYPE_2__ ifr_hwaddr;
  TYPE_1__ ifr_ifrn;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SIOCGIFHWADDR;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
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 const *, int);
int get_mac_addr(const char *ifname, char *mac) {
  int err = 0;
  int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  struct ifreq req;
  if (sockfd == -1) {
    err = -1;
    goto exit;
  }
  strncpy(req.ifr_ifrn.ifrn_name, ifname, sizeof(req.ifr_ifrn.ifrn_name));
  err = ioctl(sockfd, SIOCGIFHWADDR, &req);
  if (err == 0) {
    snprintf(mac, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
             (unsigned char)req.ifr_hwaddr.sa_data[0],
             (unsigned char)req.ifr_hwaddr.sa_data[1],
             (unsigned char)req.ifr_hwaddr.sa_data[2],
             (unsigned char)req.ifr_hwaddr.sa_data[3],
             (unsigned char)req.ifr_hwaddr.sa_data[4],
             (unsigned char)req.ifr_hwaddr.sa_data[5]);
  }
exit:
  if (sockfd > 0)
    close(sockfd);
  return err;
}


x86 assembly (clang, -O0):
<get_mac_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%edi
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1199 <get_mac_addr+0x49>
movl   $0xffffffff,-0x14(%rbp)
jmp    1246 <get_mac_addr+0xf6>
mov    -0x20(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%edi
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1241 <get_mac_addr+0xf1>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    %al,%cl
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rax
mov    %al,%r8b
mov    -0x28(%rbp),%rax
mov    0x10(%rax),%rax
mov    %al,%r9b
mov    -0x28(%rbp),%rax
mov    0x18(%rax),%rax
mov    %al,%r11b
mov    -0x28(%rbp),%rax
mov    0x20(%rax),%rax
mov    %al,%r10b
mov    -0x28(%rbp),%rax
mov    0x28(%rax),%rax
mov    $0x12,%esi
lea    0xde7(%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 %al,%eax
mov    %eax,0x10(%rsp)
call   1040 <snprintf@plt>
jmp    1246 <get_mac_addr+0xf6>
cmpl   $0x0,-0x18(%rbp)
jle    1258 <get_mac_addr+0x108>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x14(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_mac_addr>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbp
mov    0x2e83(%rip),%rax
mov    (%rax),%edi
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
cmp    $0xffffffff,%eax
je     11aa <get_mac_addr+0x5a>
mov    %eax,%ebx
mov    %rbp,%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1050 <ioctl@plt>
mov    %eax,%ebp
test   %eax,%eax
je     11b1 <get_mac_addr+0x61>
test   %ebx,%ebx
jg     11f5 <get_mac_addr+0xa5>
jmp    11fc <get_mac_addr+0xac>
mov    $0xffffffff,%ebp
jmp    11fc <get_mac_addr+0xac>
mov    (%rsp),%rax
movzbl (%rax),%ecx
movzbl 0x8(%rax),%r8d
movzbl 0x10(%rax),%r9d
movzbl 0x18(%rax),%r10d
movzbl 0x20(%rax),%r11d
movzbl 0x28(%rax),%eax
sub    $0x8,%rsp
lea    0xe25(%rip),%rdx
mov    %r14,%rdi
mov    $0x12,%esi
push   %rax
push   %r11
push   %r10
call   1040 <snprintf@plt>
add    $0x20,%rsp
test   %ebx,%ebx
jle    11fc <get_mac_addr+0xac>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { %struct.TYPE_4__, %struct.TYPE_3__ }
%struct.TYPE_4__ = type { i64* }
%struct.TYPE_3__ = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFHWADDR = global i32 0
@.str = [30 x i8] c"%02X:%02X:%02X:%02X:%02X:%02X\00"
define i32 @get_mac_addr(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.ifreq
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 0, i32* %5
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_DGRAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 -1, i32* %5
br label %65
14: ; preds = %2
%15 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 1
%16 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %15, i32 0, i32 0
%17 = load i32, i32* %16
%18 = load i8*, i8** %3
%19 = call i32 @strncpy(i32 %17, i8* %18, i32 4)
%20 = load i32, i32* %6
%21 = load i32, i32* @SIOCGIFHWADDR
%22 = call i32 @ioctl(i32 %20, i32 %21, %struct.ifreq* %7)
store i32 %22, i32* %5
%23 = load i32, i32* %5
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %64
25: ; preds = %14
%26 = load i8*, i8** %4
%27 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 0
%28 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %27, i32 0, i32 0
%29 = load i64*, i64** %28
%30 = getelementptr inbounds i64, i64* %29, i64 0
%31 = load i64, i64* %30
%32 = trunc i64 %31 to i8
%33 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 0
%34 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %33, i32 0, i32 0
%35 = load i64*, i64** %34
%36 = getelementptr inbounds i64, i64* %35, i64 1
%37 = load i64, i64* %36
%38 = trunc i64 %37 to i8
%39 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 0
%40 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %39, i32 0, i32 0
%41 = load i64*, i64** %40
%42 = getelementptr inbounds i64, i64* %41, i64 2
%43 = load i64, i64* %42
%44 = trunc i64 %43 to i8
%45 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 0
%46 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %45, i32 0, i32 0
%47 = load i64*, i64** %46
%48 = getelementptr inbounds i64, i64* %47, i64 3
%49 = load i64, i64* %48
%50 = trunc i64 %49 to i8
%51 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 0
%52 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %51, i32 0, i32 0
%53 = load i64*, i64** %52
%54 = getelementptr inbounds i64, i64* %53, i64 4
%55 = load i64, i64* %54
%56 = trunc i64 %55 to i8
%57 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 0
%58 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %57, i32 0, i32 0
%59 = load i64*, i64** %58
%60 = getelementptr inbounds i64, i64* %59, i64 5
%61 = load i64, i64* %60
%62 = trunc i64 %61 to i8
%63 = call i32 @snprintf(i8* %26, i32 18, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8 zeroext %32, i8 zeroext %38, i8 zeroext %44, i8 zeroext %50, i8 zeroext %56, i8 zeroext %62)
br label %64
64: ; preds = %25, %14
br label %65
65: ; preds = %64, %13
%66 = load i32, i32* %6
%67 = icmp sgt i32 %66, 0
br i1 %67, label %68, label %71
68: ; preds = %65
%69 = load i32, i32* %6
%70 = call i32 @close(i32 %69)
br label %71
71: ; preds = %68, %65
%72 = load i32, i32* %5
ret i32 %72
}

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