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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  FILE *fp;
  char path[20] = {0};
  char *sysCmd = "lspci";
  if ((fp = popen(sysCmd, "r")) == NULL) {
    printf("Failed to run command\n");
    exit(EXIT_FAILURE);
  }
  while (fgets(path, sizeof(path) - 1, fp) != NULL) {
    printf("%s", path);
    printf("\n");
  }
  pclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1050 <memset@plt>
lea    0xe73(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
lea    0xe6a(%rip),%rsi
call   1070 <popen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11cb <main+0x6b>
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    11d0 <main+0x70>
lea    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x13,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     1211 <main+0xb1>
lea    -0x30(%rbp),%rsi
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11d0 <main+0x70>
mov    -0x18(%rbp),%rdi
call   1040 <pclose@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
movl   $0x0,0x10(%rsp)
lea    0xe71(%rip),%rdi
lea    0xe70(%rip),%rsi
call   1080 <popen@plt>
test   %rax,%rax
je     1210 <main+0xa0>
mov    %rax,%r15
mov    %rsp,%rdi
mov    $0x13,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     11fc <main+0x8c>
lea    0xe49(%rip),%r14
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %rbx,%rdi
mov    $0x13,%esi
mov    %r15,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
jne    11d0 <main+0x60>
mov    %r15,%rdi
call   1060 <pclose@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xdf4(%rip),%rdi
call   1040 <puts@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"lspci\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [23 x i8] c"Failed to run command\0A\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [2 x i8] c"\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca [20 x i8]
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = bitcast [20 x i8]* %7 to i8*
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8** %8
%10 = load i8*, i8** %8
%11 = call i32* @popen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %6
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %2
br label %18
18: ; preds = %23, %17
%19 = getelementptr inbounds [20 x i8], [20 x i8]* %7, i64 0, i64 0
%20 = load i32*, i32** %6
%21 = call i32* @fgets(i8* %19, i32 19, i32* %20)
%22 = icmp ne i32* %21, null
br i1 %22, label %23, label %27
23: ; preds = %18
%24 = getelementptr inbounds [20 x i8], [20 x i8]* %7, i64 0, i64 0
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %24)
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %18
27: ; preds = %18
%28 = load i32*, i32** %6
%29 = call i32 @pclose(i32* %28)
ret i32 0
}

LLVM IR (-O2):
@.str = [6 x i8] c"lspci\00"
@.str.1 = [2 x i8] c"r\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [3 x i8] c"%s\00"
@str = [22 x i8] c"Failed to run command\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [20 x i8]
%4 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @popen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = call i32* @fgets(i8* %4, i32 19, i32* %5)
%9 = icmp eq i32* %8, null
br i1 %9, label %19, label %14
10: ; preds = %2
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %7, %14
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %4)
%16 = call i32 @putchar(i32 10)
%17 = call i32* @fgets(i8* %4, i32 19, i32* %5)
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %14
19: ; preds = %14, %7
%20 = call i32 @pclose(i32* %5)
ret i32 0
}