#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_4__ {
  int /*<<< orphan*/ sa_data;
};
struct TYPE_3__ {
  int /*<<< orphan*/ sa_family;
};
struct ifreq {
  TYPE_2__ ifr_hwaddr;
  TYPE_1__ ifr_addr;
  int /*<<< orphan*/ ifr_name;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SIOCGIFHWADDR;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memcpy(unsigned char *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int get_mac_address_old(char *interface, unsigned char chMAC[6]) {
  struct ifreq ifr;
  int sock;
  char *ifname = interface;
  sock = socket(AF_INET, SOCK_DGRAM, 0);
  strcpy(ifr.ifr_name, ifname);
  ifr.ifr_addr.sa_family = AF_INET;
  if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0) {
    fprintf(stderr, "Error while ioctl MAC address\n");
    return -1;
  }
  memcpy(chMAC, ifr.ifr_hwaddr.sa_data, 6);
  close(sock);
  return 0;
}


x86 assembly (clang, -O0):
<get_mac_address_old>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x38(%rbp),%rsi
call   1030 <strcpy@plt>
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x2c(%rbp),%edi
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    11ec <get_mac_address_old+0x8c>
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
lea    0xe25(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120c <get_mac_address_old+0xac>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%esi
mov    $0x6,%edx
call   1070 <memcpy@plt>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_mac_address_old>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e6f(%rip),%r12
mov    (%r12),%edi
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
xor    %r15d,%r15d
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebp
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    (%r12),%eax
mov    %eax,0x4(%rsp)
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
js     11d3 <get_mac_address_old+0x73>
mov    (%rsp),%esi
mov    %r14,%rdi
mov    $0x6,%edx
call   1070 <memcpy@plt>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    11ee <get_mac_address_old+0x8e>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
lea    0xe1d(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { %struct.TYPE_4__, %struct.TYPE_3__, i32 }
%struct.TYPE_4__ = type { i32 }
%struct.TYPE_3__ = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFHWADDR = global i32 0
@stderr = global i32 0
@.str = [31 x i8] c"Error while ioctl MAC address\0A\00"
define i32 @get_mac_address_old(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.ifreq
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
store i8* %9, i8** %8
%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* %7
%13 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 2
%14 = load i32, i32* %13
%15 = load i8*, i8** %8
%16 = call i32 @strcpy(i32 %14, i8* %15)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%19 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %18, i32 0, i32 0
store i32 %17, i32* %19
%20 = load i32, i32* %7
%21 = load i32, i32* @SIOCGIFHWADDR
%22 = call i64 @ioctl(i32 %20, i32 %21, %struct.ifreq* %6)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %2
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %35
27: ; preds = %2
%28 = load i8*, i8** %5
%29 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%30 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %29, i32 0, i32 0
%31 = load i32, i32* %30
%32 = call i32 @memcpy(i8* %28, i32 %31, i32 6)
%33 = load i32, i32* %7
%34 = call i32 @close(i32 %33)
store i32 0, i32* %3
br label %35
35: ; preds = %27, %24
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
%struct.ifreq = type { %struct.TYPE_4__, %struct.TYPE_3__, i32 }
%struct.TYPE_4__ = type { i32 }
%struct.TYPE_3__ = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFHWADDR = global i32 0
@stderr = global i32 0
@.str = [31 x i8] c"Error while ioctl MAC address\0A\00"
define i32 @get_mac_address_old(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 = tail call i32 @strcpy(i32 undef, i8* %0)
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1, i32 0
store i32 %9, i32* %10
%11 = load i32, i32* @SIOCGIFHWADDR
%12 = call i64 @ioctl(i32 %7, i32 %11, %struct.ifreq* %3)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
br label %22
17: ; preds = %2
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0, i32 0
%19 = load i32, i32* %18
%20 = call i32 @memcpy(i8* %1, i32 %19, i32 6)
%21 = call i32 @close(i32 %7)
br label %22
22: ; preds = %17, %14
%23 = phi i32 [ -1, %14 ], [ 0, %17 ]
ret i32 %23
}