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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  FILE *fp;
  char path[2048];
  if (argc != 2) {
    fprintf(stderr, "Usage: %s cmd\n", argv[0]);
    exit(2);
  }
  fp = popen(argv[1], "r");
  if (fp == NULL) {
    perror("popen");
    exit(1);
  }
  while (fgets(path, 2048, fp) != NULL) {
    printf("%s", path);
  }
  pclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x820,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11b9 <main+0x49>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe56(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe47(%rip),%rsi
call   1070 <popen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11f2 <main+0x82>
lea    0xe2e(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    11f7 <main+0x87>
lea    -0x820(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x800,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     122e <main+0xbe>
lea    -0x820(%rbp),%rsi
lea    0xdf3(%rip),%rdi
call   1030 <printf@plt>
jmp    11f7 <main+0x87>
mov    -0x18(%rbp),%rdi
call   1040 <pclose@plt>
xor    %eax,%eax
add    $0x820,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x800,%rsp
cmp    $0x2,%edi
jne    11f7 <main+0x87>
mov    0x8(%rsi),%rdi
lea    0xe83(%rip),%rsi
call   1070 <popen@plt>
test   %rax,%rax
je     1219 <main+0xa9>
mov    %rax,%r15
mov    %rsp,%rdi
mov    $0x800,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11e0 <main+0x70>
lea    0xe5e(%rip),%r14
mov    %rsp,%rbx
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    $0x800,%esi
mov    %r15,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
jne    11c0 <main+0x50>
mov    %r15,%rdi
call   1040 <pclose@plt>
xor    %eax,%eax
add    $0x800,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xdf6(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
lea    0xdf1(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [15 x i8] c"Usage: %s cmd\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [6 x i8] c"popen\00"
@.str.3 = [3 x i8] c"%s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca [2048 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 2
br i1 %9, label %10, label %17
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = call i32 @exit(i32 2)
unreachable
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32* @popen(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %21, i32** %6
%22 = load i32*, i32** %6
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %27
24: ; preds = %17
%25 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %17
br label %28
28: ; preds = %33, %27
%29 = getelementptr inbounds [2048 x i8], [2048 x i8]* %7, i64 0, i64 0
%30 = load i32*, i32** %6
%31 = call i32* @fgets(i8* %29, i32 2048, i32* %30)
%32 = icmp ne i32* %31, null
br i1 %32, label %33, label %36
33: ; preds = %28
%34 = getelementptr inbounds [2048 x i8], [2048 x i8]* %7, i64 0, i64 0
%35 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %34)
br label %28
36: ; preds = %28
%37 = load i32*, i32** %6
%38 = call i32 @pclose(i32* %37)
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [15 x i8] c"Usage: %s cmd\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [6 x i8] c"popen\00"
@.str.3 = [3 x i8] c"%s\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [2048 x i8]
%4 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 2
br i1 %5, label %11, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = tail call i32 @exit(i32 2)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i32* @popen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %19, label %16
16: ; preds = %11
%17 = call i32* @fgets(i8* %4, i32 2048, i32* %14)
%18 = icmp eq i32* %17, null
br i1 %18, label %26, label %22
19: ; preds = %11
%20 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %16, %22
%23 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %4)
%24 = call i32* @fgets(i8* %4, i32 2048, i32* %14)
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %22
26: ; preds = %22, %16
%27 = call i32 @pclose(i32* %14)
ret i32 0
}