#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*/ u_char;
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*/ ETH_ALEN;
int /*<<< orphan*/ SIOCGIFHWADDR;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
int get_mac(const char *device_name, u_char *addr) {
  struct ifreq req;
  int err, i;
  int s = socket(AF_INET, SOCK_DGRAM, 0);
  strcpy(req.ifr_name, device_name);
  err = ioctl(s, SIOCGIFHWADDR, &req);
  close(s);
  if (err != -1) {
    memcpy(addr, req.ifr_hwaddr.sa_data, ETH_ALEN);
    return 0;
  }
  return -1;
}


x86 assembly (clang, -O0):
<get_mac>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e81(%rip),%rax
mov    (%rax),%edi
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x2c(%rbp),%edi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x24(%rbp)
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
cmpl   $0xffffffff,-0x24(%rbp)
je     11d3 <get_mac+0x83>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    0x2e20(%rip),%rax
mov    (%rax),%edx
call   1060 <memcpy@plt>
movl   $0x0,-0x4(%rbp)
jmp    11da <get_mac+0x8a>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_mac>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e84(%rip),%rax
mov    (%rax),%edi
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
xor    %r15d,%r15d
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,%ebp
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1050 <close@plt>
cmp    $0xffffffff,%ebx
je     11ba <get_mac+0x6a>
mov    (%rsp),%esi
mov    0x2e32(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
call   1060 <memcpy@plt>
jmp    11c0 <get_mac+0x70>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFHWADDR = global i32 0
@ETH_ALEN = global i32 0
define i32 @get_mac(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca %struct.ifreq
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_DGRAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %9
%13 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%14 = load i32, i32* %13
%15 = load i8*, i8** %4
%16 = call i32 @strcpy(i32 %14, i8* %15)
%17 = load i32, i32* %9
%18 = load i32, i32* @SIOCGIFHWADDR
%19 = call i32 @ioctl(i32 %17, i32 %18, %struct.ifreq* %6)
store i32 %19, i32* %7
%20 = load i32, i32* %9
%21 = call i32 @close(i32 %20)
%22 = load i32, i32* %7
%23 = icmp ne i32 %22, -1
br i1 %23, label %24, label %31
24: ; preds = %2
%25 = load i32*, i32** %5
%26 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%27 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %26, i32 0, i32 0
%28 = load i32, i32* %27
%29 = load i32, i32* @ETH_ALEN
%30 = call i32 @memcpy(i32* %25, i32 %28, i32 %29)
store i32 0, i32* %3
br label %32
31: ; preds = %2
store i32 -1, i32* %3
br label %32
32: ; preds = %31, %24
%33 = load i32, i32* %3
ret i32 %33
}

LLVM IR (-O2):
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFHWADDR = global i32 0
@ETH_ALEN = global i32 0
define i32 @get_mac(i8* %0, i32* %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_DGRAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = tail call i32 @strcpy(i32 undef, i8* %0)
%9 = load i32, i32* @SIOCGIFHWADDR
%10 = call i32 @ioctl(i32 %7, i32 %9, %struct.ifreq* %3)
%11 = call i32 @close(i32 %7)
%12 = icmp eq i32 %10, -1
br i1 %12, label %18, label %13
13: ; preds = %2
%14 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0, i32 0
%15 = load i32, i32* %14
%16 = load i32, i32* @ETH_ALEN
%17 = call i32 @memcpy(i32* %1, i32 %15, i32 %16)
br label %18
18: ; preds = %2, %13
%19 = phi i32 [ 0, %13 ], [ -1, %2 ]
ret i32 %19
}