#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 */
struct stat {
  int /*<<< orphan*/ st_size;
};

/* Variables and functions */
char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
scalar_t__ fstat(int, struct stat *);
char *mac_get_oui(char *);
char *malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
char *mmap(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/,
           int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strpbrk(char *, char *);
char *strstr(char *, char *);
char *mac_get_vendor(char *full_mac) {
  char oui[9] = {0};
  strncpy(oui, mac_get_oui(full_mac), 9);
  struct stat st;
  char *str, *map, *off, *end;
  int fd;
  fd = open("oui.txt", O_RDONLY);
  if (fd < 0)
    return ("NO_OUI_FILE");
  if (fstat(fd, &st) < 0) {
    close(fd);
    return ("CANT_STAT_FILE");
  }
  str = malloc(128);
  if (!str) {
    close(fd);
    return ("MALLOC_FAILED");
  }
  memset(str, 0, 128);
  map = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
  if (!map || map == MAP_FAILED) {
    free(str);
    close(fd);
    return ("MMAP_FAILED");
  }
  off = strstr(map, oui);
  if (off) {
    off += 9;
    end = strpbrk(off, "\r\n");
    strncpy(str, off, end - off);
  } else {
    free(str);
    str = "No Record";
  }
  munmap(map, st.st_size);
  close(fd);
  return str;
}


x86 assembly (clang, -O0):
<mac_get_vendor>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x19(%rbp),%rdi
xor    %esi,%esi
mov    $0x9,%edx
call   1060 <memset@plt>
lea    -0x19(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rdi
call   10c0 <mac_get_oui@plt>
mov    -0x50(%rbp),%rdi
mov    %rax,%rsi
mov    $0x9,%edx
call   1040 <strncpy@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
lea    0xdf2(%rip),%rdi
call   10b0 <open@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    1230 <mac_get_vendor+0x70>
lea    0xde1(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1392 <mac_get_vendor+0x1d2>
mov    -0x44(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   10d0 <fstat@plt>
cmp    $0x0,%rax
jge    125e <mac_get_vendor+0x9e>
mov    -0x44(%rbp),%edi
call   1070 <close@plt>
lea    0xdbf(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1392 <mac_get_vendor+0x1d2>
mov    $0x80,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    128f <mac_get_vendor+0xcf>
mov    -0x44(%rbp),%edi
call   1070 <close@plt>
lea    0xd9d(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1392 <mac_get_vendor+0x1d2>
mov    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1060 <memset@plt>
mov    -0x20(%rbp),%esi
mov    0x2d4f(%rip),%rax
mov    (%rax),%edx
mov    0x2d36(%rip),%rax
mov    (%rax),%ecx
mov    -0x44(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1050 <mmap@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     12e6 <mac_get_vendor+0x126>
mov    -0x30(%rbp),%rax
mov    0x2ceb(%rip),%rcx
cmp    (%rcx),%rax
jne    1307 <mac_get_vendor+0x147>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x44(%rbp),%edi
call   1070 <close@plt>
lea    0xd33(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1392 <mac_get_vendor+0x1d2>
mov    -0x30(%rbp),%rdi
lea    -0x19(%rbp),%rsi
call   10e0 <strstr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1362 <mac_get_vendor+0x1a2>
mov    -0x38(%rbp),%rax
add    $0x9,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
lea    0xd03(%rip),%rsi
call   1090 <strpbrk@plt>
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x40(%rbp),%rax
mov    -0x38(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1040 <strncpy@plt>
jmp    1376 <mac_get_vendor+0x1b6>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
lea    0xcce(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   10a0 <munmap@plt>
mov    -0x44(%rbp),%edi
call   1070 <close@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mac_get_vendor>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
movb   $0x0,0x18(%rsp)
movq   $0x0,0x10(%rsp)
call   10c0 <mac_get_oui@plt>
lea    0x10(%rsp),%rdi
mov    %rax,%rsi
mov    $0x9,%edx
call   1040 <strncpy@plt>
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
lea    0xdff(%rip),%rdi
call   10b0 <open@plt>
test   %eax,%eax
js     12bf <mac_get_vendor+0xff>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   10d0 <fstat@plt>
test   %rax,%rax
js     12c8 <mac_get_vendor+0x108>
mov    $0x80,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     12d1 <mac_get_vendor+0x111>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1060 <memset@plt>
mov    0x8(%rsp),%r14d
mov    0x2da2(%rip),%rax
mov    (%rax),%edx
mov    0x2d89(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r14d,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
test   %rax,%rax
je     12da <mac_get_vendor+0x11a>
mov    %rax,%r15
mov    0x2d48(%rip),%rax
cmp    (%rax),%r15
je     12da <mac_get_vendor+0x11a>
lea    0x10(%rsp),%rsi
mov    %r15,%rdi
call   10e0 <strstr@plt>
test   %rax,%rax
je     12eb <mac_get_vendor+0x12b>
mov    %rax,%r12
add    $0x9,%r12
lea    0xd98(%rip),%rsi
mov    %r12,%rdi
call   1090 <strpbrk@plt>
sub    %r12d,%eax
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %eax,%edx
call   1040 <strncpy@plt>
jmp    12fa <mac_get_vendor+0x13a>
lea    0xd42(%rip),%rbx
jmp    130c <mac_get_vendor+0x14c>
lea    0xd45(%rip),%rbx
jmp    1305 <mac_get_vendor+0x145>
lea    0xd4b(%rip),%rbx
jmp    1305 <mac_get_vendor+0x145>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xd48(%rip),%rbx
jmp    1305 <mac_get_vendor+0x145>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xd46(%rip),%rbx
mov    %r15,%rdi
mov    %r14d,%esi
call   10a0 <munmap@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    %rbx,%rax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [8 x i8] c"oui.txt\00"
@O_RDONLY = global i32 0
@.str.1 = [12 x i8] c"NO_OUI_FILE\00"
@.str.2 = [15 x i8] c"CANT_STAT_FILE\00"
@.str.3 = [14 x i8] c"MALLOC_FAILED\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.4 = [12 x i8] c"MMAP_FAILED\00"
@.str.5 = [3 x i8] c"\0D\0A\00"
@.str.6 = [10 x i8] c"No Record\00"
define i8* @mac_get_vendor(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca [9 x i8]
%5 = alloca %struct.stat
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
store i8* %0, i8** %3
%11 = bitcast [9 x i8]* %4 to i8*
%12 = getelementptr inbounds [9 x i8], [9 x i8]* %4, i64 0, i64 0
%13 = load i8*, i8** %3
%14 = call i8* @mac_get_oui(i8* %13)
%15 = call i32 @strncpy(i8* %12, i8* %14, i32 9)
%16 = load i32, i32* @O_RDONLY
%17 = call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %16)
store i32 %17, i32* %10
%18 = load i32, i32* %10
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %1
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8** %2
br label %86
21: ; preds = %1
%22 = load i32, i32* %10
%23 = call i64 @fstat(i32 %22, %struct.stat* %5)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = load i32, i32* %10
%27 = call i32 @close(i32 %26)
store i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8** %2
br label %86
28: ; preds = %21
%29 = call i8* @malloc(i32 128)
store i8* %29, i8** %6
%30 = load i8*, i8** %6
%31 = icmp ne i8* %30, null
br i1 %31, label %35, label %32
32: ; preds = %28
%33 = load i32, i32* %10
%34 = call i32 @close(i32 %33)
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8** %2
br label %86
35: ; preds = %28
%36 = load i8*, i8** %6
%37 = call i32 @memset(i8* %36, i32 0, i32 128)
%38 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%39 = load i32, i32* %38
%40 = load i32, i32* @PROT_READ
%41 = load i32, i32* @MAP_SHARED
%42 = load i32, i32* %10
%43 = call i8* @mmap(i32 0, i32 %39, i32 %40, i32 %41, i32 %42, i32 0)
store i8* %43, i8** %7
%44 = load i8*, i8** %7
%45 = icmp ne i8* %44, null
br i1 %45, label %46, label %50
46: ; preds = %35
%47 = load i8*, i8** %7
%48 = load i8*, i8** @MAP_FAILED
%49 = icmp eq i8* %47, %48
br i1 %49, label %50, label %55
50: ; preds = %46, %35
%51 = load i8*, i8** %6
%52 = call i32 @free(i8* %51)
%53 = load i32, i32* %10
%54 = call i32 @close(i32 %53)
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8** %2
br label %86
55: ; preds = %46
%56 = load i8*, i8** %7
%57 = getelementptr inbounds [9 x i8], [9 x i8]* %4, i64 0, i64 0
%58 = call i8* @strstr(i8* %56, i8* %57)
store i8* %58, i8** %8
%59 = load i8*, i8** %8
%60 = icmp ne i8* %59, null
br i1 %60, label %61, label %75
61: ; preds = %55
%62 = load i8*, i8** %8
%63 = getelementptr inbounds i8, i8* %62, i64 9
store i8* %63, i8** %8
%64 = load i8*, i8** %8
%65 = call i8* @strpbrk(i8* %64, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
store i8* %65, i8** %9
%66 = load i8*, i8** %6
%67 = load i8*, i8** %8
%68 = load i8*, i8** %9
%69 = load i8*, i8** %8
%70 = ptrtoint i8* %68 to i64
%71 = ptrtoint i8* %69 to i64
%72 = sub i64 %70, %71
%73 = trunc i64 %72 to i32
%74 = call i32 @strncpy(i8* %66, i8* %67, i32 %73)
br label %78
75: ; preds = %55
%76 = load i8*, i8** %6
%77 = call i32 @free(i8* %76)
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i8** %6
br label %78
78: ; preds = %75, %61
%79 = load i8*, i8** %7
%80 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%81 = load i32, i32* %80
%82 = call i32 @munmap(i8* %79, i32 %81)
%83 = load i32, i32* %10
%84 = call i32 @close(i32 %83)
%85 = load i8*, i8** %6
store i8* %85, i8** %2
br label %86
86: ; preds = %78, %50, %32, %25, %20
%87 = load i8*, i8** %2
ret i8* %87
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [8 x i8] c"oui.txt\00"
@O_RDONLY = global i32 0
@.str.1 = [12 x i8] c"NO_OUI_FILE\00"
@.str.2 = [15 x i8] c"CANT_STAT_FILE\00"
@.str.3 = [14 x i8] c"MALLOC_FAILED\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.4 = [12 x i8] c"MMAP_FAILED\00"
@.str.5 = [3 x i8] c"\0D\0A\00"
@.str.6 = [10 x i8] c"No Record\00"
define i8* @mac_get_vendor(i8* %0) {
%2 = alloca [9 x i8]
%3 = alloca %struct.stat
%4 = getelementptr inbounds [9 x i8], [9 x i8]* %2, i64 0, i64 0
%5 = tail call i8* @mac_get_oui(i8* %0)
%6 = call i32 @strncpy(i8* %4, i8* %5, i32 9)
%7 = bitcast %struct.stat* %3 to i8*
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %49, label %11
11: ; preds = %1
%12 = call i64 @fstat(i32 %9, %struct.stat* %3)
%13 = icmp slt i64 %12, 0
br i1 %13, label %46, label %14
14: ; preds = %11
%15 = call dereferenceable_or_null(128) i8* @malloc(i32 128)
%16 = icmp eq i8* %15, null
br i1 %16, label %46, label %17
17: ; preds = %14
%18 = call i32 @memset(i8* %15, i32 0, i32 128)
%19 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%20 = load i32, i32* %19
%21 = load i32, i32* @PROT_READ
%22 = load i32, i32* @MAP_SHARED
%23 = call i8* @mmap(i32 0, i32 %20, i32 %21, i32 %22, i32 %9, i32 0)
%24 = icmp eq i8* %23, null
%25 = load i8*, i8** @MAP_FAILED
%26 = icmp eq i8* %23, %25
%27 = select i1 %24, i1 true, i1 %26
br i1 %27, label %28, label %30
28: ; preds = %17
%29 = call i32 @free(i8* %15)
br label %46
30: ; preds = %17
%31 = call i8* @strstr(i8* %23, i8* %4)
%32 = icmp eq i8* %31, null
br i1 %32, label %41, label %33
33: ; preds = %30
%34 = getelementptr inbounds i8, i8* %31, i64 9
%35 = call i8* @strpbrk(i8* %34, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%36 = ptrtoint i8* %35 to i64
%37 = ptrtoint i8* %34 to i64
%38 = sub i64 %36, %37
%39 = trunc i64 %38 to i32
%40 = call i32 @strncpy(i8* %15, i8* %34, i32 %39)
br label %43
41: ; preds = %30
%42 = call i32 @free(i8* %15)
br label %43
43: ; preds = %41, %33
%44 = phi i8* [ %15, %33 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), %41 ]
%45 = call i32 @munmap(i8* %23, i32 %20)
br label %46
46: ; preds = %14, %11, %28, %43
%47 = phi i8* [ %44, %43 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), %28 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), %11 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), %14 ]
%48 = call i32 @close(i32 %9)
br label %49
49: ; preds = %46, %1
%50 = phi i8* [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), %1 ], [ %47, %46 ]
ret i8* %50
}