#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_4__ {
  int /*<<< orphan*/ *sa_data;
};
struct TYPE_3__ {
  int /*<<< orphan*/ sa_family;
};
struct ifreq {
  TYPE_2__ ifr_hwaddr;
  int /*<<< orphan*/ ifr_name;
  TYPE_1__ ifr_addr;
};
typedef int /*<<< orphan*/ ifr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ IPPROTO_IP;
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 snprintf(char *, int, char *, unsigned char, unsigned char, unsigned char,
             unsigned char, unsigned char, unsigned char);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int show_hwaddr(char *ifname, char *ptr, int length) {
  int fd = -1, n;
  unsigned char *tmp;
  struct ifreq ifr;
  if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP)) <= -1)
    goto err;
  memset(&ifr, 0, sizeof(ifr));
  ifr.ifr_addr.sa_family = AF_INET;
  strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
  if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0)
    goto err;
  tmp = (unsigned char *)&ifr.ifr_hwaddr.sa_data[0];
  n = snprintf(ptr, length, "%02X:%02X:%02X:%02X:%02X:%02X", tmp[0], tmp[1],
               tmp[2], tmp[3], tmp[4], tmp[5]);
  if (n <= 0)
    goto err;
  close(fd);
  return 0;
err:
  if (fd > -1)
    close(fd);
  return 1;
}


x86 assembly (clang, -O0):
<show_hwaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0xffffffff,-0x20(%rbp)
mov    0x2e67(%rip),%rax
mov    (%rax),%edi
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
mov    0x2e4d(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
jg     11ab <show_hwaddr+0x4b>
jmp    128f <show_hwaddr+0x12f>
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2e26(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x38(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2deb(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%edi
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    1200 <show_hwaddr+0xa0>
jmp    128f <show_hwaddr+0x12f>
mov    -0x40(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x30(%rbp),%rax
mov    (%rax),%cl
mov    -0x30(%rbp),%rax
mov    0x1(%rax),%r8b
mov    -0x30(%rbp),%rax
mov    0x2(%rax),%r9b
mov    -0x30(%rbp),%rax
mov    0x3(%rax),%r11b
mov    -0x30(%rbp),%rax
mov    0x4(%rax),%r10b
mov    -0x30(%rbp),%rax
lea    0xdc0(%rip),%rdx
movzbl %cl,%ecx
movzbl %r8b,%r8d
movzbl %r9b,%r9d
movzbl %r11b,%r11d
mov    %r11d,(%rsp)
movzbl %r10b,%r10d
mov    %r10d,0x8(%rsp)
movzbl 0x5(%rax),%eax
mov    %eax,0x10(%rsp)
call   1040 <snprintf@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jg     127b <show_hwaddr+0x11b>
jmp    128f <show_hwaddr+0x12f>
mov    -0x20(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    12a8 <show_hwaddr+0x148>
cmpl   $0xffffffff,-0x20(%rbp)
jle    12a1 <show_hwaddr+0x141>
mov    -0x20(%rbp),%edi
call   1070 <close@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<show_hwaddr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edx,%ebx
mov    %rsi,%rbp
mov    %rdi,%r15
mov    0x2e6b(%rip),%r14
mov    (%r14),%edi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
mov    0x2e50(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
test   %eax,%eax
js     124b <show_hwaddr+0xeb>
mov    %eax,%r13d
mov    %rbp,0x10(%rsp)
mov    %ebx,0xc(%rsp)
lea    0x18(%rsp),%rbx
xor    %r12d,%r12d
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    (%r14),%eax
mov    %eax,0x24(%rsp)
mov    0x20(%rsp),%edi
mov    0x2dec(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rsi
call   1030 <strncpy@plt>
mov    0x2de3(%rip),%rax
mov    (%rax),%esi
mov    %r13d,%edi
mov    %rbx,%rdx
call   1060 <ioctl@plt>
test   %rax,%rax
js     123b <show_hwaddr+0xdb>
mov    0x18(%rsp),%rax
movzbl (%rax),%ecx
movzbl 0x1(%rax),%r8d
movzbl 0x2(%rax),%r9d
movzbl 0x3(%rax),%r10d
movzbl 0x4(%rax),%ebx
movzbl 0x5(%rax),%eax
sub    $0x8,%rsp
lea    0xddf(%rip),%rdx
mov    0x18(%rsp),%rdi
mov    0x14(%rsp),%esi
push   %rax
push   %rbx
push   %r10
call   1040 <snprintf@plt>
add    $0x20,%rsp
test   %eax,%eax
jg     1241 <show_hwaddr+0xe1>
mov    $0x1,%r12d
mov    %r13d,%edi
call   1070 <close@plt>
jmp    1251 <show_hwaddr+0xf1>
mov    $0x1,%r12d
mov    %r12d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { %struct.TYPE_4__, i32, %struct.TYPE_3__ }
%struct.TYPE_4__ = type { i32* }
%struct.TYPE_3__ = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_IP = global i32 0
@IFNAMSIZ = global i32 0
@SIOCGIFHWADDR = global i32 0
@.str = [30 x i8] c"%02X:%02X:%02X:%02X:%02X:%02X\00"
define i32 @show_hwaddr(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca %struct.ifreq
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 -1, i32* %8
%12 = load i32, i32* @AF_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* %8
%16 = icmp sle i32 %15, -1
br i1 %16, label %17, label %18
17: ; preds = %3
br label %66
18: ; preds = %3
%19 = call i32 @memset(%struct.ifreq* %11, i32 0, i32 16)
%20 = load i32, i32* @AF_INET
%21 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %11, i32 0, i32 2
%22 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %21, i32 0, i32 0
store i32 %20, i32* %22
%23 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %11, i32 0, i32 1
%24 = load i32, i32* %23
%25 = load i8*, i8** %5
%26 = load i32, i32* @IFNAMSIZ
%27 = call i32 @strncpy(i32 %24, i8* %25, i32 %26)
%28 = load i32, i32* %8
%29 = load i32, i32* @SIOCGIFHWADDR
%30 = call i64 @ioctl(i32 %28, i32 %29, %struct.ifreq* %11)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %18
br label %66
33: ; preds = %18
%34 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %11, i32 0, i32 0
%35 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %34, i32 0, i32 0
%36 = load i32*, i32** %35
%37 = getelementptr inbounds i32, i32* %36, i64 0
%38 = bitcast i32* %37 to i8*
store i8* %38, i8** %10
%39 = load i8*, i8** %6
%40 = load i32, i32* %7
%41 = load i8*, i8** %10
%42 = getelementptr inbounds i8, i8* %41, i64 0
%43 = load i8, i8* %42
%44 = load i8*, i8** %10
%45 = getelementptr inbounds i8, i8* %44, i64 1
%46 = load i8, i8* %45
%47 = load i8*, i8** %10
%48 = getelementptr inbounds i8, i8* %47, i64 2
%49 = load i8, i8* %48
%50 = load i8*, i8** %10
%51 = getelementptr inbounds i8, i8* %50, i64 3
%52 = load i8, i8* %51
%53 = load i8*, i8** %10
%54 = getelementptr inbounds i8, i8* %53, i64 4
%55 = load i8, i8* %54
%56 = load i8*, i8** %10
%57 = getelementptr inbounds i8, i8* %56, i64 5
%58 = load i8, i8* %57
%59 = call i32 @snprintf(i8* %39, i32 %40, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8 zeroext %43, i8 zeroext %46, i8 zeroext %49, i8 zeroext %52, i8 zeroext %55, i8 zeroext %58)
store i32 %59, i32* %9
%60 = load i32, i32* %9
%61 = icmp sle i32 %60, 0
br i1 %61, label %62, label %63
62: ; preds = %33
br label %66
63: ; preds = %33
%64 = load i32, i32* %8
%65 = call i32 @close(i32 %64)
store i32 0, i32* %4
br label %73
66: ; preds = %62, %32, %17
%67 = load i32, i32* %8
%68 = icmp sgt i32 %67, -1
br i1 %68, label %69, label %72
69: ; preds = %66
%70 = load i32, i32* %8
%71 = call i32 @close(i32 %70)
br label %72
72: ; preds = %69, %66
store i32 1, i32* %4
br label %73
73: ; preds = %72, %63
%74 = load i32, i32* %4
ret i32 %74
}

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