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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ console_print(char const *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ remove(char *);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char *);
char *strchr(char const *, int);
int /*<<< orphan*/ system(char *);
int cmd_exec(const char *cmd) {
  int fd;
  char buf[1024];
  char *end_chr;
  if (*(unsigned char *)cmd == 0x8a) {
    console_print("\n");
    return 1;
  }
  end_chr = strchr(cmd, 0x8a);
  if (end_chr != NULL) {
    *end_chr = '\0';
  }
  console_print(cmd);
  console_print("\n");
  memset(buf, 0, 1024);
  sprintf(buf, "%s > %s 2>&1", cmd, "/tmp/echo_shell.tmp");
  system(buf);
  fd = open("/tmp/echo_shell.tmp", O_RDONLY);
  if (fd != 0) {
    memset(buf, 0, 1024);
    while (read(fd, buf, 1024) > 0) {
      console_print(buf);
      memset(buf, 0, 1024);
    }
    close(fd);
  }
  remove("/tmp/echo_shell.tmp");
  return 1;
}


x86 assembly (clang, -O0):
<cmd_exec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x8a,%eax
jne    11c9 <cmd_exec+0x39>
lea    0xe48(%rip),%rdi
call   1040 <console_print@plt>
movl   $0x1,-0x4(%rbp)
jmp    12da <cmd_exec+0x14a>
mov    -0x10(%rbp),%rdi
mov    $0x8a,%esi
call   1060 <strchr@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
je     11f6 <cmd_exec+0x66>
mov    -0x428(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
call   1040 <console_print@plt>
lea    0xdfa(%rip),%rdi
call   1040 <console_print@plt>
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xdd2(%rip),%rsi
lea    0xdd8(%rip),%rcx
call   10b0 <sprintf@plt>
lea    -0x420(%rbp),%rdi
call   1050 <system@plt>
mov    0x2d99(%rip),%rax
mov    (%rax),%esi
lea    0xdb7(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     12c7 <cmd_exec+0x137>
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1090 <read@plt>
cmp    $0x0,%rax
jle    12bf <cmd_exec+0x12f>
lea    -0x420(%rbp),%rdi
call   1040 <console_print@plt>
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
jmp    127d <cmd_exec+0xed>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
lea    0xd41(%rip),%rdi
call   1030 <remove@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmd_exec>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
cmpb   $0x8a,(%rdi)
jne    11b1 <cmd_exec+0x21>
lea    0xe59(%rip),%rdi
call   1040 <console_print@plt>
jmp    128e <cmd_exec+0xfe>
mov    %rdi,%rbx
mov    $0x8a,%esi
call   1060 <strchr@plt>
test   %rax,%rax
je     11c6 <cmd_exec+0x36>
movb   $0x0,(%rax)
mov    %rbx,%rdi
call   1040 <console_print@plt>
lea    0xe2b(%rip),%rdi
call   1040 <console_print@plt>
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
lea    0xe0f(%rip),%rsi
lea    0xe15(%rip),%r14
mov    %rbp,%rdi
mov    %rbx,%rdx
mov    %r14,%rcx
call   10b0 <sprintf@plt>
mov    %rbp,%rdi
call   1050 <system@plt>
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   10a0 <open@plt>
test   %eax,%eax
je     1282 <cmd_exec+0xf2>
mov    %eax,%ebp
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1090 <read@plt>
test   %rax,%rax
jle    127b <cmd_exec+0xeb>
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1040 <console_print@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1090 <read@plt>
test   %rax,%rax
jg     1250 <cmd_exec+0xc0>
mov    %ebp,%edi
call   1080 <close@plt>
lea    0xd86(%rip),%rdi
call   1030 <remove@plt>
mov    $0x1,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"\0A\00"
@.str.1 = [13 x i8] c"%s > %s 2>&1\00"
@.str.2 = [20 x i8] c"/tmp/echo_shell.tmp\00"
@O_RDONLY = global i32 0
define i32 @cmd_exec(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [1024 x i8]
%6 = alloca i8*
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = load i8, i8* %7
%9 = zext i8 %8 to i32
%10 = icmp eq i32 %9, 138
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 @console_print(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %2
br label %53
13: ; preds = %1
%14 = load i8*, i8** %3
%15 = call i8* @strchr(i8* %14, i32 138)
store i8* %15, i8** %6
%16 = load i8*, i8** %6
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = load i8*, i8** %6
store i8 0, i8* %19
br label %20
20: ; preds = %18, %13
%21 = load i8*, i8** %3
%22 = call i32 @console_print(i8* %21)
%23 = call i32 @console_print(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%25 = call i32 @memset(i8* %24, i32 0, i32 1024)
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%27 = load i8*, i8** %3
%28 = call i32 @sprintf(i8* %26, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %27, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%30 = call i32 @system(i8* %29)
%31 = load i32, i32* @O_RDONLY
%32 = call i32 @open(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %31)
store i32 %32, i32* %4
%33 = load i32, i32* %4
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %51
35: ; preds = %20
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%37 = call i32 @memset(i8* %36, i32 0, i32 1024)
br label %38
38: ; preds = %43, %35
%39 = load i32, i32* %4
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%41 = call i64 @read(i32 %39, i8* %40, i32 1024)
%42 = icmp sgt i64 %41, 0
br i1 %42, label %43, label %48
43: ; preds = %38
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%45 = call i32 @console_print(i8* %44)
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%47 = call i32 @memset(i8* %46, i32 0, i32 1024)
br label %38
48: ; preds = %38
%49 = load i32, i32* %4
%50 = call i32 @close(i32 %49)
br label %51
51: ; preds = %48, %20
%52 = call i32 @remove(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %2
br label %53
53: ; preds = %51, %11
%54 = load i32, i32* %2
ret i32 %54
}

LLVM IR (-O2):
@.str = [2 x i8] c"\0A\00"
@.str.1 = [13 x i8] c"%s > %s 2>&1\00"
@.str.2 = [20 x i8] c"/tmp/echo_shell.tmp\00"
@O_RDONLY = global i32 0
define i32 @cmd_exec(i8* %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = load i8, i8* %0
%5 = icmp eq i8 %4, -118
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = tail call i32 @console_print(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %34
8: ; preds = %1
%9 = tail call i8* @strchr(i8* %0, i32 138)
%10 = icmp eq i8* %9, null
br i1 %10, label %12, label %11
11: ; preds = %8
store i8 0, i8* %9
br label %12
12: ; preds = %11, %8
%13 = tail call i32 @console_print(i8* %0)
%14 = tail call i32 @console_print(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @memset(i8* %3, i32 0, i32 1024)
%16 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%17 = call i32 @system(i8* %3)
%18 = load i32, i32* @O_RDONLY
%19 = call i32 @open(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %18)
%20 = icmp eq i32 %19, 0
br i1 %20, label %32, label %21
21: ; preds = %12
%22 = call i32 @memset(i8* %3, i32 0, i32 1024)
%23 = call i64 @read(i32 %19, i8* %3, i32 1024)
%24 = icmp sgt i64 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %21, %25
%26 = call i32 @console_print(i8* %3)
%27 = call i32 @memset(i8* %3, i32 0, i32 1024)
%28 = call i64 @read(i32 %19, i8* %3, i32 1024)
%29 = icmp sgt i64 %28, 0
br i1 %29, label %25, label %30
30: ; preds = %25, %21
%31 = call i32 @close(i32 %19)
br label %32
32: ; preds = %30, %12
%33 = call i32 @remove(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %34
34: ; preds = %32, %6
ret i32 1
}