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

/* 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*/ memcpy(void *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
int get_hwaddr(const char *ifname, char *hwaddr) {
  int fd = socket(PF_INET, SOCK_DGRAM, 0);
  if (fd < 0)
    return -1;
  struct ifreq req_hwaddr;
  strcpy(req_hwaddr.ifr_name, ifname);
  if (ioctl(fd, SIOCGIFHWADDR, &req_hwaddr) < 0)
    goto err;
  memcpy((void *)hwaddr, req_hwaddr.ifr_hwaddr.sa_data, sizeof(char) * 6);
  close(fd);
  return 0;
err:
  close(fd);
  return -1;
}


x86 assembly (clang, -O0):
<get_hwaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1192 <get_hwaddr+0x42>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f6 <get_hwaddr+0xa6>
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x1c(%rbp),%edi
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    11c2 <get_hwaddr+0x72>
jmp    11e7 <get_hwaddr+0x97>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%esi
mov    $0x6,%edx
call   1060 <memcpy@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    11f6 <get_hwaddr+0xa6>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_hwaddr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e6c(%rip),%rax
mov    (%rax),%edi
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
test   %eax,%eax
js     11b0 <get_hwaddr+0x60>
mov    %eax,%ebx
mov    %r15,%rsi
call   1030 <strcpy@plt>
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
js     11b7 <get_hwaddr+0x67>
mov    (%rsp),%esi
mov    %r14,%rdi
mov    $0x6,%edx
call   1060 <memcpy@plt>
xor    %ebp,%ebp
jmp    11bc <get_hwaddr+0x6c>
mov    $0xffffffff,%ebp
jmp    11c3 <get_hwaddr+0x73>
mov    $0xffffffff,%ebp
mov    %ebx,%edi
call   1050 <close@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFHWADDR = global i32 0
define i32 @get_hwaddr(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.ifreq
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i32, i32* @PF_INET
%9 = load i32, i32* @SOCK_DGRAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 -1, i32* %3
br label %35
14: ; preds = %2
%15 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 1
%16 = load i32, i32* %15
%17 = load i8*, i8** %4
%18 = call i32 @strcpy(i32 %16, i8* %17)
%19 = load i32, i32* %6
%20 = load i32, i32* @SIOCGIFHWADDR
%21 = call i64 @ioctl(i32 %19, i32 %20, %struct.ifreq* %7)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %14
br label %32
24: ; preds = %14
%25 = load i8*, i8** %5
%26 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 0
%27 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %26, i32 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 @memcpy(i8* %25, i32 %28, i32 6)
%30 = load i32, i32* %6
%31 = call i32 @close(i32 %30)
store i32 0, i32* %3
br label %35
32: ; preds = %23
%33 = load i32, i32* %6
%34 = call i32 @close(i32 %33)
store i32 -1, i32* %3
br label %35
35: ; preds = %32, %24, %13
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFHWADDR = global i32 0
define i32 @get_hwaddr(i8* %0, i8* %1) {
%3 = alloca %struct.ifreq
%4 = load i32, i32* @PF_INET
%5 = load i32, i32* @SOCK_DGRAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %21, label %8
8: ; preds = %2
%9 = bitcast %struct.ifreq* %3 to i8*
%10 = tail call i32 @strcpy(i32 undef, i8* %0)
%11 = load i32, i32* @SIOCGIFHWADDR
%12 = call i64 @ioctl(i32 %6, i32 %11, %struct.ifreq* %3)
%13 = icmp slt i64 %12, 0
br i1 %13, label %18, label %14
14: ; preds = %8
%15 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0, i32 0
%16 = load i32, i32* %15
%17 = call i32 @memcpy(i8* %1, i32 %16, i32 6)
br label %18
18: ; preds = %8, %14
%19 = phi i32 [ 0, %14 ], [ -1, %8 ]
%20 = call i32 @close(i32 %6)
br label %21
21: ; preds = %2, %18
%22 = phi i32 [ %19, %18 ], [ -1, %2 ]
ret i32 %22
}