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

/* Variables and functions */
int /*<<< orphan*/ WIFEXITED(int);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int main(int argc, char **argv) {
  char buf[1024];
  FILE *ls;
  int result;
  int i;
  strcpy(buf, "ls ");
  for (i = 1; i < argc; i++) {
    strcat(buf, argv[i]);
    strcat(buf, " ");
  }
  ls = popen(buf, "r");
  if (!ls) {
    perror("popen");
    return 1;
  }
  while (fgets(buf, sizeof(buf), ls))
    printf("%s", buf);
  result = pclose(ls);
  if (!WIFEXITED(result))
    return 1;
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x410(%rbp),%rdi
lea    0xe59(%rip),%rsi
call   1030 <strcpy@plt>
movl   $0x1,-0x420(%rbp)
mov    -0x420(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1207 <main+0x87>
lea    -0x410(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x420(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
call   1090 <strcat@plt>
lea    -0x410(%rbp),%rdi
lea    0xe14(%rip),%rsi
call   1090 <strcat@plt>
mov    -0x420(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x420(%rbp)
jmp    11b6 <main+0x36>
lea    -0x410(%rbp),%rdi
lea    0xdef(%rip),%rsi
call   1070 <popen@plt>
mov    %rax,-0x418(%rbp)
cmpq   $0x0,-0x418(%rbp)
jne    1247 <main+0xc7>
lea    0xdd0(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12bf <main+0x13f>
jmp    124c <main+0xcc>
lea    -0x410(%rbp),%rdi
mov    -0x418(%rbp),%rdx
mov    $0x400,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     1286 <main+0x106>
lea    -0x410(%rbp),%rsi
lea    0xd90(%rip),%rdi
call   1040 <printf@plt>
jmp    124c <main+0xcc>
mov    -0x418(%rbp),%rdi
call   1050 <pclose@plt>
mov    %eax,-0x41c(%rbp)
mov    -0x41c(%rbp),%edi
call   10a0 <WIFEXITED@plt>
cmp    $0x0,%eax
jne    12b8 <main+0x138>
movl   $0x1,-0x4(%rbp)
jmp    12bf <main+0x13f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %rsi,%r14
mov    %edi,%ebx
lea    0xe64(%rip),%rsi
mov    %rsp,%rdi
call   1030 <strcpy@plt>
cmp    $0x2,%ebx
jl     11e0 <main+0x60>
mov    %ebx,%r12d
mov    $0x1,%ebx
mov    %rsp,%r13
lea    0xe47(%rip),%r15
nopl   0x0(%rax,%rax,1)
mov    (%r14,%rbx,8),%rsi
mov    %r13,%rdi
call   1090 <strcat@plt>
mov    %r13,%rdi
mov    %r15,%rsi
call   1090 <strcat@plt>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    11c0 <main+0x40>
lea    0xe1d(%rip),%rsi
mov    %rsp,%rdi
call   1070 <popen@plt>
test   %rax,%rax
je     1269 <main+0xe9>
mov    %rax,%r15
mov    %rsp,%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     1240 <main+0xc0>
lea    0xdf9(%rip),%r14
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %r15,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
jne    1220 <main+0xa0>
mov    %r15,%rdi
call   1050 <pclose@plt>
mov    %eax,%edi
call   10a0 <WIFEXITED@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
sete   %al
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xd96(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%eax
jmp    1258 <main+0xd8>

LLVM IR (-O0):
@.str = [4 x i8] c"ls \00"
@.str.1 = [2 x i8] c" \00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [6 x i8] c"popen\00"
@.str.4 = [3 x i8] c"%s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [1024 x i8]
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%11 = call i32 @strcpy(i8* %10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %9
br label %12
12: ; preds = %26, %2
%13 = load i32, i32* %9
%14 = load i32, i32* %4
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %29
16: ; preds = %12
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%18 = load i8**, i8*** %5
%19 = load i32, i32* %9
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8*, i8** %18, i64 %20
%22 = load i8*, i8** %21
%23 = call i32 @strcat(i8* %17, i8* %22)
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%25 = call i32 @strcat(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %16
%27 = load i32, i32* %9
%28 = add nsw i32 %27, 1
store i32 %28, i32* %9
br label %12
29: ; preds = %12
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%31 = call i32* @popen(i8* %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %31, i32** %7
%32 = load i32*, i32** %7
%33 = icmp ne i32* %32, null
br i1 %33, label %36, label %34
34: ; preds = %29
%35 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %53
36: ; preds = %29
br label %37
37: ; preds = %42, %36
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%39 = load i32*, i32** %7
%40 = call i64 @fgets(i8* %38, i32 1024, i32* %39)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %37
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%44 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %43)
br label %37
45: ; preds = %37
%46 = load i32*, i32** %7
%47 = call i32 @pclose(i32* %46)
store i32 %47, i32* %8
%48 = load i32, i32* %8
%49 = call i32 @WIFEXITED(i32 %48)
%50 = icmp ne i32 %49, 0
br i1 %50, label %52, label %51
51: ; preds = %45
store i32 1, i32* %3
br label %53
52: ; preds = %45
store i32 0, i32* %3
br label %53
53: ; preds = %52, %51, %34
%54 = load i32, i32* %3
ret i32 %54
}

LLVM IR (-O2):
@.str = [4 x i8] c"ls \00"
@.str.1 = [2 x i8] c" \00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [6 x i8] c"popen\00"
@.str.4 = [3 x i8] c"%s\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = call i32 @strcpy(i8* %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%6 = icmp sgt i32 %0, 1
br i1 %6, label %7, label %17
7: ; preds = %2
%8 = zext i32 %0 to i64
br label %9
9: ; preds = %7, %9
%10 = phi i64 [ 1, %7 ], [ %15, %9 ]
%11 = getelementptr inbounds i8*, i8** %1, i64 %10
%12 = load i8*, i8** %11
%13 = call i32 @strcat(i8* %4, i8* %12)
%14 = call i32 @strcat(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%15 = add nuw nsw i64 %10, 1
%16 = icmp eq i64 %15, %8
br i1 %16, label %17, label %9
17: ; preds = %9, %2
%18 = call i32* @popen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %23, label %20
20: ; preds = %17
%21 = call i64 @fgets(i8* %4, i32 1024, i32* %18)
%22 = icmp eq i64 %21, 0
br i1 %22, label %29, label %25
23: ; preds = %17
%24 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %34
25: ; preds = %20, %25
%26 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %4)
%27 = call i64 @fgets(i8* %4, i32 1024, i32* %18)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %25
29: ; preds = %25, %20
%30 = call i32 @pclose(i32* %18)
%31 = call i32 @WIFEXITED(i32 %30)
%32 = icmp eq i32 %31, 0
%33 = zext i1 %32 to i32
br label %34
34: ; preds = %29, %23
%35 = phi i32 [ 1, %23 ], [ %33, %29 ]
ret i32 %35
}