#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 */

/* Type definitions */
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ addr_file;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *, char *);
char *strchr(char *, char);
char *strdup(char *);
unsigned int strlen(char *);
char toupper(char);
char *kvp_if_name_to_mac(char *if_name) {
  FILE *file;
  char *p, *x;
  char buf[256];
  char addr_file[PATH_MAX];
  unsigned int i;
  char *mac_addr = NULL;
  snprintf(addr_file, sizeof(addr_file), "%s%s%s", "/sys/class/net/", if_name,
           "/address");
  file = fopen(addr_file, "r");
  if (file == NULL)
    return NULL;
  p = fgets(buf, sizeof(buf), file);
  if (p) {
    x = strchr(p, '\n');
    if (x)
      *x = '\0';
    for (i = 0; i < strlen(p); i++)
      p[i] = toupper(p[i]);
    mac_addr = strdup(p);
  }
  fclose(file);
  return mac_addr;
}


x86 assembly (clang, -O0):
<kvp_if_name_to_mac>:
push   %rbp
mov    %rsp,%rbp
sub    $0x170,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x138(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x160(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x140(%rbp)
movq   $0x0,-0x150(%rbp)
mov    %eax,%esi
mov    -0x10(%rbp),%r8
lea    0xe24(%rip),%rdx
lea    0xe24(%rip),%rcx
lea    0xe2d(%rip),%r9
call   1070 <snprintf@plt>
mov    -0x160(%rbp),%rdi
lea    0xe23(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1228 <kvp_if_name_to_mac+0xa8>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x154(%rbp)
jmp    1303 <kvp_if_name_to_mac+0x183>
lea    -0x130(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x100,%esi
call   1080 <fgets@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     12e5 <kvp_if_name_to_mac+0x165>
mov    -0x20(%rbp),%rdi
mov    $0xa,%esi
call   1060 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1270 <kvp_if_name_to_mac+0xf0>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x0,-0x144(%rbp)
mov    -0x144(%rbp),%eax
mov    %eax,-0x164(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x164(%rbp),%eax
cmp    %ecx,%eax
jae    12d5 <kvp_if_name_to_mac+0x155>
mov    -0x20(%rbp),%rax
mov    -0x144(%rbp),%ecx
movsbl (%rax,%rcx,1),%edi
call   1030 <toupper@plt>
mov    %al,%dl
mov    -0x20(%rbp),%rax
mov    -0x144(%rbp),%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0x144(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x144(%rbp)
jmp    127a <kvp_if_name_to_mac+0xfa>
mov    -0x20(%rbp),%rdi
call   10a0 <strdup@plt>
mov    %rax,-0x150(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x150(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x154(%rbp)
mov    -0x138(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kvp_if_name_to_mac>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x100,%rsp
mov    %rdi,%r8
mov    %rsp,%r15
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rbx
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe44(%rip),%rdx
lea    0xe44(%rip),%rcx
lea    0xe4d(%rip),%r9
mov    %rbx,%rdi
call   1070 <snprintf@plt>
lea    0xe47(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     125e <kvp_if_name_to_mac+0xde>
mov    %rax,%r14
lea    -0x120(%rbp),%rdi
mov    $0x100,%esi
mov    %rax,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     1262 <kvp_if_name_to_mac+0xe2>
mov    %rax,%r12
mov    %rax,%rdi
mov    $0xa,%esi
call   1060 <strchr@plt>
test   %rax,%rax
je     121a <kvp_if_name_to_mac+0x9a>
movb   $0x0,(%rax)
mov    %r12,%rdi
call   1050 <strlen@plt>
test   %eax,%eax
je     1251 <kvp_if_name_to_mac+0xd1>
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
movsbl (%r12,%rbx,1),%edi
call   1030 <toupper@plt>
mov    %al,(%r12,%rbx,1)
add    $0x1,%rbx
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    %eax,%eax
cmp    %rax,%rbx
jb     1230 <kvp_if_name_to_mac+0xb0>
mov    %r12,%rdi
call   10a0 <strdup@plt>
mov    %rax,%rbx
jmp    1264 <kvp_if_name_to_mac+0xe4>
xor    %ebx,%ebx
jmp    126c <kvp_if_name_to_mac+0xec>
xor    %ebx,%ebx
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %r15,%rsp
mov    %rbx,%rax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [7 x i8] c"%s%s%s\00"
@.str.1 = [16 x i8] c"/sys/class/net/\00"
@.str.2 = [9 x i8] c"/address\00"
@.str.3 = [2 x i8] c"r\00"
define i8* @kvp_if_name_to_mac(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca [256 x i8]
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
store i8* %0, i8** %3
%13 = load i32, i32* @PATH_MAX
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %8
%16 = alloca i8, i64 %14
store i64 %14, i64* %9
store i8* null, i8** %11
%17 = trunc i64 %14 to i32
%18 = load i8*, i8** %3
%19 = call i32 @snprintf(i8* %16, i32 %17, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %18, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%20 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %20, i32** %4
%21 = load i32*, i32** %4
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %24
23: ; preds = %1
store i8* null, i8** %2
store i32 1, i32* %12
br label %64
24: ; preds = %1
%25 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%26 = load i32*, i32** %4
%27 = call i8* @fgets(i8* %25, i32 256, i32* %26)
store i8* %27, i8** %5
%28 = load i8*, i8** %5
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %60
30: ; preds = %24
%31 = load i8*, i8** %5
%32 = call i8* @strchr(i8* %31, i8 signext 10)
store i8* %32, i8** %6
%33 = load i8*, i8** %6
%34 = icmp ne i8* %33, null
br i1 %34, label %35, label %37
35: ; preds = %30
%36 = load i8*, i8** %6
store i8 0, i8* %36
br label %37
37: ; preds = %35, %30
store i32 0, i32* %10
br label %38
38: ; preds = %54, %37
%39 = load i32, i32* %10
%40 = load i8*, i8** %5
%41 = call i32 @strlen(i8* %40)
%42 = icmp ult i32 %39, %41
br i1 %42, label %43, label %57
43: ; preds = %38
%44 = load i8*, i8** %5
%45 = load i32, i32* %10
%46 = zext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %44, i64 %46
%48 = load i8, i8* %47
%49 = call signext i8 @toupper(i8 signext %48)
%50 = load i8*, i8** %5
%51 = load i32, i32* %10
%52 = zext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %50, i64 %52
store i8 %49, i8* %53
br label %54
54: ; preds = %43
%55 = load i32, i32* %10
%56 = add i32 %55, 1
store i32 %56, i32* %10
br label %38
57: ; preds = %38
%58 = load i8*, i8** %5
%59 = call i8* @strdup(i8* %58)
store i8* %59, i8** %11
br label %60
60: ; preds = %57, %24
%61 = load i32*, i32** %4
%62 = call i32 @fclose(i32* %61)
%63 = load i8*, i8** %11
store i8* %63, i8** %2
store i32 1, i32* %12
br label %64
64: ; preds = %60, %23
%65 = load i8*, i8** %8
%66 = load i8*, i8** %2
ret i8* %66
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [7 x i8] c"%s%s%s\00"
@.str.1 = [16 x i8] c"/sys/class/net/\00"
@.str.2 = [9 x i8] c"/address\00"
@.str.3 = [2 x i8] c"r\00"
define noalias i8* @kvp_if_name_to_mac(i8* %0) {
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @PATH_MAX
%5 = zext i32 %4 to i64
%6 = tail call i8* @llvm.stacksave()
%7 = alloca i8, i64 %5
%8 = call i32 @snprintf(i8* %7, i32 %4, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%9 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %35, label %11
11: ; preds = %1
%12 = call i8* @fgets(i8* %3, i32 256, i32* %9)
%13 = icmp eq i8* %12, null
br i1 %13, label %32, label %14
14: ; preds = %11
%15 = call i8* @strchr(i8* %12, i8 signext 10)
%16 = icmp eq i8* %15, null
br i1 %16, label %18, label %17
17: ; preds = %14
store i8 0, i8* %15
br label %18
18: ; preds = %17, %14
%19 = call i32 @strlen(i8* %12)
%20 = icmp eq i32 %19, 0
br i1 %20, label %30, label %21
21: ; preds = %18, %21
%22 = phi i64 [ %26, %21 ], [ 0, %18 ]
%23 = getelementptr inbounds i8, i8* %12, i64 %22
%24 = load i8, i8* %23
%25 = call signext i8 @toupper(i8 signext %24)
store i8 %25, i8* %23
%26 = add nuw nsw i64 %22, 1
%27 = call i32 @strlen(i8* %12)
%28 = zext i32 %27 to i64
%29 = icmp ult i64 %26, %28
br i1 %29, label %21, label %30
30: ; preds = %21, %18
%31 = call i8* @strdup(i8* %12)
br label %32
32: ; preds = %30, %11
%33 = phi i8* [ %31, %30 ], [ null, %11 ]
%34 = call i32 @fclose(i32* %9)
br label %35
35: ; preds = %1, %32
%36 = phi i8* [ %33, %32 ], [ null, %1 ]
ret i8* %36
}