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

/* Variables and functions */
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SIOCGIFHWADDR;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int /*<<< orphan*/ snprintf(char *, int, char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int getifmac(char *interface, char *mac, int size) {
  int s;
  struct ifreq req;
  s = socket(PF_INET, SOCK_DGRAM, 0);
  if (s == -1)
    return -1;
  memset(&req, 0, sizeof(req));
  snprintf(req.ifr_name, sizeof(req.ifr_name), "%s", interface);
  if (ioctl(s, SIOCGIFHWADDR, &req) != 0) {
    close(s);
    return -1;
  }
  close(s);
  snprintf(mac, size, "%02X:%02X:%02X:%02X:%02X:%02X",
           (unsigned char)(req.ifr_hwaddr.sa_data)[0],
           (unsigned char)(req.ifr_hwaddr.sa_data)[1],
           (unsigned char)(req.ifr_hwaddr.sa_data)[2],
           (unsigned char)(req.ifr_hwaddr.sa_data)[3],
           (unsigned char)(req.ifr_hwaddr.sa_data)[4],
           (unsigned char)(req.ifr_hwaddr.sa_data)[5]);
  return 0;
}


x86 assembly (clang, -O0):
<getifmac>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    0x2e66(%rip),%rax
mov    (%rax),%edi
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    1195 <getifmac+0x45>
movl   $0xffffffff,-0x4(%rbp)
jmp    1269 <getifmac+0x119>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
movslq -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x4,%esi
lea    0xe47(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
mov    -0x20(%rbp),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
je     11f3 <getifmac+0xa3>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1269 <getifmac+0x119>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
movzbl %al,%ecx
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rax
movzbl %al,%r8d
mov    -0x30(%rbp),%rax
mov    0x10(%rax),%rax
movzbl %al,%r9d
mov    -0x30(%rbp),%rax
mov    0x18(%rax),%rax
movzbl %al,%r11d
mov    -0x30(%rbp),%rax
mov    0x20(%rax),%rax
movzbl %al,%r10d
mov    -0x30(%rbp),%rax
mov    0x28(%rax),%rax
movzbl %al,%eax
lea    0xdb5(%rip),%rdx
mov    %r11d,(%rsp)
mov    %r10d,0x8(%rsp)
mov    %eax,0x10(%rsp)
mov    $0x0,%al
call   1030 <snprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getifmac>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%ebp
mov    %rsi,%r15
mov    %rdi,%r12
mov    0x2e63(%rip),%rax
mov    (%rax),%edi
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    $0xffffffff,%r14d
cmp    $0xffffffff,%eax
je     1228 <getifmac+0xd8>
mov    %eax,%ebx
mov    %ebp,0x4(%rsp)
lea    0x8(%rsp),%r13
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
movslq 0x10(%rsp),%rdi
lea    0xe4c(%rip),%rdx
mov    $0x4,%esi
mov    %r12,%rcx
xor    %eax,%eax
call   1030 <snprintf@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r13,%rdx
call   1050 <ioctl@plt>
mov    %rax,%rbp
mov    %ebx,%edi
call   1060 <close@plt>
test   %rbp,%rbp
jne    1228 <getifmac+0xd8>
mov    0x8(%rsp),%rax
movzbl (%rax),%ecx
movzbl 0x8(%rax),%r8d
movzbl 0x10(%rax),%r9d
movzbl 0x18(%rax),%r10d
movzbl 0x20(%rax),%ebx
movzbl 0x28(%rax),%ebp
sub    $0x8,%rsp
lea    0xdf4(%rip),%rdx
mov    %r15,%rdi
mov    0xc(%rsp),%esi
xor    %eax,%eax
push   %rbp
push   %rbx
push   %r10
call   1030 <snprintf@plt>
add    $0x20,%rsp
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i64* }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [3 x i8] c"%s\00"
@SIOCGIFHWADDR = global i32 0
@.str.1 = [30 x i8] c"%02X:%02X:%02X:%02X:%02X:%02X\00"
define i32 @getifmac(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.ifreq
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i32, i32* @PF_INET
%11 = load i32, i32* @SOCK_DGRAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %16
15: ; preds = %3
store i32 -1, i32* %4
br label %79
16: ; preds = %3
%17 = call i32 @memset(%struct.ifreq* %9, i32 0, i32 16)
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %9, i32 0, i32 1
%19 = load i32, i32* %18
%20 = sext i32 %19 to i64
%21 = inttoptr i64 %20 to i8*
%22 = load i8*, i8** %5
%23 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %21, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %22)
%24 = load i32, i32* %8
%25 = load i32, i32* @SIOCGIFHWADDR
%26 = call i64 @ioctl(i32 %24, i32 %25, %struct.ifreq* %9)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %16
%29 = load i32, i32* %8
%30 = call i32 @close(i32 %29)
store i32 -1, i32* %4
br label %79
31: ; preds = %16
%32 = load i32, i32* %8
%33 = call i32 @close(i32 %32)
%34 = load i8*, i8** %6
%35 = load i32, i32* %7
%36 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %9, i32 0, i32 0
%37 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %36, i32 0, i32 0
%38 = load i64*, i64** %37
%39 = getelementptr inbounds i64, i64* %38, i64 0
%40 = load i64, i64* %39
%41 = trunc i64 %40 to i8
%42 = zext i8 %41 to i32
%43 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %9, i32 0, i32 0
%44 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %43, i32 0, i32 0
%45 = load i64*, i64** %44
%46 = getelementptr inbounds i64, i64* %45, i64 1
%47 = load i64, i64* %46
%48 = trunc i64 %47 to i8
%49 = zext i8 %48 to i32
%50 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %9, i32 0, i32 0
%51 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %50, i32 0, i32 0
%52 = load i64*, i64** %51
%53 = getelementptr inbounds i64, i64* %52, i64 2
%54 = load i64, i64* %53
%55 = trunc i64 %54 to i8
%56 = zext i8 %55 to i32
%57 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %9, i32 0, i32 0
%58 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %57, i32 0, i32 0
%59 = load i64*, i64** %58
%60 = getelementptr inbounds i64, i64* %59, i64 3
%61 = load i64, i64* %60
%62 = trunc i64 %61 to i8
%63 = zext i8 %62 to i32
%64 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %9, i32 0, i32 0
%65 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %64, i32 0, i32 0
%66 = load i64*, i64** %65
%67 = getelementptr inbounds i64, i64* %66, i64 4
%68 = load i64, i64* %67
%69 = trunc i64 %68 to i8
%70 = zext i8 %69 to i32
%71 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %9, i32 0, i32 0
%72 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %71, i32 0, i32 0
%73 = load i64*, i64** %72
%74 = getelementptr inbounds i64, i64* %73, i64 5
%75 = load i64, i64* %74
%76 = trunc i64 %75 to i8
%77 = zext i8 %76 to i32
%78 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %34, i32 %35, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %42, i32 %49, i32 %56, i32 %63, i32 %70, i32 %77)
store i32 0, i32* %4
br label %79
79: ; preds = %31, %28, %15
%80 = load i32, i32* %4
ret i32 %80
}

LLVM IR (-O2):
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i64* }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [3 x i8] c"%s\00"
@SIOCGIFHWADDR = global i32 0
@.str.1 = [30 x i8] c"%02X:%02X:%02X:%02X:%02X:%02X\00"
define i32 @getifmac(i8* %0, i8* %1, i32 %2) {
%4 = alloca %struct.ifreq
%5 = bitcast %struct.ifreq* %4 to i8*
%6 = load i32, i32* @PF_INET
%7 = load i32, i32* @SOCK_DGRAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %48, label %10
10: ; preds = %3
%11 = call i32 @memset(%struct.ifreq* %4, i32 0, i32 16)
%12 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 1
%13 = load i32, i32* %12
%14 = sext i32 %13 to i64
%15 = inttoptr i64 %14 to i8*
%16 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %15, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%17 = load i32, i32* @SIOCGIFHWADDR
%18 = call i64 @ioctl(i32 %8, i32 %17, %struct.ifreq* %4)
%19 = icmp eq i64 %18, 0
%20 = call i32 @close(i32 %8)
br i1 %19, label %21, label %48
21: ; preds = %10
%22 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 0, i32 0
%23 = load i64*, i64** %22
%24 = load i64, i64* %23
%25 = trunc i64 %24 to i32
%26 = and i32 %25, 255
%27 = getelementptr inbounds i64, i64* %23, i64 1
%28 = load i64, i64* %27
%29 = trunc i64 %28 to i32
%30 = and i32 %29, 255
%31 = getelementptr inbounds i64, i64* %23, i64 2
%32 = load i64, i64* %31
%33 = trunc i64 %32 to i32
%34 = and i32 %33, 255
%35 = getelementptr inbounds i64, i64* %23, i64 3
%36 = load i64, i64* %35
%37 = trunc i64 %36 to i32
%38 = and i32 %37, 255
%39 = getelementptr inbounds i64, i64* %23, i64 4
%40 = load i64, i64* %39
%41 = trunc i64 %40 to i32
%42 = and i32 %41, 255
%43 = getelementptr inbounds i64, i64* %23, i64 5
%44 = load i64, i64* %43
%45 = trunc i64 %44 to i32
%46 = and i32 %45, 255
%47 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %1, i32 %2, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %26, i32 %30, i32 %34, i32 %38, i32 %42, i32 %46)
br label %48
48: ; preds = %10, %3, %21
%49 = phi i32 [ 0, %21 ], [ -1, %3 ], [ -1, %10 ]
ret i32 %49
}