#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 unsigned int uint32_t;
typedef int /*<<< orphan*/ s;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ ENODEV;
int /*<<< orphan*/ errno;
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int sscanf(char *, char *, unsigned int *);
scalar_t__ strstr(char *, char *);
uint32_t spec_get_base(void) {
  FILE *f;
  int found = 0;
  unsigned int res = ~0;
  char s[160];
  f = popen("lspci -v", "r");
  while (fgets(s, sizeof(s), f)) {
    if (!found) {
      if (strstr(s, "Device 1a39:0004"))
        found = 1;
      if (strstr(s, "Device 10dc:018d"))
        found = 1;
    }
    if (found) {
      if (sscanf(s, " Memory at %x", &res) == 1)
        break;
    }
  }
  pclose(f);
  if (res == ~0)
    errno = ENODEV;
  return res;
}


x86 assembly (clang, -O0):
<spec_get_base>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x0,-0xc(%rbp)
movl   $0xffffffff,-0x10(%rbp)
lea    0xe90(%rip),%rdi
lea    0xe92(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,-0x8(%rbp)
lea    -0xb0(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    $0xa0,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     122f <spec_get_base+0xdf>
cmpl   $0x0,-0xc(%rbp)
jne    11f6 <spec_get_base+0xa6>
lea    -0xb0(%rbp),%rdi
lea    0xe54(%rip),%rsi
call   1070 <strstr@plt>
cmp    $0x0,%rax
je     11cd <spec_get_base+0x7d>
movl   $0x1,-0xc(%rbp)
lea    -0xb0(%rbp),%rdi
lea    0xe41(%rip),%rsi
call   1070 <strstr@plt>
cmp    $0x0,%rax
je     11f1 <spec_get_base+0xa1>
movl   $0x1,-0xc(%rbp)
jmp    11f6 <spec_get_base+0xa6>
cmpl   $0x0,-0xc(%rbp)
je     122a <spec_get_base+0xda>
lea    -0xb0(%rbp),%rdi
lea    0xe1f(%rip),%rsi
lea    -0x10(%rbp),%rdx
call   1050 <sscanf@plt>
cmp    $0x1,%eax
jne    1225 <spec_get_base+0xd5>
jmp    122f <spec_get_base+0xdf>
jmp    122a <spec_get_base+0xda>
jmp    1180 <spec_get_base+0x30>
mov    -0x8(%rbp),%rdi
call   1030 <pclose@plt>
cmpl   $0xffffffff,-0x10(%rbp)
jne    1254 <spec_get_base+0x104>
mov    0x2d8f(%rip),%rax
mov    (%rax),%ecx
mov    0x2d8e(%rip),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spec_get_base>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xb8,%rsp
movl   $0xffffffff,0xc(%rsp)
ff
lea    0xe90(%rip),%rdi
lea    0xe92(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,%r15
lea    0x10(%rsp),%rdi
mov    $0xa0,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     120e <spec_get_base+0xbe>
xor    %r12d,%r12d
lea    0x10(%rsp),%rbx
lea    0xe88(%rip),%r13
lea    0xc(%rsp),%rbp
jmp    11de <spec_get_base+0x8e>
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %rbp,%rdx
call   1050 <sscanf@plt>
mov    $0x1,%r12d
cmp    $0x1,%eax
je     120e <spec_get_base+0xbe>
mov    %rbx,%rdi
mov    $0xa0,%esi
mov    %r15,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     120e <spec_get_base+0xbe>
test   %r12d,%r12d
jne    11b0 <spec_get_base+0x60>
mov    %rbx,%rdi
lea    0xe1e(%rip),%rsi
call   1070 <strstr@plt>
mov    %rax,%r14
mov    %rbx,%rdi
lea    0xe1d(%rip),%rsi
call   1070 <strstr@plt>
xor    %r12d,%r12d
or     %r14,%rax
jne    11b0 <spec_get_base+0x60>
jmp    11c9 <spec_get_base+0x79>
mov    %r15,%rdi
call   1030 <pclose@plt>
mov    0xc(%rsp),%eax
cmp    $0xffffffff,%eax
jne    1231 <spec_get_base+0xe1>
mov    0x2db2(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2db1(%rip),%rdx
mov    %ecx,(%rdx)
add    $0xb8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"lspci -v\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [17 x i8] c"Device 1a39:0004\00"
@.str.3 = [17 x i8] c"Device 10dc:018d\00"
@.str.4 = [14 x i8] c" Memory at %x\00"
@ENODEV = global i32 0
@errno = global i32 0
define i32 @spec_get_base() {
%1 = alloca i32*
%2 = alloca i32
%3 = alloca i32
%4 = alloca [160 x i8]
store i32 0, i32* %2
store i32 -1, i32* %3
%5 = call i32* @popen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** %1
br label %6
6: ; preds = %34, %0
%7 = getelementptr inbounds [160 x i8], [160 x i8]* %4, i64 0, i64 0
%8 = load i32*, i32** %1
%9 = call i64 @fgets(i8* %7, i32 160, i32* %8)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %35
11: ; preds = %6
%12 = load i32, i32* %2
%13 = icmp ne i32 %12, 0
br i1 %13, label %25, label %14
14: ; preds = %11
%15 = getelementptr inbounds [160 x i8], [160 x i8]* %4, i64 0, i64 0
%16 = call i64 @strstr(i8* %15, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %14
store i32 1, i32* %2
br label %19
19: ; preds = %18, %14
%20 = getelementptr inbounds [160 x i8], [160 x i8]* %4, i64 0, i64 0
%21 = call i64 @strstr(i8* %20, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %19
store i32 1, i32* %2
br label %24
24: ; preds = %23, %19
br label %25
25: ; preds = %24, %11
%26 = load i32, i32* %2
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %25
%29 = getelementptr inbounds [160 x i8], [160 x i8]* %4, i64 0, i64 0
%30 = call i32 @sscanf(i8* %29, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32* %3)
%31 = icmp eq i32 %30, 1
br i1 %31, label %32, label %33
32: ; preds = %28
br label %35
33: ; preds = %28
br label %34
34: ; preds = %33, %25
br label %6
35: ; preds = %32, %6
%36 = load i32*, i32** %1
%37 = call i32 @pclose(i32* %36)
%38 = load i32, i32* %3
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %35
%41 = load i32, i32* @ENODEV
store i32 %41, i32* @errno
br label %42
42: ; preds = %40, %35
%43 = load i32, i32* %3
ret i32 %43
}

LLVM IR (-O2):
@.str = [9 x i8] c"lspci -v\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [17 x i8] c"Device 1a39:0004\00"
@.str.3 = [17 x i8] c"Device 10dc:018d\00"
@.str.4 = [14 x i8] c" Memory at %x\00"
@ENODEV = global i32 0
@errno = global i32 0
define i32 @spec_get_base() {
%1 = alloca i32
%2 = alloca [160 x i8]
%3 = bitcast i32* %1 to i8*
store i32 -1, i32* %1
%4 = getelementptr inbounds [160 x i8], [160 x i8]* %2, i64 0, i64 0
%5 = tail call i32* @popen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = call i64 @fgets(i8* %4, i32 160, i32* %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %24, label %8
8: ; preds = %0, %20
%9 = phi i32 [ %21, %20 ], [ 0, %0 ]
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %17
11: ; preds = %8
%12 = call i64 @strstr(i8* %4, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%13 = icmp eq i64 %12, 0
%14 = call i64 @strstr(i8* %4, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%15 = icmp eq i64 %14, 0
%16 = select i1 %15, i1 %13, i1 false
br i1 %16, label %20, label %17
17: ; preds = %11, %8
%18 = call i32 @sscanf(i8* %4, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32* %1)
%19 = icmp eq i32 %18, 1
br i1 %19, label %24, label %20
20: ; preds = %11, %17
%21 = phi i32 [ 1, %17 ], [ 0, %11 ]
%22 = call i64 @fgets(i8* %4, i32 160, i32* %5)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %8
24: ; preds = %20, %17, %0
%25 = call i32 @pclose(i32* %5)
%26 = load i32, i32* %1
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = load i32, i32* @ENODEV
store i32 %29, i32* @errno
br label %30
30: ; preds = %28, %24
ret i32 %26
}