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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFHWADDR;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int errno;
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memcpy(void *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(char *, char *, int /*<<< orphan*/);
int get_mac(char *name, void *buf) {
  struct ifreq ifr;
  int ret = 0;
  int s;
  if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    perror("socket");
    return errno;
  }
  strncpy(ifr.ifr_name, name, IFNAMSIZ);
  if ((ret = ioctl(s, SIOCGIFHWADDR, &ifr)) < 0)
    perror(ifr.ifr_name);
  close(s);
  memcpy(buf, &ifr.ifr_hwaddr.sa_data, 6);
  return ret;
}


x86 assembly (clang, -O0):
<get_mac>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%edi
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x30(%rbp)
cmp    $0x0,%eax
jge    11b9 <get_mac+0x59>
lea    0xe5d(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e11(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    121d <get_mac+0xbd>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2e00(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x30(%rbp),%edi
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    11f9 <get_mac+0x99>
mov    -0x28(%rbp),%rdi
call   1070 <perror@plt>
mov    -0x30(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
add    $0x8,%rsi
mov    $0x6,%edx
call   1060 <memcpy@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_mac>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e73(%rip),%rax
mov    (%rax),%edi
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
test   %eax,%eax
js     11db <get_mac+0x7b>
mov    %eax,%ebp
mov    0x2e34(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rsi
call   1030 <strncpy@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11f2 <get_mac+0x92>
mov    %ebp,%edi
call   1050 <close@plt>
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
mov    $0x6,%edx
call   1060 <memcpy@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe1e(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dd2(%rip),%rax
mov    (%rax),%ebx
jmp    11d0 <get_mac+0x70>
mov    (%rsp),%rdi
call   1070 <perror@plt>
jmp    11b7 <get_mac+0x57>

LLVM IR (-O0):
%struct.ifreq = type { i8*, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [7 x i8] c"socket\00"
@errno = global i32 0
@IFNAMSIZ = global i32 0
@SIOCGIFHWADDR = global i32 0
define i32 @get_mac(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.ifreq
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 0, i32* %7
%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* %8
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @errno
store i32 %15, i32* %3
br label %38
16: ; preds = %2
%17 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%18 = load i8*, i8** %17
%19 = load i8*, i8** %4
%20 = load i32, i32* @IFNAMSIZ
%21 = call i32 @strncpy(i8* %18, i8* %19, i32 %20)
%22 = load i32, i32* %8
%23 = load i32, i32* @SIOCGIFHWADDR
%24 = call i32 @ioctl(i32 %22, i32 %23, %struct.ifreq* %6)
store i32 %24, i32* %7
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %16
%27 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%28 = load i8*, i8** %27
%29 = call i32 @perror(i8* %28)
br label %30
30: ; preds = %26, %16
%31 = load i32, i32* %8
%32 = call i32 @close(i32 %31)
%33 = load i8*, i8** %5
%34 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%35 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %34, i32 0, i32 0
%36 = call i32 @memcpy(i8* %33, i32* %35, i32 6)
%37 = load i32, i32* %7
store i32 %37, i32* %3
br label %38
38: ; preds = %30, %13
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
%struct.ifreq = type { i8*, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [7 x i8] c"socket\00"
@errno = global i32 0
@IFNAMSIZ = global i32 0
@SIOCGIFHWADDR = global i32 0
define i32 @get_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_DGRAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @errno
br label %26
12: ; preds = %2
%13 = load i32, i32* @IFNAMSIZ
%14 = tail call i32 @strncpy(i8* undef, i8* %0, i32 %13)
%15 = load i32, i32* @SIOCGIFHWADDR
%16 = call i32 @ioctl(i32 %7, i32 %15, %struct.ifreq* %3)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %12
%19 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
%20 = load i8*, i8** %19
%21 = call i32 @perror(i8* %20)
br label %22
22: ; preds = %18, %12
%23 = call i32 @close(i32 %7)
%24 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1, i32 0
%25 = call i32 @memcpy(i8* %1, i32* %24, i32 6)
br label %26
26: ; preds = %22, %9
%27 = phi i32 [ %11, %9 ], [ %16, %22 ]
ret i32 %27
}