#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_addr;
  int /*<<< orphan*/ ifr_name;
};

/* Variables and functions */
int /*<<< orphan*/ IPPROTO_IP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SIOCGIFHWADDR;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int sprintf(char *, char *, char *, unsigned char);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
char *strdup(char *);
char *macgetter_get_mac(const char *iface) {
  struct ifreq s;
  int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
  char buf[20];
  strcpy(s.ifr_name, iface);
  if (0 == ioctl(fd, SIOCGIFHWADDR, &s)) {
    char *mac = "";
    char *sep = "";
    int i;
    int p = 0;
    for (i = 0; i < 6; ++i) {
      int c =
          sprintf(buf + p, "%s%02x", sep, (unsigned char)s.ifr_addr.sa_data[i]);
      sep = ":";
      p = p + c;
    }
    return strdup(buf);
  }
  return NULL;
}


x86 assembly (clang, -O0):
<macgetter_get_mac>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
mov    0x2e73(%rip),%rax
mov    (%rax),%edx
call   1070 <socket@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x24(%rbp),%edi
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    123d <macgetter_get_mac+0xed>
lea    0xe53(%rip),%rax
mov    %rax,-0x48(%rbp)
lea    0xe48(%rip),%rax
mov    %rax,-0x50(%rbp)
movl   $0x0,-0x58(%rbp)
movl   $0x0,-0x54(%rbp)
cmpl   $0x6,-0x54(%rbp)
jge    122b <macgetter_get_mac+0xdb>
lea    -0x40(%rbp),%rdi
movslq -0x58(%rbp),%rax
add    %rax,%rdi
mov    -0x50(%rbp),%rdx
mov    -0x20(%rbp),%rax
movslq -0x54(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
lea    0xe02(%rip),%rsi
movzbl %al,%ecx
call   1050 <sprintf@plt>
mov    %eax,-0x5c(%rbp)
lea    0xdf7(%rip),%rax
mov    %rax,-0x50(%rbp)
mov    -0x58(%rbp),%eax
add    -0x5c(%rbp),%eax
mov    %eax,-0x58(%rbp)
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    11d2 <macgetter_get_mac+0x82>
lea    -0x40(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1245 <macgetter_get_mac+0xf5>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<macgetter_get_mac>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%rbx
mov    0x2e64(%rip),%rax
mov    (%rax),%edi
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
mov    0x2e72(%rip),%rax
mov    (%rax),%edx
call   1070 <socket@plt>
mov    %eax,%ebp
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
je     11a6 <macgetter_get_mac+0x56>
xor    %eax,%eax
jmp    1278 <macgetter_get_mac+0x128>
mov    (%rsp),%rax
movzbl (%rax),%ecx
lea    0xe4c(%rip),%rbx
lea    0xe4d(%rip),%rdx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    %rbx,%rsi
call   1050 <sprintf@plt>
movslq %eax,%rbp
lea    (%rsp,%rbp,1),%rdi
add    $0x10,%rdi
mov    (%rsp),%rax
movzbl 0x8(%rax),%ecx
lea    0xe22(%rip),%r15
mov    %rbx,%rsi
mov    %r15,%rdx
call   1050 <sprintf@plt>
add    %eax,%ebp
movslq %ebp,%rbp
lea    (%rsp,%rbp,1),%rdi
add    $0x10,%rdi
mov    (%rsp),%rax
movzbl 0x10(%rax),%ecx
mov    %rbx,%rsi
mov    %r15,%rdx
call   1050 <sprintf@plt>
add    %eax,%ebp
movslq %ebp,%rbp
lea    (%rsp,%rbp,1),%rdi
add    $0x10,%rdi
mov    (%rsp),%rax
movzbl 0x18(%rax),%ecx
mov    %rbx,%rsi
mov    %r15,%rdx
call   1050 <sprintf@plt>
add    %eax,%ebp
movslq %ebp,%rbp
lea    (%rsp,%rbp,1),%rdi
add    $0x10,%rdi
mov    (%rsp),%rax
movzbl 0x20(%rax),%ecx
mov    %rbx,%rsi
mov    %r15,%rdx
call   1050 <sprintf@plt>
add    %eax,%ebp
movslq %ebp,%rax
lea    (%rsp,%rax,1),%rdi
add    $0x10,%rdi
mov    (%rsp),%rax
movzbl 0x28(%rax),%ecx
mov    %rbx,%rsi
mov    %r15,%rdx
call   1050 <sprintf@plt>
mov    %r14,%rdi
call   1060 <strdup@plt>
add    $0x28,%rsp
pop    %rbx
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
@IPPROTO_IP = global i32 0
@SIOCGIFHWADDR = global i32 0
@.str = [1 x i8] zeroinitializer
@.str.1 = [7 x i8] c"%s%02x\00"
@.str.2 = [2 x i8] c":\00"
define i8* @macgetter_get_mac(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca %struct.ifreq
%5 = alloca i32
%6 = alloca [20 x i8]
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %3
%12 = load i32, i32* @PF_INET
%13 = load i32, i32* @SOCK_DGRAM
%14 = load i32, i32* @IPPROTO_IP
%15 = call i32 @socket(i32 %12, i32 %13, i32 %14)
store i32 %15, i32* %5
%16 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 1
%17 = load i32, i32* %16
%18 = load i8*, i8** %3
%19 = call i32 @strcpy(i32 %17, i8* %18)
%20 = load i32, i32* %5
%21 = load i32, i32* @SIOCGIFHWADDR
%22 = call i64 @ioctl(i32 %20, i32 %21, %struct.ifreq* %4)
%23 = icmp eq i64 0, %22
br i1 %23, label %24, label %52
24: ; preds = %1
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8** %7
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8** %8
store i32 0, i32* %10
store i32 0, i32* %9
br label %25
25: ; preds = %46, %24
%26 = load i32, i32* %9
%27 = icmp slt i32 %26, 6
br i1 %27, label %28, label %49
28: ; preds = %25
%29 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%30 = load i32, i32* %10
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
%33 = load i8*, i8** %8
%34 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 0
%35 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %34, i32 0, i32 0
%36 = load i64*, i64** %35
%37 = load i32, i32* %9
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i64, i64* %36, i64 %38
%40 = load i64, i64* %39
%41 = trunc i64 %40 to i8
%42 = call i32 @sprintf(i8* %32, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %33, i8 zeroext %41)
store i32 %42, i32* %11
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8** %8
%43 = load i32, i32* %10
%44 = load i32, i32* %11
%45 = add nsw i32 %43, %44
store i32 %45, i32* %10
br label %46
46: ; preds = %28
%47 = load i32, i32* %9
%48 = add nsw i32 %47, 1
store i32 %48, i32* %9
br label %25
49: ; preds = %25
%50 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%51 = call i8* @strdup(i8* %50)
store i8* %51, i8** %2
br label %53
52: ; preds = %1
store i8* null, i8** %2
br label %53
53: ; preds = %52, %49
%54 = load i8*, i8** %2
ret i8* %54
}

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
@IPPROTO_IP = global i32 0
@SIOCGIFHWADDR = global i32 0
@.str = [1 x i8] zeroinitializer
@.str.1 = [7 x i8] c"%s%02x\00"
@.str.2 = [2 x i8] c":\00"
define noalias i8* @macgetter_get_mac(i8* %0) {
%2 = alloca %struct.ifreq
%3 = alloca [20 x i8]
%4 = bitcast %struct.ifreq* %2 to i8*
%5 = load i32, i32* @PF_INET
%6 = load i32, i32* @SOCK_DGRAM
%7 = load i32, i32* @IPPROTO_IP
%8 = tail call i32 @socket(i32 %5, i32 %6, i32 %7)
%9 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%10 = tail call i32 @strcpy(i32 undef, i8* %0)
%11 = load i32, i32* @SIOCGIFHWADDR
%12 = call i64 @ioctl(i32 %8, i32 %11, %struct.ifreq* %2)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %60
14: ; preds = %1
%15 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0, i32 0
%16 = load i64*, i64** %15
%17 = load i64, i64* %16
%18 = trunc i64 %17 to i8
%19 = call i32 @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8 zeroext %18)
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 %20
%22 = load i64*, i64** %15
%23 = getelementptr inbounds i64, i64* %22, i64 1
%24 = load i64, i64* %23
%25 = trunc i64 %24 to i8
%26 = call i32 @sprintf(i8* %21, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %25)
%27 = add nsw i32 %26, %19
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 %28
%30 = load i64*, i64** %15
%31 = getelementptr inbounds i64, i64* %30, i64 2
%32 = load i64, i64* %31
%33 = trunc i64 %32 to i8
%34 = call i32 @sprintf(i8* %29, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %33)
%35 = add nsw i32 %34, %27
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 %36
%38 = load i64*, i64** %15
%39 = getelementptr inbounds i64, i64* %38, i64 3
%40 = load i64, i64* %39
%41 = trunc i64 %40 to i8
%42 = call i32 @sprintf(i8* %37, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %41)
%43 = add nsw i32 %42, %35
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 %44
%46 = load i64*, i64** %15
%47 = getelementptr inbounds i64, i64* %46, i64 4
%48 = load i64, i64* %47
%49 = trunc i64 %48 to i8
%50 = call i32 @sprintf(i8* %45, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %49)
%51 = add nsw i32 %50, %43
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 %52
%54 = load i64*, i64** %15
%55 = getelementptr inbounds i64, i64* %54, i64 5
%56 = load i64, i64* %55
%57 = trunc i64 %56 to i8
%58 = call i32 @sprintf(i8* %53, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %57)
%59 = call i8* @strdup(i8* %9)
br label %60
60: ; preds = %1, %14
%61 = phi i8* [ %59, %14 ], [ null, %1 ]
ret i8* %61
}