#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*/ IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFHWADDR;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned char);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
size_t strlen(char *);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int get_net_mac(char *net_if_name, char *mac, int mac_len) {
  int ret = 1;
  int i = 0;
  int sock = 0;
  if (mac_len < 12 || net_if_name == NULL) {
    return 1;
  }
  struct ifreq ifreq;
  sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock < 0) {
    perror("error sock");
    goto OUT;
  }
  strncpy(ifreq.ifr_name, net_if_name, IFNAMSIZ);
  if (ioctl(sock, SIOCGIFHWADDR, &ifreq) < 0) {
    perror("error ioctl");
    goto OUT;
  }
  for (i = 0; i < 6; i++) {
    snprintf(mac + 2 * i, mac_len - 2 * i, "%02X",
             (unsigned char)ifreq.ifr_hwaddr.sa_data[i]);
  }
  mac[strlen(mac)] = 0;
  ret = 0;
OUT:
  close(sock);
  return ret;
}


x86 assembly (clang, -O0):
<get_net_mac>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x1,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
cmpl   $0xc,-0x1c(%rbp)
jl     11ad <get_net_mac+0x3d>
cmpq   $0x0,-0x10(%rbp)
jne    11b9 <get_net_mac+0x49>
movl   $0x1,-0x4(%rbp)
jmp    12ba <get_net_mac+0x14a>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    11f0 <get_net_mac+0x80>
lea    0xe1a(%rip),%rdi
call   1080 <perror@plt>
jmp    12ac <get_net_mac+0x13c>
mov    -0x30(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2dd2(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%edi
mov    0x2dc9(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    1235 <get_net_mac+0xc5>
lea    0xde0(%rip),%rdi
call   1080 <perror@plt>
jmp    12ac <get_net_mac+0x13c>
movl   $0x0,-0x24(%rbp)
cmpl   $0x6,-0x24(%rbp)
jge    1289 <get_net_mac+0x119>
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%eax
shl    $0x1,%eax
cltq
add    %rax,%rdi
mov    -0x1c(%rbp),%esi
mov    -0x24(%rbp),%eax
shl    $0x1,%eax
sub    %eax,%esi
mov    -0x38(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
lea    0xda4(%rip),%rdx
movzbl %al,%ecx
call   1050 <snprintf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    123c <get_net_mac+0xcc>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x20(%rbp)
mov    -0x28(%rbp),%edi
call   1070 <close@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_net_mac>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    $0x1,%ebp
test   %rdi,%rdi
je     12ab <get_net_mac+0x13b>
mov    %edx,%r13d
cmp    $0xc,%edx
jl     12ab <get_net_mac+0x13b>
mov    %rsi,%r12
mov    %rdi,%rbx
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%r14d
test   %eax,%eax
js     12bc <get_net_mac+0x14c>
mov    0x2e07(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rsi
call   1030 <strncpy@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %r14d,%edi
call   1060 <ioctl@plt>
test   %rax,%rax
js     12c5 <get_net_mac+0x155>
mov    0x8(%rsp),%rax
movzbl (%rax),%ecx
lea    0xe16(%rip),%r15
mov    %r12,%rdi
mov    %r13d,%esi
mov    %r15,%rdx
call   1050 <snprintf@plt>
lea    0x2(%r12),%rdi
lea    -0x2(%r13),%esi
mov    0x8(%rsp),%rax
movzbl 0x8(%rax),%ecx
mov    %r15,%rdx
call   1050 <snprintf@plt>
lea    0x4(%r12),%rdi
lea    -0x4(%r13),%esi
mov    0x8(%rsp),%rax
movzbl 0x10(%rax),%ecx
mov    %r15,%rdx
call   1050 <snprintf@plt>
lea    0x6(%r12),%rdi
lea    -0x6(%r13),%esi
mov    0x8(%rsp),%rax
movzbl 0x18(%rax),%ecx
mov    %r15,%rdx
call   1050 <snprintf@plt>
lea    0x8(%r12),%rdi
lea    -0x8(%r13),%esi
mov    0x8(%rsp),%rax
movzbl 0x20(%rax),%ecx
mov    %r15,%rdx
call   1050 <snprintf@plt>
lea    0xa(%r12),%rdi
add    $0xfffffff6,%r13d
mov    0x8(%rsp),%rax
movzbl 0x28(%rax),%ecx
mov    %r13d,%esi
mov    %r15,%rdx
call   1050 <snprintf@plt>
mov    %r12,%rdi
call   1040 <strlen@plt>
movb   $0x0,(%r12,%rax,1)
xor    %ebp,%ebp
mov    %r14d,%edi
call   1070 <close@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd3d(%rip),%rdi
jmp    12cc <get_net_mac+0x15c>
lea    0xd3f(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%ebp
jmp    12a3 <get_net_mac+0x133>

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 = [11 x i8] c"error sock\00"
@IFNAMSIZ = global i32 0
@SIOCGIFHWADDR = global i32 0
@.str.1 = [12 x i8] c"error ioctl\00"
@.str.2 = [5 x i8] c"%02X\00"
define i32 @get_net_mac(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 i32
%11 = alloca %struct.ifreq
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 1, i32* %8
store i32 0, i32* %9
store i32 0, i32* %10
%12 = load i32, i32* %7
%13 = icmp slt i32 %12, 12
br i1 %13, label %17, label %14
14: ; preds = %3
%15 = load i8*, i8** %5
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %18
17: ; preds = %14, %3
store i32 1, i32* %4
br label %73
18: ; preds = %14
%19 = load i32, i32* @AF_INET
%20 = load i32, i32* @SOCK_STREAM
%21 = call i32 @socket(i32 %19, i32 %20, i32 0)
store i32 %21, i32* %10
%22 = load i32, i32* %10
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %69
26: ; preds = %18
%27 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %11, i32 0, i32 1
%28 = load i32, i32* %27
%29 = load i8*, i8** %5
%30 = load i32, i32* @IFNAMSIZ
%31 = call i32 @strncpy(i32 %28, i8* %29, i32 %30)
%32 = load i32, i32* %10
%33 = load i32, i32* @SIOCGIFHWADDR
%34 = call i64 @ioctl(i32 %32, i32 %33, %struct.ifreq* %11)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %26
%37 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %69
38: ; preds = %26
store i32 0, i32* %9
br label %39
39: ; preds = %61, %38
%40 = load i32, i32* %9
%41 = icmp slt i32 %40, 6
br i1 %41, label %42, label %64
42: ; preds = %39
%43 = load i8*, i8** %6
%44 = load i32, i32* %9
%45 = mul nsw i32 2, %44
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %43, i64 %46
%48 = load i32, i32* %7
%49 = load i32, i32* %9
%50 = mul nsw i32 2, %49
%51 = sub nsw i32 %48, %50
%52 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %11, i32 0, i32 0
%53 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %52, i32 0, i32 0
%54 = load i64*, i64** %53
%55 = load i32, i32* %9
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i64, i64* %54, i64 %56
%58 = load i64, i64* %57
%59 = trunc i64 %58 to i8
%60 = call i32 @snprintf(i8* %47, i32 %51, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %59)
br label %61
61: ; preds = %42
%62 = load i32, i32* %9
%63 = add nsw i32 %62, 1
store i32 %63, i32* %9
br label %39
64: ; preds = %39
%65 = load i8*, i8** %6
%66 = load i8*, i8** %6
%67 = call i64 @strlen(i8* %66)
%68 = getelementptr inbounds i8, i8* %65, i64 %67
store i8 0, i8* %68
store i32 0, i32* %8
br label %69
69: ; preds = %64, %36, %24
%70 = load i32, i32* %10
%71 = call i32 @close(i32 %70)
%72 = load i32, i32* %8
store i32 %72, i32* %4
br label %73
73: ; preds = %69, %17
%74 = load i32, i32* %4
ret i32 %74
}

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 = [11 x i8] c"error sock\00"
@IFNAMSIZ = global i32 0
@SIOCGIFHWADDR = global i32 0
@.str.1 = [12 x i8] c"error ioctl\00"
@.str.2 = [5 x i8] c"%02X\00"
define i32 @get_net_mac(i8* %0, i8* %1, i32 %2) {
%4 = alloca %struct.ifreq
%5 = icmp slt i32 %2, 12
%6 = icmp eq i8* %0, null
%7 = or i1 %6, %5
br i1 %7, label %70, label %8
8: ; preds = %3
%9 = bitcast %struct.ifreq* %4 to i8*
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = tail call i32 @socket(i32 %10, i32 %11, i32 0)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %8
%15 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %67
16: ; preds = %8
%17 = load i32, i32* @IFNAMSIZ
%18 = tail call i32 @strncpy(i32 undef, i8* %0, i32 %17)
%19 = load i32, i32* @SIOCGIFHWADDR
%20 = call i64 @ioctl(i32 %12, i32 %19, %struct.ifreq* %4)
%21 = icmp slt i64 %20, 0
br i1 %21, label %65, label %22
22: ; preds = %16
%23 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 0, i32 0
%24 = load i64*, i64** %23
%25 = load i64, i64* %24
%26 = trunc i64 %25 to i8
%27 = call i32 @snprintf(i8* %1, i32 %2, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %26)
%28 = getelementptr inbounds i8, i8* %1, i64 2
%29 = add nsw i32 %2, -2
%30 = load i64*, i64** %23
%31 = getelementptr inbounds i64, i64* %30, i64 1
%32 = load i64, i64* %31
%33 = trunc i64 %32 to i8
%34 = call i32 @snprintf(i8* %28, i32 %29, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %33)
%35 = getelementptr inbounds i8, i8* %1, i64 4
%36 = add nsw i32 %2, -4
%37 = load i64*, i64** %23
%38 = getelementptr inbounds i64, i64* %37, i64 2
%39 = load i64, i64* %38
%40 = trunc i64 %39 to i8
%41 = call i32 @snprintf(i8* %35, i32 %36, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %40)
%42 = getelementptr inbounds i8, i8* %1, i64 6
%43 = add nsw i32 %2, -6
%44 = load i64*, i64** %23
%45 = getelementptr inbounds i64, i64* %44, i64 3
%46 = load i64, i64* %45
%47 = trunc i64 %46 to i8
%48 = call i32 @snprintf(i8* %42, i32 %43, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %47)
%49 = getelementptr inbounds i8, i8* %1, i64 8
%50 = add nsw i32 %2, -8
%51 = load i64*, i64** %23
%52 = getelementptr inbounds i64, i64* %51, i64 4
%53 = load i64, i64* %52
%54 = trunc i64 %53 to i8
%55 = call i32 @snprintf(i8* %49, i32 %50, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %54)
%56 = getelementptr inbounds i8, i8* %1, i64 10
%57 = add nsw i32 %2, -10
%58 = load i64*, i64** %23
%59 = getelementptr inbounds i64, i64* %58, i64 5
%60 = load i64, i64* %59
%61 = trunc i64 %60 to i8
%62 = call i32 @snprintf(i8* %56, i32 %57, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %61)
%63 = call i64 @strlen(i8* dereferenceable(1) %1)
%64 = getelementptr inbounds i8, i8* %1, i64 %63
store i8 0, i8* %64
br label %67
65: ; preds = %16
%66 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %67
67: ; preds = %22, %65, %14
%68 = phi i32 [ 1, %14 ], [ 1, %65 ], [ 0, %22 ]
%69 = call i32 @close(i32 %12)
br label %70
70: ; preds = %3, %67
%71 = phi i32 [ %68, %67 ], [ 1, %3 ]
ret i32 %71
}