#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*/ exit(int);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ snprintf(char *, int, char *, char *);
int main(int argc, char *argv[]) {
  char buf[1024];
  FILE *fp;
  int i, n;
  for (n = 0, i = 1; i < argc; i++) {
    n += snprintf(&buf[n], sizeof(buf) - n, "%s ", argv[i]);
  }
  buf[n] = '\0';
  printf("run cmd: %s\n", buf);
  fp = popen(buf, "r");
  if (fp == NULL) {
    printf("Failed to run command\n");
    exit(1);
  }
  while (fgets(buf, sizeof(buf), fp) != NULL) {
    printf("%s", buf);
  }
  pclose(fp);
  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)
movl   $0x0,-0x420(%rbp)
movl   $0x1,-0x41c(%rbp)
mov    -0x41c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1200 <main+0xa0>
movslq -0x420(%rbp),%rax
lea    -0x410(%rbp),%rdi
add    %rax,%rdi
movslq -0x420(%rbp),%rcx
mov    $0x400,%eax
sub    %rcx,%rax
mov    %eax,%esi
mov    -0x10(%rbp),%rax
movslq -0x41c(%rbp),%rcx
mov    (%rax,%rcx,8),%rcx
lea    0xe2c(%rip),%rdx
call   1050 <snprintf@plt>
mov    %rax,%rcx
movslq -0x420(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x420(%rbp)
mov    -0x41c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x41c(%rbp)
jmp    118d <main+0x2d>
movslq -0x420(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
lea    -0x410(%rbp),%rsi
lea    0xde7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x410(%rbp),%rdi
lea    0xddf(%rip),%rsi
call   1070 <popen@plt>
mov    %rax,-0x418(%rbp)
cmpq   $0x0,-0x418(%rbp)
jne    1264 <main+0x104>
lea    0xdc0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    1269 <main+0x109>
lea    -0x410(%rbp),%rdi
mov    -0x418(%rbp),%rdx
mov    $0x400,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     12a5 <main+0x145>
lea    -0x410(%rbp),%rsi
lea    0xd91(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1269 <main+0x109>
mov    -0x418(%rbp),%rdi
call   1040 <pclose@plt>
xor    %eax,%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
cmp    $0x2,%edi
jl     11c9 <main+0x59>
mov    %rsi,%r15
mov    %edi,%r12d
mov    $0x1,%ebx
xor    %r13d,%r13d
lea    0xe66(%rip),%r14
nopw   0x0(%rax,%rax,1)
lea    (%rsp,%r13,1),%rdi
mov    $0x400,%esi
sub    %r13d,%esi
mov    (%r15,%rbx,8),%rcx
mov    %r14,%rdx
call   1060 <snprintf@plt>
add    %r13d,%eax
add    $0x1,%rbx
movslq %eax,%r13
cmp    %rbx,%r12
jne    11a0 <main+0x30>
jmp    11cc <main+0x5c>
xor    %r13d,%r13d
movb   $0x0,(%rsp,%r13,1)
lea    0xe2c(%rip),%rdi
mov    %rsp,%rbx
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe25(%rip),%rsi
mov    %rbx,%rdi
call   1080 <popen@plt>
test   %rax,%rax
je     125d <main+0xed>
mov    %rax,%r15
mov    %rsp,%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     1242 <main+0xd2>
lea    0xdfb(%rip),%r14
mov    %rsp,%rbx
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %r15,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
jne    1220 <main+0xb0>
mov    %r15,%rdi
call   1050 <pclose@plt>
xor    %eax,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xdb2(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"%s \00"
@.str.1 = [13 x i8] c"run cmd: %s\0A\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [23 x i8] c"Failed to run command\0A\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
store i32 0, i32* %9
store i32 1, i32* %8
br label %10
10: ; preds = %32, %2
%11 = load i32, i32* %8
%12 = load i32, i32* %4
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %35
14: ; preds = %10
%15 = load i32, i32* %9
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 %16
%18 = load i32, i32* %9
%19 = sext i32 %18 to i64
%20 = sub i64 1024, %19
%21 = trunc i64 %20 to i32
%22 = load i8**, i8*** %5
%23 = load i32, i32* %8
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8*, i8** %22, i64 %24
%26 = load i8*, i8** %25
%27 = call i64 @snprintf(i8* %17, i32 %21, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %26)
%28 = load i32, i32* %9
%29 = sext i32 %28 to i64
%30 = add nsw i64 %29, %27
%31 = trunc i64 %30 to i32
store i32 %31, i32* %9
br label %32
32: ; preds = %14
%33 = load i32, i32* %8
%34 = add nsw i32 %33, 1
store i32 %34, i32* %8
br label %10
35: ; preds = %10
%36 = load i32, i32* %9
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 %37
store i8 0, i8* %38
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %39)
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%42 = call i32* @popen(i8* %41, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %42, i32** %7
%43 = load i32*, i32** %7
%44 = icmp eq i32* %43, null
br i1 %44, label %45, label %48
45: ; preds = %35
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %35
br label %49
49: ; preds = %54, %48
%50 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%51 = load i32*, i32** %7
%52 = call i32* @fgets(i8* %50, i32 1024, i32* %51)
%53 = icmp ne i32* %52, null
br i1 %53, label %54, label %57
54: ; preds = %49
%55 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %55)
br label %49
57: ; preds = %49
%58 = load i32*, i32** %7
%59 = call i32 @pclose(i32* %58)
ret i32 0
}

LLVM IR (-O2):
@.str = [4 x i8] c"%s \00"
@.str.1 = [13 x i8] c"run cmd: %s\0A\00"
@.str.2 = [2 x i8] c"r\00"
@.str.4 = [3 x i8] c"%s\00"
@str = [22 x i8] c"Failed to run command\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 = icmp sgt i32 %0, 1
br i1 %5, label %6, label %22
6: ; preds = %2
%7 = zext i32 %0 to i64
br label %8
8: ; preds = %6, %8
%9 = phi i64 [ 1, %6 ], [ %18, %8 ]
%10 = phi i64 [ 0, %6 ], [ %20, %8 ]
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %10
%12 = trunc i64 %10 to i32
%13 = sub i32 1024, %12
%14 = getelementptr inbounds i8*, i8** %1, i64 %9
%15 = load i8*, i8** %14
%16 = call i64 @snprintf(i8* %11, i32 %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = add nsw i64 %16, %10
%18 = add nuw nsw i64 %9, 1
%19 = shl i64 %17, 32
%20 = ashr exact i64 %19, 32
%21 = icmp eq i64 %18, %7
br i1 %21, label %22, label %8
22: ; preds = %8, %2
%23 = phi i64 [ 0, %2 ], [ %20, %8 ]
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %23
store i8 0, i8* %24
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %4)
%26 = call i32* @popen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%27 = icmp eq i32* %26, null
br i1 %27, label %31, label %28
28: ; preds = %22
%29 = call i32* @fgets(i8* %4, i32 1024, i32* %26)
%30 = icmp eq i32* %29, null
br i1 %30, label %38, label %34
31: ; preds = %22
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %28, %34
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %4)
%36 = call i32* @fgets(i8* %4, i32 1024, i32* %26)
%37 = icmp eq i32* %36, null
br i1 %37, label %38, label %34
38: ; preds = %34, %28
%39 = call i32 @pclose(i32* %26)
ret i32 0
}