#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 scalar_t__ pid_t;
typedef int /*<<< orphan*/ command;
typedef int /*<<< orphan*/ buff;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
scalar_t__ fork();
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ *stdin;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strcmp(char *, char *);
scalar_t__ waitpid(scalar_t__, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  char command[256];
  char buff[512];
  char salir[5];
  FILE *in;
  extern FILE *popen();
  while (1) {
    printf("Comando >");
    fflush(stdout);
    fgets(command, sizeof(command), stdin);
    salir[0] = command[0];
    salir[1] = command[1];
    salir[2] = command[2];
    salir[3] = command[3];
    if (!strcmp(salir, "exit")) {
      return 0;
    } else {
      pid_t returnedValue = fork();
      if (returnedValue < 0) {
        perror("error forking");
        return -1;
      } else if (returnedValue == 0) {
        if (!(in = popen(command, "r"))) {
          return -1;
        }
        while (fgets(buff, sizeof(buff), in) != NULL) {
          printf("%s", buff);
        }
        return -1;
      } else {
        if (waitpid(returnedValue, 0, 0) < 0) {
          perror("error waiting for child");
          return -1;
        }
      }
    }
  }
  pclose(in);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x330,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe67(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
lea    -0x110(%rbp),%rdi
mov    0x2e2c(%rip),%rax
mov    (%rax),%rdx
mov    $0x100,%esi
call   1040 <fgets@plt>
mov    -0x110(%rbp),%al
mov    %al,-0x315(%rbp)
mov    -0x10f(%rbp),%al
mov    %al,-0x314(%rbp)
mov    -0x10e(%rbp),%al
mov    %al,-0x313(%rbp)
mov    -0x10d(%rbp),%al
mov    %al,-0x312(%rbp)
lea    -0x315(%rbp),%rdi
lea    0xe03(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
jne    1221 <main+0xa1>
movl   $0x0,-0x4(%rbp)
jmp    1328 <main+0x1a8>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x328(%rbp)
cmpq   $0x0,-0x328(%rbp)
jge    1255 <main+0xd5>
lea    0xdcb(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1328 <main+0x1a8>
cmpq   $0x0,-0x328(%rbp)
jne    12e2 <main+0x162>
lea    -0x110(%rbp),%rdi
lea    0xdac(%rip),%rsi
mov    $0x0,%al
call   1080 <popen@plt>
mov    %rax,-0x320(%rbp)
cmp    $0x0,%rax
jne    1295 <main+0x115>
movl   $0xffffffff,-0x4(%rbp)
jmp    1328 <main+0x1a8>
jmp    129a <main+0x11a>
lea    -0x310(%rbp),%rdi
mov    -0x320(%rbp),%rdx
mov    $0x200,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     12d6 <main+0x156>
lea    -0x310(%rbp),%rsi
lea    0xd55(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    129a <main+0x11a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1328 <main+0x1a8>
mov    -0x328(%rbp),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   1070 <waitpid@plt>
cmp    $0x0,%rax
jge    1314 <main+0x194>
lea    0xd1f(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1328 <main+0x1a8>
jmp    1319 <main+0x199>
jmp    131e <main+0x19e>
jmp    1323 <main+0x1a3>
jmp    1192 <main+0x12>
mov    -0x4(%rbp),%eax
add    $0x330,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x318,%rsp
lea    0xe78(%rip),%r12
mov    0x2e49(%rip),%r13
mov    0x2e52(%rip),%rbx
lea    0x10(%rsp),%rbp
xor    %r15d,%r15d
lea    0xe6a(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r12,%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x0(%r13),%edi
call   1050 <fflush@plt>
mov    (%rbx),%rdx
mov    %rbp,%rdi
mov    $0x100,%esi
call   1040 <fgets@plt>
mov    0x10(%rsp),%eax
mov    %eax,0xb(%rsp)
xor    $0x74697865,%eax
movzbl 0xf(%rsp),%ecx
or     %eax,%ecx
je     1292 <main+0x122>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     1213 <main+0xa3>
je     1223 <main+0xb3>
mov    %rax,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <waitpid@plt>
test   %rax,%rax
jns    11b0 <main+0x40>
lea    0xe0f(%rip),%r14
mov    %r14,%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%r15d
jmp    1292 <main+0x122>
lea    0xdf3(%rip),%rsi
lea    0x10(%rsp),%rdi
xor    %eax,%eax
call   1070 <popen@plt>
mov    $0xffffffff,%r15d
test   %rax,%rax
je     1292 <main+0x122>
mov    %rax,%rbx
lea    0x110(%rsp),%rdi
mov    $0x200,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     1292 <main+0x122>
lea    0xdba(%rip),%r14
lea    0x110(%rsp),%rbp
nopl   (%rax)
mov    %r14,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rbp,%rdi
mov    $0x200,%esi
mov    %rbx,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
jne    1270 <main+0x100>
mov    %r15d,%eax
add    $0x318,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"Comando >\00"
@stdout = global i32 0
@stdin = global i32* null
@.str.1 = [5 x i8] c"exit\00"
@.str.2 = [14 x i8] c"error forking\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [3 x i8] c"%s\00"
@.str.5 = [24 x i8] c"error waiting for child\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [256 x i8]
%3 = alloca [512 x i8]
%4 = alloca [5 x i8]
%5 = alloca i32*
%6 = alloca i64
store i32 0, i32* %1
br label %7
7: ; preds = %0, %63
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @stdout
%10 = call i32 @fflush(i32 %9)
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%12 = load i32*, i32** @stdin
%13 = call i32* @fgets(i8* %11, i32 256, i32* %12)
%14 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%15 = load i8, i8* %14
%16 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 0
store i8 %15, i8* %16
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 1
%18 = load i8, i8* %17
%19 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 1
store i8 %18, i8* %19
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 2
%21 = load i8, i8* %20
%22 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 2
store i8 %21, i8* %22
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 3
%24 = load i8, i8* %23
%25 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 3
store i8 %24, i8* %25
%26 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 0
%27 = call i32 @strcmp(i8* %26, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%28 = icmp ne i32 %27, 0
br i1 %28, label %30, label %29
29: ; preds = %7
store i32 0, i32* %1
br label %64
30: ; preds = %7
%31 = call i64 (...) @fork()
store i64 %31, i64* %6
%32 = load i64, i64* %6
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %30
%35 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %64
36: ; preds = %30
%37 = load i64, i64* %6
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %54
39: ; preds = %36
%40 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%41 = call i32* (i8*, i8*, ...) bitcast (i32* (...)* @popen to i32* (i8*, i8*, ...)*)(i8* %40, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %41, i32** %5
%42 = icmp ne i32* %41, null
br i1 %42, label %44, label %43
43: ; preds = %39
store i32 -1, i32* %1
br label %64
44: ; preds = %39
br label %45
45: ; preds = %50, %44
%46 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%47 = load i32*, i32** %5
%48 = call i32* @fgets(i8* %46, i32 512, i32* %47)
%49 = icmp ne i32* %48, null
br i1 %49, label %50, label %53
50: ; preds = %45
%51 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %51)
br label %45
53: ; preds = %45
store i32 -1, i32* %1
br label %64
54: ; preds = %36
%55 = load i64, i64* %6
%56 = call i64 @waitpid(i64 %55, i32 0, i32 0)
%57 = icmp slt i64 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %54
%59 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %1
br label %64
60: ; preds = %54
br label %61
61: ; preds = %60
br label %62
62: ; preds = %61
br label %63
63: ; preds = %62
br label %7
64: ; preds = %58, %53, %43, %34, %29
%65 = load i32, i32* %1
ret i32 %65
}

LLVM IR (-O2):
@.str = [10 x i8] c"Comando >\00"
@stdout = global i32 0
@stdin = global i32* null
@.str.1 = [5 x i8] c"exit\00"
@.str.2 = [14 x i8] c"error forking\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [3 x i8] c"%s\00"
@.str.5 = [24 x i8] c"error waiting for child\00"
define i32 @main() {
%1 = alloca [256 x i8]
%2 = alloca [512 x i8]
%3 = alloca [5 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 1
%8 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 1
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 2
%10 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 2
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 3
%12 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 3
br label %13
13: ; preds = %40, %0
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @stdout
%16 = call i32 @fflush(i32 %15)
%17 = load i32*, i32** @stdin
%18 = call i32* @fgets(i8* %4, i32 256, i32* %17)
%19 = load i8, i8* %4
store i8 %19, i8* %6
%20 = load i8, i8* %7
store i8 %20, i8* %8
%21 = load i8, i8* %9
store i8 %21, i8* %10
%22 = load i8, i8* %11
store i8 %22, i8* %12
%23 = call i32 @bcmp(i8* dereferenceable(5) %6, i8* dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64 5)
%24 = icmp eq i32 %23, 0
br i1 %24, label %46, label %25
25: ; preds = %13
%26 = call i64 (...) @fork()
%27 = icmp slt i64 %26, 0
br i1 %27, label %43, label %28
28: ; preds = %25
%29 = icmp eq i64 %26, 0
br i1 %29, label %30, label %40
30: ; preds = %28
%31 = call i32* (i8*, i8*, ...) bitcast (i32* (...)* @popen to i32* (i8*, i8*, ...)*)(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%32 = icmp eq i32* %31, null
br i1 %32, label %46, label %33
33: ; preds = %30
%34 = call i32* @fgets(i8* %5, i32 512, i32* %31)
%35 = icmp eq i32* %34, null
br i1 %35, label %46, label %36
36: ; preds = %33, %36
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %5)
%38 = call i32* @fgets(i8* %5, i32 512, i32* %31)
%39 = icmp eq i32* %38, null
br i1 %39, label %46, label %36
40: ; preds = %28
%41 = call i64 @waitpid(i64 %26, i32 0, i32 0)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %13
43: ; preds = %40, %25
%44 = phi i8* [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), %25 ], [ getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), %40 ]
%45 = call i32 @perror(i8* %44)
br label %46
46: ; preds = %13, %36, %43, %33, %30
%47 = phi i32 [ -1, %30 ], [ -1, %33 ], [ -1, %43 ], [ -1, %36 ], [ 0, %13 ]
ret i32 %47
}