#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 */

/* Type definitions */
struct ifreq {
  scalar_t__ ifr_data;
  int /*<<< orphan*/ ifr_name;
};
struct ethtool_perm_addr {
  int size;
  int *data;
  int /*<<< orphan*/ cmd;
};
typedef int /*<<< orphan*/ ifr;
typedef scalar_t__ caddr_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ ETHTOOL_GPERMADDR;
int /*<<< orphan*/ SIOCETHTOOL;
int /*<<< orphan*/ SOCK_DGRAM;
struct ethtool_perm_addr *calloc(int, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(struct ethtool_perm_addr *);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int);
int main(int argc, char **argv) {
  int retval = 0;
  int i;
  char *ifname;
  if (argv[1])
    ifname = argv[1];
  else
    ifname = "eth0";
  struct ifreq ifr;
  memset(&ifr, 0, sizeof(ifr));
  strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
  int fd = socket(AF_INET, SOCK_DGRAM, 0);
  if (fd == -1) {
    perror("Cannot open socket");
    exit(1);
  }
  struct ethtool_perm_addr *a;
  a = calloc(1, sizeof(*a) + 32);
  a->cmd = ETHTOOL_GPERMADDR;
  a->size = 32;
  ifr.ifr_data = (caddr_t)a;
  if (ioctl(fd, SIOCETHTOOL, &ifr) != -1) {
    for (i = 0; i < a->size; i++)
      printf("%02x", a->data[i]);
    printf("\n");
  } else {
    perror("Cannot perform socket ioctl");
    retval = 1;
  }
  close(fd);
  free(a);
  exit(retval);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x8(%rax)
je     11dd <main+0x3d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
jmp    11e8 <main+0x48>
lea    0xe1c(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    -0x28(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x3,%edx
call   1040 <strncpy@plt>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    1245 <main+0xa5>
lea    0xdcf(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    $0x1,%edi
mov    $0x38,%esi
call   1090 <calloc@plt>
mov    %rax,-0x40(%rbp)
mov    0x2d71(%rip),%rax
mov    (%rax),%ecx
mov    -0x40(%rbp),%rax
mov    %ecx,0x10(%rax)
mov    -0x40(%rbp),%rax
movl   $0x20,(%rax)
mov    -0x40(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x34(%rbp),%edi
mov    0x2d3c(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1070 <ioctl@plt>
cmp    $0xffffffff,%eax
je     12ec <main+0x14c>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    -0x40(%rbp),%rcx
cmp    (%rcx),%eax
jge    12d9 <main+0x139>
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
lea    0xd54(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    129f <main+0xff>
lea    0xd3d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12ff <main+0x15f>
lea    0xd2c(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x14(%rbp)
mov    -0x34(%rbp),%edi
call   1080 <close@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x8(%rsi),%rax
test   %rax,%rax
lea    0xe38(%rip),%rbx
cmovne %rax,%rbx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <memset@plt>
mov    0x10(%rsp),%edi
mov    %rbx,%rsi
mov    $0x3,%edx
call   1050 <strncpy@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
cmp    $0xffffffff,%eax
jne    1222 <main+0x72>
lea    0xdf2(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    %eax,%r15d
mov    $0x1,%r14d
mov    $0x1,%edi
mov    $0x38,%esi
call   10a0 <calloc@plt>
mov    %rax,%rbx
mov    0x2d8c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rbx)
movl   $0x20,(%rbx)
mov    %rbx,0x8(%rsp)
mov    0x2d65(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %r15d,%edi
call   1080 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    1293 <main+0xe3>
lea    0xda7(%rip),%rdi
call   10b0 <perror@plt>
mov    %r15d,%edi
call   1090 <close@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14d,%edi
call   10c0 <exit@plt>
cmpl   $0x0,(%rbx)
jle    12cd <main+0x11d>
lea    0xd79(%rip),%r14
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x8(%rbx),%rax
mov    (%rax,%rbp,4),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
add    $0x1,%rbp
movslq (%rbx),%rax
cmp    %rax,%rbp
jl     12b0 <main+0x100>
mov    $0xa,%edi
call   1040 <putchar@plt>
xor    %r14d,%r14d
jmp    127b <main+0xcb>

LLVM IR (-O0):
%struct.ifreq = type { i64, i32 }
%struct.ethtool_perm_addr = type { i32, i32*, i32 }
@.str = [5 x i8] c"eth0\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [19 x i8] c"Cannot open socket\00"
@ETHTOOL_GPERMADDR = global i32 0
@SIOCETHTOOL = global i32 0
@.str.2 = [5 x i8] c"%02x\00"
@.str.3 = [2 x i8] c"\0A\00"
@.str.4 = [28 x i8] c"Cannot perform socket ioctl\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca %struct.ifreq
%10 = alloca i32
%11 = alloca %struct.ethtool_perm_addr*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
store i8* %19, i8** %8
br label %21
20: ; preds = %2
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %8
br label %21
21: ; preds = %20, %16
%22 = call i32 @memset(%struct.ifreq* %9, i32 0, i32 16)
%23 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %9, i32 0, i32 1
%24 = load i32, i32* %23
%25 = load i8*, i8** %8
%26 = call i32 @strncpy(i32 %24, i8* %25, i32 3)
%27 = load i32, i32* @AF_INET
%28 = load i32, i32* @SOCK_DGRAM
%29 = call i32 @socket(i32 %27, i32 %28, i32 0)
store i32 %29, i32* %10
%30 = load i32, i32* %10
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %21
%33 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %21
%36 = call %struct.ethtool_perm_addr* @calloc(i32 1, i32 56)
store %struct.ethtool_perm_addr* %36, %struct.ethtool_perm_addr** %11
%37 = load i32, i32* @ETHTOOL_GPERMADDR
%38 = load %struct.ethtool_perm_addr*, %struct.ethtool_perm_addr** %11
%39 = getelementptr inbounds %struct.ethtool_perm_addr, %struct.ethtool_perm_addr* %38, i32 0, i32 2
store i32 %37, i32* %39
%40 = load %struct.ethtool_perm_addr*, %struct.ethtool_perm_addr** %11
%41 = getelementptr inbounds %struct.ethtool_perm_addr, %struct.ethtool_perm_addr* %40, i32 0, i32 0
store i32 32, i32* %41
%42 = load %struct.ethtool_perm_addr*, %struct.ethtool_perm_addr** %11
%43 = ptrtoint %struct.ethtool_perm_addr* %42 to i64
%44 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %9, i32 0, i32 0
store i64 %43, i64* %44
%45 = load i32, i32* %10
%46 = load i32, i32* @SIOCETHTOOL
%47 = call i32 @ioctl(i32 %45, i32 %46, %struct.ifreq* %9)
%48 = icmp ne i32 %47, -1
br i1 %48, label %49, label %70
49: ; preds = %35
store i32 0, i32* %7
br label %50
50: ; preds = %65, %49
%51 = load i32, i32* %7
%52 = load %struct.ethtool_perm_addr*, %struct.ethtool_perm_addr** %11
%53 = getelementptr inbounds %struct.ethtool_perm_addr, %struct.ethtool_perm_addr* %52, i32 0, i32 0
%54 = load i32, i32* %53
%55 = icmp slt i32 %51, %54
br i1 %55, label %56, label %68
56: ; preds = %50
%57 = load %struct.ethtool_perm_addr*, %struct.ethtool_perm_addr** %11
%58 = getelementptr inbounds %struct.ethtool_perm_addr, %struct.ethtool_perm_addr* %57, i32 0, i32 1
%59 = load i32*, i32** %58
%60 = load i32, i32* %7
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds i32, i32* %59, i64 %61
%63 = load i32, i32* %62
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %63)
br label %65
65: ; preds = %56
%66 = load i32, i32* %7
%67 = add nsw i32 %66, 1
store i32 %67, i32* %7
br label %50
68: ; preds = %50
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %72
70: ; preds = %35
%71 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %6
br label %72
72: ; preds = %70, %68
%73 = load i32, i32* %10
%74 = call i32 @close(i32 %73)
%75 = load %struct.ethtool_perm_addr*, %struct.ethtool_perm_addr** %11
%76 = call i32 @free(%struct.ethtool_perm_addr* %75)
%77 = load i32, i32* %6
%78 = call i32 @exit(i32 %77)
unreachable
}

LLVM IR (-O2):
%struct.ifreq = type { i64, i32 }
%struct.ethtool_perm_addr = type { i32, i32*, i32 }
@.str = [5 x i8] c"eth0\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [19 x i8] c"Cannot open socket\00"
@ETHTOOL_GPERMADDR = global i32 0
@SIOCETHTOOL = global i32 0
@.str.2 = [5 x i8] c"%02x\00"
@.str.4 = [28 x i8] c"Cannot perform socket ioctl\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.ifreq
%4 = getelementptr inbounds i8*, i8** %1, i64 1
%5 = load i8*, i8** %4
%6 = icmp eq i8* %5, null
%7 = select i1 %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %5
%8 = bitcast %struct.ifreq* %3 to i8*
%9 = call i32 @memset(%struct.ifreq* %3, i32 0, i32 16)
%10 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%11 = load i32, i32* %10
%12 = call i32 @strncpy(i32 %11, i8* %7, i32 3)
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_DGRAM
%15 = call i32 @socket(i32 %13, i32 %14, i32 0)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = call %struct.ethtool_perm_addr* @calloc(i32 1, i32 56)
%22 = load i32, i32* @ETHTOOL_GPERMADDR
%23 = getelementptr inbounds %struct.ethtool_perm_addr, %struct.ethtool_perm_addr* %21, i64 0, i32 2
store i32 %22, i32* %23
%24 = getelementptr inbounds %struct.ethtool_perm_addr, %struct.ethtool_perm_addr* %21, i64 0, i32 0
store i32 32, i32* %24
%25 = ptrtoint %struct.ethtool_perm_addr* %21 to i64
%26 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
store i64 %25, i64* %26
%27 = load i32, i32* @SIOCETHTOOL
%28 = call i32 @ioctl(i32 %15, i32 %27, %struct.ifreq* %3)
%29 = icmp eq i32 %28, -1
br i1 %29, label %47, label %30
30: ; preds = %20
%31 = load i32, i32* %24
%32 = icmp sgt i32 %31, 0
br i1 %32, label %33, label %45
33: ; preds = %30
%34 = getelementptr inbounds %struct.ethtool_perm_addr, %struct.ethtool_perm_addr* %21, i64 0, i32 1
br label %35
35: ; preds = %33, %35
%36 = phi i64 [ 0, %33 ], [ %41, %35 ]
%37 = load i32*, i32** %34
%38 = getelementptr inbounds i32, i32* %37, i64 %36
%39 = load i32, i32* %38
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %39)
%41 = add nuw nsw i64 %36, 1
%42 = load i32, i32* %24
%43 = sext i32 %42 to i64
%44 = icmp slt i64 %41, %43
br i1 %44, label %35, label %45
45: ; preds = %35, %30
%46 = call i32 @putchar(i32 10)
br label %49
47: ; preds = %20
%48 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
br label %49
49: ; preds = %47, %45
%50 = phi i32 [ 0, %45 ], [ 1, %47 ]
%51 = call i32 @close(i32 %15)
%52 = call i32 @free(%struct.ethtool_perm_addr* %21)
%53 = call i32 @exit(i32 %50)
unreachable
}