#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;
int /*<<< orphan*/ exit(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
char *malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned char);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
char *getmac(char *iface) {
  char *ret = malloc(13);
  struct ifreq s;
  int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
  strcpy(s.ifr_name, iface);
  if (fd >= 0 && ret && 0 == ioctl(fd, SIOCGIFHWADDR, &s)) {
    int i;
    for (i = 0; i < 6; ++i)
      snprintf(ret + i * 2, 13 - i * 2, "%02x",
               (unsigned char)s.ifr_addr.sa_data[i]);
  } else {
    perror("malloc/socket/ioctl failed");
    exit(1);
  }
  return (ret);
}


x86 assembly (clang, -O0):
<getmac>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0xd,%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
mov    0x2e45(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
cmpl   $0x0,-0x24(%rbp)
jl     124c <getmac+0xdc>
cmpq   $0x0,-0x10(%rbp)
je     124c <getmac+0xdc>
mov    -0x24(%rbp),%edi
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    124c <getmac+0xdc>
movl   $0x0,-0x28(%rbp)
cmpl   $0x6,-0x28(%rbp)
jge    1247 <getmac+0xd7>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%eax
shl    $0x1,%eax
cltq
add    %rax,%rdi
mov    -0x28(%rbp),%eax
shl    $0x1,%eax
mov    $0xd,%esi
sub    %eax,%esi
mov    -0x20(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
lea    0xdcf(%rip),%rdx
movzbl %al,%ecx
call   1040 <snprintf@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11f8 <getmac+0x88>
jmp    1262 <getmac+0xf2>
lea    0xdb2(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getmac>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    $0xd,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
mov    0x2e47(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,%ebp
mov    %r14,%rsi
call   1030 <strcpy@plt>
test   %rbx,%rbx
je     1289 <getmac+0x119>
test   %ebp,%ebp
js     1289 <getmac+0x119>
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1050 <ioctl@plt>
test   %rax,%rax
jne    1289 <getmac+0x119>
mov    (%rsp),%rax
movzbl (%rax),%ecx
lea    0xe13(%rip),%r14
mov    %rbx,%rdi
mov    $0xd,%esi
mov    %r14,%rdx
call   1040 <snprintf@plt>
lea    0x2(%rbx),%rdi
mov    (%rsp),%rax
movzbl 0x8(%rax),%ecx
mov    $0xb,%esi
mov    %r14,%rdx
call   1040 <snprintf@plt>
lea    0x4(%rbx),%rdi
mov    (%rsp),%rax
movzbl 0x10(%rax),%ecx
mov    $0x9,%esi
mov    %r14,%rdx
call   1040 <snprintf@plt>
lea    0x6(%rbx),%rdi
mov    (%rsp),%rax
movzbl 0x18(%rax),%ecx
mov    $0x7,%esi
mov    %r14,%rdx
call   1040 <snprintf@plt>
lea    0x8(%rbx),%rdi
mov    (%rsp),%rax
movzbl 0x20(%rax),%ecx
mov    $0x5,%esi
mov    %r14,%rdx
call   1040 <snprintf@plt>
mov    %rbx,%rdi
add    $0xa,%rdi
mov    (%rsp),%rax
movzbl 0x28(%rax),%ecx
mov    $0x3,%esi
mov    %r14,%rdx
call   1040 <snprintf@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd75(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

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 = [5 x i8] c"%02x\00"
@.str.1 = [27 x i8] c"malloc/socket/ioctl failed\00"
define i8* @getmac(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca %struct.ifreq
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %2
%7 = call i8* @malloc(i32 13)
store i8* %7, i8** %3
%8 = load i32, i32* @PF_INET
%9 = load i32, i32* @SOCK_DGRAM
%10 = load i32, i32* @IPPROTO_IP
%11 = call i32 @socket(i32 %8, i32 %9, i32 %10)
store i32 %11, i32* %5
%12 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 1
%13 = load i32, i32* %12
%14 = load i8*, i8** %2
%15 = call i32 @strcpy(i32 %13, i8* %14)
%16 = load i32, i32* %5
%17 = icmp sge i32 %16, 0
br i1 %17, label %18, label %52
18: ; preds = %1
%19 = load i8*, i8** %3
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %52
21: ; preds = %18
%22 = load i32, i32* %5
%23 = load i32, i32* @SIOCGIFHWADDR
%24 = call i64 @ioctl(i32 %22, i32 %23, %struct.ifreq* %4)
%25 = icmp eq i64 0, %24
br i1 %25, label %26, label %52
26: ; preds = %21
store i32 0, i32* %6
br label %27
27: ; preds = %48, %26
%28 = load i32, i32* %6
%29 = icmp slt i32 %28, 6
br i1 %29, label %30, label %51
30: ; preds = %27
%31 = load i8*, i8** %3
%32 = load i32, i32* %6
%33 = mul nsw i32 %32, 2
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %31, i64 %34
%36 = load i32, i32* %6
%37 = mul nsw i32 %36, 2
%38 = sub nsw i32 13, %37
%39 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 0
%40 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %39, i32 0, i32 0
%41 = load i64*, i64** %40
%42 = load i32, i32* %6
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i64, i64* %41, i64 %43
%45 = load i64, i64* %44
%46 = trunc i64 %45 to i8
%47 = call i32 @snprintf(i8* %35, i32 %38, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %46)
br label %48
48: ; preds = %30
%49 = load i32, i32* %6
%50 = add nsw i32 %49, 1
store i32 %50, i32* %6
br label %27
51: ; preds = %27
br label %55
52: ; preds = %21, %18, %1
%53 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%54 = call i32 @exit(i32 1)
unreachable
55: ; preds = %51
%56 = load i8*, i8** %3
ret i8* %56
}

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 = [5 x i8] c"%02x\00"
@.str.1 = [27 x i8] c"malloc/socket/ioctl failed\00"
define i8* @getmac(i8* %0) {
%2 = alloca %struct.ifreq
%3 = tail call dereferenceable_or_null(13) i8* @malloc(i32 13)
%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 = tail call i32 @strcpy(i32 undef, i8* %0)
%10 = icmp sgt i32 %8, -1
%11 = icmp ne i8* %3, null
%12 = and i1 %11, %10
br i1 %12, label %13, label %53
13: ; preds = %1
%14 = load i32, i32* @SIOCGIFHWADDR
%15 = call i64 @ioctl(i32 %8, i32 %14, %struct.ifreq* %2)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %53
17: ; preds = %13
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0, i32 0
%19 = load i64*, i64** %18
%20 = load i64, i64* %19
%21 = trunc i64 %20 to i8
%22 = call i32 @snprintf(i8* %3, i32 13, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %21)
%23 = getelementptr inbounds i8, i8* %3, i64 2
%24 = load i64*, i64** %18
%25 = getelementptr inbounds i64, i64* %24, i64 1
%26 = load i64, i64* %25
%27 = trunc i64 %26 to i8
%28 = call i32 @snprintf(i8* %23, i32 11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %27)
%29 = getelementptr inbounds i8, i8* %3, i64 4
%30 = load i64*, i64** %18
%31 = getelementptr inbounds i64, i64* %30, i64 2
%32 = load i64, i64* %31
%33 = trunc i64 %32 to i8
%34 = call i32 @snprintf(i8* %29, i32 9, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %33)
%35 = getelementptr inbounds i8, i8* %3, i64 6
%36 = load i64*, i64** %18
%37 = getelementptr inbounds i64, i64* %36, i64 3
%38 = load i64, i64* %37
%39 = trunc i64 %38 to i8
%40 = call i32 @snprintf(i8* %35, i32 7, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %39)
%41 = getelementptr inbounds i8, i8* %3, i64 8
%42 = load i64*, i64** %18
%43 = getelementptr inbounds i64, i64* %42, i64 4
%44 = load i64, i64* %43
%45 = trunc i64 %44 to i8
%46 = call i32 @snprintf(i8* %41, i32 5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %45)
%47 = getelementptr inbounds i8, i8* %3, i64 10
%48 = load i64*, i64** %18
%49 = getelementptr inbounds i64, i64* %48, i64 5
%50 = load i64, i64* %49
%51 = trunc i64 %50 to i8
%52 = call i32 @snprintf(i8* %47, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %51)
ret i8* %3
53: ; preds = %13, %1
%54 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%55 = call i32 @exit(i32 1)
unreachable
}