#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 */

/* Variables and functions */
char *getenv(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned long, unsigned long,
                            unsigned long, unsigned long, unsigned char,
                            unsigned char, unsigned char);
char *strchr(char *, char);
int strlen(char *);
unsigned long strtoul(char *, int /*<<< orphan*/ *, int);
int get_pci_modalias(char *modalias, int modalias_len) {
  char *class_env, *id_env, *subsys_env;
  char *ptr;
  unsigned long vendor, device, sub_vendor, sub_device, class_type;
  unsigned char baseclass, subclass, interface;
  id_env = getenv("PCI_ID");
  subsys_env = getenv("PCI_SUBSYS_ID");
  class_env = getenv("PCI_CLASS");
  if (id_env == NULL || subsys_env == NULL || class_env == NULL)
    return -1;
  if (strlen(id_env) < 9 || strlen(subsys_env) < 9)
    return -1;
  ptr = strchr(id_env, ':');
  if (ptr == NULL || ptr[1] == '\0')
    return -1;
  vendor = strtoul(id_env, NULL, 16);
  device = strtoul(ptr + 1, NULL, 16);
  ptr = strchr(subsys_env, ':');
  if (ptr == NULL || ptr[1] == '\0')
    return -1;
  sub_vendor = strtoul(id_env, NULL, 16);
  sub_device = strtoul(ptr + 1, NULL, 16);
  class_type = strtoul(class_env, NULL, 16);
  baseclass = (unsigned char)(class_type >> 16);
  subclass = (unsigned char)(class_type >> 8);
  interface = (unsigned char)class_type;
  snprintf(modalias, modalias_len,
           "MODALIAS=pci:v%08lXd%08lXsv%08lXsd%08lXbc%02Xsc%02Xi%02X", vendor,
           device, sub_vendor, sub_device, baseclass, subclass, interface);
  return 0;
}


x86 assembly (clang, -O0):
<get_pci_modalias>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    0xe97(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
lea    0xe8e(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x30(%rbp)
lea    0xe8c(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11b3 <get_pci_modalias+0x63>
cmpq   $0x0,-0x30(%rbp)
je     11b3 <get_pci_modalias+0x63>
cmpq   $0x0,-0x20(%rbp)
jne    11bf <get_pci_modalias+0x6f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1343 <get_pci_modalias+0x1f3>
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x9,%eax
jl     11e3 <get_pci_modalias+0x93>
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x9,%eax
jge    11ef <get_pci_modalias+0x9f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1343 <get_pci_modalias+0x1f3>
mov    -0x28(%rbp),%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     121d <get_pci_modalias+0xcd>
mov    -0x38(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x0,%eax
jne    1229 <get_pci_modalias+0xd9>
movl   $0xffffffff,-0x4(%rbp)
jmp    1343 <get_pci_modalias+0x1f3>
mov    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1070 <strtoul@plt>
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
add    $0x1,%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1070 <strtoul@plt>
mov    %rax,-0x48(%rbp)
mov    -0x30(%rbp),%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1287 <get_pci_modalias+0x137>
mov    -0x38(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x0,%eax
jne    1293 <get_pci_modalias+0x143>
movl   $0xffffffff,-0x4(%rbp)
jmp    1343 <get_pci_modalias+0x1f3>
mov    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1070 <strtoul@plt>
mov    %rax,-0x50(%rbp)
mov    -0x38(%rbp),%rdi
add    $0x1,%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1070 <strtoul@plt>
mov    %rax,-0x58(%rbp)
mov    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1070 <strtoul@plt>
mov    %rax,-0x60(%rbp)
mov    -0x60(%rbp),%rax
shr    $0x10,%rax
mov    %al,-0x61(%rbp)
mov    -0x60(%rbp),%rax
shr    $0x8,%rax
mov    %al,-0x62(%rbp)
mov    -0x60(%rbp),%rax
mov    %al,-0x63(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x40(%rbp),%rcx
mov    -0x48(%rbp),%r8
mov    -0x50(%rbp),%r9
mov    -0x58(%rbp),%r11
mov    -0x61(%rbp),%r10b
mov    -0x62(%rbp),%al
lea    0xd04(%rip),%rdx
mov    %r11,(%rsp)
movzbl %r10b,%r10d
mov    %r10d,0x8(%rsp)
movzbl %al,%eax
mov    %eax,0x10(%rsp)
movzbl -0x63(%rbp),%eax
mov    %eax,0x18(%rsp)
call   1060 <snprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_pci_modalias>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %esi,%r14d
mov    %rdi,%r15
lea    0xe95(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rbx
lea    0xe8d(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rbp
lea    0xe8c(%rip),%rdi
call   1030 <getenv@plt>
mov    $0xffffffff,%r12d
test   %rbx,%rbx
je     12b6 <get_pci_modalias+0x166>
test   %rbp,%rbp
je     12b6 <get_pci_modalias+0x166>
mov    %rax,%r13
test   %rax,%rax
je     12b6 <get_pci_modalias+0x166>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cmp    $0x9,%eax
jl     12b6 <get_pci_modalias+0x166>
mov    %rbp,%rdi
call   1040 <strlen@plt>
cmp    $0x9,%eax
jl     12b6 <get_pci_modalias+0x166>
mov    %r14d,0xc(%rsp)
mov    %rbx,%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     12b6 <get_pci_modalias+0x166>
mov    %rax,%r14
cmpb   $0x0,0x1(%rax)
je     12b6 <get_pci_modalias+0x166>
add    $0x1,%r14
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <strtoul@plt>
mov    %rax,0x20(%rsp)
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <strtoul@plt>
mov    %rax,%r14
mov    %rbp,%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     12b6 <get_pci_modalias+0x166>
mov    %rax,%rbp
cmpb   $0x0,0x1(%rax)
je     12b6 <get_pci_modalias+0x166>
add    $0x1,%rbp
xor    %r12d,%r12d
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <strtoul@plt>
mov    %rax,0x10(%rsp)
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <strtoul@plt>
mov    %rax,0x18(%rsp)
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <strtoul@plt>
movzbl %al,%ebx
movzbl %ah,%ebp
shr    $0x10,%eax
movzbl %al,%eax
lea    0xd8d(%rip),%rdx
mov    %r15,%rdi
mov    0xc(%rsp),%esi
mov    0x20(%rsp),%rcx
mov    %r14,%r8
mov    0x10(%rsp),%r9
push   %rbx
push   %rbp
push   %rax
push   0x30(%rsp)
call   1060 <snprintf@plt>
add    $0x20,%rsp
mov    %r12d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"PCI_ID\00"
@.str.1 = [14 x i8] c"PCI_SUBSYS_ID\00"
@.str.2 = [10 x i8] c"PCI_CLASS\00"
@.str.3 = [57 x i8] c"MODALIAS=pci:v%08lXd%08lXsv%08lXsd%08lXbc%02Xsc%02Xi%02X\00"
define i32 @get_pci_modalias(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
%13 = alloca i64
%14 = alloca i64
%15 = alloca i8
%16 = alloca i8
%17 = alloca i8
store i8* %0, i8** %4
store i32 %1, i32* %5
%18 = call i8* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i8* %18, i8** %7
%19 = call i8* @getenv(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i8* %19, i8** %8
%20 = call i8* @getenv(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
store i8* %20, i8** %6
%21 = load i8*, i8** %7
%22 = icmp eq i8* %21, null
br i1 %22, label %29, label %23
23: ; preds = %2
%24 = load i8*, i8** %8
%25 = icmp eq i8* %24, null
br i1 %25, label %29, label %26
26: ; preds = %23
%27 = load i8*, i8** %6
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %30
29: ; preds = %26, %23, %2
store i32 -1, i32* %3
br label %94
30: ; preds = %26
%31 = load i8*, i8** %7
%32 = call i32 @strlen(i8* %31)
%33 = icmp slt i32 %32, 9
br i1 %33, label %38, label %34
34: ; preds = %30
%35 = load i8*, i8** %8
%36 = call i32 @strlen(i8* %35)
%37 = icmp slt i32 %36, 9
br i1 %37, label %38, label %39
38: ; preds = %34, %30
store i32 -1, i32* %3
br label %94
39: ; preds = %34
%40 = load i8*, i8** %7
%41 = call i8* @strchr(i8* %40, i8 signext 58)
store i8* %41, i8** %9
%42 = load i8*, i8** %9
%43 = icmp eq i8* %42, null
br i1 %43, label %50, label %44
44: ; preds = %39
%45 = load i8*, i8** %9
%46 = getelementptr inbounds i8, i8* %45, i64 1
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = icmp eq i32 %48, 0
br i1 %49, label %50, label %51
50: ; preds = %44, %39
store i32 -1, i32* %3
br label %94
51: ; preds = %44
%52 = load i8*, i8** %7
%53 = call i64 @strtoul(i8* %52, i32* null, i32 16)
store i64 %53, i64* %10
%54 = load i8*, i8** %9
%55 = getelementptr inbounds i8, i8* %54, i64 1
%56 = call i64 @strtoul(i8* %55, i32* null, i32 16)
store i64 %56, i64* %11
%57 = load i8*, i8** %8
%58 = call i8* @strchr(i8* %57, i8 signext 58)
store i8* %58, i8** %9
%59 = load i8*, i8** %9
%60 = icmp eq i8* %59, null
br i1 %60, label %67, label %61
61: ; preds = %51
%62 = load i8*, i8** %9
%63 = getelementptr inbounds i8, i8* %62, i64 1
%64 = load i8, i8* %63
%65 = sext i8 %64 to i32
%66 = icmp eq i32 %65, 0
br i1 %66, label %67, label %68
67: ; preds = %61, %51
store i32 -1, i32* %3
br label %94
68: ; preds = %61
%69 = load i8*, i8** %7
%70 = call i64 @strtoul(i8* %69, i32* null, i32 16)
store i64 %70, i64* %12
%71 = load i8*, i8** %9
%72 = getelementptr inbounds i8, i8* %71, i64 1
%73 = call i64 @strtoul(i8* %72, i32* null, i32 16)
store i64 %73, i64* %13
%74 = load i8*, i8** %6
%75 = call i64 @strtoul(i8* %74, i32* null, i32 16)
store i64 %75, i64* %14
%76 = load i64, i64* %14
%77 = lshr i64 %76, 16
%78 = trunc i64 %77 to i8
store i8 %78, i8* %15
%79 = load i64, i64* %14
%80 = lshr i64 %79, 8
%81 = trunc i64 %80 to i8
store i8 %81, i8* %16
%82 = load i64, i64* %14
%83 = trunc i64 %82 to i8
store i8 %83, i8* %17
%84 = load i8*, i8** %4
%85 = load i32, i32* %5
%86 = load i64, i64* %10
%87 = load i64, i64* %11
%88 = load i64, i64* %12
%89 = load i64, i64* %13
%90 = load i8, i8* %15
%91 = load i8, i8* %16
%92 = load i8, i8* %17
%93 = call i32 @snprintf(i8* %84, i32 %85, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.3, i64 0, i64 0), i64 %86, i64 %87, i64 %88, i64 %89, i8 zeroext %90, i8 zeroext %91, i8 zeroext %92)
store i32 0, i32* %3
br label %94
94: ; preds = %68, %67, %50, %38, %29
%95 = load i32, i32* %3
ret i32 %95
}

LLVM IR (-O2):
@.str = [7 x i8] c"PCI_ID\00"
@.str.1 = [14 x i8] c"PCI_SUBSYS_ID\00"
@.str.2 = [10 x i8] c"PCI_CLASS\00"
@.str.3 = [57 x i8] c"MODALIAS=pci:v%08lXd%08lXsv%08lXsd%08lXbc%02Xsc%02Xi%02X\00"
define i32 @get_pci_modalias(i8* %0, i32 %1) {
%3 = tail call i8* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%4 = tail call i8* @getenv(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%5 = tail call i8* @getenv(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%6 = icmp eq i8* %3, null
%7 = icmp eq i8* %4, null
%8 = or i1 %6, %7
%9 = icmp eq i8* %5, null
%10 = or i1 %8, %9
br i1 %10, label %43, label %11
11: ; preds = %2
%12 = tail call i32 @strlen(i8* %3)
%13 = icmp slt i32 %12, 9
br i1 %13, label %43, label %14
14: ; preds = %11
%15 = tail call i32 @strlen(i8* %4)
%16 = icmp slt i32 %15, 9
br i1 %16, label %43, label %17
17: ; preds = %14
%18 = tail call i8* @strchr(i8* dereferenceable(1) %3, i8 signext 58)
%19 = icmp eq i8* %18, null
br i1 %19, label %43, label %20
20: ; preds = %17
%21 = getelementptr inbounds i8, i8* %18, i64 1
%22 = load i8, i8* %21
%23 = icmp eq i8 %22, 0
br i1 %23, label %43, label %24
24: ; preds = %20
%25 = tail call i64 @strtoul(i8* nocapture %3, i32* null, i32 16)
%26 = tail call i64 @strtoul(i8* nocapture %21, i32* null, i32 16)
%27 = tail call i8* @strchr(i8* dereferenceable(1) %4, i8 signext 58)
%28 = icmp eq i8* %27, null
br i1 %28, label %43, label %29
29: ; preds = %24
%30 = getelementptr inbounds i8, i8* %27, i64 1
%31 = load i8, i8* %30
%32 = icmp eq i8 %31, 0
br i1 %32, label %43, label %33
33: ; preds = %29
%34 = tail call i64 @strtoul(i8* nocapture %3, i32* null, i32 16)
%35 = tail call i64 @strtoul(i8* nocapture %30, i32* null, i32 16)
%36 = tail call i64 @strtoul(i8* nocapture %5, i32* null, i32 16)
%37 = lshr i64 %36, 16
%38 = trunc i64 %37 to i8
%39 = lshr i64 %36, 8
%40 = trunc i64 %39 to i8
%41 = trunc i64 %36 to i8
%42 = tail call i32 @snprintf(i8* %0, i32 %1, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.3, i64 0, i64 0), i64 %25, i64 %26, i64 %34, i64 %35, i8 zeroext %38, i8 zeroext %40, i8 zeroext %41)
br label %43
43: ; preds = %24, %29, %17, %20, %11, %14, %2, %33
%44 = phi i32 [ 0, %33 ], [ -1, %2 ], [ -1, %14 ], [ -1, %11 ], [ -1, %20 ], [ -1, %17 ], [ -1, %29 ], [ -1, %24 ]
ret i32 %44
}