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

/* Variables and functions */
int asprintf(char **, char *, ...);
int /*<<< orphan*/ free(char *);
int readlink(char *, char *, int);
int sscanf(char *, char *, int *, int *, int *, int *);
char *strchr(char const *, char);
size_t strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *strrchr(char *, char);
char *get_pci_dev_from_netdev(const char *netdev) {
  char buf[1024];
  char *pcidev;
  char *path;
  char *cp;
  int func;
  int dom;
  int bus;
  int dev;
  int ret;
  ret = asprintf(&path, "%s/%s/device", "/sys/class/net", netdev);
  if (ret == -1)
    return NULL;
  ret = readlink(path, buf, sizeof(buf) - 1);
  free(path);
  if (ret == -1) {
    char realdev[256];
    char *subif;
    size_t len;
    subif = strchr(netdev, '.');
    if (!subif)
      return NULL;
    len = strlen(netdev) - strlen(subif);
    if (len > (sizeof(realdev) - 1))
      return NULL;
    strncpy(realdev, netdev, sizeof(realdev) - 1);
    if (realdev[len] != '\0')
      realdev[len] = '\0';
    ret = asprintf(&path, "%s/%s/lower_%s", "/sys/class/net", netdev, realdev);
    if (ret == -1)
      return NULL;
    ret = readlink(path, buf, sizeof(buf) - 1);
    free(path);
    if (ret == -1)
      return NULL;
  }
  do {
    cp = strrchr(buf, '/');
    if (!cp)
      break;
    ret = sscanf(cp + 1, "%x:%x:%x.%x", &dom, &bus, &dev, &func);
    if (ret == 4)
      break;
    *cp = '\0';
  } while (cp && cp > buf);
  ret = asprintf(&pcidev, "%04x:%02x:%02x.%x", dom, bus, dev, func);
  if (ret == -1)
    return NULL;
  return pcidev;
}


x86 assembly (clang, -O0):
<get_pci_dev_from_netdev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x560,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rcx
lea    -0x420(%rbp),%rdi
lea    0xe5f(%rip),%rsi
lea    0xe65(%rip),%rdx
mov    $0x0,%al
call   10a0 <asprintf@plt>
mov    %eax,-0x43c(%rbp)
cmpl   $0xffffffff,-0x43c(%rbp)
jne    11cf <get_pci_dev_from_netdev+0x4f>
movq   $0x0,-0x8(%rbp)
jmp    146d <get_pci_dev_from_netdev+0x2ed>
mov    -0x420(%rbp),%rdi
lea    -0x410(%rbp),%rsi
mov    $0x3ff,%edx
call   1050 <readlink@plt>
mov    %eax,-0x43c(%rbp)
mov    -0x420(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0xffffffff,-0x43c(%rbp)
jne    1350 <get_pci_dev_from_netdev+0x1d0>
mov    -0x10(%rbp),%rdi
mov    $0x2e,%esi
call   1070 <strchr@plt>
mov    %rax,-0x548(%rbp)
cmpq   $0x0,-0x548(%rbp)
jne    1236 <get_pci_dev_from_netdev+0xb6>
movq   $0x0,-0x8(%rbp)
jmp    146d <get_pci_dev_from_netdev+0x2ed>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x558(%rbp)
mov    -0x548(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,%rcx
mov    -0x558(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x550(%rbp)
cmpq   $0xff,-0x550(%rbp)
jbe    1284 <get_pci_dev_from_netdev+0x104>
movq   $0x0,-0x8(%rbp)
jmp    146d <get_pci_dev_from_netdev+0x2ed>
lea    -0x540(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0xff,%edx
call   1040 <strncpy@plt>
mov    -0x550(%rbp),%rax
movsbl -0x540(%rbp,%rax,1),%eax
ff
cmp    $0x0,%eax
je     12c0 <get_pci_dev_from_netdev+0x140>
mov    -0x550(%rbp),%rax
movb   $0x0,-0x540(%rbp,%rax,1)
mov    -0x10(%rbp),%rcx
lea    -0x540(%rbp),%r8
lea    -0x420(%rbp),%rdi
lea    0xd43(%rip),%rsi
lea    0xd2d(%rip),%rdx
mov    $0x0,%al
call   10a0 <asprintf@plt>
mov    %eax,-0x43c(%rbp)
cmpl   $0xffffffff,-0x43c(%rbp)
jne    1307 <get_pci_dev_from_netdev+0x187>
movq   $0x0,-0x8(%rbp)
jmp    146d <get_pci_dev_from_netdev+0x2ed>
mov    -0x420(%rbp),%rdi
lea    -0x410(%rbp),%rsi
mov    $0x3ff,%edx
call   1050 <readlink@plt>
mov    %eax,-0x43c(%rbp)
mov    -0x420(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0xffffffff,-0x43c(%rbp)
jne    134b <get_pci_dev_from_netdev+0x1cb>
movq   $0x0,-0x8(%rbp)
jmp    146d <get_pci_dev_from_netdev+0x2ed>
jmp    1350 <get_pci_dev_from_netdev+0x1d0>
jmp    1355 <get_pci_dev_from_netdev+0x1d5>
lea    -0x410(%rbp),%rdi
mov    $0x2f,%esi
call   1080 <strrchr@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jne    1380 <get_pci_dev_from_netdev+0x200>
jmp    1413 <get_pci_dev_from_netdev+0x293>
mov    -0x428(%rbp),%rdi
add    $0x1,%rdi
lea    0xc99(%rip),%rsi
lea    -0x430(%rbp),%rdx
lea    -0x434(%rbp),%rcx
lea    -0x438(%rbp),%r8
lea    -0x42c(%rbp),%r9
call   1090 <sscanf@plt>
mov    %eax,-0x43c(%rbp)
cmpl   $0x4,-0x43c(%rbp)
jne    13cb <get_pci_dev_from_netdev+0x24b>
jmp    1413 <get_pci_dev_from_netdev+0x293>
mov    -0x428(%rbp),%rax
movb   $0x0,(%rax)
xor    %eax,%eax
cmpq   $0x0,-0x428(%rbp)
mov    %al,-0x559(%rbp)
je     1405 <get_pci_dev_from_netdev+0x285>
mov    -0x428(%rbp),%rax
lea    -0x410(%rbp),%rcx
cmp    %rcx,%rax
seta   %al
mov    %al,-0x559(%rbp)
mov    -0x559(%rbp),%al
test   $0x1,%al
jne    1355 <get_pci_dev_from_netdev+0x1d5>
mov    -0x430(%rbp),%edx
mov    -0x434(%rbp),%ecx
mov    -0x438(%rbp),%r8d
mov    -0x42c(%rbp),%r9d
lea    -0x418(%rbp),%rdi
lea    0xbfc(%rip),%rsi
mov    $0x0,%al
call   10a0 <asprintf@plt>
mov    %eax,-0x43c(%rbp)
cmpl   $0xffffffff,-0x43c(%rbp)
jne    1462 <get_pci_dev_from_netdev+0x2e2>
movq   $0x0,-0x8(%rbp)
jmp    146d <get_pci_dev_from_netdev+0x2ed>
mov    -0x418(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x560,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_pci_dev_from_netdev>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x528,%rsp
mov    %rdi,%r14
lea    0xe65(%rip),%rsi
lea    0xe6b(%rip),%rdx
xor    %ebx,%ebx
lea    0x8(%rsp),%rdi
mov    %r14,%rcx
xor    %eax,%eax
call   10a0 <asprintf@plt>
cmp    $0xffffffff,%eax
je     133d <get_pci_dev_from_netdev+0x1bd>
mov    0x8(%rsp),%rdi
lea    0x120(%rsp),%rsi
mov    $0x3ff,%edx
call   1050 <readlink@plt>
mov    %eax,%ebx
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
cmp    $0xffffffff,%ebx
je     1292 <get_pci_dev_from_netdev+0x112>
lea    0xe3c(%rip),%r14
lea    0x18(%rsp),%r12
lea    0x14(%rsp),%r13
lea    0x10(%rsp),%rbx
lea    0x1c(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    0x120(%rsp),%rdi
mov    $0x2f,%esi
call   1080 <strrchr@plt>
test   %rax,%rax
je     1258 <get_pci_dev_from_netdev+0xd8>
mov    %rax,%r15
lea    0x1(%rax),%rdi
mov    %r14,%rsi
mov    %r12,%rdx
mov    %r13,%rcx
mov    %rbx,%r8
mov    %rbp,%r9
call   1090 <sscanf@plt>
cmp    $0x4,%eax
je     1258 <get_pci_dev_from_netdev+0xd8>
movb   $0x0,(%r15)
lea    0x120(%rsp),%rax
cmp    %rax,%r15
ja     1210 <get_pci_dev_from_netdev+0x90>
mov    0x18(%rsp),%edx
mov    0x14(%rsp),%ecx
mov    0x10(%rsp),%r8d
mov    0x1c(%rsp),%r9d
lea    0xdc6(%rip),%rsi
xor    %ebx,%ebx
lea    0x20(%rsp),%rdi
xor    %eax,%eax
call   10a0 <asprintf@plt>
cmp    $0xffffffff,%eax
je     133d <get_pci_dev_from_netdev+0x1bd>
mov    0x20(%rsp),%rbx
jmp    133d <get_pci_dev_from_netdev+0x1bd>
mov    %r14,%rdi
mov    $0x2e,%esi
call   1070 <strchr@plt>
test   %rax,%rax
je     133b <get_pci_dev_from_netdev+0x1bb>
mov    %rax,%rbp
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1060 <strlen@plt>
sub    %rax,%rbx
cmp    $0xff,%rbx
ja     133b <get_pci_dev_from_netdev+0x1bb>
lea    0x20(%rsp),%rdi
mov    %r14,%rsi
mov    $0xff,%edx
call   1040 <strncpy@plt>
cmpb   $0x0,0x20(%rsp,%rbx,1)
je     12e8 <get_pci_dev_from_netdev+0x168>
movb   $0x0,0x20(%rsp,%rbx,1)
lea    0xd2d(%rip),%rsi
lea    0xd17(%rip),%rdx
lea    0x8(%rsp),%rdi
lea    0x20(%rsp),%r8
mov    %r14,%rcx
xor    %eax,%eax
call   10a0 <asprintf@plt>
cmp    $0xffffffff,%eax
je     133b <get_pci_dev_from_netdev+0x1bb>
mov    0x8(%rsp),%rdi
lea    0x120(%rsp),%rsi
mov    $0x3ff,%edx
call   1050 <readlink@plt>
mov    %eax,%ebx
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
cmp    $0xffffffff,%ebx
jne    11e8 <get_pci_dev_from_netdev+0x68>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x528,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"%s/%s/device\00"
@.str.1 = [15 x i8] c"/sys/class/net\00"
@.str.2 = [15 x i8] c"%s/%s/lower_%s\00"
@.str.3 = [12 x i8] c"%x:%x:%x.%x\00"
@.str.4 = [18 x i8] c"%04x:%02x:%02x.%x\00"
define i8* @get_pci_dev_from_netdev(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca [1024 x i8]
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca [256 x i8]
%14 = alloca i8*
%15 = alloca i64
store i8* %0, i8** %3
%16 = load i8*, i8** %3
%17 = call i32 (i8**, i8*, ...) @asprintf(i8** %6, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %16)
store i32 %17, i32* %12
%18 = load i32, i32* %12
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %21
20: ; preds = %1
store i8* null, i8** %2
br label %109
21: ; preds = %1
%22 = load i8*, i8** %6
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%24 = call i32 @readlink(i8* %22, i8* %23, i32 1023)
store i32 %24, i32* %12
%25 = load i8*, i8** %6
%26 = call i32 @free(i8* %25)
%27 = load i32, i32* %12
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %73
29: ; preds = %21
%30 = load i8*, i8** %3
%31 = call i8* @strchr(i8* %30, i8 signext 46)
store i8* %31, i8** %14
%32 = load i8*, i8** %14
%33 = icmp ne i8* %32, null
br i1 %33, label %35, label %34
34: ; preds = %29
store i8* null, i8** %2
br label %109
35: ; preds = %29
%36 = load i8*, i8** %3
%37 = call i64 @strlen(i8* %36)
%38 = load i8*, i8** %14
%39 = call i64 @strlen(i8* %38)
%40 = sub i64 %37, %39
store i64 %40, i64* %15
%41 = load i64, i64* %15
%42 = icmp ugt i64 %41, 255
br i1 %42, label %43, label %44
43: ; preds = %35
store i8* null, i8** %2
br label %109
44: ; preds = %35
%45 = getelementptr inbounds [256 x i8], [256 x i8]* %13, i64 0, i64 0
%46 = load i8*, i8** %3
%47 = call i32 @strncpy(i8* %45, i8* %46, i32 255)
%48 = load i64, i64* %15
%49 = getelementptr inbounds [256 x i8], [256 x i8]* %13, i64 0, i64 %48
%50 = load i8, i8* %49
%51 = sext i8 %50 to i32
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %44
%54 = load i64, i64* %15
%55 = getelementptr inbounds [256 x i8], [256 x i8]* %13, i64 0, i64 %54
store i8 0, i8* %55
br label %56
56: ; preds = %53, %44
%57 = load i8*, i8** %3
%58 = getelementptr inbounds [256 x i8], [256 x i8]* %13, i64 0, i64 0
%59 = call i32 (i8**, i8*, ...) @asprintf(i8** %6, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %57, i8* %58)
store i32 %59, i32* %12
%60 = load i32, i32* %12
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %63
62: ; preds = %56
store i8* null, i8** %2
br label %109
63: ; preds = %56
%64 = load i8*, i8** %6
%65 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%66 = call i32 @readlink(i8* %64, i8* %65, i32 1023)
store i32 %66, i32* %12
%67 = load i8*, i8** %6
%68 = call i32 @free(i8* %67)
%69 = load i32, i32* %12
%70 = icmp eq i32 %69, -1
br i1 %70, label %71, label %72
71: ; preds = %63
store i8* null, i8** %2
br label %109
72: ; preds = %63
br label %73
73: ; preds = %72, %21
br label %74
74: ; preds = %96, %73
%75 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%76 = call i8* @strrchr(i8* %75, i8 signext 47)
store i8* %76, i8** %7
%77 = load i8*, i8** %7
%78 = icmp ne i8* %77, null
br i1 %78, label %80, label %79
79: ; preds = %74
br label %98
80: ; preds = %74
%81 = load i8*, i8** %7
%82 = getelementptr inbounds i8, i8* %81, i64 1
%83 = call i32 @sscanf(i8* %82, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32* %9, i32* %10, i32* %11, i32* %8)
store i32 %83, i32* %12
%84 = load i32, i32* %12
%85 = icmp eq i32 %84, 4
br i1 %85, label %86, label %87
86: ; preds = %80
br label %98
87: ; preds = %80
%88 = load i8*, i8** %7
store i8 0, i8* %88
br label %89
89: ; preds = %87
%90 = load i8*, i8** %7
%91 = icmp ne i8* %90, null
br i1 %91, label %92, label %96
92: ; preds = %89
%93 = load i8*, i8** %7
%94 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%95 = icmp ugt i8* %93, %94
br label %96
96: ; preds = %92, %89
%97 = phi i1 [ false, %89 ], [ %95, %92 ]
br i1 %97, label %74, label %98
98: ; preds = %96, %86, %79
%99 = load i32, i32* %9
%100 = load i32, i32* %10
%101 = load i32, i32* %11
%102 = load i32, i32* %8
%103 = call i32 (i8**, i8*, ...) @asprintf(i8** %5, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %99, i32 %100, i32 %101, i32 %102)
store i32 %103, i32* %12
%104 = load i32, i32* %12
%105 = icmp eq i32 %104, -1
br i1 %105, label %106, label %107
106: ; preds = %98
store i8* null, i8** %2
br label %109
107: ; preds = %98
%108 = load i8*, i8** %5
store i8* %108, i8** %2
br label %109
109: ; preds = %107, %106, %71, %62, %43, %34, %20
%110 = load i8*, i8** %2
ret i8* %110
}

LLVM IR (-O2):
@.str = [13 x i8] c"%s/%s/device\00"
@.str.1 = [15 x i8] c"/sys/class/net\00"
@.str.2 = [15 x i8] c"%s/%s/lower_%s\00"
@.str.3 = [12 x i8] c"%x:%x:%x.%x\00"
@.str.4 = [18 x i8] c"%04x:%02x:%02x.%x\00"
define i8* @get_pci_dev_from_netdev(i8* %0) {
%2 = alloca [1024 x i8]
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [256 x i8]
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%11 = bitcast i8** %3 to i8*
%12 = bitcast i8** %4 to i8*
%13 = bitcast i32* %5 to i8*
%14 = bitcast i32* %6 to i8*
%15 = bitcast i32* %7 to i8*
%16 = bitcast i32* %8 to i8*
%17 = call i32 (i8**, i8*, ...) @asprintf(i8** %4, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%18 = icmp eq i32 %17, -1
br i1 %18, label %71, label %19
19: ; preds = %1
%20 = load i8*, i8** %4
%21 = call i32 @readlink(i8* %20, i8* %10, i32 1023)
%22 = load i8*, i8** %4
%23 = call i32 @free(i8* %22)
%24 = icmp eq i32 %21, -1
br i1 %24, label %25, label %49
25: ; preds = %19
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%27 = call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 46)
%28 = icmp eq i8* %27, null
br i1 %28, label %68, label %29
29: ; preds = %25
%30 = call i64 @strlen(i8* dereferenceable(1) %0)
%31 = call i64 @strlen(i8* %27)
%32 = sub i64 %30, %31
%33 = icmp ugt i64 %32, 255
br i1 %33, label %69, label %34
34: ; preds = %29
%35 = call i32 @strncpy(i8* %26, i8* %0, i32 255)
%36 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 %32
%37 = load i8, i8* %36
%38 = icmp eq i8 %37, 0
br i1 %38, label %40, label %39
39: ; preds = %34
store i8 0, i8* %36
br label %40
40: ; preds = %39, %34
%41 = call i32 (i8**, i8*, ...) @asprintf(i8** %4, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %26)
%42 = icmp eq i32 %41, -1
br i1 %42, label %70, label %43
43: ; preds = %40
%44 = load i8*, i8** %4
%45 = call i32 @readlink(i8* %44, i8* %10, i32 1023)
%46 = load i8*, i8** %4
%47 = call i32 @free(i8* %46)
%48 = icmp eq i32 %45, -1
br i1 %48, label %71, label %49
49: ; preds = %43, %19
br label %50
50: ; preds = %49, %57
%51 = call i8* @strrchr(i8* %10, i8 signext 47)
%52 = icmp eq i8* %51, null
br i1 %52, label %59, label %53
53: ; preds = %50
%54 = getelementptr inbounds i8, i8* %51, i64 1
%55 = call i32 @sscanf(i8* %54, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32* %6, i32* %7, i32* %8, i32* %5)
%56 = icmp eq i32 %55, 4
br i1 %56, label %59, label %57
57: ; preds = %53
store i8 0, i8* %51
%58 = icmp ugt i8* %51, %10
br i1 %58, label %50, label %59
59: ; preds = %53, %50, %57
%60 = load i32, i32* %6
%61 = load i32, i32* %7
%62 = load i32, i32* %8
%63 = load i32, i32* %5
%64 = call i32 (i8**, i8*, ...) @asprintf(i8** %3, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %60, i32 %61, i32 %62, i32 %63)
%65 = icmp eq i32 %64, -1
%66 = load i8*, i8** %3
%67 = select i1 %65, i8* null, i8* %66
br label %71
68: ; preds = %25
br label %71
69: ; preds = %29
br label %71
70: ; preds = %40
br label %71
71: ; preds = %59, %70, %69, %68, %1, %43
%72 = phi i8* [ null, %43 ], [ null, %1 ], [ null, %68 ], [ null, %69 ], [ null, %70 ], [ %67, %59 ]
ret i8* %72
}