#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_3__ {
  int *sa_data;
};
struct TYPE_4__ {
  TYPE_1__ ifru_hwaddr;
};
struct ifreq {
  char *ifr_name;
  TYPE_2__ ifr_ifru;
};
typedef int /*<<< orphan*/ devEA;

/* Variables and functions */
int /*<<< orphan*/ AF_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 *, size_t, char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
int getHWAddrForInterface(const char *ifname, char *hwaddr, size_t n) {
  int sd;
  struct ifreq devEA;
  int resultCode = 0;
  memset(&devEA, 0, sizeof(devEA));
  if (ifname)
    strncpy(devEA.ifr_name, ifname, sizeof(devEA.ifr_name) - 1);
  else
    snprintf(devEA.ifr_name, sizeof(devEA.ifr_name) - 1, "eth0");
  if ((sd = socket(AF_INET, SOCK_DGRAM, 0)) >= 0) {
    if (ioctl(sd, SIOCGIFHWADDR, &devEA) < 0) {
      strncpy(hwaddr, "unknown", n);
      resultCode = 2;
    } else {
      snprintf(hwaddr, n, "%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX",
               devEA.ifr_ifru.ifru_hwaddr.sa_data[0] & 0xFF,
               devEA.ifr_ifru.ifru_hwaddr.sa_data[1] & 0xFF,
               devEA.ifr_ifru.ifru_hwaddr.sa_data[2] & 0xFF,
               devEA.ifr_ifru.ifru_hwaddr.sa_data[3] & 0xFF,
               devEA.ifr_ifru.ifru_hwaddr.sa_data[4] & 0xFF,
               devEA.ifr_ifru.ifru_hwaddr.sa_data[5] & 0xFF);
    }
    close(sd);
  } else {
    resultCode = 1;
  }
  return resultCode;
}


x86 assembly (clang, -O0):
<getHWAddrForInterface>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x34(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
cmpq   $0x0,-0x8(%rbp)
je     11ad <getHWAddrForInterface+0x4d>
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    $0x7,%edx
call   1030 <strncpy@plt>
jmp    11c4 <getHWAddrForInterface+0x64>
mov    -0x30(%rbp),%rdi
mov    $0x7,%esi
lea    0xe43(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jl     12ac <getHWAddrForInterface+0x14c>
mov    -0x1c(%rbp),%edi
mov    0x2ddd(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    1228 <getHWAddrForInterface+0xc8>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xdee(%rip),%rsi
call   1030 <strncpy@plt>
movl   $0x2,-0x34(%rbp)
jmp    129f <getHWAddrForInterface+0x13f>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rax
mov    (%rax),%ecx
and    $0xff,%ecx
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%r8d
and    $0xff,%r8d
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%r9d
and    $0xff,%r9d
mov    -0x28(%rbp),%rax
mov    0xc(%rax),%r11d
and    $0xff,%r11d
mov    -0x28(%rbp),%rax
mov    0x10(%rax),%r10d
and    $0xff,%r10d
mov    -0x28(%rbp),%rax
mov    0x14(%rax),%eax
and    $0xff,%eax
lea    0xd82(%rip),%rdx
mov    %r11d,(%rsp)
mov    %r10d,0x8(%rsp)
mov    %eax,0x10(%rsp)
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
jmp    12b3 <getHWAddrForInterface+0x153>
movl   $0x1,-0x34(%rbp)
mov    -0x34(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getHWAddrForInterface>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    (%rsp),%rdi
test   %rbx,%rbx
je     119c <getHWAddrForInterface+0x3c>
mov    $0x7,%edx
mov    %rbx,%rsi
call   1030 <strncpy@plt>
jmp    11a6 <getHWAddrForInterface+0x46>
movb   $0x0,0x4(%rdi)
movl   $0x30687465,(%rdi)
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
test   %eax,%eax
js     1224 <getHWAddrForInterface+0xc4>
mov    %eax,%ebp
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1060 <ioctl@plt>
test   %rax,%rax
js     122b <getHWAddrForInterface+0xcb>
mov    0x8(%rsp),%rax
movzbl (%rax),%ecx
movzbl 0x4(%rax),%r8d
movzbl 0x8(%rax),%r9d
movzbl 0xc(%rax),%r10d
movzbl 0x10(%rax),%r11d
movzbl 0x14(%rax),%r12d
sub    $0x8,%rsp
lea    0xe04(%rip),%rdx
xor    %ebx,%ebx
mov    %r15,%rdi
mov    %r14,%rsi
xor    %eax,%eax
push   %r12
push   %r11
push   %r10
call   1040 <snprintf@plt>
add    $0x20,%rsp
jmp    1242 <getHWAddrForInterface+0xe2>
mov    $0x1,%ebx
jmp    1249 <getHWAddrForInterface+0xe9>
lea    0xdd3(%rip),%rsi
mov    %r15,%rdi
mov    %r14,%rdx
call   1030 <strncpy@plt>
mov    $0x2,%ebx
mov    %ebp,%edi
call   1070 <close@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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