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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/);
int getline(char **, size_t *, int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdout;
int main(int argc, char *argv[]) {
  FILE *fp;
  char *sysCmd = "lspci";
  if ((fp = popen(sysCmd, "r")) == NULL) {
    printf("Failed to run command\n");
    exit(EXIT_FAILURE);
  }
  char *line = NULL;
  size_t len = 0;
  ssize_t nread;
  while ((nread = getline(&line, &len, fp)) != -1) {
    printf("Retrieved line of length %zu:\n", nread);
    fwrite(line, nread, 1, stdout);
  }
  free(line);
  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    0xe73(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe6a(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11cb <main+0x5b>
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x18(%rbp),%rdx
lea    -0x28(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1070 <getline@plt>
mov    %eax,-0x34(%rbp)
cmp    $0xffffffff,%eax
je     1228 <main+0xb8>
mov    -0x34(%rbp),%esi
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x34(%rbp),%esi
mov    0x2dc1(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%edx
call   1090 <fwrite@plt>
jmp    11db <main+0x6b>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1050 <pclose@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xe6b(%rip),%rdi
lea    0xe6a(%rip),%rsi
call   1070 <popen@plt>
test   %rax,%rax
je     1249 <main+0xc9>
mov    %rax,%rbx
movq   $0x0,0x8(%rsp)
movq   $0x0,0x10(%rsp)
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
mov    %rax,%rdx
call   1080 <getline@plt>
cmp    $0xffffffff,%eax
je     1226 <main+0xa6>
mov    %eax,%ebp
lea    0xe29(%rip),%r14
mov    0x2df2(%rip),%r13
lea    0x8(%rsp),%r15
lea    0x10(%rsp),%r12
mov    %r14,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rsp),%rdi
mov    0x0(%r13),%ecx
mov    %ebp,%esi
mov    $0x1,%edx
call   10a0 <fwrite@plt>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1080 <getline@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11f0 <main+0x70>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1060 <pclose@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdd7(%rip),%rdi
call   1040 <puts@plt>
mov    0x2d84(%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 = [31 x i8] c"Retrieved line of length %zu:\0A\00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8** %7
%11 = load i8*, i8** %7
%12 = call i32* @popen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %12, i32** %6
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %2
store i8* null, i8** %8
store i64 0, i64* %9
br label %19
19: ; preds = %23, %18
%20 = load i32*, i32** %6
%21 = call i32 @getline(i8** %8, i64* %9, i32* %20)
store i32 %21, i32* %10
%22 = icmp ne i32 %21, -1
br i1 %22, label %23, label %30
23: ; preds = %19
%24 = load i32, i32* %10
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %24)
%26 = load i8*, i8** %8
%27 = load i32, i32* %10
%28 = load i32, i32* @stdout
%29 = call i32 @fwrite(i8* %26, i32 %27, i32 1, i32 %28)
br label %19
30: ; preds = %19
%31 = load i8*, i8** %8
%32 = call i32 @free(i8* %31)
%33 = load i32*, i32** %6
%34 = call i32 @pclose(i32* %33)
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 = [31 x i8] c"Retrieved line of length %zu:\0A\00"
@stdout = global i32 0
@str = [22 x i8] c"Failed to run command\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i8*
%4 = alloca i64
%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 %7, label %11
7: ; preds = %2
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %2
%12 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%13 = bitcast i64* %4 to i8*
store i64 0, i64* %4
%14 = call i32 @getline(i8** %3, i64* %4, i32* %5)
%15 = icmp eq i32 %14, -1
br i1 %15, label %24, label %16
16: ; preds = %11, %16
%17 = phi i32 [ %22, %16 ], [ %14, %11 ]
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %17)
%19 = load i8*, i8** %3
%20 = load i32, i32* @stdout
%21 = call i32 @fwrite(i8* %19, i32 %17, i32 1, i32 %20)
%22 = call i32 @getline(i8** %3, i64* %4, i32* %5)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %16
24: ; preds = %16, %11
%25 = load i8*, i8** %3
%26 = call i32 @free(i8* %25)
%27 = call i32 @pclose(i32* %5)
ret i32 0
}