#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 size_t ssize_t;
typedef int /*<<< orphan*/ path;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int IF_NAMESIZE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ if_indextoname(int, char *);
int open(char *, int /*<<< orphan*/);
size_t read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int strtol(char *, int /*<<< orphan*/ *, int /*<<< orphan*/);
int get_vendor_id(int ifindex) {
  char ifname[IF_NAMESIZE], path[64], buf[8];
  ssize_t len;
  int fd;
  if (!if_indextoname(ifindex, ifname))
    return -1;
  snprintf(path, sizeof(path), "/sys/class/net/%s/device/vendor", ifname);
  fd = open(path, O_RDONLY);
  if (fd < 0)
    return -1;
  len = read(fd, buf, sizeof(buf));
  close(fd);
  if (len < 0)
    return -1;
  if (len >= (ssize_t)sizeof(buf))
    return -1;
  buf[len] = '\0';
  return strtol(buf, NULL, 0);
}


x86 assembly (clang, -O0):
<get_vendor_id>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e6b(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x80(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
call   1070 <if_indextoname@plt>
cmp    $0x0,%eax
jne    11be <get_vendor_id+0x5e>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x78(%rbp)
jmp    1284 <get_vendor_id+0x124>
mov    -0x80(%rbp),%rcx
lea    -0x60(%rbp),%rdi
mov    $0x40,%esi
lea    0xe2e(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x60(%rbp),%rdi
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x74(%rbp)
cmpl   $0x0,-0x74(%rbp)
jge    1209 <get_vendor_id+0xa9>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x78(%rbp)
jmp    1284 <get_vendor_id+0x124>
mov    -0x74(%rbp),%edi
lea    -0x68(%rbp),%rsi
mov    $0x8,%edx
call   1050 <read@plt>
mov    %rax,-0x70(%rbp)
mov    -0x74(%rbp),%edi
call   1040 <close@plt>
cmpq   $0x0,-0x70(%rbp)
jae    1244 <get_vendor_id+0xe4>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x78(%rbp)
jmp    1284 <get_vendor_id+0x124>
cmpq   $0x8,-0x70(%rbp)
jb     1262 <get_vendor_id+0x102>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x78(%rbp)
jmp    1284 <get_vendor_id+0x124>
mov    -0x70(%rbp),%rax
movb   $0x0,-0x68(%rbp,%rax,1)
lea    -0x68(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1060 <strtol@plt>
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x78(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_vendor_id>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
sub    $0x48,%rsp
mov    0x2e6c(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rsi
call   1070 <if_indextoname@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
je     1201 <get_vendor_id+0xa1>
lea    0xe60(%rip),%rdx
lea    -0x60(%rbp),%r15
mov    %r15,%rdi
mov    $0x40,%esi
mov    %rbx,%rcx
call   1030 <snprintf@plt>
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     1201 <get_vendor_id+0xa1>
mov    %eax,%ebx
lea    -0x20(%rbp),%rsi
mov    %eax,%edi
mov    $0x8,%edx
call   1050 <read@plt>
mov    %rax,%r15
mov    %ebx,%edi
call   1040 <close@plt>
cmp    $0x7,%r15
ja     1201 <get_vendor_id+0xa1>
movb   $0x0,-0x20(%rbp,%r15,1)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <strtol@plt>
mov    %eax,%r14d
mov    %r14d,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@IF_NAMESIZE = global i32 0
@.str = [32 x i8] c"/sys/class/net/%s/device/vendor\00"
@O_RDONLY = global i32 0
define i32 @get_vendor_id(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca [64 x i8]
%7 = alloca [8 x i8]
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %3
%11 = load i32, i32* @IF_NAMESIZE
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %4
%14 = alloca i8, i64 %12
store i64 %12, i64* %5
%15 = load i32, i32* %3
%16 = call i32 @if_indextoname(i32 %15, i8* %14)
%17 = icmp ne i32 %16, 0
br i1 %17, label %19, label %18
18: ; preds = %1
store i32 -1, i32* %2
store i32 1, i32* %10
br label %46
19: ; preds = %1
%20 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%21 = call i32 @snprintf(i8* %20, i32 64, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %14)
%22 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%23 = load i32, i32* @O_RDONLY
%24 = call i32 @open(i8* %22, i32 %23)
store i32 %24, i32* %9
%25 = load i32, i32* %9
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %19
store i32 -1, i32* %2
store i32 1, i32* %10
br label %46
28: ; preds = %19
%29 = load i32, i32* %9
%30 = getelementptr inbounds [8 x i8], [8 x i8]* %7, i64 0, i64 0
%31 = call i64 @read(i32 %29, i8* %30, i32 8)
store i64 %31, i64* %8
%32 = load i32, i32* %9
%33 = call i32 @close(i32 %32)
%34 = load i64, i64* %8
%35 = icmp ult i64 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %28
store i32 -1, i32* %2
store i32 1, i32* %10
br label %46
37: ; preds = %28
%38 = load i64, i64* %8
%39 = icmp uge i64 %38, 8
br i1 %39, label %40, label %41
40: ; preds = %37
store i32 -1, i32* %2
store i32 1, i32* %10
br label %46
41: ; preds = %37
%42 = load i64, i64* %8
%43 = getelementptr inbounds [8 x i8], [8 x i8]* %7, i64 0, i64 %42
store i8 0, i8* %43
%44 = getelementptr inbounds [8 x i8], [8 x i8]* %7, i64 0, i64 0
%45 = call i32 @strtol(i8* %44, i32* null, i32 0)
store i32 %45, i32* %2
store i32 1, i32* %10
br label %46
46: ; preds = %41, %40, %36, %27, %18
%47 = load i8*, i8** %4
%48 = load i32, i32* %2
ret i32 %48
}

LLVM IR (-O2):
@IF_NAMESIZE = global i32 0
@.str = [32 x i8] c"/sys/class/net/%s/device/vendor\00"
@O_RDONLY = global i32 0
define i32 @get_vendor_id(i32 %0) {
%2 = alloca [64 x i8]
%3 = alloca [8 x i8]
%4 = load i32, i32* @IF_NAMESIZE
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%8 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 0
%9 = call i32 @if_indextoname(i32 %0, i8* %6)
%10 = icmp eq i32 %9, 0
br i1 %10, label %23, label %11
11: ; preds = %1
%12 = call i32 @snprintf(i8* %7, i32 64, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %6)
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %7, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %23, label %16
16: ; preds = %11
%17 = call i64 @read(i32 %14, i8* %8, i32 8)
%18 = call i32 @close(i32 %14)
%19 = icmp ugt i64 %17, 7
br i1 %19, label %23, label %20
20: ; preds = %16
%21 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 %17
store i8 0, i8* %21
%22 = call i32 @strtol(i8* nocapture %8, i32* null, i32 0)
br label %23
23: ; preds = %16, %11, %1, %20
%24 = phi i32 [ %22, %20 ], [ -1, %1 ], [ -1, %11 ], [ -1, %16 ]
ret i32 %24
}