#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_hwaddr;
  int /*<<< orphan*/ ifr_name;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SIOCGIFHWADDR;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char, unsigned char,
                           unsigned char, unsigned char, unsigned char,
                           unsigned char);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int readmac(char *ethchar, char *macaddr) {
  struct ifreq ifreq;
  int sock;
  if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    perror("socket ");
    return 2;
  }
  strcpy(ifreq.ifr_name, ethchar);
  if (ioctl(sock, SIOCGIFHWADDR, &ifreq) < 0) {
    perror("ioctl ");
    return 3;
  }
  sprintf(macaddr, "%02x:%02x:%02x:%02x:%02x:%02x",
          (unsigned char)ifreq.ifr_hwaddr.sa_data[0],
          (unsigned char)ifreq.ifr_hwaddr.sa_data[1],
          (unsigned char)ifreq.ifr_hwaddr.sa_data[2],
          (unsigned char)ifreq.ifr_hwaddr.sa_data[3],
          (unsigned char)ifreq.ifr_hwaddr.sa_data[4],
          (unsigned char)ifreq.ifr_hwaddr.sa_data[5]);
  return 0;
}


x86 assembly (clang, -O0):
<readmac>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e81(%rip),%rax
mov    (%rax),%edi
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    119d <readmac+0x4d>
lea    0xe74(%rip),%rdi
call   1050 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1250 <readmac+0x100>
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x2c(%rbp),%edi
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    11e0 <readmac+0x90>
lea    0xe39(%rip),%rdi
call   1050 <perror@plt>
movl   $0x3,-0x4(%rbp)
jmp    1250 <readmac+0x100>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    %al,%dl
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rax
mov    %al,%cl
mov    -0x28(%rbp),%rax
mov    0x10(%rax),%rax
mov    %al,%r8b
mov    -0x28(%rbp),%rax
mov    0x18(%rax),%rax
mov    %al,%r9b
mov    -0x28(%rbp),%rax
mov    0x20(%rax),%rax
mov    %al,%r10b
mov    -0x28(%rbp),%rax
mov    0x28(%rax),%rax
lea    0xde8(%rip),%rsi
movzbl %dl,%edx
movzbl %cl,%ecx
movzbl %r8b,%r8d
movzbl %r9b,%r9d
movzbl %r10b,%r10d
mov    %r10d,(%rsp)
movzbl %al,%eax
mov    %eax,0x8(%rsp)
call   1060 <sprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readmac>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e83(%rip),%rax
mov    (%rax),%edi
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
test   %eax,%eax
js     11da <readmac+0x8a>
mov    %eax,%ebp
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
js     11ed <readmac+0x9d>
mov    (%rsp),%rax
movzbl (%rax),%edx
movzbl 0x8(%rax),%ecx
movzbl 0x10(%rax),%r8d
movzbl 0x18(%rax),%r9d
movzbl 0x20(%rax),%ebp
movzbl 0x28(%rax),%eax
lea    0xe4e(%rip),%rsi
mov    %r14,%rdi
push   %rax
push   %rbp
call   1060 <sprintf@plt>
add    $0x10,%rsp
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe1f(%rip),%rdi
call   1050 <perror@plt>
mov    $0x2,%eax
jmp    11d1 <readmac+0x81>
lea    0xe14(%rip),%rdi
call   1050 <perror@plt>
mov    $0x3,%eax
jmp    11d1 <readmac+0x81>

LLVM IR (-O0):
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i64* }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [8 x i8] c"socket \00"
@SIOCGIFHWADDR = global i32 0
@.str.1 = [7 x i8] c"ioctl \00"
@.str.2 = [30 x i8] c"%02x:%02x:%02x:%02x:%02x:%02x\00"
define i32 @readmac(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.ifreq
%7 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %7
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i32 2, i32* %3
br label %64
14: ; preds = %2
%15 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, 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* %7
%20 = load i32, i32* @SIOCGIFHWADDR
%21 = call i64 @ioctl(i32 %19, i32 %20, %struct.ifreq* %6)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %14
%24 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 3, i32* %3
br label %64
25: ; preds = %14
%26 = load i8*, i8** %5
%27 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 0
%29 = load i64*, i64** %28
%30 = getelementptr inbounds i64, i64* %29, i64 0
%31 = load i64, i64* %30
%32 = trunc i64 %31 to i8
%33 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%34 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %33, i32 0, i32 0
%35 = load i64*, i64** %34
%36 = getelementptr inbounds i64, i64* %35, i64 1
%37 = load i64, i64* %36
%38 = trunc i64 %37 to i8
%39 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%40 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %39, i32 0, i32 0
%41 = load i64*, i64** %40
%42 = getelementptr inbounds i64, i64* %41, i64 2
%43 = load i64, i64* %42
%44 = trunc i64 %43 to i8
%45 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%46 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %45, i32 0, i32 0
%47 = load i64*, i64** %46
%48 = getelementptr inbounds i64, i64* %47, i64 3
%49 = load i64, i64* %48
%50 = trunc i64 %49 to i8
%51 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%52 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %51, i32 0, i32 0
%53 = load i64*, i64** %52
%54 = getelementptr inbounds i64, i64* %53, i64 4
%55 = load i64, i64* %54
%56 = trunc i64 %55 to i8
%57 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%58 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %57, i32 0, i32 0
%59 = load i64*, i64** %58
%60 = getelementptr inbounds i64, i64* %59, i64 5
%61 = load i64, i64* %60
%62 = trunc i64 %61 to i8
%63 = call i32 @sprintf(i8* %26, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %32, i8 zeroext %38, i8 zeroext %44, i8 zeroext %50, i8 zeroext %56, i8 zeroext %62)
store i32 0, i32* %3
br label %64
64: ; preds = %25, %23, %12
%65 = load i32, i32* %3
ret i32 %65
}

LLVM IR (-O2):
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i64* }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [8 x i8] c"socket \00"
@SIOCGIFHWADDR = global i32 0
@.str.1 = [7 x i8] c"ioctl \00"
@.str.2 = [30 x i8] c"%02x:%02x:%02x:%02x:%02x:%02x\00"
define i32 @readmac(i8* %0, i8* nocapture writeonly %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %39
11: ; preds = %2
%12 = tail call i32 @strcpy(i32 undef, i8* %0)
%13 = load i32, i32* @SIOCGIFHWADDR
%14 = call i64 @ioctl(i32 %7, i32 %13, %struct.ifreq* %3)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %39
18: ; preds = %11
%19 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0, i32 0
%20 = load i64*, i64** %19
%21 = load i64, i64* %20
%22 = trunc i64 %21 to i8
%23 = getelementptr inbounds i64, i64* %20, i64 1
%24 = load i64, i64* %23
%25 = trunc i64 %24 to i8
%26 = getelementptr inbounds i64, i64* %20, i64 2
%27 = load i64, i64* %26
%28 = trunc i64 %27 to i8
%29 = getelementptr inbounds i64, i64* %20, i64 3
%30 = load i64, i64* %29
%31 = trunc i64 %30 to i8
%32 = getelementptr inbounds i64, i64* %20, i64 4
%33 = load i64, i64* %32
%34 = trunc i64 %33 to i8
%35 = getelementptr inbounds i64, i64* %20, i64 5
%36 = load i64, i64* %35
%37 = trunc i64 %36 to i8
%38 = call i32 @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %22, i8 zeroext %25, i8 zeroext %28, i8 zeroext %31, i8 zeroext %34, i8 zeroext %37)
br label %39
39: ; preds = %18, %16, %9
%40 = phi i32 [ 2, %9 ], [ 3, %16 ], [ 0, %18 ]
ret i32 %40
}