#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_3__ {
  scalar_t__ sa_data;
};
struct TYPE_4__ {
  TYPE_1__ ifru_hwaddr;
};
struct ifreq {
  TYPE_2__ ifr_ifru;
  int /*<<< orphan*/ ifr_name;
};

/* Variables and functions */
scalar_t__ ETH_ALEN;
scalar_t__ IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFHWADDR;
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
scalar_t__ malloc(scalar_t__);
int /*<<< orphan*/ memcpy(void *, void *, scalar_t__);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, unsigned char *, scalar_t__);
unsigned char *get_mac(int sock, unsigned char *interface) {
  struct ifreq ifreq_c;
  memset(&ifreq_c, 0, sizeof(struct ifreq));
  strncpy(ifreq_c.ifr_name, interface, IFNAMSIZ - 1);
  if ((ioctl(sock, SIOCGIFHWADDR, &ifreq_c)) < 0) {
    printf("error in mac address ioctl reading\n");
    return NULL;
  }
  unsigned char *mac = (unsigned char *)malloc(ETH_ALEN + 1);
  if (mac == NULL) {
    printf("error malloc mac buffer\n");
    return NULL;
  }
  memcpy((void *)mac, (void *)(ifreq_c.ifr_ifru.ifru_hwaddr.sa_data), ETH_ALEN);
  return mac;
}


x86 assembly (clang, -O0):
<get_mac>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    0x2e43(%rip),%rax
mov    (%rax),%rdx
sub    $0x1,%rdx
call   1030 <strncpy@plt>
mov    -0xc(%rbp),%edi
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    11d1 <get_mac+0x71>
lea    0xe41(%rip),%rdi
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    122b <get_mac+0xcb>
mov    0x2e10(%rip),%rax
mov    (%rax),%rdi
add    $0x1,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    120c <get_mac+0xac>
lea    0xe2a(%rip),%rdi
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    122b <get_mac+0xcb>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    0x2dcd(%rip),%rax
mov    (%rax),%rdx
call   1070 <memcpy@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_mac>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x8(%rsp),%edi
mov    0x2e46(%rip),%rax
mov    (%rax),%rdx
add    $0xffffffffffffffff,%rdx
mov    %rbx,%rsi
call   1030 <strncpy@plt>
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r14,%rdx
call   1060 <ioctl@plt>
test   %rax,%rax
js     11df <get_mac+0x7f>
mov    0x2e30(%rip),%rbp
mov    0x0(%rbp),%rdi
add    $0x1,%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     11e8 <get_mac+0x88>
mov    %rax,%rbx
mov    (%rsp),%rsi
mov    0x0(%rbp),%rdx
mov    %rax,%rdi
call   1070 <memcpy@plt>
jmp    11f6 <get_mac+0x96>
lea    0xe32(%rip),%rdi
jmp    11ef <get_mac+0x8f>
lea    0xe11(%rip),%rdi
call   1040 <puts@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { %struct.TYPE_4__, i32 }
%struct.TYPE_4__ = type { %struct.TYPE_3__ }
%struct.TYPE_3__ = type { i64 }
@IFNAMSIZ = global i64 0
@SIOCGIFHWADDR = global i32 0
@.str = [36 x i8] c"error in mac address ioctl reading\0A\00"
@ETH_ALEN = global i64 0
@.str.1 = [25 x i8] c"error malloc mac buffer\0A\00"
define i8* @get_mac(i32 %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca %struct.ifreq
%7 = alloca i8*
store i32 %0, i32* %4
store i8* %1, i8** %5
%8 = call i32 @memset(%struct.ifreq* %6, i32 0, i32 16)
%9 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%10 = load i32, i32* %9
%11 = load i8*, i8** %5
%12 = load i64, i64* @IFNAMSIZ
%13 = sub nsw i64 %12, 1
%14 = call i32 @strncpy(i32 %10, i8* %11, i64 %13)
%15 = load i32, i32* %4
%16 = load i32, i32* @SIOCGIFHWADDR
%17 = call i64 @ioctl(i32 %15, i32 %16, %struct.ifreq* %6)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %3
br label %40
21: ; preds = %2
%22 = load i64, i64* @ETH_ALEN
%23 = add nsw i64 %22, 1
%24 = call i64 @malloc(i64 %23)
%25 = inttoptr i64 %24 to i8*
store i8* %25, i8** %7
%26 = load i8*, i8** %7
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %30
28: ; preds = %21
%29 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %3
br label %40
30: ; preds = %21
%31 = load i8*, i8** %7
%32 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%33 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %32, i32 0, i32 0
%34 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %33, i32 0, i32 0
%35 = load i64, i64* %34
%36 = inttoptr i64 %35 to i8*
%37 = load i64, i64* @ETH_ALEN
%38 = call i32 @memcpy(i8* %31, i8* %36, i64 %37)
%39 = load i8*, i8** %7
store i8* %39, i8** %3
br label %40
40: ; preds = %30, %28, %19
%41 = load i8*, i8** %3
ret i8* %41
}

LLVM IR (-O2):
%struct.ifreq = type { %struct.TYPE_4__, i32 }
%struct.TYPE_4__ = type { %struct.TYPE_3__ }
%struct.TYPE_3__ = type { i64 }
@IFNAMSIZ = global i64 0
@SIOCGIFHWADDR = global i32 0
@ETH_ALEN = global i64 0
@str = [24 x i8] c"error malloc mac buffer\00"
@str.2 = [35 x i8] c"error in mac address ioctl reading\00"
define i8* @get_mac(i32 %0, i8* %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = call i32 @memset(%struct.ifreq* %3, i32 0, i32 16)
%6 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%7 = load i32, i32* %6
%8 = load i64, i64* @IFNAMSIZ
%9 = add nsw i64 %8, -1
%10 = call i32 @strncpy(i32 %7, i8* %1, i64 %9)
%11 = load i32, i32* @SIOCGIFHWADDR
%12 = call i64 @ioctl(i32 %0, i32 %11, %struct.ifreq* %3)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.2, i64 0, i64 0))
br label %30
16: ; preds = %2
%17 = load i64, i64* @ETH_ALEN
%18 = add nsw i64 %17, 1
%19 = call i64 @malloc(i64 %18)
%20 = inttoptr i64 %19 to i8*
%21 = icmp eq i64 %19, 0
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
br label %30
24: ; preds = %16
%25 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0, i32 0, i32 0
%26 = load i64, i64* %25
%27 = inttoptr i64 %26 to i8*
%28 = load i64, i64* @ETH_ALEN
%29 = call i32 @memcpy(i8* %20, i8* %27, i64 %28)
br label %30
30: ; preds = %22, %24, %14
%31 = phi i8* [ null, %14 ], [ null, %22 ], [ %20, %24 ]
ret i8* %31
}