#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*/ tmp;
struct TYPE_2__ {
  int /*<<< orphan*/ 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*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memcpy(unsigned char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, int);
int get_interface_mac(const char *device, unsigned char mac[6]) {
  struct ifreq tmp;
  int fd;
  fd = socket(AF_INET, SOCK_STREAM, 0);
  if (fd == -1) {
    perror("create socket fail\n");
    return -1;
  }
  memset(&tmp, 0, sizeof(tmp));
  strncpy(tmp.ifr_name, device, sizeof(tmp.ifr_name) - 1);
  if ((ioctl(fd, SIOCGIFHWADDR, &tmp)) < 0) {
    printf("ioctl");
    close(fd);
    return -1;
  }
  close(fd);
  memcpy(mac, tmp.ifr_hwaddr.sa_data, 6);
  return 0;
}


x86 assembly (clang, -O0):
<get_interface_mac>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11ce <get_interface_mac+0x4e>
lea    0xe43(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    124e <get_interface_mac+0xce>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    -0x24(%rbp),%edi
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    122e <get_interface_mac+0xae>
lea    0xdff(%rip),%rdi
call   1040 <printf@plt>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    124e <get_interface_mac+0xce>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    $0x6,%edx
call   1080 <memcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_interface_mac>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbp
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     1239 <get_interface_mac+0xb9>
mov    %eax,%ebx
lea    0x8(%rsp),%r12
xor    %r15d,%r15d
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0xc(%rsp),%edi
mov    %rbp,%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r12,%rdx
call   1060 <ioctl@plt>
test   %rax,%rax
js     1210 <get_interface_mac+0x90>
mov    %ebx,%edi
call   1070 <close@plt>
mov    0x8(%rsp),%esi
mov    %r14,%rdi
mov    $0x6,%edx
call   1080 <memcpy@plt>
jmp    1229 <get_interface_mac+0xa9>
lea    0xdfd(%rip),%rdi
call   1040 <printf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdc0(%rip),%rdi
call   1090 <perror@plt>
jmp    1223 <get_interface_mac+0xa3>

LLVM IR (-O0):
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [20 x i8] c"create socket fail\0A\00"
@SIOCGIFHWADDR = global i32 0
@.str.1 = [6 x i8] c"ioctl\00"
define i32 @get_interface_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 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %37
15: ; preds = %2
%16 = call i32 @memset(%struct.ifreq* %6, i32 0, i32 8)
%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 %29
25: ; preds = %15
%26 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%27 = load i32, i32* %7
%28 = call i32 @close(i32 %27)
store i32 -1, i32* %3
br label %37
29: ; preds = %15
%30 = load i32, i32* %7
%31 = call i32 @close(i32 %30)
%32 = load i8*, i8** %5
%33 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%34 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 @memcpy(i8* %32, i32 %35, i32 6)
store i32 0, i32* %3
br label %37
37: ; preds = %29, %25, %13
%38 = load i32, i32* %3
ret i32 %38
}

LLVM IR (-O2):
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [20 x i8] c"create socket fail\0A\00"
@SIOCGIFHWADDR = global i32 0
@.str.1 = [6 x i8] c"ioctl\00"
define i32 @get_interface_mac(i8* %0, i8* %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %27
11: ; preds = %2
%12 = call i32 @memset(%struct.ifreq* %3, i32 0, i32 8)
%13 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%14 = load i32, i32* %13
%15 = call i32 @strncpy(i32 %14, i8* %0, i32 3)
%16 = load i32, i32* @SIOCGIFHWADDR
%17 = call i64 @ioctl(i32 %7, i32 %16, %struct.ifreq* %3)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %11
%20 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @close(i32 %7)
br label %27
22: ; preds = %11
%23 = call i32 @close(i32 %7)
%24 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0, i32 0
%25 = load i32, i32* %24
%26 = call i32 @memcpy(i8* %1, i32 %25, i32 6)
br label %27
27: ; preds = %22, %19, %9
%28 = phi i32 [ -1, %9 ], [ -1, %19 ], [ 0, %22 ]
ret i32 %28
}