#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 /*<<< 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[256];
  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    $0x250,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x240(%rbp)
lea    -0x230(%rbp),%rdi
mov    -0x10(%rbp),%r8
mov    $0x100,%esi
lea    0xe4f(%rip),%rdx
lea    0xe4f(%rip),%rcx
lea    0xe58(%rip),%r9
call   1070 <snprintf@plt>
lea    -0x230(%rbp),%rdi
lea    0xe4e(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11f3 <kvp_if_name_to_mac+0x73>
movq   $0x0,-0x8(%rbp)
jmp    12c4 <kvp_if_name_to_mac+0x144>
lea    -0x130(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x100,%esi
call   1080 <fgets@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     12b0 <kvp_if_name_to_mac+0x130>
mov    -0x20(%rbp),%rdi
mov    $0xa,%esi
call   1060 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     123b <kvp_if_name_to_mac+0xbb>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x0,-0x234(%rbp)
mov    -0x234(%rbp),%eax
mov    %eax,-0x244(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x244(%rbp),%eax
cmp    %ecx,%eax
jae    12a0 <kvp_if_name_to_mac+0x120>
mov    -0x20(%rbp),%rax
mov    -0x234(%rbp),%ecx
movsbl (%rax,%rcx,1),%edi
call   1030 <toupper@plt>
mov    %al,%dl
mov    -0x20(%rbp),%rax
mov    -0x234(%rbp),%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0x234(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x234(%rbp)
jmp    1245 <kvp_if_name_to_mac+0xc5>
mov    -0x20(%rbp),%rdi
call   10a0 <strdup@plt>
mov    %rax,-0x240(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x240(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x250,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kvp_if_name_to_mac>:
push   %r15
push   %r14
push   %rbx
sub    $0x200,%rsp
mov    %rdi,%r8
lea    0xe6a(%rip),%rdx
lea    0xe6a(%rip),%rcx
lea    0xe73(%rip),%r9
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1070 <snprintf@plt>
lea    0xe65(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     123e <kvp_if_name_to_mac+0xbe>
mov    %rax,%r14
lea    0x100(%rsp),%rdi
mov    $0x100,%esi
mov    %rax,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     1242 <kvp_if_name_to_mac+0xc2>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0xa,%esi
call   1060 <strchr@plt>
test   %rax,%rax
je     11fd <kvp_if_name_to_mac+0x7d>
movb   $0x0,(%rax)
mov    %r15,%rdi
call   1050 <strlen@plt>
test   %eax,%eax
je     1231 <kvp_if_name_to_mac+0xb1>
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
movsbl (%r15,%rbx,1),%edi
call   1030 <toupper@plt>
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %eax,%eax
cmp    %rax,%rbx
jb     1210 <kvp_if_name_to_mac+0x90>
mov    %r15,%rdi
call   10a0 <strdup@plt>
mov    %rax,%rbx
jmp    1244 <kvp_if_name_to_mac+0xc4>
xor    %ebx,%ebx
jmp    124c <kvp_if_name_to_mac+0xcc>
xor    %ebx,%ebx
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %rbx,%rax
add    $0x200,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.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 [256 x i8]
%9 = alloca i32
%10 = alloca i8*
store i8* %0, i8** %3
store i8* null, i8** %10
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%12 = load i8*, i8** %3
%13 = call i32 @snprintf(i8* %11, i32 256, 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* %12, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%14 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %15, i32** %4
%16 = load i32*, i32** %4
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %19
18: ; preds = %1
store i8* null, i8** %2
br label %59
19: ; preds = %1
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%21 = load i32*, i32** %4
%22 = call i8* @fgets(i8* %20, i32 256, i32* %21)
store i8* %22, i8** %5
%23 = load i8*, i8** %5
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %55
25: ; preds = %19
%26 = load i8*, i8** %5
%27 = call i8* @strchr(i8* %26, i8 signext 10)
store i8* %27, i8** %6
%28 = load i8*, i8** %6
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = load i8*, i8** %6
store i8 0, i8* %31
br label %32
32: ; preds = %30, %25
store i32 0, i32* %9
br label %33
33: ; preds = %49, %32
%34 = load i32, i32* %9
%35 = load i8*, i8** %5
%36 = call i32 @strlen(i8* %35)
%37 = icmp ult i32 %34, %36
br i1 %37, label %38, label %52
38: ; preds = %33
%39 = load i8*, i8** %5
%40 = load i32, i32* %9
%41 = zext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %39, i64 %41
%43 = load i8, i8* %42
%44 = call signext i8 @toupper(i8 signext %43)
%45 = load i8*, i8** %5
%46 = load i32, i32* %9
%47 = zext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %45, i64 %47
store i8 %44, i8* %48
br label %49
49: ; preds = %38
%50 = load i32, i32* %9
%51 = add i32 %50, 1
store i32 %51, i32* %9
br label %33
52: ; preds = %33
%53 = load i8*, i8** %5
%54 = call i8* @strdup(i8* %53)
store i8* %54, i8** %10
br label %55
55: ; preds = %52, %19
%56 = load i32*, i32** %4
%57 = call i32 @fclose(i32* %56)
%58 = load i8*, i8** %10
store i8* %58, i8** %2
br label %59
59: ; preds = %55, %18
%60 = load i8*, i8** %2
ret i8* %60
}

LLVM IR (-O2):
@.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 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%6 = call i32 @snprintf(i8* %5, i32 256, 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))
%7 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %33, label %9
9: ; preds = %1
%10 = call i8* @fgets(i8* %4, i32 256, i32* %7)
%11 = icmp eq i8* %10, null
br i1 %11, label %30, label %12
12: ; preds = %9
%13 = call i8* @strchr(i8* %10, i8 signext 10)
%14 = icmp eq i8* %13, null
br i1 %14, label %16, label %15
15: ; preds = %12
store i8 0, i8* %13
br label %16
16: ; preds = %15, %12
%17 = call i32 @strlen(i8* %10)
%18 = icmp eq i32 %17, 0
br i1 %18, label %28, label %19
19: ; preds = %16, %19
%20 = phi i64 [ %24, %19 ], [ 0, %16 ]
%21 = getelementptr inbounds i8, i8* %10, i64 %20
%22 = load i8, i8* %21
%23 = call signext i8 @toupper(i8 signext %22)
store i8 %23, i8* %21
%24 = add nuw nsw i64 %20, 1
%25 = call i32 @strlen(i8* %10)
%26 = zext i32 %25 to i64
%27 = icmp ult i64 %24, %26
br i1 %27, label %19, label %28
28: ; preds = %19, %16
%29 = call i8* @strdup(i8* %10)
br label %30
30: ; preds = %28, %9
%31 = phi i8* [ %29, %28 ], [ null, %9 ]
%32 = call i32 @fclose(i32* %7)
br label %33
33: ; preds = %1, %30
%34 = phi i8* [ %31, %30 ], [ null, %1 ]
ret i8* %34
}