#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__ {
  scalar_t__ *sa_data;
};
struct ifreq {
  TYPE_1__ ifr_hwaddr;
  int /*<<< orphan*/ ifr_name;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SIOCGIFHWADDR;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(struct ifreq *, int);
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ printf(char *, char const *);
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_local_mac(const char *eth_inf, char *mac) {
  struct ifreq ifr;
  int sd;
  bzero(&ifr, sizeof(struct ifreq));
  if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    printf("get %s mac address socket creat error\n", eth_inf);
    return -1;
  }
  strncpy(ifr.ifr_name, eth_inf, sizeof(ifr.ifr_name) - 1);
  if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0) {
    printf("get %s mac address error\n", eth_inf);
    close(sd);
    return -1;
  }
  snprintf(mac, 18, "%02x:%02x:%02x:%02x:%02x:%02x",
           (unsigned char)ifr.ifr_hwaddr.sa_data[0],
           (unsigned char)ifr.ifr_hwaddr.sa_data[1],
           (unsigned char)ifr.ifr_hwaddr.sa_data[2],
           (unsigned char)ifr.ifr_hwaddr.sa_data[3],
           (unsigned char)ifr.ifr_hwaddr.sa_data[4],
           (unsigned char)ifr.ifr_hwaddr.sa_data[5]);
  close(sd);
  return 0;
}


x86 assembly (clang, -O0):
<get_local_mac>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x28(%rbp),%rdi
mov    $0x10,%esi
call   1080 <bzero@plt>
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    11cf <get_local_mac+0x5f>
mov    -0x10(%rbp),%rsi
lea    0xe42(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a7 <get_local_mac+0x137>
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    -0x2c(%rbp),%edi
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    1223 <get_local_mac+0xb3>
mov    -0x10(%rbp),%rsi
lea    0xe1d(%rip),%rdi
call   1040 <printf@plt>
mov    -0x2c(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a7 <get_local_mac+0x137>
mov    -0x18(%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    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 %al,%eax
mov    %eax,0x10(%rsp)
call   1050 <snprintf@plt>
mov    -0x2c(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_local_mac>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsp,%rdi
mov    $0x10,%esi
call   1080 <bzero@plt>
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
test   %eax,%eax
js     121f <get_local_mac+0xaf>
mov    %eax,%ebx
mov    0x8(%rsp),%edi
mov    %r15,%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1060 <ioctl@plt>
test   %rax,%rax
js     1230 <get_local_mac+0xc0>
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    0xe43(%rip),%rdx
mov    %r14,%rdi
mov    $0x12,%esi
push   %rax
push   %r11
push   %r10
call   1050 <snprintf@plt>
add    $0x20,%rsp
mov    %ebx,%edi
call   1070 <close@plt>
xor    %eax,%eax
jmp    124b <get_local_mac+0xdb>
lea    0xdda(%rip),%rdi
mov    %r15,%rsi
call   1040 <printf@plt>
jmp    1246 <get_local_mac+0xd6>
lea    0xdf0(%rip),%rdi
mov    %r15,%rsi
call   1040 <printf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i64* }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [39 x i8] c"get %s mac address socket creat error\0A\00"
@SIOCGIFHWADDR = global i32 0
@.str.1 = [26 x i8] c"get %s mac address error\0A\00"
@.str.2 = [30 x i8] c"%02x:%02x:%02x:%02x:%02x:%02x\00"
define i32 @get_local_mac(i8* %0, i8* %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = call i32 @bzero(%struct.ifreq* %3, i32 16)
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %44
12: ; preds = %2
%13 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%14 = load i32, i32* %13
%15 = tail call i32 @strncpy(i32 %14, i8* %0, i32 3)
%16 = load i32, i32* @SIOCGIFHWADDR
%17 = call i64 @ioctl(i32 %8, i32 %16, %struct.ifreq* %3)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %12
%20 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%21 = call i32 @close(i32 %8)
br label %44
22: ; preds = %12
%23 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0, i32 0
%24 = load i64*, i64** %23
%25 = load i64, i64* %24
%26 = trunc i64 %25 to i8
%27 = getelementptr inbounds i64, i64* %24, i64 1
%28 = load i64, i64* %27
%29 = trunc i64 %28 to i8
%30 = getelementptr inbounds i64, i64* %24, i64 2
%31 = load i64, i64* %30
%32 = trunc i64 %31 to i8
%33 = getelementptr inbounds i64, i64* %24, i64 3
%34 = load i64, i64* %33
%35 = trunc i64 %34 to i8
%36 = getelementptr inbounds i64, i64* %24, i64 4
%37 = load i64, i64* %36
%38 = trunc i64 %37 to i8
%39 = getelementptr inbounds i64, i64* %24, i64 5
%40 = load i64, i64* %39
%41 = trunc i64 %40 to i8
%42 = call i32 @snprintf(i8* %1, i32 18, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %26, i8 zeroext %29, i8 zeroext %32, i8 zeroext %35, i8 zeroext %38, i8 zeroext %41)
%43 = call i32 @close(i32 %8)
br label %44
44: ; preds = %22, %19, %10
%45 = phi i32 [ -1, %10 ], [ -1, %19 ], [ 0, %22 ]
ret i32 %45
}