#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;

/* Variables and functions */
int getPCIslot_bus(unsigned char);
size_t readlink(char *, char *, int);
int sscanf(char *, char *, unsigned short *, unsigned char *, unsigned char *,
           unsigned char *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ *strchr(char *, char);
scalar_t__ strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, size_t);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
int getPCIslot_str(char *pci) {
  unsigned short dom = 0;
  unsigned char bus = 0, dev = 0, func = 0;
  int slot;
  char pcilink[1025];
  char syspci[256];
  int i;
  ssize_t link_sz;
  ssize_t remaining = 255;
  if (pci == NULL)
    return 0;
  strncpy(syspci, "/sys/bus/pci/devices/", remaining);
  remaining -= strlen(syspci);
  strncat(syspci, pci, remaining);
  if ((strchr(pci, '.') == NULL))
    strcat(syspci, ".0");
  link_sz = readlink(syspci, pcilink, 1024);
  if (link_sz > 0)
    pcilink[link_sz] = '\0';
  else
    return 0;
  for (i = 0; i < link_sz; i++) {
    if (pcilink[i] == '/') {
      i++;
      if (sscanf(&pcilink[i], "%4hx:%2hhx:%2hhx.%1hhx", &dom, &bus, &dev,
                 &func) == 4)
        if ((slot = getPCIslot_bus(bus)) > 0)
          return slot;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<getPCIslot_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x540,%rsp
mov    %rdi,-0x10(%rbp)
movw   $0x0,-0x12(%rbp)
movb   $0x0,-0x13(%rbp)
movb   $0x0,-0x14(%rbp)
movb   $0x0,-0x15(%rbp)
movq   $0xff,-0x538(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11c3 <getPCIslot_str+0x43>
movl   $0x0,-0x4(%rbp)
jmp    1351 <getPCIslot_str+0x1d1>
lea    -0x520(%rbp),%rdi
mov    -0x538(%rbp),%rdx
lea    0xe28(%rip),%rsi
call   1030 <strncpy@plt>
lea    -0x520(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x538(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x538(%rbp)
lea    -0x520(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x538(%rbp),%rdx
call   1070 <strncat@plt>
mov    -0x10(%rbp),%rdi
mov    $0x2e,%esi
call   1060 <strchr@plt>
cmp    $0x0,%rax
jne    123f <getPCIslot_str+0xbf>
lea    -0x520(%rbp),%rdi
lea    0xddc(%rip),%rsi
call   10a0 <strcat@plt>
lea    -0x520(%rbp),%rdi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1040 <readlink@plt>
mov    %rax,-0x530(%rbp)
cmpq   $0x0,-0x530(%rbp)
jbe    1280 <getPCIslot_str+0x100>
mov    -0x530(%rbp),%rax
movb   $0x0,-0x420(%rbp,%rax,1)
jmp    128c <getPCIslot_str+0x10c>
movl   $0x0,-0x4(%rbp)
jmp    1351 <getPCIslot_str+0x1d1>
movl   $0x0,-0x524(%rbp)
movslq -0x524(%rbp),%rax
cmp    -0x530(%rbp),%rax
jae    134a <getPCIslot_str+0x1ca>
movslq -0x524(%rbp),%rax
movsbl -0x420(%rbp,%rax,1),%eax
ff
cmp    $0x2f,%eax
jne    1331 <getPCIslot_str+0x1b1>
mov    -0x524(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x524(%rbp)
movslq -0x524(%rbp),%rax
lea    -0x420(%rbp),%rdi
add    %rax,%rdi
lea    0xd30(%rip),%rsi
lea    -0x12(%rbp),%rdx
lea    -0x13(%rbp),%rcx
lea    -0x14(%rbp),%r8
lea    -0x15(%rbp),%r9
call   1080 <sscanf@plt>
cmp    $0x4,%eax
jne    132c <getPCIslot_str+0x1ac>
movzbl -0x13(%rbp),%edi
call   1090 <getPCIslot_bus@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jle    1327 <getPCIslot_str+0x1a7>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1351 <getPCIslot_str+0x1d1>
jmp    132c <getPCIslot_str+0x1ac>
jmp    1331 <getPCIslot_str+0x1b1>
jmp    1336 <getPCIslot_str+0x1b6>
mov    -0x524(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x524(%rbp)
jmp    1296 <getPCIslot_str+0x116>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x540,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getPCIslot_str>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x518,%rsp
movw   $0x0,0xe(%rsp)
movb   $0x0,0xb(%rsp)
movb   $0x0,0xd(%rsp)
movb   $0x0,0xc(%rsp)
xor    %ebp,%ebp
test   %rdi,%rdi
je     12b0 <getPCIslot_str+0x130>
mov    %rdi,%r15
lea    0xe44(%rip),%rsi
lea    0x10(%rsp),%r14
mov    $0xff,%ebx
mov    $0xff,%edx
mov    %r14,%rdi
call   1030 <strncpy@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
sub    %rax,%rbx
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1070 <strncat@plt>
mov    %r15,%rdi
mov    $0x2e,%esi
call   1060 <strchr@plt>
test   %rax,%rax
jne    120f <getPCIslot_str+0x8f>
lea    0xe11(%rip),%rsi
lea    0x10(%rsp),%rdi
call   10a0 <strcat@plt>
lea    0x10(%rsp),%rdi
lea    0x110(%rsp),%rsi
mov    $0x400,%edx
call   1040 <readlink@plt>
test   %rax,%rax
je     12b0 <getPCIslot_str+0x130>
mov    %rax,%rbx
movb   $0x0,0x110(%rsp,%rax,1)
xor    %eax,%eax
lea    0xb(%rsp),%r13
lea    0xd(%rsp),%r14
lea    0xc(%rsp),%r15
xor    %r12d,%r12d
jmp    125c <getPCIslot_str+0xdc>
add    $0x1,%r12d
movslq %r12d,%rax
cmp    %rax,%rbx
jbe    12ae <getPCIslot_str+0x12e>
cmpb   $0x2f,0x110(%rsp,%rax,1)
2f
jne    1250 <getPCIslot_str+0xd0>
movslq %r12d,%rax
add    $0x1,%r12d
lea    0x110(%rsp),%rcx
lea    (%rax,%rcx,1),%rdi
add    $0x1,%rdi
lea    0xd95(%rip),%rsi
lea    0xe(%rsp),%rdx
mov    %r13,%rcx
mov    %r14,%r8
mov    %r15,%r9
call   1080 <sscanf@plt>
cmp    $0x4,%eax
jne    1250 <getPCIslot_str+0xd0>
movzbl 0xb(%rsp),%edi
call   1090 <getPCIslot_bus@plt>
test   %eax,%eax
jle    1250 <getPCIslot_str+0xd0>
mov    %eax,%ebp
jmp    12b0 <getPCIslot_str+0x130>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x518,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [22 x i8] c"/sys/bus/pci/devices/\00"
@.str.1 = [3 x i8] c".0\00"
@.str.2 = [23 x i8] c"%4hx:%2hhx:%2hhx.%1hhx\00"
define i32 @getPCIslot_str(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i16
%5 = alloca i8
%6 = alloca i8
%7 = alloca i8
%8 = alloca i32
%9 = alloca [1025 x i8]
%10 = alloca [256 x i8]
%11 = alloca i32
%12 = alloca i64
%13 = alloca i64
store i8* %0, i8** %3
store i16 0, i16* %4
store i8 0, i8* %5
store i8 0, i8* %6
store i8 0, i8* %7
store i64 255, i64* %13
%14 = load i8*, i8** %3
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %17
16: ; preds = %1
store i32 0, i32* %2
br label %79
17: ; preds = %1
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%19 = load i64, i64* %13
%20 = call i32 @strncpy(i8* %18, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i64 %19)
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%22 = call i64 @strlen(i8* %21)
%23 = load i64, i64* %13
%24 = sub i64 %23, %22
store i64 %24, i64* %13
%25 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%26 = load i8*, i8** %3
%27 = load i64, i64* %13
%28 = call i32 @strncat(i8* %25, i8* %26, i64 %27)
%29 = load i8*, i8** %3
%30 = call i32* @strchr(i8* %29, i8 signext 46)
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %35
32: ; preds = %17
%33 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%34 = call i32 @strcat(i8* %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %35
35: ; preds = %32, %17
%36 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%37 = getelementptr inbounds [1025 x i8], [1025 x i8]* %9, i64 0, i64 0
%38 = call i64 @readlink(i8* %36, i8* %37, i32 1024)
store i64 %38, i64* %12
%39 = load i64, i64* %12
%40 = icmp ugt i64 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %35
%42 = load i64, i64* %12
%43 = getelementptr inbounds [1025 x i8], [1025 x i8]* %9, i64 0, i64 %42
store i8 0, i8* %43
br label %45
44: ; preds = %35
store i32 0, i32* %2
br label %79
45: ; preds = %41
store i32 0, i32* %11
br label %46
46: ; preds = %75, %45
%47 = load i32, i32* %11
%48 = sext i32 %47 to i64
%49 = load i64, i64* %12
%50 = icmp ult i64 %48, %49
br i1 %50, label %51, label %78
51: ; preds = %46
%52 = load i32, i32* %11
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds [1025 x i8], [1025 x i8]* %9, i64 0, i64 %53
%55 = load i8, i8* %54
%56 = sext i8 %55 to i32
%57 = icmp eq i32 %56, 47
br i1 %57, label %58, label %74
58: ; preds = %51
%59 = load i32, i32* %11
%60 = add nsw i32 %59, 1
store i32 %60, i32* %11
%61 = load i32, i32* %11
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds [1025 x i8], [1025 x i8]* %9, i64 0, i64 %62
%64 = call i32 @sscanf(i8* %63, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i16* %4, i8* %5, i8* %6, i8* %7)
%65 = icmp eq i32 %64, 4
br i1 %65, label %66, label %73
66: ; preds = %58
%67 = load i8, i8* %5
%68 = call i32 @getPCIslot_bus(i8 zeroext %67)
store i32 %68, i32* %8
%69 = icmp sgt i32 %68, 0
br i1 %69, label %70, label %72
70: ; preds = %66
%71 = load i32, i32* %8
store i32 %71, i32* %2
br label %79
72: ; preds = %66
br label %73
73: ; preds = %72, %58
br label %74
74: ; preds = %73, %51
br label %75
75: ; preds = %74
%76 = load i32, i32* %11
%77 = add nsw i32 %76, 1
store i32 %77, i32* %11
br label %46
78: ; preds = %46
store i32 0, i32* %2
br label %79
79: ; preds = %78, %70, %44, %16
%80 = load i32, i32* %2
ret i32 %80
}

LLVM IR (-O2):
@.str = [22 x i8] c"/sys/bus/pci/devices/\00"
@.str.1 = [3 x i8] c".0\00"
@.str.2 = [23 x i8] c"%4hx:%2hhx:%2hhx.%1hhx\00"
define i32 @getPCIslot_str(i8* %0) {
%2 = alloca i16
%3 = alloca i8
%4 = alloca i8
%5 = alloca i8
%6 = alloca [1025 x i8]
%7 = alloca [256 x i8]
%8 = bitcast i16* %2 to i8*
store i16 0, i16* %2
store i8 0, i8* %3
store i8 0, i8* %4
store i8 0, i8* %5
%9 = getelementptr inbounds [1025 x i8], [1025 x i8]* %6, i64 0, i64 0
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%11 = icmp eq i8* %0, null
br i1 %11, label %47, label %12
12: ; preds = %1
%13 = call i32 @strncpy(i8* %10, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i64 255)
%14 = call i64 @strlen(i8* %10)
%15 = sub i64 255, %14
%16 = call i32 @strncat(i8* %10, i8* %0, i64 %15)
%17 = call i32* @strchr(i8* %0, i8 signext 46)
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %21
19: ; preds = %12
%20 = call i32 @strcat(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %19, %12
%22 = call i64 @readlink(i8* %10, i8* %9, i32 1024)
%23 = icmp eq i64 %22, 0
br i1 %23, label %47, label %24
24: ; preds = %21
%25 = getelementptr inbounds [1025 x i8], [1025 x i8]* %6, i64 0, i64 %22
store i8 0, i8* %25
br label %26
26: ; preds = %24, %42
%27 = phi i64 [ %45, %42 ], [ 0, %24 ]
%28 = phi i32 [ %44, %42 ], [ 0, %24 ]
%29 = getelementptr inbounds [1025 x i8], [1025 x i8]* %6, i64 0, i64 %27
%30 = load i8, i8* %29
%31 = icmp eq i8 %30, 47
br i1 %31, label %32, label %42
32: ; preds = %26
%33 = add nsw i32 %28, 1
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [1025 x i8], [1025 x i8]* %6, i64 0, i64 %34
%36 = call i32 @sscanf(i8* %35, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i16* %2, i8* %3, i8* %4, i8* %5)
%37 = icmp eq i32 %36, 4
br i1 %37, label %38, label %42
38: ; preds = %32
%39 = load i8, i8* %3
%40 = call i32 @getPCIslot_bus(i8 zeroext %39)
%41 = icmp sgt i32 %40, 0
br i1 %41, label %47, label %42
42: ; preds = %26, %38, %32
%43 = phi i32 [ %33, %38 ], [ %33, %32 ], [ %28, %26 ]
%44 = add nsw i32 %43, 1
%45 = sext i32 %44 to i64
%46 = icmp ugt i64 %22, %45
br i1 %46, label %26, label %47
47: ; preds = %38, %42, %21, %1
%48 = phi i32 [ 0, %1 ], [ 0, %21 ], [ %40, %38 ], [ 0, %42 ]
ret i32 %48
}